Tag Archives: Java

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.


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.


Selenium WebDriver on Firefox: Working with Add-Ons

Selenium WebDriver on Firefox: Working with Add-Ons.

via Selenium WebDriver on Firefox: Working with Add-Ons.


Switching to a new tab in WebDriver

public void switchWindow() throws NoSuchWindowException{
       try {
           Set handles = driver.getWindowHandles();
           String current = driver.getWindowHandle();
           handles.remove(current);
           String newTab = handles.iterator().next();
           driver.switchTo().window(newTab);
        } catch( Exception e ) {
            logger.logError(e.getMessage());
        }
}

Getting the return value from an anonymous JavaScript function with Webdriver

I needed to get the return value of a JavaScript function executed through WebDriver. Specifically, I needed to get the text from a WYSIWYG editor called TinyMCE. Here was my solution. Example:

String javascript = " ( function getText() { " +
" for(i=0; i < window.frames.length; i++) { " +
" if( window.frames[i].document.getElementById('tinymce') != null ) { " +
" return window.frames[i].document.getElementById('tinymce').innerHTML; " +
" } } } )() ";

Here is my runScript method:

public String runScript(String javasript){
JavascriptExecutor js = (JavascriptExecutor) driver;
return (String) js.executeScript("return " + javascript);
}

This is the basic syntax:
( function x() { //do something & return value } )()

To Use with an argument:
( function(x) { //do something & return value } )(arg);


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