David Gray

About David Gray

David is a software engineer with a passion for everything Java and the web. He is a server side java developer based in Derby, England by day and a Android, jQuery, jack of all trades by night.

Java testing with Selenium and dynamic ids in html

One of the cool aspects of Selenium, is that not only can you record yourself using a site, you can actually run this as a junit test.

Firstly, I’ll install Selenium in Firefox (as this is the official version) and record a quick test.  It’s important to note that Selenium will give you a number of different ways to remember which html tag you invoked.  For instance, it can just invoke a specific id on a page.

However, when using a portal system like say JSF under Liferay, the id values are generated on the fly, so you’d record one test, then never be able to run it successfully again.

One really nice feature of Selenium is you can invoke a HTML xpath so in the Liferay example, your code would still find the tag it needed to click.  Lets say I record myself logging into the page below…

Now because this page is generated with liferay, I can see the input text id for the form is…

<input aria-required="true" class="aui-field-input
aui-field-input-text aui-form-validator-error" id="_58_login" 
name="_58_login" type="text" value="" />

As JSF under Liferay will create a new id quite regularly for this textbox (each time the server is restarted I believe, although it may be even more frequent), this means we can’t just get the id and hook into that, as the tests will only ever run once.

What we can do however is hook into liferay by using the html tag directly as this won’t be different each time Liferay loads the JSF.  I noticed I had to use this same technique for every page in Liferay as the id for nearly all the html rendered through JSF had a different id each time the page was accessed.

We can then export this to a junit class from the file menu File | Export Test Case As… | Java / JUnit 4 / Web Driver which would give us the following class to run and test.

import static org.junit.Assert.fail;

import java.util.concurrent.TimeUnit;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

public class TestExample {
  private WebDriver driver;
  private String baseUrl;
  private StringBuffer verificationErrors = new StringBuffer();

  @Before
  public void setUp() throws Exception {
    driver = new FirefoxDriver();
    baseUrl = "http://localhost:8080";
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
  }

  @Test
  public void testExample() throws Exception {
    driver.get(baseUrl + "/en_GB/web/myapp/home?p_p_id=58&p_p_lifecycle=0&p_p_state=maximized&p_p_mode=view&saveLastPath=0&_58_struts_action=%2Flogin%2Flogin");
    driver.findElement(By.xpath("//span/input")).clear();
    driver.findElement(By.xpath("//span/input")).sendKeys("user");
    driver.findElement(By.xpath("//span[2]/span/span/input")).clear();
    driver.findElement(By.xpath("//span[2]/span/span/input")).sendKeys("pass");
    driver.findElement(By.xpath("//div/span/span/input")).click();
  }

  @After
  public void tearDown() throws Exception {
    driver.quit();
    String verificationErrorString = verificationErrors.toString();
    if (!"".equals(verificationErrorString)) {
      fail(verificationErrorString);
    }
  }
}

 

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

One Response to "Java testing with Selenium and dynamic ids in html"

  1. Dzmitry says:

    Thanks! Simple and useful door-to-door summary for this kind of encoding test case to be i-testing-ready. Works perfect for having mvn verify regress your web ui functionality.

Leave a Reply


eight − = 2



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy | Contact
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close