Codeception 未定义索引:ELEMENT 错误

Codeception undefined index: ELEMENT error

我正在使用 Codeception 进行验收测试。在我的本地电脑上,我可以毫无问题地 运行 使用 selenium 和 chromedriver 进行测试。直到 1 周,它在我的 git 上也能正常工作,但现在出现了这个错误。

"[PHPUnit\Framework\Exception] 未定义索引:ELEMENT"

本地它工作正常没有错误。

我已经搜索了这个错误并找到了一个提示,我必须用“-enablepassthrough false”启动 selenium。但这并不真正有效。

在 1 周之前,所有相同的设置,它工作正常。

这是我的 acceptance.suite.yml:

actor: AcceptanceTester
modules:
    enabled:
        - WebDriver:
            url: 'https://website.com' 
            host: 'selenium__standalone-chrome'
            port: 4444
            browser: chrome 
            window_size: 1920x1080 

这个错误信息...

[PHPUnit\Framework\Exception] Undefined index: ELEMENT

...暗示 ChromeDriverclick()Codeception 有问题。

根据@reinholdfuereder 的 comment within the discussion facebook/php-webdriver - W3C WebDriver protocol support:

  • 当使用默认 W3C 协议时,Codeception (v2.5.6) 测试失败并在 waitForElement() 操作中显示 Undefined index: ELEMENT
  • 使用遗留协议时,waitForElement() 操作测试成功,紧接着看似也成功的 click() 操作,但在 waitForElementNotVisible() 操作中失败。
  • Codeception 的 click() 操作被翻译成 clickElement WebDriver 命令,ChromeDriver v75.
  • 似乎不再支持该命令

解决方案

如果您正在使用 ChromeDriver v75.xChromium v75.x 并且您看到了这个错误,您需要传递额外的 chromeOptions w3c 设置为 true.


结尾

您可以在以下位置找到一些详细的讨论:

  • Cannot call non W3C standard command while in W3C mode (Selenium::WebDriver::Error::UnknownCommandError) with Selenium ChromeDriver in Cucumber Ruby

正如@DebanjanB 所解释的那样,设置上述选项是一种解决方案,这就是将 facebook/php-webdriver 更新到版本 >= 1.7.0

所得到的结果

我认为对您的应用程序来说更可靠的解决方案(或习惯)是使用特定的 docker 图像。

你的 yaml 让我猜你正在使用 selenium/standalone-chrome docker 图像。在最新版本 (3.141.59-palladium) 中,他们将包含的 Chrome 版本从 74 更新为 75(参见:https://github.com/SeleniumHQ/docker-selenium/releases)。

您的本地计算机可能仍然 运行 是 Chrome 74 的旧版本,所以没有问题。但是每当您重建 docker 机器时(例如,当使用某些 CI 环境时),您都会获得最新版本。在这种情况下,一个新的 Chrome 版本。 "forces" 你还用新的 facebook/php-webdriver 升级你的 php 机器。

我建议指定图像的一个版本以防止这些不需要的副作用,例如

image: selenium/standalone-chrome:3.141.59-oxygen

或者,如果您已经 运行 并且 Chrome 75,3.141.59-palladium

对于 Chrome/ChromeDriver v79 设置 w3cacceptance.suite.yml 中的 false帮助:

modules:
  enabled:
    - WebDriver:
        capabilities:
          chromeOptions:
            w3c: false