如何使用Nightwatch滚动到元素? [英] How to scroll to element using Nightwatch?

查看:160
本文介绍了如何使用Nightwatch滚动到元素?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用夜表对我的应用程序进行e2etesting.测试之一失败,因为它无法滚动到我怀疑正在测试的元素.问题我需要滚动还是有其他方法可以滚动?这是我正在测试的元素:

I am using nightwatch for e2etesting my app. One of the tests fails because it cannot scroll to the element that it is testing I suspect. Question do I need to scroll or is there another way to do it? This is the element I am testing:

 return this.waitForElementVisible('#myElement', 4000) //wait for it to be visible
       .assert.visible('#myElement')
       .click('#myElement')

该元素位于页面顶部,但testrunner已向下滚动页面一点,在屏幕截图中不可见. 如果需要滚动到该元素怎么办?或:如何测试此元素?

The element is at the top of the page but the testrunner has scrolled a bit down the page and it is not visible in the screenshot. How can I if need be scroll to this element? or : How can I test this element?

推荐答案

在守夜中有一个本机方法可以将元素显示在视图中. (一般来说,元素应始终从夜视/硒中滚动到视图中.但是,如果您想手动操作,则可以使用 getLocationInView():

There is a native method in nightwatch to get elements into view. (In general elements should always be scrolled into view from nightwatch/selenium. But if you want to do that by hand you can use getLocationInView():

return this.getLocationInView('#myElement')
   .assert.visible('#myElement')
   .click('#myElement')

Nightwatch还支持使用 moveTo()通过Webdriver协议直接进行此操作,而无需进行任何抽象.在这种情况下,它看起来更像是:

Nightwatch also supports doing this directly via the Webdriver Protocol using moveTo() without any abstraction. In that case it would look more like:

const self = this;
return this.api.element('#myElement', (res) => {
  self.api.moveTo(res.value.ELEMENT, 0, 0, () => {
    self.assert.visible('#myElement');
    self.click('#myElement');
  })
});

(这只是我脑海中写的,希望我没有记错)

(this was just written from top of my head, hope I didn't make a mistake)

但是在您的情况下可能有帮助的是更改配置中的硒元素滚动行为,例如:

But what could help in your case is changing seleniums element scroll behaviour in the config like:

firefox: {
  desiredCapabilities: {
    browserName: 'firefox',
    javascriptEnabled: true,
    acceptSslCerts: true,
    elementScrollBehavior: 1
  }
}

默认为0->元素滚动到页面顶部

Default is 0 -> Elements are scrolled to top of page

elementScrollBavior 1->元素滚动到页面底部

elementScrollBavior 1 -> Elements are scrolled to bottom of page

这篇关于如何使用Nightwatch滚动到元素?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆