Skip to content

Commit

Permalink
fix actionability when element has scroll-behavior: smooth (#15453)
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisbreiding authored Mar 15, 2021
1 parent 1491ab0 commit db6b00d
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 1 deletion.
18 changes: 18 additions & 0 deletions packages/driver/cypress/integration/commands/actions/click_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1607,6 +1607,24 @@ describe('src/cy/commands/actions/click', () => {
expect(args[2]).to.eq(animationDistanceThreshold)
})
})

describe('scroll-behavior', () => {
afterEach(() => {
cy.get('html').invoke('css', 'scrollBehavior', 'inherit')
})

// https://github.com/cypress-io/cypress/issues/3200
it('can scroll to and click elements in html with scroll-behavior: smooth', () => {
cy.get('html').invoke('css', 'scrollBehavior', 'smooth')
cy.get('#table tr:first').click()
})

// https://github.com/cypress-io/cypress/issues/3200
it('can scroll to and click elements in ancestor element with scroll-behavior: smooth', () => {
cy.get('#dom').invoke('css', 'scrollBehavior', 'smooth')
cy.get('#table tr:first').click()
})
})
})

describe('assertion verification', () => {
Expand Down
32 changes: 31 additions & 1 deletion packages/driver/src/cy/actionability.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,32 @@ const verify = function (cy, $el, options, callbacks) {
}
}

// scroll-behavior: smooth delays scrolling and causes the actionability
// check to fail, so the only solution is to remove the behavior and
// make scrolling occur instantly. we do this by adding a style tag
// and then removing it after we finish scrolling
// https://github.com/cypress-io/cypress/issues/3200
const addScrollBehaviorFix = () => {
let style

try {
const doc = $el.get(0).ownerDocument

style = doc.createElement('style')
style.innerHTML = '* { scroll-behavior: inherit !important; }'
// there's guaranteed to be a <script> tag, so that's the safest thing
// to query for and add the style tag after
doc.querySelector('script').after(style)
} catch (err) {
// the above shouldn't error, but out of an abundance of caution, we
// ignore any errors since this fix isn't worth failing the test over
}

return () => {
if (style) style.remove()
}
}

return Promise.try(() => {
let retryActionability
const coordsHistory = []
Expand All @@ -329,8 +355,12 @@ const verify = function (cy, $el, options, callbacks) {
// scroll the element into view
const scrollBehavior = scrollBehaviorOptionsMap[options.scrollBehavior]

$el.get(0).scrollIntoView({ block: scrollBehavior })
const removeScrollBehaviorFix = addScrollBehaviorFix()

debug('scrollIntoView:', $el[0])
$el.get(0).scrollIntoView({ block: scrollBehavior })

removeScrollBehaviorFix()

if (onScroll) {
onScroll($el, 'element')
Expand Down

3 comments on commit db6b00d

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on db6b00d Mar 15, 2021

Choose a reason for hiding this comment

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

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/6.7.1/circle-develop-db6b00d8cac4b9629fe6c41df1d08e2aac7dffe4/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on db6b00d Mar 15, 2021

Choose a reason for hiding this comment

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

AppVeyor has built the win32 x64 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/6.7.1/appveyor-develop-db6b00d8cac4b9629fe6c41df1d08e2aac7dffe4/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on db6b00d Mar 15, 2021

Choose a reason for hiding this comment

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

AppVeyor has built the win32 ia32 version of the Test Runner.

Learn more about this pre-release platform-specific build at https://on.cypress.io/installing-cypress#Install-pre-release-version.

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/6.7.1/appveyor-develop-db6b00d8cac4b9629fe6c41df1d08e2aac7dffe4/cypress.tgz

Please sign in to comment.