简体   繁体   中英

issues while running protractor tests

I am new to Protractor . Here is my config file

config :

exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',

capabilities: {
    'browserName': 'chrome'
},

specs: ['./protractor-tests/*.js'],

jasmineNodeOpts: {
    showColors: true
}
}

Here I have a scenario where I am testing the login where the user login through outlook and when click on login it redirects to home page.

Login test :

exports.login = function() {
describe('Login Test', function() {
    beforeEach(function() {
        browser.get('http://domain/login');
    });
    afterEach(function() {
        browser.ignoreSynchronization = false;
    });
    it('It should redirect to outlook for auth', function() {
        browser.ignoreSynchronization = true;
        var currentUrl;
        browser.getCurrentUrl().then(function(url) {
            currentUrl = url;
        }).then(function() {
            browser.wait(function() {
                return browser.getCurrentUrl().then(function(url) {
                    return url !== currentUrl;
                });
            });
        }).then(function() {
            setTimeout(function() {
                var userName = element(By.xpath('/html/body/div[1]/header/div[1]/section/div[2]/apt-user-profile/div/div/button[1]/span/span'));
                if (userName)
                    expect(browser.getCurrentUrl()).toContain('http://domain/home');
                else
                    expect(browser.getCurrentUrl()).toContain('https://login.microsoftonline.com');
            }, 10000);


        });
    });

    it('It should login into the application and display user as Tom White', function() {
        setTimeout(function() {
            browser.driver.findElement(by.id('cred_userid_inputtext')).sendKeys('tomwhite@gmail.com');
            browser.driver.findElement(by.id('cred_password_inputtext')).sendKeys('****');
            browser.driver.findElement(by.id('cred_sign_in_button')).click();
            var userName = element(By.xpath('/html/body/div[1]/header/div[1]/section/div[2]/apt-user-profile/div/div/button[1]/span/span'));
            expect(userName.getText()).toEqual('Hello Tom White');
        }, 10000);
    });

});
}

These test cases are actually working fine, but when in home page, I need to also click on an element that redirects me to some other page it is failing with error

  Failed: javascript error: document unloaded while waiting for result

home tests:

 var loginPage = require('./loginTest.js');

describe('Home Test', function() {
    loginPage.login();
    it('It should click product', function() {

            var productMenu = element(By.xpath('/html/body/div[1]/headerdiv[1]/section/div[1]/apt-nav-bar/div/div[2]/div[2]/ul/li[2]/a/span'));
            productMenu.click();
            expect(browser.getCurrentUrl()).not.toEqual('http://domain/home');
            expect(browser.getCurrentUrl()).toEqual('http://domain/products');


        })

});

Any help would be highly appreciated.

Your issue is due to absence of waiting for the page to do something. You can implement an explicit wait using the ExpectionConditions functionality provided by Protractor. Here is the link from the API .

A dirty solution is to use a sleep() method so that the execution is paused for a certain period of time, say 5 seconds. So you can try browser.sleep(5000) , which should do away with the error. However, this is not a good solution.

Edit 1 : The rootElement param of the exports.config object defined in your protractor configuration file must match the element containing your ng-app directive.

In your protractor-conf.js, please add

rootElement: '.my-app',

when your HTML is:

<div ng-app="myApp" class="my-app">

Source : SO Post .

You are calling the login function outside of it or "before" or "after" functions which are under the Protractor's control flow. Move your loginPage.login(); call to beforeEach() or beforeAll() :

describe('Home Test', function() {
    beforeEach(function () {
        loginPage.login();
    });

    it('It should click product', function() {
        var productMenu = element(By.xpath('/html/body/div[1]/headerdiv[1]/section/div[1]/apt-nav-bar/div/div[2]/div[2]/ul/li[2]/a/span'));
        productMenu.click();
        expect(browser.getCurrentUrl()).not.toEqual('http://domain/home');
        expect(browser.getCurrentUrl()).toEqual('http://domain/products');
    })
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM