The use of selenium webdriver helps in testing every aspect of the web application. It is an open-source website automation tool that 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
1. To Select Multiple Items in a Drop down
2. get() commands
3. Use of linkText() and partialLinkText() command
4. Form Submission Command
5. Using quit() and close() Commands
6. Command to handle Multiple Frames
7. findElements(By,by) and click() Command
8. isEnabled() Command
9. Using findElements(By, by) with sendKeys() Command
10. Using findElements(By, by) with getText() Command
11. Using findElements(By, by) with size() Command
12. select() Command
13. navigate() Command
14. getScreenshotAs() Command
15. pageLoadTimeout(time,unit) Command
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
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.
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.
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.
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.
These commands can be used by the user to return back to the main window.
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
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.
16. Switch to window
driver.switchTo().window(“windowName”);
17. Find the location of the element
WebElement name = driver.findElement(By.id(“Name”));
Point point = name.getLocation();
String strLine = System.getProperty(“line.separator”);
System.out.println(“X cordinate# ” + point.x + strLine + “Y cordinate# ” + point.y);
19. Find the value of CSS property
WebElement name = driver.findElement(By.id(“Name”));
String strAlign = name.getCssValue(“text-align”);
20. Check the visibility of web command
WebElement user = driver.findElement(By.id(“User”));
boolean is_displayed = user.isDisplayed();
//Or write the code in the below style.
boolean is_displayed = driver.findElement(By.id(“User”)).isDisplayed();
We genuinely hope that this selenium webcommands is of great use to you
Also Read : 10 Best Automation Testing Tools For 2018