【问题标题】:PHPUnit and WebDriver - Where should I add implicitWait() to?PHPUnit 和 WebDriver - 我应该在哪里添加implicitWait()?
【发布时间】:2014-01-20 20:56:08
【问题描述】:

我读了一些文章,他们说implicitWait() 在全球范围内有效。但我不确定 我应该在哪里设置它。

这是我的代码:

public  function setUp(){
    $this->setHost('10.10.60.95');
    $this->setPort(4444);
    $this->setBrowser('firefox');
    $this->setBrowserUrl('http://www.ABC.com');
    $this->prepareSession();

}

public function testTitle()
{
    $this->url('http://www.ABC.com/');
    $this->assertEquals('ABC', $this->title());
}

我应该在哪里添加implicitWait() ? setUp 函数还是 testTitle 函数?

$this->timeouts()->implicitWait(10000);

谢谢!

回答:

正如@Arran 所说,实际上我应该在大多数情况下将此代码放在 setUp() 函数中,因为它可以在全球范围内工作。

public  function setUp(){
    $this->setHost('10.10.60.95');
    $this->setPort(4444);
    $this->setBrowser('firefox');
    $this->setBrowserUrl('http://www.ABC.com');
    $this->prepareSession();
    $this->timeouts()->implicitWait(10000);
}

如果你想为一个测试函数设置一个特殊的超时时间,你可以在里面添加这个代码。

public function testTitle()
{
    $this->url('http://www.ABC.com/');
    $this->timeouts()->implicitWait(5000);
    $this->assertEquals('ABC', $this->title());
}

【问题讨论】:

  • 严格来说应该在setup函数中,除非你打算不断改变它的值。
  • 谢谢@Arran,我会写更多的脚本来测试这个

标签: selenium selenium-webdriver phpunit


【解决方案1】:

我认为您有 2 个选项可以尝试。虽然不是 100% 确定它们,但您可以尝试一下。请根据您的需要更改它们。

1.
// wait for at most 10 seconds until the URL is 'http://example.com/account'.
// check again 500ms after the previous attempt.
$driver->wait(10, 500)->until(function ($driver) {
  return $driver->getCurrentURL() === 'http://example.com/account';
})

2.
$this->_session->timeouts()->implicit_wait(10000); // 1 sec = 1000

【讨论】:

  • 谢谢,我在 github 上找到了一些代码示例。看起来像这样: public function testTitle() { $this->url('ABC.com/');$this->timeouts()->implicitWait(10000); $this->assertEquals('ABC', $this->title()); }
  • 如果答案对您有帮助,请选择它作为答案,它将对其他人有所帮助。谢谢!
猜你喜欢
  • 2020-12-29
  • 1970-01-01
  • 2020-03-12
  • 1970-01-01
  • 2016-09-08
  • 2020-11-17
  • 1970-01-01
  • 2020-03-28
  • 1970-01-01
相关资源
最近更新 更多