2

We are using Protractor 3.6 version in our project to automate the angularJS application. We have master angular application where many standalone angular apps been implemented as iframe.

that is we can access those apps directly hitting the SPA and also through our master integrated application.

Current Approach: Team is using Implicit wait() with 30 seconds and static wait (may be 60 seconds) in places where implicit wait does not help.

Problem: Could see inconsistent results every time we execute test scripts and more over execution time is considerably high.

Solution: We brought up explicit wait added to implicit wait and static wait which improved the consistency and reducing in execution time.

Question: Is it correct using all three waits ( Implicit wait, explicit wait, thred.sleep) together in the framework. Note: In few navigation we had to use thread.sleep (). Both implicit wait and explicit wait is not working.

Ory Zaidenvorm
  • 896
  • 1
  • 6
  • 20
barat21
  • 31
  • 3

2 Answers2

1

Yes it is fine to be using multiple types of waiting, I use implicit waits 95% of the time but need to use explicit waits for some conditions. If you app is fully Angular and properly synchronizes (i.e. no outstanding $http calls - more info) you shouldn't need too many waits as Protractor tends to know when Angular/your app is ready. However I don't know your app and this could very well not be true for your case, I'll give a few thoughts on the topic:

Implicit: These are your best bet, for both consistency and stability. I would challenge you and say most of your wait problems could probably be resolved with a proper use of Implicit waits (stronger use cases, compounded across multiple conditions etc). But again, I don't know your app, I think it's worth revisiting though. You can review the list here, the ones I frequently use are presenceOf(), visibilityOf() and their counterparts stalenessOf() and invisibilityOf().

Explicit: I try very hard to avoid these, however I have found it necessary in some cases. Such as an animation occurring over a few seconds and there is nothing to track with an implicit wait. I have created a few methods of my own to try and capture these scenarios and use a more implicit wait approach, such as the one below:

// wait for an attribute to be present i.e. "ng-invalid" to be added to a class
// el = element, attr = attribute to check (i.e. class, id etc), target = attribute value to look for
Util.prototype.waitForAttributePresent = function (el, attr, target, time) {
    var timeout = time || 0;
    return browser.wait(function () {
        return el.getAttribute(attr).then(function (val) {
            return ~val.indexOf(target) < 0;
        });
    }, timeout);
};

Use case:

// waits 5 seconds for the elements class to contain "active"
var el = $('div');
Util.waitForAttributePresent(el, 'class', 'active', 5000);
expect(true).toBe(true); 

Static: Not sure what you mean by static, that sounds the same as an explicit wait. You are stopping it for a set amount of time, not based on any conditions.

Gunderson
  • 3,263
  • 2
  • 16
  • 34
0

Perhaps sharing more details about why the implicit waits were not working would enable the community to help you more technically speaking.

The short answer to your question is that it is correct to use both types of waits (not sure what is the difference between "Explicit" and "Static), depending on the SUT, your goals for automation, your project priorities and your technical knowledge.

I've seen countless of cases where explicit waits were used but there was a way to implement a more efficient implicit wait. Sometimes, however, the effort to do so outweighs the benefit so I always go for the "big wins" first - areas that figuring out how to implicitly wait will save the most amount of execution time or (more importantly) improve execution stability. Some analysis work is usually needed first to determine where those areas are.

Another thing to note is that in most cases, execution stability is more important than execution time; if using explicit waits results in more stable tests then it is absolutely valid to wait explicitly. You can always come back to those cases later during rounds of optimisations to look for ways to reduce wait time while keeping the stability up.

My order for approaching test automation is:

  1. Write tests that work
  2. Stabilise tests
  3. Optimise

Steps 1 & 2 are mandatory but step 3 is not and depends on your projects priorities. For example it may be more important to add new tests than to optimise existing tests. On the other hand it may be the case that execution time is very long thus lowering the benefit a CI system is providing. In such a case it is probably more important to optimise existing tests.

Last but not least: it is not recommended to mix explicit and implicit waits. See- http://docs.seleniumhq.org/docs/04_webdriver_advanced.jsp#explicit-and-implicit-waits

Combining implicit wait and explicit wait together results in unexpected wait times

Clarification on the cause of mixing Implicit and Explicit waits of Selenium doc

Ory Zaidenvorm
  • 896
  • 1
  • 6
  • 20