15 Top Selenium WebDriver Commands For Test Automation — Testbytes
Use of selenium webdriver helps in testing every aspect of the web application. It is an open source website automation tool which is used mostly by the automation testers.
With the help of Selenium Webdriver, applications are tested to see whether they are working as expected or not.
To ease your work we will provide you with some basic commands list which you can use in selenium webdriver. Using these commands it will make things easier for you.
Basic Commands List for Selenium Web driver
There are two options which you can use to select items in a drop-down i.e. single select dropdown and multi-select drop-down. Single select dropdown allows the user to select only one item from the drop-down whereas Multiple-select dropdown allows the user to select multiple items from the dropdown list.
You can use this code to generate a list in which you can select multiple items in a drop-down.
<select name=”Country” multiple size=”6">
When a form is submitted the value has to be sent to a server, this value is sent specifically by the value attribute. Content will pass as a value if the value attribute is not specified.
Syntax- <option value=” value”> where ‘ value’ is the value which has to be sent o the server.
3. Use of linkText() and partialLinkText() command
These commands are used to access the hyperlinks which are available on a webpage. By using these commands user is redirected to another page.
Let us consider there are two links mentioned in the webpage Google and Yahoo.
This command finds the element by using linkText() and then click on that link. The user is then redirected to the page followed by the link.
This command finds the element partially by using partialLinkText() and then clicks on it.
Almost every webpage contain forms which have to be filled by the user. There are various types of forms like login, registration, file upload or new signup etc. While testing of the website the command submit() is used. It triggers the submit button without clicking on the submit button. The code for the form submission is as follows:
<input type=”text” name=”FirstName”>
<inpur type=”text” name=”LastName”>
<input type=”text” name=”EmailID”>
<input type=”text” name=”MobileNo”>
<input type=”submit” value=”submit”>
These commands are used to close the web pages which are currently used by the user.
- quit(): The quit() command is used to close down all the web pages in the web browser. All the web pages which are being opened by the users are closes down instantly. The syntax of the command is given as driver.quit(); This command doesn’t need any other parameters and doesn’t return any value.
- close(): the close() command is used to close down the current webpage which is being opened by the user. This command only closes a single webpage unlike quit(). This command doesn’t need any other parameters and doesn’t return any value. The syntax of the command is given as driver.close();
There are scenarios where the users have to work on various frames and iframes. The script tester verifies the working of the frames through script code. The illustration of the code is given below where there are different frames in the webpage.
<input type=”text” id=”Name”>FirstName</input>
<input type=”text” id=”Name”>LastName</input>
In this HTML code, two iframes are present. Thus to access the second frame user has to navigate through the first frame. Only by dealing first frame user are allowed to navigate to the second frame. It is impossible for the user to access directly the second frame without using the first frame.
- Frame(index): swtichTo().frame(0);
- Frame(frame name):switchTo().frame(Frame name”);
- Frame(Web element):switchTo().defaultContent();
These commands can be used by the user to return back to the main window.
This command is used by the user to search or locate the first element on the webpage. The parameters which are used in the syntax fetch the element on the current working page. Click, submit or another type of actions are mainly used by this command. The syntax of this command is given as driver.findElements(By.Name(“login”)).click();
This command is used to locate and searches the first element of the web page with the name “login” and then clicks on it.
This command is used to check whether the element in the selenium webdriver is enabled or not. The syntax of this command is given as
Boolean check = driver.findElements(By.xpath(“Name”)).isEnabled();
This command finds the element and checks whether the element is enabled or disabled.
This command is typically used for filling in forms. The general syntax for this command is given as driver.findElements(By.name(“FirstName”)).sendkeys(“Tony”);
This command will search for the first name field and then enter the value “Tony” in it.
With the help of the getText() command, it will get the inner element of the webpage. By using this command we can store the value of the element into the string object. The syntax for this command can be given as
String new = driver.findElements(By.TagName(“NewFile”)).getText();
This command will look for the field name “new file” then take its inner file and stores it into the string name “new”.
With the help of this command, we can verify whether the element which we are looking for is present in the webpage or not. The syntax for this command can be given as
Boolean check = driver.findElements(By.xpath(“FileName”)).size()! = 0;
It will check the element whether it is available or not. The Boolean will set the “Check” to TRUE or FALSE respectively.
This command is used to select or deselect the values from the list. To select a value we can use different commands like selectByVisbibleText(), selectByValue() or selectByIndex() according to the situations. The syntax for these commands can be given as
These syntaxes are used for selection only. We can also deselect the values from the list by the following syntax.
“New file” is the element containing the values which has to be selected.
This command is used to navigate between different URLs in the webpage. By using this command we can navigate back and forth in the current webpage. The syntax for the command can be given as
driver.navigate().to(“ http://www.Google.com “);
This command will help the user to navigate http://www.Google.com, navigate back and navigate forward.
This command will enable the user to screenshot the entire page in the selenium webdriver. The syntax of the command is given as
File screenshot = ((TakeScreenshot)driver).getScreenshotAs(outputType.FILE);
FileUtils.copyFile(screenshot, new File(“c:\\ss.jpeg”));
This command will take the screenshot and will save the file in C drive as ss.jpeg
15. pageLoadTimeout(time,unit) Command
When the servers are down or there is an issue in the network, the page often takes more time to load. This might cause an error in the program. To avoid this situation, a command is used to set a wait time. The syntax can be given as
By using this command 200 seconds will be enabled. It will wait 200 seconds for a page to load.
Also Read : 10 Best Automation Testing Tools For 2020
Originally published at https://www.testbytes.net on December 27, 2019.