0

There is an inbuilt method to get the browser width and height in protractor or webdriver API

browser.manage().window().getSize().then(function(size) {
return size.width;
}); 

How can i convert above in a single method ?

I want to use that method in an if condition within my tests e.g.

if (width > 500) {
it ('test following', () => {
expectations goes here
});
}
Naveen
  • 31
  • 1
  • 6

1 Answers1

0

The manage().window().getSize() has to be executed in the Protractor's control flow which would not be yet initialized if you call it outside of before*(), after*() or it() contexts.

Just put your condition into the test itself:

function getWidth() {
    return browser.manage().window().getSize().then(function(size) { 
        return size.width
    });
}

it ('test following', () => {
    getWidth().then(function (width) {
        if (width > 500) {
            // test something
        }
        else {
            console.log("Skipped testing something");
        }
    });
})

Or, depending on your use case, you can set the dimensions of the browser window for a specific capability (example) in your protractor config and specify tests that would be executed for this capability - this should eliminate the need to do this check in the test itself.

alecxe
  • 462,703
  • 120
  • 1,088
  • 1,195
  • Thanks for quick response. But what does it mean when you say "manage().window().getSize() has to be executed in the Protractor's control flow" ? Is it correct approach to make a method mentioned in your example. Looks like that method is actually not doing anything, we are resolving the same promise twice ? – Naveen Aug 27 '18 at 18:15
  • @Naveen no, the promise is gonna be resolved once when the control flow queue dequeues this promise. Also, here is a link to the control flow doc: https://github.com/angular/protractor/blob/master/docs/control-flow.md. – alecxe Aug 27 '18 at 19:30