之前配置的 ChromeDriver 服务仍在运行/使用 Grunt 运行 Webdriver 测试 [英] The previously configured ChromeDriver service is still running / Running Webdriver tests with Grunt

查看:32
本文介绍了之前配置的 ChromeDriver 服务仍在运行/使用 Grunt 运行 Webdriver 测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为利用 selenium-webdriver 的 React 应用程序编写 mocha 测试.

Hi I am trying to write mocha tests for my react application that leverage selenium-webdriver.

我有几个问题,但帮助他们中的任何一个都会有所帮助,因此我可以继续前进.

I have a few questions but help either of them would help so I can move forward.

  1. 首先,理想情况下,我想在不同的测试中共享相同的 webdriver 会话,因为我不关心它们运行的​​顺序.我只想加载一次网页,运行所有测试,然后关闭网页.这可能吗?我最初将我的前后案例放在描述之外的不同文件中,它工作正常......但后来我无法访问我的任何测试文件中的驱动程序实例.

  1. First of all, ideally, I would like to share the same webdriver sessions across my different tests since I do not care about what order they run. I just want to load the webpage once, run all of the tests and then close the webpage. Is this possible? I initially put my before and after cases in a different file outside of a describe and it was working fine...but then I could not access the instance of the driver in any of my test files.

如果无法共享同一个会话,那么我该如何解决当我尝试运行两个 specFiles 时出现的以下错误..

If sharing the same session is not possible, then how can I solve the error below which occurs when I try to run two specFiles..

这里是错误:

$ grunt test-e2e
Running "mochatest:e2e" (mochatest) task
Running Mocha tests on files
/Users/userName/Desktop/myReactApp/tests/e2e/testSpecOne.js
/Users/userName/Desktop/myReactApp/tests/e2e/testSpecTwo.js

Error: The previously configured ChromeDriver service is still running. You must shut it down before you may adjust its configuration.
    at Error (native)
    at Object.setDefaultService (/Users/userName/Desktop/myReactApp/node_modules/selenium-webdriver/chrome.js:264:11)
    at Object.<anonymous> (/Users/userName/Desktop/myReactApp/tests/e2e/testSpecTwo.js:8:8)
    at Module._compile (module.js:556:32)
    at loader (/Users/userName/Desktop/myReactApp/node_modules/babel-register/lib/node.js:144:5)
    at Object.require.extensions.(anonymous function) [as .js] (/Users/userName/Desktop/myReactApp/node_modules/babel-register/lib/node.js:154:7)
    at Module.load (module.js:473:32)
    at tryModuleLoad (module.js:432:12)

典型的测试如下所示:

import assert from 'assert';
import test from 'selenium-webdriver/testing';
import webdriver, {By, until} from 'selenium-webdriver';
import chrome from 'selenium-webdriver/chrome';
import chromedriver from 'chromedriver';
import helpers from './helpers.js';

chrome.setDefaultService(new chrome.ServiceBuilder(chromedriver.path).build());


test.describe('Main page', () => {

  let driver = new webdriver
                    .Builder()
                    .withCapabilities(webdriver.Capabilities.chrome())
                    .build();

  test.before(() => {
      helpers.launchTheApp(driver, 'http://localhost:8000/myApp', 'elementOne', 10000);
  });

  test.after(() => {
    helpers.closeTheApp(driver);
  })

  test.it('Test some items appear', () => {

    helpers.checkIfElementIsPresent(driver, By.className, 'elementOne');
    helpers.checkIfElementIsPresent(driver, By.className, 'elementTwo');
    helpers.checkIfElementIsPresent(driver, By.className, 'elementThree');

   });
});

我正在使用 grunt-mocha-test 来运行这些配置如下的测试

I am using a grunt-mocha-test to run these tests configured like this

e2e:{
    options: {
        timeout: 3000000,
        ignoreLeaks: true,
        ui: 'bdd',
        run: true,
        log: true,
        reporter: typeof process.env.FUSION_BUILD_GENERATED === 'undefined' ? 'spec' : 'xunit-file',
        grep: grunt.option('grep')
    },
    src: ['tests/e2e/**/**/*Spec.js']
}

推荐答案

一种可能的解决方案是在隔离的环境中运行会话(例如在 Docker 容器).然而,尝试使用标准 Selenium 实现这一点很复杂 - 您必须手动启动和停止容器或使用基础设施自动化工具,例如 Ansible.这是一个名为 Selenoid 的与 Selenium 兼容的新工具发挥作用的地方.只需在您的测试中请求两个单独的会话,它们将在 Docker 容器内并行运行.这为您提供了很大的灵活性.我的谈话:https://www.youtube.com/watch?v=TGjpc32my0Y

One possible solution is to run your sessions in isolated environments (for example in Docker containers). However trying to achieve this with standard Selenium is complicated - you have to manually start and stop containers or use an infrastructure automation tool like Ansible. Here is where a new Selenium-compatible tool called Selenoid comes into play. Just request two separate sessions in your test and they will run in parallel inside Docker containers. This gives you a lot of flexibility. My talk about it: https://www.youtube.com/watch?v=TGjpc32my0Y

这篇关于之前配置的 ChromeDriver 服务仍在运行/使用 Grunt 运行 Webdriver 测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆