【问题标题】:Expression is not allowed in a variable outside of a function函数外的变量中不允许表达式
【发布时间】:2015-05-16 21:37:38
【问题描述】:

我想将目录链接到 $screenshotPath 字符串,但 PHPStorm 中出现错误:不允许将表达式作为字段默认值。我该如何解决?代码:

use PHPUnit\Extensions\SeleniumTestCase;
use ApplicationTest\Bootstrap;

class AdminLoginLogoutTest extends PHPUnit_Extensions_SeleniumTestCase {
    protected $captureScreenshotOnFailure = true;
    //$screenshotPath is giving me this error...
    protected $screenshotPath = __DIR__ . "/FailedTestsScreenshots";
    protected $screenshotUrl = "http://icho/screenshots";

    protected $path = "http://icho";

    protected function SetUp() {
        $this->setBrowser( "*firefox" );
        $this->SetBrowserUrl( $this->path );
    }

    public function testAdminLoginLogout() {
        $this->open( "/admin" );
        $this->type( "name=username", "test" );
        $this->type( "name=password", "test" );
        $this->click( "id=submitbutton" );
        $this->waitForPageToLoad( "30000" );
        $this->assertEquals( "Dashboard", $this->getText( "link=Dashboard" ) );
        $this->assertEquals( "Haios", $this->getText( "link=Haios" ) );
        $this->assertEquals( "POs", $this->getText( "link=POs" ) );
        $this->assertEquals( "Staco's", $this->getText( "link=Staco's" ) );
        $this->assertEquals( "Mail templates", $this->getText( "link=Mail templates" ) );
        $this->assertEquals( "Mailings", $this->getText( "link=Mailings" ) );
        $this->assertEquals( "Sytem texts", $this->getText( "link=System texts" ) );
        $this->assertEquals( "Advanced admin", $this->getText( "link=Advanced admin" ) );
        $this->click( "css=a[title='Sign Out']" );
        $this->click( "id=bot2-Msg1" );
        $this->waitForPageToLoad( "30000" );
        $this->assertEquals( "Login is vereist", $this->getText( "css=h2" ) );
    }
}

【问题讨论】:

  • 你在setUp()期间尝试过decrale吗?

标签: php selenium phpunit


【解决方案1】:

将其移至构造函数(或任何其他函数) - 您似乎没有该类的任何构造函数,但无论如何都会调用 __construct

protected $screenshotPath = '';

public function __construct() {
    $this->$screenshotPath = __DIR__ . "/FailedTestsScreenshots";
}

或进入SetUp()

protected function SetUp() {
    $this->$screenshotPath = __DIR__ . "/FailedTestsScreenshots";
    $this->setBrowser( "*firefox" );
    $this->SetBrowserUrl( $this->path );
}

【讨论】:

  • 如果它们不在函数之外,Selenium 无法识别它们
  • @Milen 你声明$screenshotPath 如上,只是一个空值protected $screenshotPath = '';然后在函数或构造函数中设置所需的值。
猜你喜欢
  • 1970-01-01
  • 2016-05-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-28
  • 1970-01-01
相关资源
最近更新 更多