如何在脚本中只调用一次 browser.get?

How to call browser.get only once in a script?

我是量角器的新手,现在我将 browser.get(URL) 放在第一个 'it' 语句中,然后对于我的 afterEach 语句我做了浏览器返回首页

我想知道是否有更好的地方放置 browser.get 语句以使我的测试更加模块化?我尝试将它放在 beforeEach 语句中,但我不想为我 运行 的每个测试打开一个新的浏览器。有没有更好的地方放置这些前置条件的物品?

欢迎为初学者提供任何其他提示。

您想在 beforeEach 方法中坚持您的 browser.get() 调用:

describe('My page', function() {
  beforeEach(function() {
    browser.get('/the/url');
  });

  it('should load', function() {
    // Some verification logic
  });
});

如果您只想为套件中的所有测试调用一次 browser.get(),请使用 beforeAll():

The beforeAll function is called only once before all the specs in describe are run, and the afterAll function is called after all specs finish. These functions can be used to speed up test suites with expensive setup and teardown.

describe('My page', function() {
    beforeAll(function() {
        browser.get('/the/url');
    });

    it('test 1', function() {
        // ...  
    });

    it('test 2', function() {
        // ...  
    });
});

beforeAll 内置于 jasmine2。对于 jasmine1,有一个第三方 jasmine-beforeAll 包提供完全相同的功能。