WDIO:如何访问在 beforeAll 中声明的描述块中的变量?

WDIO: How to access variables in a describe block, that were declared in beforeAll?

我有将近 20 个变量要声明,所以我不想为每个测试都声明它们。我想在 beforeAll() 中声明它们,但我认为这行不通。我怎样才能一次声明很多变量(在 beforeAll 或其他地方),并在多个测试中访问它们?

这行不通:

const assert = require('assert');

beforeAll(() => {
  browser.url('example.com');
  // ### declare a bunch of variables ###
})

describe('something', () => {
  it('should do cool stuff', () => {
    // access a bunch of variables

  });
});

这也不行:

const assert = require('assert');

beforeAll(() => {
  browser.url('example.com');
})

describe('something', () => {
  // ### declare a bunch of variables ###
  it('should do cool stuff', () => {
    // access a bunch of variables

  });
});

呃,我知道我错过了一些简单的东西,我以前甚至用过这种方法...

在与测试相同的目录中创建一个名为 config.js 或任何您想要的文件。在该文件中声明所有变量,就像这样

module.exports = {
  get var1 () { return browser.element(yourSelector); },
  get var2 () { return browser.element(yourSelector); },      
  ...
  get var20 () { etc },
}

然后在你的规范文件的顶部,需要上面的文件,如 const gv = require('./config.js');

使用这样的变量 gv.var1.click()gv.var2.whatever()