Selenium WebDriver- Commands

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.

Note
Note: A java method is a collection of statements that are grouped together to perform a specific operation.

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.

Selenium WebDriver Commands

Let us consider an example form the suggestions displayed by Eclipse to understand the syntax of the methods provided by WebDriver.

Selenium WebDriver Commands

Method Name

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.

Parameter

A parameter is an argument which is passed to a method to perform some specific operation.

Return type

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:

  1. Browser Commands
  2. Navigation Commands
  3. WebElement Commands

Given below are some of the most commonly used Selenium commands in WebDriver:

1. Fetching a web page

There are two methods to fetch a web page:

  • Using Get method
snippet
driver.get("www.javatpoint.com")
  • Using Navigate method
snippet
driver.navigate().to("https://javatpoint.com/selenium-tutorial");

2. Locating forms and sending user inputs

snippet
driver.findElement(By.id("lst-ib")).sendKeys("javatpoint tutorials");

3. Clearing User inputs

The clear() method is used to clear the user inputs from the text box.

snippet
driver.findElement(By.name("q")).clear();

4. Fetching data over any web element

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.

snippet
driver.findElement(By.id("element567")).getText();

5. Performing Click event

The click() method is used to perform click operation on any web element.

snippet
driver.findElement(By.id("btnK")).click();

6. Navigating backward in browser history

snippet
driver.navigate().back();

7. Navigating forward in browser history

snippet
driver.navigate().forward();

8. Refresh/ Reload a web page

snippet
driver.navigate().refresh();

9. Closing Browser

snippet
driver.close();

10. Closing Browser and other all other windows associated with the driver

snippet
driver.quit();

11. Moving between Windows

snippet
driver.switchTo().window("windowName");

13. Moving between Frames

snippet
driver.switchTo().frame("frameName");

14. Drag and Drop

Drag and Drop operation is performed using the Action class.

snippet
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:

Selenium WebDriver Commands

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.

snippet
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("JavaTpoint");
	 
	 	// 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();
	
	}

}
Related Tutorial
Follow Us
https://www.facebook.com/Rookie-Nerd-638990322793530 https://twitter.com/RookieNerdTutor https://plus.google.com/b/117136517396468545840 #
Contents