0

As I am working in protractor test automation and slowly diving into more & more advanced JavaScript features, I am wondering how can I take advantage of closure feature in Protractor test automation.

What are those typical test automation situations where closures might prove a useful feature to use?

I am asking this question purely from UI test automation perspective, not in general JavaScript programming. I would love to hear from senior folks who are using a protractor on an advanced level in large scale projects and share their experiences.

Vishal Aggarwal
  • 1,929
  • 1
  • 13
  • 23

1 Answers1

3

Here are some closure use cases that we've been using in our test automation project:

  • when a Page Object method has a promise resolution function in which you need to access the other Page Object fields or methods, you need to create a closure:

    var SelectEnvironmentPage = function () {    
        this.title = this.container.element(by.css("b.modal-title"));
        this.goButton = element(by.id("selEnvBtnGo"));
    
        this.passIfPresent = function () {
            var self = this;
            this.title.isPresent().then(function (isTitlePresent) {
                if (isTitlePresent) {
                    self.goButton.click().then(function () {
                        helpers.passMaxSessionPopup();
                    });
                }
            });
        };
    };
    
  • when extending ElementArrayFinder methods and defining the getWebElements() method

Community
  • 1
  • 1
alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195