Selenium IDE, Builder and WebDriver API


Location strategy

  • The majority of today’s software applications are web-based applications. Being run in an Internet browser.
  • XPath helper
  • Firebug and FirePath are like very long Google translate results. DON'T use them for generating selectors!
    Locating Web Elements via
  • Identifier
  • Id
  • Name
  • Link
  • DOM
  • XPath
  • CSS
  • UI-element

NOTE: Each one has PROS and CONS!


  • For many Selenium commands, a target is required
    • Identifies an element in the content of the web application
    • Consists of the location strategy followed by the location in the format: locatorType=location
  • The locator type can be omitted in many cases

Target (2)

  • Locating by identifier is the most common method of locating elements
    • A catch-all default when no recognized locator type is used
    • The first element with the id attribute value matching the location is used
    • If no element has a matching id attribute, then the first element with a name attribute matching the location is used
    • Preferably follow the order id > name > Identifier > css > xpath

Locating Web Elements by ID

  • Locating by Id is more explicit, but also more limited than the identifier locator type
  • We must use it when there is available element id attribute

Locating Web Elements by Name

    The name locator type will locate the first element with a matching name attribute
  • If multiple elements have the same value for a name attribute, then you can use filters to further refine your location strategy
  • The default filter type is value (matching the value attribute)

Locating Web Elements by XPath

    XPath is the language used for locating nodes in an XML (XHTML) document
  • Useful when we don’t have a suitable id or name attribute for the element
  • XPath locators can also be used to specify elements via attributes other than id and name
  • Only xpath locators start with “//” or “./

Absolute or Relative Xpath Location

    Types of XPath location:
  • Absolute
    • Contains the location of all elements from the root (html) (e.g., xpath=/html/body/form)
    • Very likely to fail after adjustments
  • Relative
    • Relative to an element that does have an id or name attribute
    • e.g., //input[@name=’username’] - First input element with attribute named ‘name’ and the value ‘username’

Locating Hyperlinks by Link Text

  • Hyperlink can be located in the web page by using the text of the link.
  • If two links with the same text are present, then the first match will be used.
  • e.g. link=Yes / link=No

Locating Web Elements by DOM

  • The Document Object Model (DOM) represents an HTML document and can be accessed via JavaScript.
  • Takes JavaScript that evaluates to an element on the page
  • Can be simply the element’s location using the hierarchical dotted notation
  • Since only dom locators start with “document”, the dom= label can be omitted
  • e.g. dom=document.getElementById(’loginForm’)

Locating Web Elements by CSS

  • CSS (Cascading Style Sheets) is a language for describing the rendering of HTML and XML documents
  • CSS uses Selectors for binding style properties to elements in the document
  • These Selectors can be used by Selenium as another locating strategy
  • Faster than Xpath
  • Can find the most complicated objects in an intrinsic HTML document
  • e.g. css=div > a

Selenium flavours

Easy !?

Selenium IDE

Selenium Builder

Selenium WebDriver

Why use the Se WebDriver?

  • A tool for automating web application testing
  • Developed to better support dynamic web pages where elements of a page may change without the page itself being reloaded(AJAX)
  • Makes direct calls to the browser using each browser’s native support for automation the page itself being reloaded.

WebDriver Wire Protocol

  • All implementations of WebDriver that communicate with the browser, or a Remote WebDriver server shall use a common wire protocol
  • The wire protocol defines a RESTful web service using JSON over HTTP implemented in request/response pairs of "commands" and "responses“

Creating Driver

  • Create an instance of a driver
  • Additional steps are required to use Chrome Driver, Opera Driver, Android Driver and iPhone Driver

// Create a new instance of the Firefox driver.
// Note that it is wrapped in a using clause so that the browser is closed
// and the webdriver is disposed (even in the face of exceptions).

// Also note that the remainder of the code relies on the interface,
// not the implementation.

// Further note that other drivers (InternetExplorerDriver,
// ChromeDriver, etc.) will require further configuration
// before this example will work.
using (IWebDriver driver = new FirefoxDriver())
    //Notice navigation is slightly different than the Java version
    //This is because 'get' is a keyword in C#

Element Locators

  • By ID
    IWebElement element = driver.FindElement(By.Id("submit"));
    // Action can be performed on Input Button element
  • By Class
    IWebElement parentElement = driver.FindElement(By.ClassName("button"));
  • By Tag Name
    IWebElement element = driver.FindElement(By.TagName("button"));

Element Locators (2)

  • By Name
    IWebElement element = driver.FindElement(By.Name("firstname"));
    // Action can be performed on Input Text element
  • By Link text
    IWebElement element = driver.FindElement(By.LinkText("Partial Link Test"));
    //Or can be identified as
    IWebElement element = driver.FindElement(By.PartialLinkText("Partial");

Element Locators (3)

  • By XPath
    IWebElement element = driver.FindElement(By.XPath("//div[@id='bottom']/div[1]/a[@class='ext-link']"));
  • By CSS
    IWebElement el = driver.FindElement(By.CssSelector(".primary-btn"));
  • Chain of locators (first one serve as container)
    IWebElement anElement = driver.FindElement(By.Id(“parent-id”))

XPath Syntax

    What is XPath?
  • A (programming language) syntax for defining parts of an XML documen
  • Uses path expressions to navigate in XML documents
  • Contains a library of standard functions
  • Is a major element in XSLT
  • Is a W3C recommendation


(JAVA example)

Call Wrappers

Common Actions

Type Text into a field using Selenium WebDriver sendKeys() function
// Find the text input element by its name
IWebElement element = driver.FindElement(By.Name("search"));

// Enter something to search for

Common Actions (2)

Select DropDown value
SelectElement selectElement =
	new SelectElement(driver.FindElement(By.XPath("//select")));
selectElement.SelectByText(" Mercedes ");

Common Actions (3)

Verify WebElement Text 
String text =


No built-in method to assert text on a page
WebDriver driver = new ChromeDriver("C://libs//chromedriver.exe");
Assert.AreEqual( expectedTitle, driver.title);

DRY your code

Clicks and Waits for page to load
public void ClickAndWait(string elementLocator, string waitPeriod) {
  var wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
  wait.Until(d =>
	  d.Title.StartsWith("foo", StringComparison.OrdinalIgnoreCase));

Safe Operations’ for Element Presence

Clicks on an element only if it is available on a page.
public void SafeClick(String elementLocator) {
  if(selenium.isElementPresent(elementLocator)) {
  } else {
    // Using the API for logging
    Reporter.log("Element: " + elementLocator +
		  ", is not available on a page - " + selenium.getLocation());

Selenium Grid

Allows us to run our tests on different machines against different browsers in parallel

  • Tool for distributing tests across multiple physical or virtual machines simultaneously.
  • Dramatically accelerates the testing process and returns a quick feedback.
  • The code doesn’t need to be present on the machines which run the tests.
    Hub and Node
  • Hub - central point where the tests would be triggered. A Selenium Grid has only one Hub and it is launched on a single machine once.
  • Node - Nodes are the Selenium instances that are attached to the Hub which execute the tests. There can be one or more nodes in a grid which can be of any OS and can contain any of the Selenium supported browsers.

Setup Selenium-Grid

  • Installation is simple. Download the Selenium-Server jar file from the SeleniumHq website’s download page. You want the link under the section “Selenium-Server (formerly Selenium-RC)”.
  • Install it in a folder of your choice. You’ll need to be sure the java executable is on your execution path so you can run it from the command-line. If it does not run correcly, verify your system’s path variable includes the path to the java.exe.

Setup Selenium-Grid (2)

Step 1: Start the hub
java -jar selenium-server-standalone-2.14.0.jar -role hub
Step 2: Start the nodes 
java -jar selenium-server-standalone-2.14.0.jar -role node -hub http://localhost:4444/grid/register
Using grid to run tests
DesiredCapabilities capability =
Driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capability);



Problem 1: Selectors

    Open a web site of your choice and try to select one and the same WebElement via:
  • Id
  • Name
  • Link
  • XPath
  • CSS
Hint: look for a link with an ID.


Problem 2: Capture-Replay

Using IDE/Builder create at least 2 tests on a web site of your choice. Export them to NUnit and refactor.
Challenge: Run the exported tests.


Problem 3: Hello Word!

Using WebDriver API create at least 5 tests on a web site of your choice.