2

I do have an e2e test for an Angular app. It works fine on my local machine, but on the production repo, it runs inconsistently.. in other words sometimes it fails but sometimes it's runs successful.

Here is the code for testing:

it('LOGIN - Submit-Button "Login" must be enable after entering UserName and Password', () => {
    page.getInputFields().userNameInput.sendKeys('Qw12');
    page.getInputFields().userPassInput.sendKeys('!qe4Dfqq');
    expect(page.getInputFields().submitButton.isEnabled()).toBe(true);
});

and here is the error when it fails:

LOGIN - Submit-Button "Login" must be enable after entering UserName and Password
- Failed: stale element reference: element is not attached to the page document
(Session info: chrome=63.0.3239.132)
(Driver info: chromedriver=2.35.528139 (47ead77cb35ad2a9a83248b292151462a66cd881),platform=Linux 4.12.10-coreos x86_64)

I think that it has to do with asynchronous testing with jasmine. Is that correct?

Is there a way to fix this? How can I avoid this kind of behavior and/or how can I make run consistently?

5
  • Looks like after your login button had been initiated, the dom changed, so since you have not re-bound the element after that dom had changed you get stale element exception. I am not an expert in protractor unfortunately so I cannot help you with fix, however the root cause seems to be defined correctly. I believe there is a kind of your code design issue.
    – Alexey R.
    Jan 29 '18 at 12:40
  • This is exactly what I thought... after login page the test moves to register page/component. So therefore there is no Login button in the DOM any more.
    – k.vincent
    Jan 29 '18 at 12:44
  • I think it has to do with the PO (Page Object) e.g. login.po.ts file which uses functions that return needed elements for the test.
    – k.vincent
    Jan 29 '18 at 13:55
  • Implicity Wait for it: stackoverflow.com/a/33299717/631619 May 28 '18 at 23:15
  • @K.vincent, please let me know if the below solution did not work and share the relevant HTML source of the page. May 29 '18 at 1:18
1

It is happening because JavaScript is asynchronous and it may not execute statements in sequence as you might be expecting.I would rewrite as below using async/await:

it('LOGIN - Submit-Button "Login" must be enable after entering UserName and Password', async() => { 

await page.getInputFields().userNameInput.sendKeys('Qw12');

 await page.getInputFields().userPassInput.sendKeys('!qe4Dfqq');

expect(page.getInputFields().submitButton.isEnabled()).toBe(true); });

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.