nightwatch.js

.getLocationInView()

Determine an element's location on the screen once it has been scrolled into view. Uses elementIdLocationInViewprotocol command.

Parameters:
NameTypedescription
selector string The CSS/Xpath selector used to locate the element.
callback function Callback function which is called with the result value.
Returns
Typedescription
x: number, y: number The X and Y coordinates for the element on the page.
this.demoTest = function (browser) {
  browser.getLocationInView("#main ul li a.first", function(result) {
    this.assert.equal(typeof result, "object");
    this.assert.equal(result.status, 0);
    this.assert.equal(result.value.x, 200);
    this.assert.equal(result.value.y, 200);
  });
};

 

1down voteaccepted

If you are using JQuery you can do it like this:

browser.execute(function () {
    $(window).scrollTop($('some-element').offset().top - ($(window).height() / 2));
}, []);

Or using plain JavaScript:

browser.execute(function () {
    document.getElementById("some-id").scrollIntoView();
}, []);

  将屏幕滚动到底部实例:

module.exports = {
    'your-test': function (browser) {
        browser
            .url("https://www.sohu.com/")
            .pause(1000)
            .waitForElementPresent('body', 2000, "Be sure that the page is loaded")
            .maximizeWindow()
            .pause(1000)
            .execute(function() { window.scrollBy(0, 10000000); }, []) 
            .pause(2000)
            .end();

    }
}

  

原文地址:https://www.cnblogs.com/saryli/p/7002122.html