用于查看OrWaitForElement的Codeception函数

问题描述 投票:3回答:3

我有一个应用程序,我想用selenium / codeception测试。它有很多ajax函数可以更改页面(显示/隐藏页面部分),此时哪些代码处理不好。

我遇到的问题是我想点击按钮/元素

  • 已经在页面上(ajax调用提前完成)
  • 还没在页面上(等待ajax响应)

如果我使用waitForElement()它似乎只在第二种情况下工作(它等待元素出现并继续)但如果元素已经存在,waitForElement将超时并触发失败。

我正在寻找的是一个seeOrWaitForElement()函数,但我无法弄清楚如何在代码中插入逻辑。

这个功能可以在某个地方使用,或者我如何以另一种方式解决这个问题?

selenium codeception
3个回答
1
投票

Codeception在版本2.3.4中引入了一个名为SmartWait的功能,它似乎是一个优雅的解决方案。请注意,它不适用于所有类型的定位器。

来自Codeception documentation

SmartWait

since 2.3.4 version

可以实用地等待元素。如果测试使用的元素尚未在页面上,则Codeception将在失败前等待几秒钟。此功能基于Selenium的隐式等待。 Codeception仅在搜索特定元素时启用隐式等待,在所有其他情况下禁用。因此,测试的性能不受影响。

可以通过在WebDriver配置中设置等待选项来启用SmartWait。它期望等待的秒数。例:

wait: 5

使用此配置,我们进行以下测试:

<?php
// we use wait: 5 instead of
// $I->waitForElement(['css' => '#click-me'], 5);
// to wait for element on page
$I->click(['css' => '#click-me']);

重要的是要了解SmartWait仅适用于特定的定位器:

  • #locator - CSS ID定位器,有效
  • //locator - 一般XPath定位器,有效
  • ['css' => 'button''] - 严格的定位器,工作

但它不会对所有其他定位器类型执行。看例子:

<?php
$I->click('Login'); // DISABLED, not a specific locator
$I->fillField('user', 'davert'); // DISABLED, not a specific locator
$I->fillField(['name' => 'password'], '123456'); // ENABLED, strict locator
$I->click('#login'); // ENABLED, locator is CSS ID
$I->see('Hello, Davert'); // DISABLED, Not a locator
$I->seeElement('#userbar'); // ENABLED
$I->dontSeeElement('#login'); // DISABLED, can't wait for element to hide
$I->seeNumberOfElements(['css' => 'button.link'], 5); // DISABLED, can wait only for one element

2
投票

你可以用

waitForJs("return document.querySelector('".$element."') != null", $seconds);

这将等到元素存在(如果元素已存在则立即返回)。


0
投票

我使用了waitForJSdocs并且它起作用了。例:

$I->see('Jane Doe', '#table tbody tr');
$I->click(['css' => '#reload-ajax']);    // Ajax trigger here
$I->waitForJS("return $.active == 0;", 60);
$I->see('John Doe', '#table tbody tr');
© www.soinside.com 2019 - 2024. All rights reserved.