As we have discussed earlier in the IDE section, Selenium commands are the set of commands that are used to run our Selenium tests.
In Selenium WebDriver, we have an entirely different set of commands for performing different operations. Since we are using Selenium WebDriver with Java, commands are simply methods written in Java language.
Before getting into the details of commands provided by Selenium WebDriver, we insist you to go through the Java OOPs (Object-Oriented Programming) concepts in java programming language. You can also refer to our Java OOPs concepts section provided in the Java Tutorial.
Now, the question arises is that how can we access methods provided by WebDriver.
Till now, we have successfully created our first test script in Selenium WebDriver. Therefore, one possible way to view the methods provided by WebDriver is to open the Eclipse IDE loaded with Selenium Webdriver jar files, create a driver object for WebDriver and press the dot key. It will show you all of the possible methods provided by WebDriver.
Let us consider an example form the suggestions displayed by Eclipse to understand the syntax of the methods provided by WebDriver.
To access any method of any class, we need to create an object of class and then all the public methods will appear for the object.
A parameter is an argument which is passed to a method to perform some specific operation.
Methods can return a value or returning nothing (void). If the void is mentioned after the method, it means, the method is returning no value. If it is returning a value, then it must display the type of the value for e.g. getTitle(): String.
Now, we will discuss the various commands provided by WebDriver. The commands provided by Selenium WebDriver can be broadly classified in following categories:
Given below are some of the most commonly used Selenium commands in WebDriver:
There are two methods to fetch a web page:
driver.get("www.rookienerd.com")
driver.navigate().to("https://rookienerd.com/selenium-tutorial");
driver.findElement(By.id("lst-ib")).sendKeys("rookienerd tutorials");
The clear() method is used to clear the user inputs from the text box.
driver.findElement(By.name("q")).clear();
Sometimes we need to fetch the text written over a web element for performing some assertions and debugging. We use getText() method to fetch data written over any web element.
driver.findElement(By.id("element567")).getText();
The click() method is used to perform click operation on any web element.
driver.findElement(By.id("btnK")).click();
driver.navigate().back();
driver.navigate().forward();
driver.navigate().refresh();
driver.close();
driver.quit();
driver.switchTo().window("windowName");
driver.switchTo().frame("frameName");
Drag and Drop operation is performed using the Action class.
WebElement element = driver.findElement(By.name("source")); WebElement target = driver.findElement(By.name("target")); (new Actions(driver)).dragAndDrop(element, target).perform();
Let us consider a sample test script which will cover most of the commonly used WebDriver commands.
For our test purpose, we are using a dummy web page under the URL:
https://www.testandquiz.com/selenium/testing.html
The default interface of the web page looks like this:
You can also use this dummy web page for your Selenium Testing practice.
First you need to download the browser driver for the browser on which you are willing to automate your test scenarios. We have already discussed execution of Selenium test scripts on different browsers in the previous sections of this tutorial.
For this test, we are using Firefox Gecko driver to automate our test scenarios on Firefox browser.
Below is the sample test script with embedded comments.
import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.remote.DesiredCapabilities; import org.openqa.selenium.support.ui.Select; public class Second { public static void main(String[] args) { // System Property for Gecko Driver System.setProperty("webdriver.gecko.driver","D:\\GeckoDriver\\geckodriver.exe" ); // Initialize Gecko Driver using Desired Capabilities Class DesiredCapabilities capabilities = DesiredCapabilities.firefox(); capabilities.setCapability("marionette",true); WebDriver driver= new FirefoxDriver(capabilities); // Launch Website driver.navigate().to("https://www.testandquiz.com/selenium/testing.html"); // Fetch the text "This is sample text." and print it on console // Use the class name of the div to locate it and then fetch text using getText() method String sampleText = driver.findElement(By.className("col-md-12")).getText(); System.out.println(sampleText); // Use the linkText locator method to find the link and perform click using click() method driver.findElement(By.linkText("This is a link")).click(); // Click on the textbox and send value driver.findElement(By.id("fname")).sendKeys("rookienerd"); // Clear the text written in the textbox driver.findElement(By.id("fname")).clear(); // Click on the Submit button using click() command driver.findElement(By.id("idOfButton")).click(); // Locate the radio button by id and check it using click() function driver.findElement(By.id("male")).click(); // Locate the checkbox by cssSelector and check it using click() function driver.findElement(By.cssSelector("input.Automation")).click(); // Use Select class for selecting value from dropdown Select dropdown = new Select(driver.findElement(By.id("testingDropdown"))); dropdown.selectByVisibleText("Automation Testing"); // Close the Browser driver.close(); } }