Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add scrollTo selector #774

Open
wants to merge 12 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ removeSelectors // Array of selectors set to display: none
onReadyScript // After the above conditions are met -- use this script to modify UI state prior to screen shots e.g. hovers, clicks etc.
hoverSelector // Move the pointer over the specified DOM element prior to screen shot (available with default onReadyScript)
clickSelector // Click the specified DOM element prior to screen shot (available with default onReadyScript)
scrollToSelector // Scrolls the specified DOM element into view prior to screen shot (available with default onReadyScript)
postInteractionWait // Wait for a selector after interacting with hoverSelector or clickSelector (optionally accepts wait time in ms. Idea for use with a click or hover element transition. available with default onReadyScript)
selectors // Array of selectors to capture. Defaults to document if omitted. Use "viewport" to capture the viewport size. See Targeting elements in the next section for more info...
selectorExpansion // See Targeting elements in the next section for more info...
Expand Down
9 changes: 9 additions & 0 deletions capture/engine_scripts/chromy/clickAndHoverHelper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = function (chromy, scenario) {
var hoverSelector = scenario.hoverSelector;
var clickSelector = scenario.clickSelector;
var scrollToSelector = scenario.scrollToSelector;
var postInteractionWait = scenario.postInteractionWait; // selector [str] | ms [int]

if (hoverSelector) {
Expand All @@ -18,6 +19,14 @@ module.exports = function (chromy, scenario) {
.click(clickSelector);
}

if (scrollToSelector) {
chromy
.wait(scrollToSelector)
.evaluate(function() {
document.querySelector(scrollToSelector).scrollIntoView();
}, scrollToSelector);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you want this...

.evaluate(function(scrollToSelector){
  document.querySelector(scrollToSelector).scrollIntoView();
}, scrollToSelector);

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You probably also need to guard against scrollToSelector not being found.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @garris, I made these updates but it's still not working (ie the screenshot shows that it did not properly scroll the container)

}

if (postInteractionWait) {
chromy.wait(postInteractionWait);
}
Expand Down
9 changes: 9 additions & 0 deletions capture/engine_scripts/puppet/clickAndHoverHelper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module.exports = async (page, scenario) => {
var hoverSelector = scenario.hoverSelectors || scenario.hoverSelector;
var clickSelector = scenario.clickSelectors || scenario.clickSelector;
var scrollToSelector = scenario.scrollToSelectors || scenario.scrollToSelector;
var postInteractionWait = scenario.postInteractionWait; // selector [str] | ms [int]

if (hoverSelector) {
Expand All @@ -17,6 +18,14 @@ module.exports = async (page, scenario) => {
}
}

if (scrollToSelector) {
await page.waitFor(scrollToSelector);
await page.evaluate(scrollToSelector => {
document.querySelector(scrollToSelector).scrollIntoView();
}, scrollToSelector);
}
}

if (postInteractionWait) {
await page.waitFor(postInteractionWait);
}
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
42 changes: 42 additions & 0 deletions test/configs/special_cases/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.scrollable-container {
background-color: lightblue;
width: 500px;
height: 175px;
overflow: scroll;
}

.image-box {
width: 200px;
margin: auto;
}

img.lemurFace {
display: block;
width: 100%;
height: auto;
}
</style>
</head>
<body>
<div class="main-page-layout">
<div class="scrollable-container">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vivamus magna tellus, pellentesque
eget sagittis quis, ullamcorper sed sapien. Proin eget mollis nulla. Sed dapibus arcu id
pretium ornare. Sed at faucibus tellus. Vestibulum sed efficitur dui, ut dapibus diam.
Suspendisse potenti. Curabitur porta, justo quis convallis dictum, nulla ex rutrum ante, eu
arius velit dui in libero. Vestibulum eleifend vitae ipsum in mattis. Vivamus sed tempor quam.
Quisque vulputate feugiat lobortis. Aliquam erat volutpat.
</p>

<div class="image-box">
<img id="lemurFace" src="../../../assets/lemurFace.png" class="lemurFace">
</div>
</div>
</div>
</body>
</html>
42 changes: 42 additions & 0 deletions test/configs/special_cases/special_cases_chromy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const ENGINE = 'chromy';
const SCRIPT_PATH = 'chromy';

module.exports = {
id: `${ENGINE}_backstop_features`,
viewports: [
{
label: 'phone',
width: 320,
height: 480
},
{
label: 'tablet',
width: 1024,
height: 768
}
],
onBeforeScript: `${SCRIPT_PATH}/onBefore.js`,
onReadyScript: `${SCRIPT_PATH}/onReady.js`,
scenarios: [
{
label: 'scrollTo',
url: './index.html',
scrollToSelector: '.lemurFace',
selectors: ['.lemurFace']
}
],
paths: {
bitmaps_reference: '../backstop_data/bitmaps_reference',
bitmaps_test: '../backstop_data/bitmaps_test',
engine_scripts: '../backstop_data/engine_scripts',
html_report: '../backstop_data/html_report',
ci_report: '../backstop_data/ci_report'
},
report: ['browser'],
engine: ENGINE,
engineOptions: {},
asyncCaptureLimit: 10,
asyncCompareLimit: 50,
debug: false,
debugWindow: false,
};
42 changes: 42 additions & 0 deletions test/configs/special_cases/special_cases_puppet.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
const ENGINE = 'puppeteer';
const SCRIPT_PATH = 'puppeteer';

module.exports = {
id: `${ENGINE}_backstop_features`,
viewports: [
{
label: 'phone',
width: 320,
height: 480
},
{
label: 'tablet',
width: 1024,
height: 768
}
],
onBeforeScript: `${SCRIPT_PATH}/onBefore.js`,
onReadyScript: `${SCRIPT_PATH}/onReady.js`,
scenarios: [
{
label: 'scrollTo',
url: './index.html',
scrollToSelector: '.lemurFace',
selectors: ['.lemurFace']
}
],
paths: {
bitmaps_reference: '../backstop_data/bitmaps_reference',
bitmaps_test: '../backstop_data/bitmaps_test',
engine_scripts: '../backstop_data/engine_scripts',
html_report: '../backstop_data/html_report',
ci_report: '../backstop_data/ci_report'
},
report: ['browser'],
engine: ENGINE,
engineOptions: {},
asyncCaptureLimit: 10,
asyncCompareLimit: 50,
debug: false,
debugWindow: false,
};