Basic Selenium Webdriver Commands for Automated Testing
Following are the list of common selenium Webdriver commands along with their examples.
Get commands are the most common and important commands among other selenium Webdriver commands. These types of commands will fetch various information about the Webpage or elements within the webpage.
1. get() Command
The get() command launches the new browser window and opens the specified URL in the browser. It is same as the open command in selenium IDE. The parameter of get() command must be a string, so you should specify the URL within the double quotes.
2. getTitle() Command
The getTitle() command will retrieve the title of the current webpage launched by selenium webdrive. It does not require any parameters to be specified. If the webpage does not have the title will return null string.
3. getClass() CommandThe getClass() command will retrieve the class name of the specified page element.
4. getPageSource() Command
This command returns the source code as a string of the webpage which is currently accessing. You may also use various string operation commands like contains() to assert some strings.
5. getCurrentUrl() Command
This command retrieves the URL of the web page currently accessed. It does not require any parameters and returns the value as a string.
6. getText() Command
The getText() command will retrieve the inner text of the web element which you will specify. You can utilize this command to verify expected messages or errors that will be displayed while automating the web page.
7. getTagName() Command
This command retrieves the tag name of the HTML element that you have specified by findElement(By.locator()) command.
These are the set of commands that will locate the web elements in a web page using “findElement(By.locator())” commands. Following are the different ways of locating GUI elements using varieties of locators.
1. Locating by Class Name
You can use the ClassName locator to find out the GUI element based on the Class Name specified.
2. Locating by CSS Selector
Locating by CSS selector is the another way of specifying HTML elements of the web page. You can locate the elements by providing appropriate CSS selectors.
3. Locating by ID
You can locate the GUI elements of web page with simply providing the value of id attribute. It will find the elements based on the value of specified id attribute.
4. Locating by Name
Similar to locating by id, you can locate the HTML elements of web page providing the value of name attribute.
5. Locating by Link Text
6. Locating by Tag Name
You may locate the HTML elements by Name of the HTML tag similar to locating by Name and ID as given below.
7. Locating by xpath
You can also locate the GUI element of a web page using xpath locator. It will specify the location of that element from the root element of the HTML document.
Navigation commands are used to go back or forward from the base URL located using get() command. Selenium Webdriver uses four navigation commands i.e. navigate().to(), navigate().refresh(), navigate().back() and navigate().forward().
1. navigate().to() Command
navigate().to() command works same as get() command. It will automatically opens the new browser window and then open the specified URL.
2. navigate().refresh() Command
This command will refresh the current page and you don’t need to specify any parameters.
3. navigate().back() Command
The navigate().back() command navigates back to the previous page from the browser history. This command does not require any parameters.
4. navigate().forward() Command
The navigate().forward() command navigates forward by one page from the browser history. You do not require to provide any parameters for this command.
These types of selenium Webdriver commands are used to perform some particular actions on the web elements. Some of the actions that can be executed by these commands may be clicking on the buttons, typing texts and submitting forms, selecting items from drop down option etc. Following are some of the most commonly used selenium Webdriver commands.
1. click() Command
The click() command allows you to click on the buttons or links. It can be used after the findElement(By.locator) command.
2. sendKeys() Command
You can use sendKeys() command to enter some texts on input fields such as text box. You can specify the desired web element with findElement(By.locator) command.
3. Submit() Command
The submit() command will submit the form element after filling the fields with sendKeys() command. This command also requires to specify the desired element with findElement(By.locator) command. It acts similar to click() command.
4. Select command
Select command is used to select or deselect items from drop-down option. There are different methods such as selectByVisibleText(), selectByValue() or selectByIndex() are available to use under select command.
Example of selecting items from drop down option based on the visible text from drop-down.
WebElement myElement = driver.findElement(By.id("choose")); select dropdown = new select(myElement); dropdown.selectByVisibleText("PHP");
Following is an example of selecting items from drop down option based on the attribute value.
WebElement myElement = driver.findElement(By.id("choose")); select dropdown = new select(myElement); dropdown.selectByValue("lang_php");
Here the example of selecting items from drop down option based on the index number.
WebElement myElement = driver.findElement(By.id("choose")); select dropdown = new select(myElement); dropdown.selectByIndex(3);
5. moveToElement() Command
This command simulates mouse hover effect over the elements of webpage. You can use this command to hover over the menu to see the sub-menu. Following is an example of the uses of moveToElement() command.
Actions actions=new Actions(driver); WebElement mouseHover=driver.findElement(By.id("menu1")); actions.moveToElement(mouseHover); actions.perform();
You can use dragAndDrop() command to drag a draggable element to drop target within the web page.
webElement sourceElement=driver.findElement(By.id("image1")); webElement destinationElement=driver.findElement(By.id("placeholder")); Actions actions=new Actions(driver); actions.dragAndDrop(sourceElement, destinationElement).build().perform();
Close and Quit Commands
Close and quit commands are used when your test was completed. The close() command will close only the current window of the browser which was controlled by Webdriver. Although the quit() command closes all the windows opened by Webdriver.