Don't stop on HTTP error 403

时光怂恿深爱的人放手 提交于 2019-12-05 21:33:22

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();
}
nirvdrum

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!