Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 781
ItsASine Avatar asked Jul 13 '26 11:07

ItsASine


2 Answers

You were so very close! This worked for me just now:

var browserSizeOfMe = browser.driver.manage().window().getSize().then(function(size) {
    console.log(" BROWSER SIZE "+ JSON.stringify(size) );
    return size;
});
like image 136
Dave Avatar answered Jul 15 '26 01:07

Dave


You don't have to approach the problem of differentiating mobile and desktop judging by the size of the browser window. Instead, you can access the configured capabilities via getCapabilities(), please see examples at:

  • Protractor: accessing capabilities
like image 36
alecxe Avatar answered Jul 15 '26 00:07

alecxe