Php 如何在Selenium RC测试中获取当前浏览器?
经过几个小时的努力搜索,我发现一个问题仍然无法解决:你是我最后的机会 我有一个简单的PHP页面(比如“bad_request.PHP”),它会以400个错误请求状态代码进行响应:Php 如何在Selenium RC测试中获取当前浏览器?,php,browser,selenium-rc,Php,Browser,Selenium Rc,经过几个小时的努力搜索,我发现一个问题仍然无法解决:你是我最后的机会 我有一个简单的PHP页面(比如“bad_request.PHP”),它会以400个错误请求状态代码进行响应: <?php header('Status: 400 Bad Request', false, 400); header($_SERVER['SERVER_PROTOCOL'].' 400 Bad Request', false, 400); exit('Error message');
<?php
header('Status: 400 Bad Request', false, 400);
header($_SERVER['SERVER_PROTOCOL'].' 400 Bad Request', false, 400);
exit('Error message');
?>
我有一个测试方法,打开“bad_request.php”并检查状态代码和错误消息
问题是,对于400状态代码,所有浏览器的行为都不相同:Internet Explorer显示自己的错误页面(并且不显示使用退出功能发送的“错误消息”),而其他浏览器显示一个带有“错误消息”的简单空白页面
这就是为什么我希望我的测试方法是这样的:
public function testBadRequest()
{
$this->open('bad_request.php');
/*
* Test the status code with my own method
*/
$this->assertStatusCode(400);
/*
* Test the page content
*/
// Case of Internet Explorer
if (preg_match('`^Internet Explorer`', $this->browserName )) {
$this->assertTextPresent('HTTP 400');
}
// Case of any other browser
else {
$this->assertTextPresent('Error message');
}
}
遗憾的是,browserName始终为空,我没有找到任何解决方案来在测试方法中获取当前浏览器
这里有人有解决方案吗?在C#中(不同于您的PHP问题,但它可能会帮助像我一样遇到这个问题的人),我使用了
driver.Capabilities.BrowserName
其中的驱动程序是当前测试运行的OpenQA.Selenium.Remote.RemoteWebDriver。我最终找到了一个解决方案:编辑文件SeleniumTestCase.php(可能在PHPUnit\Extensions文件夹中)并找到方法“getDriver”。在第一个条件块(if(isset($browser['name'))
)的末尾添加:$this->browserName=$browser['name')代码>.Nice找到+1用于发布问题。
public function testBadRequest()
{
$this->open('bad_request.php');
/*
* Test the status code with my own method
*/
$this->assertStatusCode(400);
/*
* Test the page content
*/
// Case of Internet Explorer
if (preg_match('`^Internet Explorer`', $this->browserName )) {
$this->assertTextPresent('HTTP 400');
}
// Case of any other browser
else {
$this->assertTextPresent('Error message');
}
}