将代码接收测试名称传递给浏览器堆栈

时间:2018-10-13 07:11:36

标签: 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. 测试在浏览器堆栈中登录后似乎触发了此事件(这是我的主要问题-我正在寻找合适的位置注入此名称值)
  2. 测试有时会以错误的名称记录-多个测试将使用相同的名称

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

1 个答案:

答案 0 :(得分:0)

通过代码接收,尤其是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

删除已启用和webdriver的定义将使回退到accept.suite.yml,然后可以按预期正确运行

相关问题