2

PHPUnit我正在使用and对我的 Yii 应用程序进行单元测试Selenium-server

代码:

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 文档和 PHPUnit 文档中说它可以..

Yii 框架不支持这个吗?还是我做错了什么?

4

2 回答 2

3

与其将 url 用作方法,不如将其用作属性:

$this->url

我现在在工作,可以检查一下。我认为 PHPUnit 与 Selenium 的正确代码是:

$driver->getCurrentPageUrl();

所以它是来自驱动程序实例的方法。

于 2013-05-14T15:25:43.943 回答
0

结果是我在测试中扩展的 Yii 类PHPUnit_Extensions_SeleniumTestCase_Driver的祖先类。WebTestCase

PHPUnit_Extensions_SeleniumTestCase_Driver实现方法 getLocation() 保存当前浏览器 url。这允许我使用$this->getLocation()

SeleniumTestCase_Driver 类参考

我的代码的结果:

//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()
);
于 2013-05-15T08:46:24.483 回答