Understanding Selenium WebDriver API Commands (Part 1)
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
We can create a new Firefox driver instance as shown below:
WebDriver driver = new FirefoxDriver();
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();
WebDriver driver = new HtmlUnitDriver(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.
Create a new Internet Explorer driver instance as shown below:
WebDriver driver = new InternetExplorerDriver();
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,
Create a new chrome driver instance as shown below:
WebDriver driver = new ChromeDriver();
To run tests on safari browser, we have to first download and install SafariDriver extension from http://selenium-release.storage.googleapis.com/index.html.
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:
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
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.
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:
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…