Daily Archives: April 5, 2013

Two ways to assert text with WebDriver

You can use this:

driver.getPageSource().contains(pattern)

or find the element with the text and assert that it is visible.

WebElement el = driver.findElement(By.xpath("//*[contains(.,'" + pattern + "')]"));

I find that the latter works better in most cases. I wrap it in a try/catch block to grab any NoSuchElementException.


Handling JavaScript Alert in WebDriver

This is a very helpful example of how to handle Javascript alert boxes with WebDriver.

via Handling JavaScript Alert in WebDriver.


Select element with jQuery where the id contains specific text.

I have needed to do this on occasion with long auto-generated ids.

$j("[id$='logActivitiesCheckbox']");

To get a WebElement object back from WebDriver I use this method:

  public WebElement getWebElementFromJquery(String jQuery) {
    WebElement jqElement = null;
    JavascriptExecutor js = (JavascriptExecutor) driver;
    String query = "return " + jQuery+ ".get(0);";
    try {
      jqElement = (WebElement) js.executeScript(query);
    } catch (Exception e) {
      logger.error("jQuery() " + e.getMessage());
    }
    return jqElement;
  }

Two useful methods for finding elements with generic locator strings

  
public WebElement findElement(String identifier) {
    WebElement el = null;
    try {
      if (identifier.indexOf("//") > -1) {
        el = driver.findElement(By.xpath(identifier));
      } else if (identifier.indexOf("link=") > -1) {
        String linkText = identifier.split("=")[1];
        el = driver.findElement(By.linkText(linkText));
      } else if (identifier.indexOf("$") > -1) {
        el = getWebElementFromJquery(identifier);
      } else if (identifier.indexOf(">") > -1 || identifier.indexOf(".") > -1) {
        el = driver.findElement(By.cssSelector(identifier));
      } else {
        el = driver.findElement(By.id(identifier));
      }
    } catch (Exception e) {
      logger.error("findElement() " + e.getMessage());
    }
    return el;
  }
 
  public List findElements(String identifier) {
    List el = null;
    try {
      if (identifier.indexOf("//") > -1) {
        el = driver.findElements(By.xpath(identifier));
      } else if (identifier.indexOf("link=") > -1) {
        String linkText = identifier.split("=")[1];
        el = (List) driver.findElements(By.linkText(linkText));
      } else {
        el = driver.findElements(By.id(identifier));
      }
    } catch (Exception e) {
      logger.error("findElements() " + e.getMessage());
    }
    return el;
  }


Find Web Element By Text

A td tag with the text “Settings” for example:

//td[contains(text(), 'Settings')]

Another example:

//div[contains(.,'Some Text')]

Sometimes there are cross-browser problems with using XPath. One solution is to get all elements with a specific tag name, and iterate through the list to match the values of specific attributes.


Learnings in Webdriver using C#.Net

Webdriver Tips and Techniques

The Automation Tester

My experiance with test automation

Learn WebDriver

This WordPress.com site is the cat’s pajamas

Assert Selenium

Selenium Automation in a Right Way