Fabian Schmengler
Fabian Schmengler

Reputation: 24551

PHPUnit_Selenium: Don't throw exceptions if element not found?

I am using the PHPUnit_Selenium extension and encounter some unwanted behaviour if an element does not exist:

Selenium Test Case:

$this->type('id=search', $searchTerm);

Test Output:

RuntimeException: Invalid response while accessing the Selenium Server at 'http: //localhost:4444/selenium-server/driver/': ERROR: Element id=search not found

So, I get an error but I would like to convert it to a failure instead

I considered this:

try {
    $this->type('id=search', $searchTerm);
} catch (RuntimeException $e) {
    $this->fail($e->getMessage());
}

But I don't really want to convert all runtime exceptions to failures and don't see a clean way to distinguish them.

An additional assertion would be great but I can't find one that fits my need. Something like:

$this->assertLocatorExists('id=search'); // ???
$this->type('id=search', $searchTerm);

Am I missing something? Or is there another method that I did not think about?

Used versions:

Upvotes: 0

Views: 3384

Answers (3)

Fabian Schmengler
Fabian Schmengler

Reputation: 24551

For test cases based on SeleniumTestCase, I found the following methods to be useful:

getCssCount($cssSelector)
getXpathCount($xpath)
assertCssCount($cssSelector, $expectedCount)
assertXpathCount($xpath, $expectedCount)

For test cases based on Selenium2TestCase the solution suggested by @Farlan should work, the following methods retrieve an element and throw an exception if no element was found:

byCssSelector($value)
byClassName($value)
byId($value)
byName($value)
byXPath($value)

In my case the tests descend from SeleniumTestCase, so the solution for the example in the question was:

$this->assertCssCount('#search', 1);

Upvotes: 2

Farlan
Farlan

Reputation: 1900

Why not do something like this:

$element = $this->byId('search');

//from https://github.com/sebastianbergmann/phpunit-selenium/blob/master/Tests/Selenium2TestCaseTest.php

In java (sorry, I use Selenium in Java) this would throw an exception if the element with id search is not found. I would check the documentation to see if it is the same behavior in php. Otherwise you can try to see if the $element is valid, eg: is_null($element)

Upvotes: 2

SDC
SDC

Reputation: 14222

Well, you could check the exception message text in the catch block, and re-throw it if it doesn't match Element id=search not found (or a suitable regex).

try {
    $this->type('id=search', $searchTerm);
} catch (RuntimeException $e) {
    $msg = $e->getMessage();
    if(!preg_match('/Element id=[-_a-zA-Z0-9]+ not found/',$msg)) {
        throw new RuntimeException($msg);
    }
    $this->fail($msg);
}

Not ideal, but it would do the trick.

I guess this demonstrates why one should write custom exception classes rather than re-using standard ones.

Or since it's open source, you could, of course, always modify the phpunit Selenium extension to give it a custom exception class.

Upvotes: 1

Related Questions