protractor icon indicating copy to clipboard operation
protractor copied to clipboard

if element.isPresent() returns false , protractor hangs inside then() block and jasmine spec times out

Open jagdheesh-github opened this issue 8 years ago • 9 comments

Here is the scenario

  1. I have 3 pages Login page , Change password page , go to home page
  2. On each page , i put a check , if it is true , then execute a block of code , if false don't execute block of code .

Step 1. var login = element(by.xpath('//h4[text()="Log In"]')); login.isPresent().then(function(){ // get login credentials from protractor config file and login // this block gets executed , because element is present inside dom // protractor loads the next page });

Step 2 : new users are prompted to change password upon first login , return users need not change password .so i have to check if change password page is displayed or not. Now i logged in as a return user in Step 1 for whom change password page is not displayed, but i will have to check if that page is displayed.

var changePassword = element(by.xpath('//h4[text()="Change Your Password"]')); changePassword .isPresent().then(function(){ // changePassword element is not present in the page. // protractor hangs here , the script timesout . // it doesnot execute the step 3. // expected behavior : if element is not found , then() function must be skipped and go to Step 3. // instead protractor hangs here. });

Step 3. For return users , after login , the next page displays a link to take them to the home page instead of Change password page.

var homePage = element(by.linkText('Go to Home Page')); homePage .isPresent().then(function(){ homePage .click(); // this block executes perfectly if Step 2 is not present in the code or commented. // this block does not execute if Step 2 fails , because , Protractor hangs in Step 2. finally jasmine spec // times out without executing Step 3 });

I am using protractor version 5.4.1 command : npm test

jagdheesh-github avatar Oct 25 '18 10:10 jagdheesh-github

Please, improve your code formatting using ``` symbols before and after code parts

CrispusDH avatar Oct 25 '18 11:10 CrispusDH

Please, impove your code formatting using ``` symbols before and after code parts

Ignore syntax in above code. Syntax was not the problem. Otherwise protractor strictly reports syntax problems inside script.

jagdheesh-github avatar Oct 25 '18 11:10 jagdheesh-github

I meant syntax in your question (first post). It is really hard to read!

CrispusDH avatar Oct 25 '18 15:10 CrispusDH

isPresent returns a promise that resolves to true or false depending on whether an element is present or not. So if an element is not present it just returns false inside the then() function. it does not skip and goto next step. If you need to really check if change password element is absent and execute step3 you could consume the bool from step2 and do your steps inside the then() of step2. Something like this.

image

raj8github avatar Oct 29 '18 05:10 raj8github

image

@raj8github , i tried the way you suggested , i.e trying to resolve the promise into a boolean value , pass it to then 'block', and see it returns from that block of code and execute the next steps . It does not happen. Its a bug. You know , if i close the browser (stop the test) , then the false value gets logged in the 'else' block inside 'then' block. check the attached image . Protractor returns to the next steps only if the promise resolves to true, it executes then function and returns to execute the next code.

jagdheesh-github avatar Oct 30 '18 09:10 jagdheesh-github

@jagdheesh-github Is SELENIUM_PROMISE_MANAGER enabled? Is implicitlyWait changed from default value? If SELENIUM_PROMISE_MANAGER enabled =true, and implicityWait not changed, then try to disable browser.waitForAngularEnabled(), set it to false.

niyarlatotep avatar Nov 02 '18 07:11 niyarlatotep

I struggled with a similar issue for days. This solved the problem for me:

https://stackoverflow.com/questions/43121400/run-ngrx-effect-outside-of-angulars-zone-to-prevent-timeout-in-protractor

See section in Angular docs: https://www.protractortest.org/#/timeouts#angular

Hope it helps :)

pringelmann avatar Nov 07 '18 14:11 pringelmann

@Pringels , but how to use this script inside protractor script. I am using javascript with jasmine framework, not using typescript. how does it really solve the problem i raised in my main post. Protractor guys can implicitly solve this with a small fix in upcoming releases. The problem is straight forward, which is minimum expectation from protractor.

jagdheesh-github avatar Nov 19 '18 08:11 jagdheesh-github

I struggled with a similar issue for days. This solved the problem for me:

https://stackoverflow.com/questions/43121400/run-ngrx-effect-outside-of-angulars-zone-to-prevent-timeout-in-protractor

See section in Angular docs: https://www.protractortest.org/#/timeouts#angular

Hope it helps :)

Can you please provide us more explainable code in protractor context?

StanislavChankov avatar Dec 07 '18 15:12 StanislavChankov