Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I wait for a condition?

I'm new on protractor, and I'm trying to implement an e2e test. I don't know if this is the right way to do this, but... The page that I want to test is not a full angular page based, so... I'm having some trouble.

On my first spec I have:

describe('should open contact page', function() { var ptor = protractor.getInstance();  beforeEach(function(){     var Login = require('./util/Login');    new Login(ptor); }); 

I have created this Login class, but after login I want to open the contact page, but protractor immediately try to find element before the page is fully loaded.

I've tried to use:

browser.driver.wait(function() {      expect(browser.findElement(by.xpath("//a[@href='#/contacts']")).isDisplayed());     ptor.findElement(by.xpath("//a[@href='#/contacts']")).click();  }); 

But it doesn't work... it always try to find the element before the page loads. I tried this one too:

browser.driver.wait(function() {     expect(ptor.isElementPresent(by.xpath("//a[@href='#/contacts']")));               ptor.findElement(by.xpath("//a[@href='#/contacts']")).click(); }); 

I'm able to do that using browser.sleep(); but I don't think that is a good option. Any idea? On my login class I have:

ptor.ignoreSynchronization = true; 

How can I wait for this @href='#/contacts before protractor tries to click on it?

like image 981
Muratso Avatar asked Feb 27 '14 15:02

Muratso


People also ask

What does condition variable wait do?

condition_variable::wait wait causes the current thread to block until the condition variable is notified or a spurious wakeup occurs, optionally looping until some predicate is satisfied (bool(stop_waiting()) == true).

What are condition variables in C?

A Join allows one thread to wait for another to complete but a condition variable allows any number of threads to wait for another thread to signal a condition. If you know a language and environment that supports events then you can think of condition variables as something like an event.


2 Answers

Protractor 1.7.0 has also introduced a new feature: Expected Conditions.

There are several predefined conditions to explicitly wait for. In case you want to wait for an element to become present:

var EC = protractor.ExpectedConditions;  var e = element(by.id('xyz')); browser.wait(EC.presenceOf(e), 10000);  expect(e.isPresent()).toBeTruthy(); 

See also:

  • Expected conditions in protractor
like image 122
alecxe Avatar answered Oct 13 '22 21:10

alecxe


I finally find out...

   var waitLoading = by.css('#loading.loader-state-hidden');     browser.wait(function() {        return ptor.isElementPresent(waitLoading);    }, 8000);     expect(ptor.isElementPresent(waitLoading)).toBeTruthy();     var openContact = by.xpath("//a[@href='#/contacts']");    element(openContact).click(); 

With this protractor could wait for that element until it loading page disappears. Thanks for those who tried to help XD.

like image 43
Muratso Avatar answered Oct 13 '22 23:10

Muratso