I'm using Protractor (v 1.3.1) to run E2E tests for my Angular 1.2.26 application.
But sometimes, tests are ok, sometimes not. It seems that sometimes the check is done before display is updated (or something like "synchronisation" problem). I try many options :
- add
browser.driver.sleep
instructions, - disable effects with
browser.executeScript('$.fx.off = true')
- add
browser.waitForAngular()
instructions
without success.
What are the bests practice to have reliables E2E tests with protractor?
JM.
I would add that disabling ngAnimate may not be enough. You may also have to disable all transition animation by injecting CSS (What is the cleanest way to disable CSS transition effects temporarily?).
There are two things to consider.
The first is that you should properly sequence all protractor actions (as also hinted by @jmcollin92). For this, I typically use
.then
on every step.The second important thing is to make sure that a new test
it(...)
only starts after the previousit(...)
has completed.If you use the latest version of Protractor, you can use Jasmine 2.x and its support for signalling the completion of a test:
Here the
done
argument is invoked to signal that the test is ready. Without this, Protractor will schedule theclicksomething
command, and then immediately move on with the next test, returning to the present test only onceclicksomething
has completed.Since typically both tests inspect and possibly modify the same browser/page, your tests become unpredictable if you let them happen concurrently (one test clicks to the next page, while another is still inspecting the previous page).
If you use an earlier version of Protractor (1.3 as you indicate), the Jasmine 1.3
runs
andwaitsFor
functions can be used to simulate this behavior.Note that the whole point of using Protractor is that Protractor is supposed to know when Angular is finished. So in principle, there should be no need to ever call
waitForAngular
(my own test suite with dozens of scenarios does not include a single wait/waitForAngular). The better your application-under-test adheres to Angular's design principles, the fewerWaitForAngular
's you should need.Another point which is very important in testing with Protractor is understanding the ControlFlow. You may find explaination and code example here : When should we use .then with Protractor Promise?
Jean-marc
Every time I have similar issues, I'm using
browser.wait()
with "Expected Conditions" (introduced in protractor 1.7). There is a set of built-in expected conditions that is usually enough, but you can easily define your custom expected conditions.For example, waiting for an element to become visible:
Few notes:
you can specify a custom error message in case the conditions would not be met and a timeout error would be thrown, see Custom message on wait timeout error:
you can set
EC
to be a globally available variable pointing toprotractor.ExpectedConditions
. Add this line to theonPrepare()
in your config:as an example of a custom expected condition, see this answer