0

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:

  • PHPUnit 3.7.14
  • PHPUnit_Selenium 1.2.12
  • Selenium Server 2.30.0

3 Answers 3

2
+50

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)

Sign up to request clarification or add additional context in comments.

Comments

2

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);

Comments

1

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.