2
votes

I have executed e2e test for following code using angular

it('should successfully login and create a session using valid credentials', function() {
        browser.get('http://www.angularjs.org');
        browser.driver.manage().deleteAllCookies();     
        var username = element(by.model('username'));
        var password = element(by.model('password'));   
        username.sendKeys('admin');
        password.sendKeys('password');

    });

but i got error like this

NoSuchElementError: No element found using locator: by.model("username")

how to solve this issue.

2

2 Answers

3
votes

You should build your Protractor test not based on assumptions, but with the assistance of tools such as element explorer and elementor.

Then, you can claim, that there is a problem.

Without those tools, writing Protractor test becomes very slow process.

Link to elementor repo: https://github.com/andresdominguez/elementor

2
votes

Are you sure that your element exists in DOM. If yes, then you should wait for angular to settle down and then try to check for the elements. If the element is still loading then protractor will throw an error. Include ignoreSynchronization to make sure protractor waits for angular to settle. You can provide explicit wait time for element to be visible or wait until it loads -

it('should successfully login and create a session using valid credentials', function() {
        browser.driver.manage().deleteAllCookies();
        browser.ignoreSynchronization = false;
        browser.get('http://www.angularjs.org');
        var username = element(by.model('username'));
        var password = element(by.model('password'));
        browser.sleep(2000);
        browser.wait(protractor.ExpectedConditions.visibilityOf(username), 20000);   
        username.sendKeys('admin');
        password.sendKeys('password');
});

Hope this helps.