To give a head start to Selenium WebDriver, a basic understanding of WebDriver API commands and how it operates is required. Some of the basic commands are discussed below, which can help make your life easier.

Creating a new driver instance 

  • Firefox Driver

We can create a new Firefox driver instance as shown below:

WebDriver driver = new FirefoxDriver();

  • HtmlUnit Driver

This is used if we want to run headless tests, i.e. having no GUI. We can create a new HtmlUnit driver instance as shown below:

WebDriver driver = new HtmlUnitDriver();

JavaScript is not enabled by default for HtmlUnit Driver. Enabling Java script can be accomplished in two ways:

WebDriver driver = new HtmlUnitDriver(true); 

WebDriver driver = new HtmlUnitDriver();driver.setJavascriptEnabled(true);

  • Internet Explorer Driver

To run tests on IE browser, we have to first install IEDriverServer.exe from http://www.seleniumhq.org/download/.

Now, set the path of IEDriverServer.exe. Assume that you have installed it in D: drive.

System.setProperty(“webdriver.ie.driver”, “D://IEDriverServer.exe”);

Create a new Internet Explorer driver instance as shown below:

WebDriver driver = new InternetExplorerDriver();

  • Chrome Driver

To run tests on chrome browser, we have to first install chromedriver.exe from https://sites.google.com/a/chromium.org/chromedriver/downloads.

Now, set the path of chromedriver.exe. Assume that you have installed it in D: drive,

System.setProperty(“webdriver.chrome.driver”, “D://chromedriver.exe”);

Create a new chrome driver instance as shown below:

WebDriver driver = new ChromeDriver();

  • Safari Driver

To run tests on safari browser, we have to first download and install SafariDriver extension from http://selenium-release.storage.googleapis.com/index.html.

download and install SafariDriver extension

Go to Safari>Preferences>Extensions and select the “Enable WebDriver” checkbox.

Go to Safari>Preferences>Extensions and select the “Enable WebDriver” checkbox.

After this, create a new Safari driver instance as shown below:

WebDriver driver = new SafariDriver();

Commands to open a new URL

This can be done in two ways:

  • get(“http://www.360logica.com/”);
  • navigate().to(“http://www.360logica.com/”);

Commands to locate Web elements

To locate Web elements, we can use findElement() and findElements() methods in WebDriver.

  • findElement() method

findElement() method uses By object as a parameter to locate elements on that page with the help of the specified locator (className, cssSelector, id, linkText, name, partialLinkText, tagName, xpath). If no element is found, then it throws NoSuchElementException. If more than one element is found, then it returns the first element.

The syntax for the same is as shown below:

WebElement Element = driver.FindElement(By.locator(locatorString));

 Refer some examples below for better understanding:

a) Links in HTML are defined in anchor (<a>) tag. Suppose we have the elements given as in the image below

image003

Here, we can locate elements by LinkText and partialLinkText as follows:

WebElement Element = driver.FindElement(By. linkText (“BFSI Testing Services”));

WebElement Element = driver.FindElement(By. partialLinkText (“BFSI”));

b) In the image presented below, the highlighted element has id, name, and class attributes which provide us different options to locate the Web element.

image004

WebElement Element = driver.FindElement(By. id(“pass”)); 

WebElement Element = driver.FindElement(By. name(“pass”));

WebElement Element = driver.FindElement(By. className(“inputtext”));

Finding element by id/name is the most preferred method if it’s a unique one. The issue only arises when we get a non-unique id/name.  We can also locate element by class names. In most cases, more than one element can have the same class as you can also see in above image.

Xpath and Css selectors can also be used to locate elements in the manner as shown below:

WebElement Element = driver.FindElement(By. xpath(“//input[@id=’pass’]”)); 

WebElement Element = driver.FindElement(By. cssSelector(“#passs”));

c) Suppose we have an element given as shown below:

image005

Here, we can find the element by tag name as follows:

WebElement element = driver.findElement(By.tagName(“button”));

  • findElements() method

findElements() method returns list of one or more Web elements with all matching instances. If no element is found, then it returns an empty list. We can use it as shown in the example below:

List<WebElement> iframes = driver.findElements(By.tagName(“iframe”));

 To be continued…