量角器Jasmine中规格说明中的浏览器功能

时间:2018-12-19 08:13:32

标签: javascript angular jasmine protractor httpbrowsercapabilities

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

现在,当我在规格说明的末尾使用此变量时,该值为undefinedbrowserNameforSpec在描述之前就获得了价值。仅在规范末尾使用此值时,它才显示未定义。我不知道发生这种情况的原因。在测试开始之前,有什么方法可以更改此规范描述的名称。

我的代码是

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);
    });    
});

2 个答案:

答案 0 :(得分: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);
            });
        });
    });

答案 1 :(得分: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);
    });

希望它对您有帮助。