Home » Java » Enterprise Java » Java testing with Selenium and dynamic ids in html

About David Gray

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 our best selling eBooks for FREE!

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

and many more ....

 

One comment

  1. 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

Your email address will not be published. Required fields are marked *

*


seven + = 15

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Do you want to know how to develop your skillset and become a ...

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!
Get ready to Rock!
To download the books, please verify your email address by following the instructions found on the email we just sent you.

THANK YOU!

Close