简体   繁体   English

使用intern.js进行测试时无法滚动

[英]Can't scroll when testing using intern.js

I use moveMouseTo but it doesn't seem to work. 我使用moveMouseTo但它似乎不起作用。 This is my code. 这是我的代码。 Does anyone able to see what is wrong with it? 有谁能看出它有什么问题吗? The assert should work and not return error, because if you try to scroll down the page at www.keylocation.sg , it will shows a navigation bar. 断言应该工作而不是返回错误,因为如果你试图向下滚动页面www.keylocation.sg ,它将显示一个导航栏。

Thanks before. 谢谢你。

define([
  'intern!object',
  'intern/chai!assert',
  './util',
  'intern/dojo/node!fs'
], function(registerSuite, assert, util, fs) {

  var suite = {
    name: 'home-navbar',
    afterEach: util.checkJSErrors,

    // testing the visibility of navigation bar in the home page
    'Home page navigation bar: navigation bar visibility': function() {
      var remote = this.remote
        .setWindowSize(1024, 768)
        .get('about:blank')
        .get('https://www.keylocation.sg');
      this.timeout = 300000;
      return remote
        // check: Home page loads, navbar is not visible
        .findById('header-menu').isDisplayed().then(assert.isFalse).end()
        // check: Scroll down to next page, navbar becomes visible
        .moveMouseTo(0,1000).end()
        .findById('header-menu').isDisplayed().then(assert.isTrue).end();
    }
  };

  registerSuite(suite);

});

Your test is probably failing because of how the WebDriver server you're using determines element visibility. 由于您使用的WebDriver服务器如何确定元素可见性,您的测试可能会失败。 The header menu on that page is initially not visible because it has a negative top margin, which moves it outside of the viewport. 该页面上的标题菜单最初不可见,因为它具有负的上边距,将其移动到视口之外。 However, according to the WebDriver spec , an element that's outside the viewport because of a negative margin isn't necessarily considered invisible. 但是,根据WebDriver规范 ,由于负边距而在视口外部的元素不一定被视为不可见。 Chrome's and Firefox's WebDriver servers, at least, say it's visible. Chrome和Firefox的WebDriver服务器至少可以说是可见的。 This is a WebDriver issue rather than an Intern issue; 这是WebDriver问题,而不是实习问题; Intern is basically just asking the WebDriver server "is this element visible" and telling you the answer. 实习生基本上只是询问WebDriver服务器“这个元素是否可见”并告诉你答案。

Since isDisplayed doesn't seem like it will work in this case, you could instead check whether the element has the disabled class, which is what causes it to have the negative margin. 由于isDisplayed在这种情况下似乎不起作用,您可以检查该元素是否具有disabled类,这是导致它具有负边距的原因。

Unfortunately, trying to simply move the mouse 1000 pixels outside of an element context doesn't scroll the page. 不幸的是,尝试简单地将鼠标移动到元素上下文之外1000像素不会滚动页面。 When you don't give moveMouseTo an element, it moves within the current context element (the last thing that was found). 当你不给moveMouseTo一个元素时,它会在当前的context元素中移动(找到的最后一件事)。 When there's no context, it's moving within the outermost element, which in this case is only 632px high. 当没有上下文时,它在最外层元素内移动,在这种情况下只有632px高。 You'll need to set the context to an element that's tall enough to contain your movement offset, or you can find an element at the bottom of the page, like the footer, and move the mouse to that: 您需要将上下文设置为足够高以包含移动偏移的元素,或者您可以在页面底部找到一个元素,如页脚,并将鼠标移动到:

.findByCssSelector('.wrapper')
.moveMouseTo(0, 1500)   // 1000 pixels is too small to show the scrollbar
.end()

or 要么

.findByTagName('footer')
.then(function (footer) {
    return this.parent.moveMouseTo(footer);
})
.end()

Your test has a few other issues you may want to correct as well. 您的测试还有一些您可能想要纠正的其他问题。 The initial get('about:blank') isn't necessary. 初始get('about:blank')不是必需的。 There's no reason to split the two halves of your command chain; 没有理由将命令链的两半分开; the timeout applies to the whole chain whether it's split or whole. 超时适用于整个链,无论它是分裂还是整体。 You don't need an end after the moveMouseTo ; moveMouseTo之后你不需要end ; end is for popping elements off the command chain's context, and moveMouseTo doesn't add anything to the context. end用于从命令链的上下文中弹出元素,而moveMouseTo不会向上下文添加任何内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM