简体   繁体   English

使用Mocha beforeEach的Selenium JavaScript为每个测试创建新的浏览器实例

[英]Selenium JavaScript with Mocha beforeEach to create new browser instance for every test

I am using Selenium with JavaScript and Mocha as the test framework. 我使用Selenium与JavaScript和Mocha作为测试框架。

I am trying to create a new webdriver instance before each test case (and also quit driver instance after each test case) using beforeEach() and afterEach() methods in Mocha. 我正在尝试使用Mocha中的beforeEach()和afterEach()方法在每个测试用例之前创建一个新的webdriver实例(并且还在每个测试用例之后退出驱动程序实例)。 This is currently what my tests look like right now: 这是我目前的测试结果:

test = require('selenium-webdriver/testing'),
webdriver = require('selenium-webdriver');

test.describe('User A Actions', function(){
  this.timeout(10000);

  test.describe('User A page 1', function(){
    test.it('should see X on page', function(){
      var driver = new webdriver.Builder().withCapabilities(
        webdriver.Capabilities.chrome()).build();
      driver.get('http://mywebsite.org');

      loginUserA(driver);
      goToPage1(driver);
      // assert some element is there
      driver.quit()
  });

  test.describe('User A page 2', function(){
    test.it('should see Y on page', function(){
      var driver = new webdriver.Builder().withCapabilities(
        webdriver.Capabilities.chrome()).build();
      driver.get('http://mywebsite.org');

      loginUserA(driver);
      goToPage2(driver);
      // assert some other element is there
      driver.quit()
  });
});

As you can see, it is creating a new webdriver instance at the beginning of each test, and also quitting it. 正如您所看到的,它是在每个测试开始时创建一个新的webdriver实例,并且还要退出它。 I feel it is extremely repetitive to have these at the top of each test. 我觉得在每次测试的顶部都有这些非常重复。 Also, if the test fails, it won't reach driver.quit() which leaves the browser hanging. 此外,如果测试失败,它将无法到达driver.quit() ,这会使浏览器挂起。

I was hoping to put a beforeEach() method and afterEach() method inside the top level describe, so it would resemble something like this: 我希望在顶级描述中放置一个beforeEach()方法和afterEach()方法,所以它会像这样:

test.describe('User A Actions', function(){
  this.timeout(10000);

  beforeEach(function(){ 
    var driver = new webdriver.Builder().withCapabilities(
      webdriver.Capabilities.chrome()).build();
    driver.get('http://mywebsite.org');
  });

  afterEach(function(){
    driver.quit();
  });

  test.describe('User A page 1', function(){
    loginUserA(driver);
    goToPage1(driver);
    // assert some element is there
  });

  test.describe('User A page 2', function(){
    loginUserA(driver);
    goToPage2(driver);
    // assert some other element is there
  });
});

When I try this, I am getting an error ReferenceError: driver is not defined . 当我尝试这个时,我收到一个错误ReferenceError: driver is not defined How can I fix this? 我怎样才能解决这个问题?

Mocha does not change the normal rules of JavaScript scoping. Mocha不会改变JavaScript范围的正常规则。 If you do something that does not respect the general rules of scoping in JavaScript, then Mocha won't make it work. 如果你做的事情不符合JavaScript中的范围规则,那么Mocha将无法使其发挥作用。

You need to declare driver in your test.describe block rather than in your beforeEach hook: 您需要在test.describe块中而不是在beforeEach钩子中声明driver

test.describe('User A Actions', function(){
  var driver; // Declare it here.
  this.timeout(10000);

  test.beforeEach(function(){ 
    driver = new webdriver.Builder().withCapabilities(
      webdriver.Capabilities.chrome()).build();
    driver.get('http://mywebsite.org');
  });

  // ... etc ...

Also, make sure you use the API exported by Selenium, so test.beforeEach rather than beforeEach directly (same with all the other hooks, including the afterEach you already have). 另外,请确保使用Selenium导出的API,因此test.beforeEach而不是beforeEach直接(与所有其他挂钩相同,包括您已经拥有的afterEach )。 Otherwise, you are skipping the control flow management that Selenium does for you when you use the exported API. 否则,当您使用导出的API时,您正在跳过Selenium为您执行的控制流管理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM