Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to have protractor reliable results?

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.

like image 426
jmcollin92 Avatar asked Mar 19 '15 17:03

jmcollin92


People also ask

Is Protractor going to end?

TLDR. The Angular team plans to end development of Protractor at the end of 2022 (in conjunction with Angular v15). Why? Protractor was created in 2013 when WebDriver APIs were not yet a standard and end-to-end (e2e) tests were hard to write due to lack of support for async / await .

Why is Protractor being deprecated?

Why is Protractor being deprecated? Protractor was created in 2013. During that time WebDriver APIs were not yet standard, because of which testers and developers faced difficulties in writing end to end tests as there was very little support for async/await.

What is Protractor used for in testing?

Protractor is a wrapper around Selenium Webdriver that provides an automation test framework, which simulates user interaction with an Angular web application for a range of browsers and mobile devices. It provides all features of Selenium WebDriver along with Angular specific features for seamless end to end testing.

How do you speed up a Protractor test?

To do this is quite simple in protractor. Adding the shardTestFiles and maxInstences to your capabilities config should allow you to (in this case) run at most two test in parrallel. Increase the maxInstences to increase the number of tests run. Note : be careful not to set the number too high.


1 Answers

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:

var EC = protractor.ExpectedConditions;
var e = element(by.id('xyz'));

browser.wait(EC.visibilityOf(e), 10000);
expect(e.isDisplayed()).toBeTruthy();

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:

    browser.wait(EC.visibilityOf(e), 10000, "Element 'xyz' has not become visible");
    
  • you can set EC to be a globally available variable pointing to protractor.ExpectedConditions. Add this line to the onPrepare() in your config:

    onPrepare: function () {
        global.EC = protractor.ExpectedConditions;
    }
    
  • as an example of a custom expected condition, see this answer

like image 98
alecxe Avatar answered Oct 09 '22 03:10

alecxe