Selenium Webdriver Commands Methods For Freshers & Experienced: When we think about automation and if the requirement is an open-source tool that has all functionalities. Then one name comes to our mind that’s Selenium WebDriver. Because selenium WebDiver is one of the open-source tools which support all major features which have Selenium Webdriver Commands Methods and by using this, we can perform testing any web application. Also, it can be used with different scripting languages like JAVA, PYTHON, C#, and others. But Java is used with selenium widely because of its popularity, and both have a common thing that’s both are open source tools.
So in this Selenium Tutorial post, we want to share with you the commonly used Selenium Webdriver Commands Methods, which are mostly used by any automation testers with the syntax and also a simple example which helps you to understand the basics easily.
Let’s discuss one by one all commonly used Selenium Webdriver Commands Methods with briefly and precisely so that you can make the use of those Selenium Webdriver Commands Methods effectively whenever the requirement comes.
One of the common statement which is familiar to you is the below statement. For the understanding of why we write this statement for that, you can visit our previous post, Selenium WebDiver Hierarchy, and Selenium WebDiver architecture post.
WebDriver driver= new firefoxDriver ();
Selenium Webdriver Commands Methods
get(): When your requirement is open a URL in the web browser, then you can use this method of selenium WebDiver.
Navigate.To(URL): By using this method, we can able to load a new web page in the current browser window. When this method executes internally, an HTTP get operation is performed, and during that time period, it will block all other operations until the web page is loaded completely.
Difference Between: Selenium Benefits
getCurrentUrl(): When you want to get the currently open web page URL, then you can use this method, which returns the current opened page URL in the string format.
String openedURL= driver.getCurrentUrl(); System.out.println("Opened URL is:"+openedURL);
getPageSource(): By using the getPageSource() method, you can able to get the page source of the opened web page, and this method return in string format.
String openedPagePageSource= driver.getPageSource(); System.out.println("Page source is: \n" +openedPagePageSource );
getTitle(): You can able to get the current web page <title> tag texts or web page title of the opened web page by using the getTitle() method. getTitle() return the page title in the form of a string.
String pageTitle= driver.getTitle(); System.out.println("Loaded page title: "+pageTitle);
Close(): After performing all the operations in the current window at finally, we want to close that. So we can close the active window by calling the close() of selenium WebDiver.
quit(): in the previous syntax we can see that by using close() method we are able to close the currently active window but suppose there are 5 windows are opened, and you want to close all opened windows that time you can use the quit() method and it will close all the opened windows irrespective if active or non active.
Difference Between: Selenium Webdriver Architecture
findElement(locator): If you want to find an element in the active dom, then you can use findElement() with the locators.
WebElement SoftwareTestingoLogo= driver.findElement(By.xpath("//img[@title='SoftwareTestingo']")); System.out.println(SoftwareTestingoLogo);
findElements(locator): Suppose there are multiple similar types of elements that are there, and you want to access those elements, then you can use the findElements() method. it will return a list of all those similar elements in insertion order.
List allLinks= driver.findElements(By.xpath("//a")); System.out.println("Total elements found: "+allLinks.size());
Difference Between: findElement() VS findElements()
Difference Between: fullScreen() VS maximize()
getPosition(): By using this, we can able to know what is the position of the window from the left side in terms of X and Y coordinates. the return type of this method is Point (An instance of Point class).
Point position= driver.manage().window().getPosition(); System.out.println("Position of window is: "+position);
getX() and getY(): These two methods helps in finding specific elements specific X and Y coordinates on the window. The getX() will return the position from the left side of the window, and getY() will return the position from the top side of the screen.
getSize(): By implementing this method, we can able to get the window size.
Dimension size= driver.manage().window().getSize(); System.out.println("Size of current window:"+size);
getWidth() and getheight(): By using these two getWidth() and getheight() method we can able to get the window height and width of the current window.
System.out.println("Width of window: "+ driver.manage().window().getSize().getWidth()); System.out.println("Height of window"+driver.manage().window().getSize().getHeight());
setPosition(): With the help of this method, we can able to set the position of the window on the screen. within the setPosition() method, you have to pass the x and Y coordinates.
setSize(): we are using this method to set the size of the window is software testing. The first parameter represents the width, and the second one is for height. you can find the syntax below
deleteAllCookies(): Sometimes, while testing an application, you may face some issues because of the cookies. so we can solve issues by calling the deleteAllCookies() method in our program so that it will delete all the cookies of the specific domain, and after that, we can run our program.
deleteCookieNamed(): we can also delete some specific cookies which we want to delete by using the deleteCookieNamed() method, and in that method, we have to pass the cookies name, which you want to delete. like this, we can delete the specific cookies as per our requirement.
driver.manage().deleteCookieNamed("Some cookie name");
implicitWait(): Some time some of the elements are required extra time to load, but our script is not waiting for loading that time. so we can stop the script execution for some time using the implicitWait() method.
Timeouts implicitWaitEx= driver.manage().timeouts().implicitlyWait(50, TimeUnit.SECONDS);
pageLoadTimeout(): Some times, because of the server issues, the page load times increased as compared to the normal page load time. so because of that, we are getting errors for such scenarios to we are using pageLoadTimeout() with the wait time.
setScriptTimeout(): By using setScriptTimeout() method we can mentioned the wait time duration for an asynchronous script to finish the execution. If it is not executed within the mentioned time duration, then it will through an error.
Back (): if you have opened a web URL using navigate.to() method, and you want to go back to the previous URL. Then, by using this method, we can navigate back to the previous page.
Forward (): Similarly, if you want to go to the next page, then you can use this forward() method along with navigate().
Refresh (): With the help of this method, we can able to do the page refresh.
switchTo(): at the time of testing an application if you got any alert then you can use this method
defaultContent(): Suppose you are inside a frame, and you want to come out from the frame, then you can use the defaultContent() method.
Frame (int index): Suppose there are multiple frames present, and you want to jump to a specific frame than by using the frame(int index) you can move to the particular frame.
Frame (String name/id): If you know the frame name or id, then you can switch from one frame to another frame by using the name or Id of that frame.
driver.switchTo().frame("name or ID");
Frame (WebElement element): Suppose you don’t know the name, id or index of a frame but you can able to get the locator details of the frame then also you can switch from your current frame to that frame by this:
driver.switchTo().frame(" WebElement locator");
parentFrame(): Suppose you are present inside an inner frame, and from that inner frame to you want to switchTo the parentFrame, then that time, you can use this method.
getClass(): This method helps you to find out which browser class or browser you are using
Class className= driver.getClass(); System.out.println(className.getSimpleName());
isEnabled(): With this isEnabled(), you can easily find out that a specific element in DOM is enabled or disabled. if the particular element is enabled, then it returns a boolean value true, and if that specific element is disabled, then it returns a false value.
boolean textBox = driver.findElement(By.xpath("//input[@name='textbox1']")).isEnabled();
Click(): if you to perform some operation like click on an element, then you can use click() method.
submit(): submit is an alternative of click(). When you are finding an element in DOM, and you found a button with an attribute type=submit, then, in that case, you can use submit. In some scenarios where the action is done through elements rather than a button in that place, submit works and click won’t.
Select (): We mostly see in DOM there is a drop-down are present. To deal with the drop-down scenarios, we are using the select class. we can select a value by using any of these 3 methods like selectByVisibleText(), selectByValue() or selectByIndex().
WebElement mySelectedElement = driver.findElement(By.id("select")); Select dropdown= new Select(mySelectedElement); dropdown.selectByVisibleText("Apple");
getScreenshotAs(): During the execution of the script in some places, you may see that your script is failing, and knowing the reason for failing, you need to take the screenshot of the page. so we can take the screenshot by using the getScreenshotAs() method.
File shot = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE); FileUtils.copyFile(shot, new File("D:\\ shot1.jpg"));
Still, there are so many methods are there which you are using during your automation, but I try my label best to share a few regular methods, and I hope this post may be Help you. If you found that still there are other regular used methods are there, then you can comment in the comment section, and I try to update those methods also in the list.
Thanks for visiting our post. Its an appeal to you guys kindly share your experience with us by using our contact us page so that we can make this platform more helpful.
Searching Keyword: Top Selenium Webdriver Commands Methods, Advanced Selenium Webdriver Commands Methods, Selenium Webdriver Commands Methods important for an interview purpose.