Reputation: 12322
It is simple to select an element by specifying its class, in PHPUnit Selenium 2 test case:
$element = $this->byClassName("my_class");
However, even if there are two items of my_class
, the selector picks only one of them (probably the first one). How can I select all of them? I'd appreciate something like allByClassName
:
$elements = $this->allByClassName("my_class");
foreach($elements as $element) {
doSomethingWith($element);
}
Is there anything like allByClassName
in PHPUnit Selenium 2 extension?
Upvotes: 10
Views: 9641
Reputation: 2092
I had exactly the same issue, so I tried the solution that @David posted. It works, but somehow Selenium was trying to find the element again and again, so my test time increased 15 secs just on this.
To be faster, I ended up creating a ID for my class and counting the elements inside:
$elements = $this->elements($this->using('css selector')->value('#side-menu li'));
$this->assertEquals(0, count($elements));
Upvotes: 0
Reputation: 1266
To select multiple elements by class, use:
$elements = $this->elements($this->using('css selector')->value('.my_class'));
Upvotes: 1
Reputation: 741
Pavel, you can find guidance on how to select multiple elements here: https://github.com/sebastianbergmann/phpunit-selenium/blob/b8c6494b977f79098e748343455f129af3fdb292/Tests/Selenium2TestCaseTest.php
lines 92-98:
public function testMultipleElementsSelection()
{
$this->url('html/test_element_selection.html');
$elements = $this->elements($this->using('css selector')->value('div'));
$this->assertEquals(4, count($elements));
$this->assertEquals('Other div', $elements[0]->text());
}
(This file contains the tests for the Selenium2TestCase class itself, so it's great for learning about its capabilities)
Following this method, you could retrieve all elements with a certain class like this:
$elements = $this->elements($this->using('css selector')->value('*[class="my_class"]'));
Hope this helps.
Upvotes: 19
Reputation: 9188
The WebDriver method findElements(By by) should do exactly what you need.
Upvotes: 0