return FALSE; $r = well_tag_thread__update(array('id' => $id), $update); return $r; } function well_tag_thread_find($tagid, $page, $pagesize) { $arr = well_tag_thread__find(array('tagid' => $tagid), array('id' => -1), $page, $pagesize); return $arr; } function well_tag_thread_find_by_tid($tid, $page, $pagesize) { $arr = well_tag_thread__find(array('tid' => $tid), array(), $page, $pagesize); return $arr; } ?>javascript - Protractor: How do I get the current browser width? - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Protractor: How do I get the current browser width? - Stack Overflow

programmeradmin4浏览0评论

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?

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?

Share Improve this question asked Nov 11, 2016 at 3:26 ItsASineItsASine 1411 gold badge3 silver badges12 bronze badges 1
  • Wrap the whole block inside of the .then of the promise? That's what I've done in my tests. – Daniel Commented Nov 11, 2016 at 4:09
Add a ment  | 

5 Answers 5

Reset to default 5

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;
});

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

you can get the browser width using this script

var width = window.innerWidth || document.body.clientWidth;

but i dont think this answers your problem

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();
  }
}

It can be done using "viewport", have a look following link

http://www.w3schools./css/css_rwd_viewport.asp

发布评论

评论列表(0)

  1. 暂无评论