In one of my Selenium test cases, I try to ensure that certain pages can't be accessed. Instead, HTTP return code 403 should be given.
However, here Selenium terminates test execution with the following exception:
com.thoughtworks.selenium.SeleniumException: XHR ERROR: URL = http://user:password@www.example.com/admin Response_Code = 403 Error_Message = Forbidden
Any way to work around that?
Seems like I have to answer the question myself...
I now surround the "open" call with a try...catch block. There, I parse the exception message if it contains the 403 code and XHR ERROR. Seems to me not very clean, but it works.
I was trying to test for HTTP 403 errors using PHPUnit. I examined the PHPUnit Selenium Extension code and found that the driver ends the session as soon as it receives a bad response, and there does not appear to be a way to bypass this functionality.
I ended up using a try-catch solution and restarting the Selenium session:
try {
$this->open('restricted_url');
$this->assertTitle('The page cannot be displayed');
} catch (PHPUnit_Framework_Exception $e) {
$this->start();
}
When the Selenium session is restarted, all the session information is lost, as is to be expected, so you will need to build your session again in order to run more tests:
$this->loginAsGuest();
try {
$this->open('admin_url');
$this->assertTitle('The page cannot be displayed');
} catch (PHPUnit_Framework_Exception $e) {
$this->start();
$this->loginAsGuest();
}
I don't know what language you're using, but I think you can get around this by instructing Selenium to ignore the status code when opening a page.
In Ruby:
@browser.remote_control_command('open', [url, 'true'])
In C#:
((DefaultSelenium)selenium).Processor.DoCommand("open", new string[]{url, "true"}))
I believe the behavior in Selenium trunk is to now ignore the status code by default. So, you could try building that and see if it works out for you.