Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

javascript - 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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...