• info@testbytes.net
  • +91 811 386 5000
  • HR: +91 8113 862 000
whatsapp

15 Top Selenium WebDriver Commands For Test Automation

selenium commands

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.

automation testing

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       

1. To Select Multiple Items in a Drop down

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”>

<option value=”India”>India</option>

<option value=”Belgium”>Belgium</option>

<option value=”England”>England</option>

<option value=”France”>France</option>

<option value=”Italy”>Italy</option>

</select>

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.

2. get() commands

  • get(): This command is used to launch a new browser with the specific URL in the browser. This command uses a single string type which is generally the URL of the application under test. The syntax of the command can be given as driver.get(http://facebook.com)
  • getCurrentUrl(): The command is used to fetch the current URL of the webpage which user is accessing. It returns a string value and doesn’t need any external parameters. The syntax of the command is given as driver.getCurrentURL();
  • getTitle(): This command fetches the title of the webpage which user is currently using. This command doesn’t require any external parameters and returns a string value. If the webpage doesn’t have any title it will return a null string. The syntax of the command is given as String new = driver.getTitle();
  • getAttribute(): This command is used to fetch the value of the specific attribute. This command uses a string which refers to an attribute whose value we want to know and returns a string value. The syntax of the command is given as driver.findElements(By.name(“x”)).getAttribute(“value”);
  • getText(): This command is used fetch the inner text of the element including sub-elements. This command returns a string value and doesn’t need any external parameters. This command is often used for verification or errors in the message or content in the web pages. The syntax of the command is given as String new = driver.findElements(By.name(“Inner_text”)).getText();
  • getClass(): This command is used to fetch the class object. The syntax of the command is given as driver.getClass();
  • getPageSource(): This command is used to fetch the page of the web page which user is currently working on. This command returns a string value and doesn’t require any other parameters. The syntax of the command is given as String new = driver.getPageSource();

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.

  • linkText(): Twitter and Yahoo links can be accessed using the command driver.findElements(By.linkText(“Twitter”)).click();

driver.findElements(By.linkText(“Yahoo”)).click();

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.

  • partialLinkText(): Links can be accessed by using command driver.findElements(By.partialLinkText(“Twitt”)).click();

driver.findElements(By.partialLinkText(“Yaho”)).click();

This command finds the element partially by using partialLinkText() and then clicks on it.

4. Form Submission Command

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:

//First Name

<input type=”text” name=”FirstName”>

//Last Name

<inpur type=”text” name=”LastName”>

//Email ID

<input type=”text” name=”EmailID”>

//Mobile Number

<input type=”text” name=”MobileNo”>

<input type=”submit” value=”submit”>

5. Using quit() and close() Commands

These commands are used to close the web pages which are currently used by the user.

app testing

  • 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();

6. Command to handle Multiple Frames

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.

<html>

<head>

<title>Window handle</title>

</head>

<body>

<div>

<iframe id=”FirstFrame”>

<iframe id=”SecondFrame”>

<input type=”text” id=”Name”>FirstName</input>

<input type=”text” id=”Name”>LastName</input>

</iframe>

<button id=”Submit”>Submit</button>

</iframe>

</div>

</body>

</html>

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.

  • Selecting iframe by ID: switchTo().frame(“Frame ID”);

7. findElements(By,by) and click() Command

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();

Also Read : Automation Test For Website and Web Apps Using Selenium

This command is used to locate and searches the first element of the web page with the name ”login” and then clicks on it.

8. isEnabled() Command

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.

9. Using findElements(By, by) with sendKeys() Command

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.

10. Using findElements(By, by) with getText() Command

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”.

11. Using findElements(By, by) with size() Command

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.

12. select() Command

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

Newfile.selectByVisibleText(“Google”);

Newfile.selectByIndex(“Google”);

Newfile.selectByValue(“Google”);

These syntaxes are used for selection only. We can also deselect the values from the list by the following syntax.

Newfile.deselectByVisibleText(“Google”);

Newfile.deselectByIndex(“Google”);

Newfile.deselectByValue(“Google”);

“New file” is the element containing the values which has to be selected.

13. navigate() Command

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”);

driver.navigate().back();

driver.navigate().forward();

This command will help the user to navigate http://www.Google.com, navigate back and navigate forward.

14. getScreenshotAs() Command

This command will enable the user to screenshot the entire page in the selenium webdriver. The syntax of the command is given as

banner

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

driver.manager().timeouts().pageLoadTimeout(200, SECONDS);

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 2018

Testbytes