dchosen1
dchosen1

Reputation: 163

Protractor testing AngularJS fails to find an element by an id that does exist

I have a sample AngularJS site @ https://gamersweb-js.firebaseapp.com/ with a protractor test case which is supposed to find an element by id on one of it's pages. The element with this id 'displayNameInHome' certainly does exist (in the user's 'Home' page), but the test case fails to find it there.

To get to the web page, log in using the credentials of user '[email protected]' + password '123456', which will take you to the user's home page. The following test case fails to find element 'displayNameInHome' on this page, which will be the id of string 'tester' after the Welcome ....:

beforeEach(function() {
    //browser.get('http://localhost:5000/');
    browser.get('https://gamersweb-js.firebaseapp.com');
});

it('Testing display name in profile for logged in user:', function() {
element(by.model('loginUI.email')).clear().sendKeys('[email protected]');
element(by.model('loginUI.password')).clear().sendKeys('123456');
element(by.id('loginBtn')).click();

return browser.wait(function() {
    var displayNameInHome = element(by.id('displayNameInHome'));
    return displayNameInHome.getText().then(function(text) {
        return text =="tester";
    });
}, 10000);   

});

I cannot figure out why the element 'displayNameInHome' is not found by the test case. Any help would be greatly appreciated.

Thank you.

Upvotes: 3

Views: 399

Answers (1)

Maria Ines Parnisari
Maria Ines Parnisari

Reputation: 17506

The first parameter of browser.wait() is actually a condition to wait on. So your test is exiting pretty fast because the first parameter is not a condition.

Also, your test is lacking expects.

I changed your code so that the test waits 10 seconds for the #displayNameInHome element to be present and then perform the assertion.

it('Testing display name in profile for logged in user:', function() {
    element(by.model('loginUI.email')).clear().sendKeys('[email protected]');
    element(by.model('loginUI.password')).clear().sendKeys('123456');
    element(by.id('loginBtn')).click();
    var displayNameInHome = element(by.id('displayNameInHome'));
    browser.wait(function() {
        return browser.isElementPresent(displayNameInHome)
    }, 10000);

    expect(displayNameInHome.getText()).toEqual('tester');
});

Upvotes: 2

Related Questions