量角器 - 如何将每个测试分离到一个文件并分离变量

Protractor - How to separate each test to one file and separate variabiles

我写了一些复杂的量角器测试,但所有内容都在一个文件中。 我最重要的是加载所有变量,例如:

var userLogin = "John"; 

然后在代码的某个地方我一起使用它。

我需要做的是 1.将所有变量分离到附加文件(一些配置文件) 2. 每个测试一个文件

1- 我尝试在 config.js 中添加所有变量并在 protractor.conf.js 中要求它正确加载问题是当我在某些测试中使用任何此变量时它不起作用(测试失败 "userName is not defined") 我知道有一种方法可以在每个测试脚本中要求 config.file,但这在我看来确实不是最佳选择。

2- 如果上一个脚本是分开的,我怎么知道我在上一个脚本中做了什么,例如如何知道我已登录?

谢谢。

您可以使用多种东西。

2) How can I know what I did in last script if it's separate, like for example how to know I am logged in?

这是 beforeEach(), afterEach() 可以提供帮助的地方:

To help a test suite DRY up any duplicated setup and teardown code, Jasmine provides the global beforeEach and afterEach functions. As the name implies, the beforeEach function is called once before each spec in the describe is run, and the afterEach function is called once after each spec.

还有beforeAll(), afterAll() available in jasmine 2, or via jasmine-beforeAll茉莉花1的第三方:

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.


1) I try to make config.js where I add all variabiles and i required it in protractor.conf.js it load correctly problem is that when i use any of this variabiles in some test it's not working (test fail with "userName is not defined") I know there is a way where i requre config.file in each test script but that's really not best option in my eyes.

我个人使用的一个选项是创建一个 config.js 文件,其中包含您在多个测试中需要的所有 可重用配置变量 并需要该文件一次 - 在量角器配置中 - 然后将其设置为 params 配置键值:

var config = require("./config.js");
exports.config = {
    ...

    params: config,

    ...
};

其中 config.js 例如:

var config;
config = {
    user: {
        login: "user",
        password: "password"
    }
};

module.exports = config;

然后,您 不需要在每个测试中都要求 config.js,而是使用 browser.params。例如:

expect(browser.params.user.login).toEqual("user");

此外,如果您需要某种 全局测试准备步骤 ,您可以在 onPrepare() 函数中完成,请参阅 Setting Up the System Under Test. Example configuration that performs a "global" login step is available here

还有一个简短的说明:您可以自定义全局定义变量(如内置browserprotractor),使用globalonPrepare 中。例如,我将 protractor.ExpectedConditions 定义为自定义全局变量:

onPrepare: function () {
    global.EC = protractor.ExpectedConditions;
}

然后,在测试中,不需要任何东西,`EC 变量将在范围内可用,例如:

browser.wait(EC.invisibilityOf(scope.page.dropdown), 5000)

此外,使用 "Page Object Pattern" 组织测试也有助于解决 可重用性和模块化 问题。