0

I am very new to protractor, and testing .NET Applications. I am trying to build an automation testing script from scratch. Below is the HTML:

<div class = "top">
<span id = "welcome">
<em>Hi</em> 
"," 
<strong>
<span id = "user">MyName</span></strong> 
"|"
</span>'

My protractor code is:

var greet = element(by.id('user')); expect(greet.getText()).toBe('MyName');

I have already done this code:

var greet = element(by.id('welcome')).element(by.id('user'));
expect(greet.getText()).toBe('Hi, MyName'); // or toEqual("Hi, MyName");

But I am still getting an error message saying

Failed: No element found using locator: by.id("welcome")

or

Failed: No element found using locator: by.id("user")

Hoping to hear from all of the experienced protractor testers.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
Marj
  • 302
  • 4
  • 22

1 Answers1

1

It really looks like a timing issue. Let's try to wait for the element to become present:

var EC = protractor.ExpectedConditions;
var greet = element(by.id('user'));

browser.wait(EC.presenceOf(greet), 5000);

expect(greet.getText()).toBe('MyName');
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • wow, thank you! that worked! I increased it to 20000. can I also use this to an app that is angular based? – Marj Jan 15 '16 at 21:40
  • @Marj if there is angular app under test, usually there is no need for explicit waits since protractor keeps things in sync with angular and knows when it is ready. Sometimes though waits are needed. – alecxe Jan 15 '16 at 21:44
  • yes. I had one test script that intermittently works and even if I had to put waits on it, it still fails due to the not found element. – Marj Jan 15 '16 at 21:50