【问题标题】:How can we use protractor beforeAll and afterAll hooks for opening and closing a browser in each describe block我们如何使用量角器 beforeAll 和 afterAll 钩子在每个描述块中打开和关闭浏览器
【发布时间】:2017-12-27 09:40:53
【问题描述】:

我已使用以下内容,但我得到此驱动程序实例没有有效的会话 ID(您是否调用了 WebDriver.quit()?)并且可能不再使用。有人可以帮忙吗?

Sample.js 文件,代码如下:

describe('angularjs homepage', function() {
    beforeAll(function(){
         browser.get('https://xxxx/');
         console.log('calling before all');
     });

     afterAll(function(done){
         console.log('calling after all');
         browser.quit();
         process.nextTick(done);
     });

另一个也有相同代码的文件:

describe('angularjs homepage', function() {
        beforeAll(function(){
             browser.get('https://xxxx/');
             console.log('calling before all');
         });

         afterAll(function(done){
             console.log('calling after all');
             browser.quit();
             process.nextTick(done);
         });

现在我想使用 beforeAll 和 AfterAll 函数,浏览器应该通过它们在每个 js 文件执行中打开和关闭。当我采用这种方法时,我收到错误,因为此驱动程序实例没有有效的会话 ID(您是否调用了 WebDriver.quit()?)并且可能不再使用。

【问题讨论】:

  • 使用browser.close()
  • 我也使用了相同的,但问题仍然相同。我想为每个描述块打开和关闭浏览器。
  • 你可以试试:beforeAll(async function() { await browser.get('h.ttp...'); })。也许量角器不知道您正在beforeAll() 中执行异步函数。

标签: protractor


【解决方案1】:

很难准确理解您的要求,因为您只发布了一个 describe,但在 cmets 中我看到您说您希望在每个 describe 之前打开浏览器。假设您嵌套了 describe 块,您可以使用 beforeEach()afterEach() 来执行此操作。

describe('some test', () => {
  beforeAll(() => {
    //some setup code
  }

  beforeEach(() => {
    browser.get('https://xxxx/');
  }

  afterEach(() => {
    browser.restart();
  }

  afterAll(() => {
    //some tear down code
  }

  describe('something', () => {
    it('should test something', () => {
      //test code
    }
  }

  describe('another thing', () => {
    it('should test another thing', () => {
      //test code
    }
  }
}

在上面的例子中,执行顺序是:

describe
beforeAll
beforeEach (open browser)
describe - something
it - should test something
afterEach (close browser)
beforeEach (open browser)
describe - another thing
it - should test another thing
afterEach (close browser)
afterAll

还有一个量角器配置选项restartBrowserBetweenTests 可以在每次测试之前重新启动浏览器,但我认为这不是您所要求的。

【讨论】:

  • 我已经编辑了原始问题。请让我知道它是否清楚
  • 好的,尝试将您的 browser.quit() 更改为 browser.restart() 看看是否有帮助。
【解决方案2】:

我认为您需要: 之前每个和 每次之后

它们将在“describe”中的每个“it”块之前执行。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-23
    • 2020-10-10
    • 1970-01-01
    • 2017-03-02
    • 1970-01-01
    相关资源
    最近更新 更多