【问题标题】:php Codeception not-so-quick start: [PHPUnit\Framework\Exception] Undefined index: ELEMENTphp Codeception 不那么快开始:[PHPUnit\Framework\Exception] 未定义索引:ELEMENT
【发布时间】:2026-02-24 06:10:01
【问题描述】:

所以,我忠实地遵循了 Codeception Quick Start instructions。我使用 PhpBrowser 运行第一个示例测试...

# Codeception Test Suite Configuration
#
# [further comments omitted]
# 
actor: AcceptanceTester
modules:
    enabled:
        - PhpBrowser:
            url: 'http://office.localhost/'
            browser: 'firefox'
        - \Helper\Acceptance

和测试:

<?php

class FirstCest
{               
    public function frontpageWorks(AcceptanceTester $I)
    {
        $I->amOnPage('/');
        $I->see('We hope you enjoy it');

    }
}

一切都很好。

然后我把配置改成这样:

actor: AcceptanceTester
modules:
    enabled:
        - WebDriver:
            url: 'http://office.localhost/'
            browser: 'firefox'
        - \Helper\Acceptance

按照说明,我已经安装并启动并运行了 Selenium,然后我们就走了......

1) FirstCest: Frontpage works
 Test  tests/acceptance/FirstCest.php:frontpageWorks

  [PHPUnit\Framework\Exception] Undefined index: ELEMENT  


Scenario Steps:

 2. $I->see("InterpretersOffice") at tests/acceptance/FirstCest.php:22
 1. $I->amOnPage("/") at tests/acceptance/FirstCest.php:21

#1  /opt/www/court-interpreters-office/vendor/facebook/webdriver/lib/Remote/RemoteWebDriver.php:198
#2  Codeception\Module\WebDriver->see
#3  /opt/www/court-interpreters-office/tests/_support/_generated/AcceptanceTesterActions.php:363
#4  /opt/www/court-interpreters-office/tests/acceptance/FirstCest.php:22
#5  FirstCest->frontpageWorks

Selenium 正在驱动 Firefox,页面已加载,$I 想要 see() 的内容在那里,所以这不是问题。我已经在源头上戳了一下,但还没有弄清楚。我尝试将 $I-&gt;see() 更改为 $I-&gt;seeInSource() 并发现确实有效,FWIW。

有什么想法吗?

【问题讨论】:

标签: php selenium-webdriver phpunit codeception


【解决方案1】:

问题显然是 Facebook 的 php-webdriver 与当前的 Firefox 不兼容。

Thesethreads 更详细地讨论该问题,php-webdriver issue #469 跟踪添加完整的 W3C WebDriver 支持(这将解决不兼容问题)。

一种解决方法是在启动 Selenium 时添加 -enablePassthrough false 参数。例如:

java -Dwebdriver.gecko.driver=./geckodriver -jar selenium-server-standalone-3.8.1.jar -enablePassThrough false

很遗憾,Selenium removed support for pass through mode in 3.9,所以你必须使用旧版本。

另一种解决方法是切换到 Chrome。

【讨论】:

【解决方案2】:

就我而言,唯一的解决方案是:

Install chromedriver in a path that is in $PATH (/usr/bin or /bin)

并在您的测试类中使用:

$capabilities = DesiredCapabilities::chrome()

它适用于执行 Selenium 标准方式:

 java -jar selenium-server-standalone-3.14.0.jar

【讨论】:

    【解决方案3】:

    我的 Ubuntu 版本有不同的情况。 18.x:

    1. 供应商文件已经存在多年,我必须 rm 供应商折叠并使用 php composer.phar require facebook/webdriver 为我的 PHP 库重建它。

    2. selenium-server-standalone-x.jar 和 chromedriver 的版本不匹配。所以下载更多版本并尝试,最后你会得到一对工作。

    【讨论】: