nick_tec
nick_tec

Reputation: 49

Browser capabilities in spec description in Protractor Jasmine

I want to generate two separate report for a single test case. For that, I am using browser.getCapabitities method in the test by which I am getting the browser name and version.

Now, when I use this variable at the end of the spec description, the value is undefined. The browserNameforSpec is getting value before describe. Only when I use this value at the end of the spec, it shows undefined. I am not able to get the reason why this happens. Is there any way to change the name of this spec description before test started.

My code is

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

Upvotes: 1

Views: 548

Answers (2)

Jitendra Khatri
Jitendra Khatri

Reputation: 774

Issue is : 1. You are using this.browserNameforSpec which will be undefined as the function context gets changed, You have declared browserNameforSpec as a variable which is directly available in all function but you are calling on this) 2. browser.getCapabilities() returning an instance of Promise that's why the execution flow is different. First it is executing this function and the describe(). But. they need to be called in sync manner.

Below code is using another .then for executing it in a sequential manner. Try to use below code :

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

Upvotes: 1

Madhan Raj
Madhan Raj

Reputation: 1442

Add the below code in your onPrepare()

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

You call in your test as below

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

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

Hope it helps you..

Upvotes: 1

Related Questions