ItsASine ItsASine - 13 days ago 5
Javascript Question

Protractor: How do I get the current browser width?

I want some helper function code to run based on whether the browser is in "desktop" mode, which is default, or "mobile" mode, which some specs require for mobile only functionality. Height is always 800, but width can be 600 or 1280.

login: function() {
var self = this;
var browserSize = browser.manage().window().getSize().then(function(size) {
// size is still an unresolved promise ;.;
return size;
});

// Go to login page
browser.get(browser.baseUrl); // Will redirect authed users to the application

// If not at the login page, logout first
if (browser.getCurrentUrl() !== browser.baseUrl) {
if (browserSize.width == 600) {
self.mobileLogout();
} else {
self.desktopLogout();
}
}

self.loginPage.login();
}


How do I either resolve the promise getSize returns or determine the browser width some other way?

Answer

There was indeed too many promises going on in this function. I had to nest needing the width and the current url so things would be defined and usable at the correct times.

Roughly the working code:

login: function() {
  var self = this;

  browser.get(browser.baseUrl);

  browser.getCurrentUrl().then(function(url) {
    if(url !== browser.baseUrl) {
      browser.manage().window().getSize().then(function(size) {
        if (size.width == 600) {
          self.mobileLogout();
        } else {
          self.desktopLogout();
        }
      }
    }

    self.loginPage.login();
  }
}