0

I'm testing the login page , in the scenario when I enter an invalid logIn and Password , I'm looking to collect the span "Authentication Failed"

I select the span webElement by xpath '/html/body/div[2]/div/div/snack-bar-container/simple-snack-bar/span, when I test it on the navigator console , I receive the text inside the span . But when I use it on the test script , it doesn't work .

getSnackbar() {
   return element(by.xpath('/html/body/div[2]/div/div/snack-bar- 
container/simple-snack-bar/span'));
  }

it(' SnackBar "Authentication Failed" Appears ', () => {
  login.navigateTo();
  element(by.id('username')).sendKeys('LogIn');
  element(by.id('password')).sendKeys('pwd');
  login.logIn();
  browser.sleep(5000);
  expect(login.getSnackbar().getText()).toEqual('Failed Authentication');
});

I expect to receive a positive result of the test , but I receive : " Failed to load resource: the server responded with a status of 401 (Unauthorized)".

Mo Elkinani
  • 53
  • 2
  • 8

1 Answers1

0

My first point would be not to use xpaths in tests as they are very fragile. The best way to get an element in Angular testing is to use the following

elementYouWant = fixture.debugElement.query(By.css('#id-of-element'));
OR
elementYouWant = fixture.debugElement.query(By.css('.class-of-element'));

Also don't use browser.sleep() you're making your unit test last a minimum of 5 seconds which is far too slow. Try to await the function if it is async.

As to the error message It appears like you aren't mocking out the login method. (i.e. you are making a real call to your server). You should not do this. A unit test should mock out all external dependencies.

You should create a mock login class with the logIn() method mocked and returning a failed response. Then you will be able to perform your assertion without the problem you are having.

Hope this helps

James
  • 2,516
  • 2
  • 19
  • 31
  • the test work fine in the case when we enter a valid username and a valid password . – Mo Elkinani Jun 12 '19 at 11:33
  • in those cases, the response from the api will be 200 so no error is expected. You are expecting a different message from one you're getting back. It appears to be working correctly, you just expect the message to be slightly different – James Jun 13 '19 at 12:45