Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Selenium Webdriver and PageFactory initialize List<WebElement> elements

I have searched the Selenium Webdriver APi docs hosted on google code. Currently using the PageFactory to initlize my Page objects, but having issue initilizing a list of WebElement.

What I need is a way to initialize a list of elements, ideally a list of drop-down select boxes.

I have looked at the API references to @Findsby and @ByChained but still can not figure out the best way to initlize a list of dropdown select boxes. I COULD have a seperate WebElement for each one and grab the ID but I would like to initlize a list of List selects

Currently I use the following:

public class PageObject {

        @FindBy(id="element_id")
        private WebElement element;

        public getElement() {
          return element;
        }
}

Is there some way I can use something similar to the following that I seek:

public class PageObject {   

    @FindBys(className="selectItmes")
    private List<WebElement> selects;

    public List<WebElement> getSelects() {
      return selects;
    }  
}

Or must I use a single Web Element for each element? :(

Update

Anyone know how to use the PageFactory and initlize a List elements; using the FindsBy annotation. I can't find any way of doing this yet there are google issues on the selenium google docs site saying this has been fixed in the Java api bindings and in version 2.12 as it was mistaken disabled in 2.11.... I still can't initialize a list. =/

like image 956
Patrick Magee Avatar asked Nov 04 '11 10:11

Patrick Magee


2 Answers

This feature has been recently added in Selenium 2.0. Check this issue. It is fixed now.

From the documents, you could do something like,

@FindAllBy(className="selectItmes") 
List<WebElement> selects;

If you are interested in the code, check this out

like image 110
nilesh Avatar answered Oct 05 '22 23:10

nilesh


Here is standard solution what I do in our test framework, until @FindAllBy doesn't work in Selenium library:

private List<WebElement> selects;

public List<WebElement> getSelects() {
      selects = getDriver().findElements(By.xpath("..."));
      return selects;
    } 
like image 20
Dmitry Avatar answered Oct 06 '22 00:10

Dmitry