Saturday, 17 August 2013

Using Selenium WebDriver to select JSF/PrimeFaces selectOneMenu options

I'm using JBehave with Selenium WebDriver to test my PrimeFaces (JSF2) application. Selecting an option from a SelectOne option list isn't standard though because of the HTML markup generated by the JSF component.


The facelets code to place the selectOneMenu uses the ID 'state':


This generates HTML div blocks with id's prefixed with this components id:


To select an option, I use a method which manipulates the appropriate divs - this can be used as illustrated below:


To reuse this type of utility method, I put it in a base Page Object class:



4 comments:

  1. Which versions of Selenium and PrimeFaces are you using?

    With Selenium 2.33 and PrimeFaces 3.5, I'm unable to get your example working as I get a NoSuchElementException for the xpath to the actual item in the list. I've found a solution using JavaScript, but it doesn't work with all WebDriver implementations. Previously, though, we were using PrimeFaces 3.4 and had methods similar to yours that worked just fine.

    ReplyDelete
  2. I get too a NoSuchElementException

    ReplyDelete
  3. My web application uses primefaces 3.5 - which is inherited from my primefaces-extensions 0.7.1 dependency.

    <dependency>
    <groupId>org.primefaces.extensions</groupId>
    <artifactId>primefaces-extensions</artifactId>
    <version>0.7.1</version>
    </dependency>

    Right now I'm using selenium 2.35.0 - but when I wrote this I was using 2.32.0. I upgraded selenium because Firefox upgraded to v23.0.1 and selenium webdriver no longer worked.

    Note that I am plugging in the selenium dependencies through Thucydides (currently I'm using 0.9.205).

    Relevant snippets from pom.xml:

    <properties>
    <thucydides.version>0.9.205</thucydides.version>
    <thucydides.jbehave.version>0.9.205</thucydides.jbehave.version>
    </properties>

    <dependency>
    <groupId>net.thucydides</groupId>
    <artifactId>thucydides-core</artifactId>
    <version>${thucydides.version}</version>
    </dependency>
    <dependency>
    <groupId>net.thucydides</groupId>
    <artifactId>thucydides-jbehave-plugin</artifactId>
    <version>${thucydides.jbehave.version}</version>
    </dependency>
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.6.1</version>
    </dependency>
    <dependency>
    <groupId>net.thucydides</groupId>
    <artifactId>thucydides-junit</artifactId>
    <version>${thucydides.version}</version>
    <scope>test</scope>
    </dependency>

    ReplyDelete
  4. I am trying to crawl a website using selenium. After going into base url i reach to next page where there are lots of link, i need to click each link one by one.on the source code links are in following ways
    a id="bidSearchForm:linksBidSearchResults:8:j_idt108" href="#" class="ui-commandlink" onclick="PrimeFaces.ab({source:'bidSearchForm:linksBidSearchResults:8:j_idt108'});return false;">Cuyamaca Rancho State Park Reforestation Project Planting Contract

    can you suggest me how to go to each link one by one?

    ReplyDelete