首页 > 解决方案 > 将 codeception 测试名称传递给 browserstack

问题描述

我想将我的测试名称传递给 browserstack,以便它们记录在 browserstack 界面中的会话值(名称)中

在我的验收助手中,我定义了以下方法

 /**
 * HOOK: before test
 *
 * We use this method to set the test name which will be logged in BrowserStack
 * https://www.browserstack.com/automate/capabilities
 * @param \Codeception\TestInterface $test
 */
public function _before(\Codeception\TestInterface $test)
{
    codecept_debug('_before');
    codecept_debug($this->getModule('WebDriver')->_getConfig()['capabilities']);

    $config['capabilities'] = $this->getModule('WebDriver')->_getConfig()['capabilities'];
    $config['capabilities']['name'] = $test->getName();
    $this->getModule('WebDriver')->_setConfig($config);  

    codecept_debug($this->getModule('WebDriver')->_getConfig()['capabilities']);
}

我的方法有一些问题

  1. 此事件似乎在测试登录到 browserstack 后触发(这是我的主要问题 - 我正在寻找注入此名称值的正确位置)
  2. 有时会以错误的名称记录测试 - 多个测试将使用相同的名称

我应该使用哪个事件来实现我的目标?

标签: codeceptionbrowserstack

解决方案


通过 codeception,特别是 webdriver 源代码,我看到了这个: https ://github.com/Codeception/Codeception/blob/2.5/src/Codeception/Module/WebDriver.php#L394

 /**
 * Change capabilities of WebDriver. Should be executed before starting a new browser session.
 * This method expects a function to be passed which returns array or [WebDriver Desired Capabilities](https://github.com/facebook/php-webdriver/blob/community/lib/Remote/DesiredCapabilities.php) object.
 * Additional [Chrome options](https://github.com/facebook/php-webdriver/wiki/ChromeOptions) (like adding extensions) can be passed as well.
 *
 * ```php
 * <?php // in helper
 * public function _before(TestInterface $test)
 * {
 *     $this->getModule('WebDriver')->_capabilities(function($currentCapabilities) {
 *         // or new \Facebook\WebDriver\Remote\DesiredCapabilities();
 *         return \Facebook\WebDriver\Remote\DesiredCapabilities::firefox();
 *     });
 * }
 * ```
 *
 * to make this work load `\Helper\Acceptance` before `WebDriver` in `acceptance.suite.yml`:
 *
 * ```yaml
 * modules:
 *     enabled:
 *         - \Helper\Acceptance
 *         - WebDriver
 * ```
 *
 * For instance, [**BrowserStack** cloud service](https://www.browserstack.com/automate/capabilities) may require a test name to be set in capabilities.
 * This is how it can be done via `_capabilities` method from `Helper\Acceptance`:
 *
 * ```php
 * <?php // inside Helper\Acceptance
 * public function _before(TestInterface $test)
 * {
 *      $name = $test->getMetadata()->getName();
 *      $this->getModule('WebDriver')->_capabilities(function($currentCapabilities) use ($name) {
 *          $currentCapabilities['name'] = $name;
 *          return $currentCapabilities;
 *      });
 * }
 * ```
 * In this case, please ensure that `\Helper\Acceptance` is loaded before WebDriver so new capabilities could be applied.

这似乎产生了与我原来的问题相同的错误,

  1. 有些测试没有设置他们的名字
  2. 一些测试具有前一个测试集的名称

编辑:发生上述情况的原因是因为在我的环境文件/设备定义中有一个声明

 modules:
  enabled:
   - WebDriver

删除 enabled 和 webdriver 定义允许回退到 accept.suite.yml 上,然后按预期正常工作


推荐阅读