1

我想为单个测试用例生成两个单独的报告。为此,我browser.getCapabitities在测试中使用了获取浏览器名称和版本的方法。

现在,当我在规范描述的末尾使用此变量时,值为undefined. 在browserNameforSpec描述之前获得价值。只有当我在规范末尾使用此值时,它才会显示未定义。我无法得到发生这种情况的原因。有什么方法可以在测试开始之前更改此规范描述的名称。

我的代码是

let capsPromise = browser.getCapabilities();
let browserNameforSpec;
capsPromise.then(function(caps) {
    console.log(caps);
    let browserName = caps.get('browserName');
    let browserVersion = caps.get('version');
    browserNameforSpec = browserName + '-' + browserVersion + '-';
    console.log(browserNameforSpec);

});

describe( '0030 Test for login' + browserNameforSpec, function () { // this.browserNameforSpec value is undefined

    // 1.
    it('Navigate to the login page', async () => {
        await navigate.to.the(loginPage);
    });

    // 2
    it('Click onto language button', async() => {
        await click.onto(languageButton);
        await expect(languageDropdown.isDisplayed());
    });

    // 3
    it('English Language is selected', async() => {    
        await click.onto(englishLanguage);
        await expect(languageButton.getText()).toBe('English');
    });

    // 4.
    it('Correct user name is written into email field', async() => {
        await usernameField.click();
        await enter(correctUsername, into(usernameField));    
    });

    // 5.
    it('Correct password is written into password field', async() => {
            await passwordField.click().then(function () {
                passwordField.clear();
                enter(correctPassword, into(passwordField));
            })    
    });

    // 6.
    it('Login button is clicked and home page is opened', async() => {
        await click.onto(loginButton);
    });    
});
4

2 回答 2

1

将以下代码添加到您的onPrepare()

browser.getCapabilities().then(function (cap) {
  browser.browserName = cap.caps_.browserName;
});

你在你的测试中调用如下

describe( '0030 Test for login' + browser.browserName, function () {

    // 1.
    it('Navigate to the login page', async () => {
        await navigate.to.the(loginPage);
    });

希望对你有帮助。。

于 2018-12-19T08:58:05.763 回答
1

问题是: 1.您正在使用 this.browserNameforSpec ,随着函数上下文的更改,它将未定义,您已将 browserNameforSpec 声明为可在所有函数中直接使用的变量,但您正在调用它) 2. browser.getCapabilities()返回一个 Promise 实例,这就是执行流程不同的原因。首先它正在执行这个函数和describe()。但。它们需要以同步方式调用。

下面的代码使用另一个 .then 以顺序方式执行它。尝试使用以下代码:

let capsPromise = browser.getCapabilities();
let browserNameforSpec;

capsPromise
    .then(function(caps) {
        console.log(caps);
        let browserName = caps.get('browserName');
        let browserVersion = caps.get('version');
        browserNameforSpec = browserName + '-' + browserVersion + '-';
        console.log(browserNameforSpec);

    })
    .then(function() {
        describe( '0030 Test for login' + browserNameforSpec, function () {

            // 1.
            it('Navigate to the login page', async () => {
                await navigate.to.the(loginPage);
            });

            // 2
            it('Click onto language button', async() => {
                await click.onto(languageButton);
                await expect(languageDropdown.isDisplayed());
            });

            // 3
            it('English Language is selected', async() => {

                await click.onto(englishLanguage);
                await expect(languageButton.getText()).toBe('English');
            });

            // 4.
            it('Correct user name is written into email field', async() => {
                await usernameField.click();
                await enter(correctUsername, into(usernameField));

            });

            // 5.
            it('Correct password is written into password field', async() => {
                    await passwordField.click().then(function () {
                        passwordField.clear();
                        enter(correctPassword, into(passwordField));
                    })

            });

            // 6.
            it('Login button is clicked and home page is opened', async() => {
                await click.onto(loginButton);
            });
        });
    });

于 2018-12-19T08:27:29.827 回答