phpunit-selenium
phpunit-selenium copied to clipboard
Assert element not present
I was wondering if there is a workaround for testing that an element is not present. Currently, if an element is not present, it just throws an exception. I am looking for something similar to assertElementNotPresent from PHPUnit_Selenium_Testcase
The JSONWireProtocol doesn't seem to support this, so I don't think anything non-workaroundish is possible.
Because you asked for a workaround: I try to avoid using it, but when needed, I typically use something like:
$this->assertTrue(
$this->waitUntil(
function($test) {
try {
$displayed = $test->byId('...elementId...')->displayed();
return !$displayed;
} catch (\Exception $e) {
//TODO check $e for type and errorCode
return true;
}
}
)
);
waitUntil lowers the implicitWait to a second or less, so the call will return pretty fast when the element really isn't there.
What the closure does ist: If the element is there and displayed, $displayed is true, the function returns false and the outer "assertTrue" will fail. (immediately) If the element is there and NOT dispayed, $displayed is false, the function returns true and the outer "assertTrue" will pass. (immediately) If the element is NOT there, the "byId"-Call will throw an exception (after a short timeout), which is catched and followed by the return true. Thus, the outer "assertTrue" will pass. (short timeout)
You should check the exception (specific class and errorCode) to avoid missing a more critical exception. Of course, you have to inject the '...elementId...' from some variable by using "function ($test) use ($elementId) {" or something similar.
Thanks for the reply.
After looking around a little, I came up with something similar (https://github.com/phpmyadmin/phpmyadmin/blob/5609d557916291fcc2134fb023f26f557f35c297/test/selenium/Helper.php#L234) to what you have suggested except that I haven't used the waitUntil method because I did not want to wait till the element was present.
Thanks again!
Usually you can't easily assert that something does not happen or is not there in asynchronous tests. The problem is that the element could appear some time after your check due to page being loaded partially, new Ajax requests and so on. We can never be sure when a page has stopped loading or transforming itself.
As this also applies to every assertion that an element is there (byId, etc.) wouln't it be compliant to allow the element to disappear within the implicitWait?
2013/8/24 Giorgio Sironi [email protected]
Usually you can't easily assert that something does not happen or is not there in asynchronous tests. The problem is that the element could appear some time after your check due to page being loaded partially, new Ajax requests and so on. We can never be sure when a page has stopped loading or transforming itself.
— Reply to this email directly or view it on GitHubhttps://github.com/sebastianbergmann/phpunit-selenium/issues/270#issuecomment-23205176 .
It is a nice idea, if someone wants to make a PR it should work like this. However, document thoroughly the behavior with tests in Selenium2TestCaseTest as it is not an API shipped with Selenium but our addition.