我正在使用PHPUnit
和Selenium-server
对我的Yii应用程序进行单元测试。
代码:
class StartSurveyRedirectTest extends WebTestCase
{
public $fixtures=array(
'sessions'=>'SurveySession',
'surveys' => 'Survey',
);
public function testActive() {
$survey = $this->surveys('survey_active');
$user_id = 1;
$imid = 'nl1234-9876';
$this->open('veldwerk/survey/redirect?survey_id=' . $survey->survey_id .
'&user_id=' . $user_id .
'&imid=' . $imid .
'&' . $survey->security_parameter_name . '=' . $survey->getSecurityParameterValue()
);
//assert if redirected
$this->assertNotEquals($this->url(),TEST_BASE_URL .
'veldwerk/survey/redirect?survey_id=' . $survey->survey_id .
'&user_id=' . $user_id .
'&imid=' . $imid .
'&' . $survey->security_parameter_name . '=' . $survey->getSecurityParameterValue()
);
}
}
开始测试时的结果:
There was 1 error:
1) StartSurveyRedirectTest::testActive
Method url not defined.
D:\workspace\Pi\framework\test\CWebTestCase.php:63
D:\workspace\Pi\protected\tests\functional\StartSurveyRedirectTest.php:27
D:\workspace\Pi\protected\tests\functional\StartSurveyRedirectTest.php:27
Caused by
BadMethodCallException: Method url not defined.
D:\workspace\Pi\framework\test\CWebTestCase.php:63
D:\workspace\Pi\protected\tests\functional\StartSurveyRedirectTest.php:27
D:\workspace\Pi\protected\tests\functional\StartSurveyRedirectTest.php:27
所以我真正想要测试的是用户是否已被重定向。我认为最简单的方法是检查URL,但事实证明它不起作用?虽然在selenium docs和PHPUnit文档中说它可以..
Yii框架不支持这个吗?或者我做错了什么?
答案 0 :(得分:3)
不要使用url作为方法,而是将其作为属性尝试:
$this->url
我现在在工作,能够检查出来。我认为使用Selenium的PHPUnit的正确代码是:
$driver->getCurrentPageUrl();
所以这是来自驱动程序实例的方法。
答案 1 :(得分:0)
结果PHPUnit_Extensions_SeleniumTestCase_Driver
是我测试中延伸的Yii类WebTestCase
的祖先类。
PHPUnit_Extensions_SeleniumTestCase_Driver
实现了getLocation()方法,该方法保存当前的浏览器URL。这允许我使用$this->getLocation()
SeleniumTestCase_Driver class reference
我的代码的结果:
//assert if redirected
$this->assertNotEquals($this->getLocation(),TEST_BASE_URL .
'veldwerk/survey/redirect?survey_id=' . $survey->survey_id .
'&user_id=' . $user_id .
'&imid=' . $imid .
'&' . $survey->security_parameter_name . '=' . $survey->getSecurityParameterValue()
);