Reputation: 141
I am automating tests for a WebApp with Selenium in Java. One of the test cases has to use a slider with 2 points on it (for choosing an age range)
In the HTML-code both points are represented by
The style-attribute left:
changes accordingly to the position of the point on the slider bar.
Now to the problem I am facing:
I cannot get a hold on both of those slider-elements at the same time. I have tried:
PageFactory with @FindAll
, both css selectors and xpath:
@FindAll({
@FindBy(xpath = "//slider/span")
})
private List<WebElement> ageSliders;
driver.findBy(...)
with css and xpath selectors - globally and locally in the method, just in case there are any problems with the PageFactory element initialisation
Whatever I do however, retrieves only one element twice. For example doing:
System.out.println(driver.findElement(By.xpath("//slider/span[@class='pointer'][1]")))
System.out.println(driver.findElement(By.xpath("//slider/span[@class='pointer'][2]")))
prints out two elements with the same id:
[[FirefoxDriver: firefox on MAC (ee9fc3d7-77fb-3042-9002-5316c1d94e89)] -> xpath: //slider/span[@class='pointer'][1]]
[[FirefoxDriver: firefox on MAC (ee9fc3d7-77fb-3042-9002-5316c1d94e89)] -> xpath: //slider/span[@class='pointer'][2]]
Which makes accessing both of them separately impossible. Sliding both elements moves only the left one.
The same happens while iterating through the ageSliders
list from above: I always seem to get only the left slider (= the first one in the DOM?).
The only time I was able to operate the right slider was, when I accessed only it, i.e. without trying to touch the other one:
@FindAll({
@FindBy(xpath = "//slider/span")
})
private List<WebElement> ageSliders;
(...)
resetSlider(ageSliders.get(1))
However doing for example
resetSlider(ageSliders.get(1))
resetSlider(ageSliders.get(0))
in the above example moves just the left slider again... very confusing o_O
I would be grateful for any tips or experiences, which would help me solve this issue.
Upvotes: 0
Views: 363
Reputation: 1731
I tried an experiment with the following HTML:
<html>
<body>
<slider>
<span class="pointer" style="left: 58.878px"></span>
<span class="pointer" style="left: 187.024px"></span>
</slider>
</body>
</html>
Then I ran the following simple test code:
public class FooTest {
@Test
public void foo() {
WebDriver driver = new FirefoxDriver();
FooPage page = new FooPage();
PageFactory.initElements(driver, page);
driver.get("file:///Users/aarondavis/Desktop/index.html");
for (WebElement elem : page.getPointers()) {
System.out.println(elem);
System.out.println(elem.getAttribute("style"));
}
driver.quit();
}
public static final class FooPage {
@FindBy(xpath = "//slider/span")
private List<WebElement> pointers;
public List<WebElement> getPointers() {
return pointers;
}
}
}
The output shows both elements being iterated over:
[[FirefoxDriver: firefox on MAC (fdbc9c63-a173-d44b-97be-c328210ce0bf)] -> xpath: //slider/span]
left: 58.878px;
[[FirefoxDriver: firefox on MAC (fdbc9c63-a173-d44b-97be-c328210ce0bf)] -> xpath: //slider/span]
left: 187.024px;
What is the code you are using to move the slider (e.g. the body of the resetSlider
method)? My guess is either there's an issue in that method or that there are other elements on the page that match //slider/span
that might be being retrieved. You can check that by simply printing the size of the list (e.g. System.out.println(page.getPointers.size());
). Another possibility is if there is validation of the sliders on the page side (e.g. you can't move the right one to the left of the left one and/or they have to be a certain distance, etc).
Note: BTW the output you posted above doesn't show the same id ... this sort of toString:
[[FirefoxDriver: firefox on MAC (fdbc9c63-a173-d44b-97be-c328210ce0bf)] -> xpath: //slider/span]
is simply the web element toString. That long dash-separated string is not an id of the element but rather of the web driver. Also please note you don't need to use a @FindAll...
to get a list of elements. That is for a different semantic case. Whether or not you get a list of elements with a @FindBy
in a page object is determined simply by if the field it is annotating is declared as a type of WebElement
or a List<WebElement>
.
Upvotes: 1