Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select all matching elements in PHPUnit Selenium 2 test case

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?

like image 293
Pavel S. Avatar asked May 19 '13 18:05

Pavel S.


2 Answers

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.

like image 76
David Avatar answered Oct 27 '22 17:10

David


To select multiple elements by class, use:

$elements = $this->elements($this->using('css selector')->value('.my_class'));
like image 28
Andrew Avatar answered Oct 27 '22 16:10

Andrew