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?
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.
To select multiple elements by class, use:
$elements = $this->elements($this->using('css selector')->value('.my_class'));
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With