Waiting for an element to stop moving

Search for a command to run...

No comments yet. Be the first to comment.
When performing end to end journey test automation on an eccommerce style website, it’s common for security reasons for the credit card fields to be loaded in an iframe to allow payment forms from a payment service provider to added to a website in a...

After 20 years of working with test automation, I like to think I've become quite good at it. Recently, I was advocating for a change in approach at a company I had just joined. I knew it was crucial to clearly explain my method for writing maintaina...

At Glofox, we regularly perform whole-team exploratory testing sessions. These are very effective at discovering problems that are not found when testing around acceptance criteria, as we get multiple different perspectives, and it gives developers t...

Lighthouse has a new user-flow API that allows lab testing at any point within a page's lifespan. There is support for generating a lighthouse report from a puppeteer script, but I wanted to explore using WebdriverIO to do the same! Why would you wan...

Much has been written about flaky tests, the perils of ignoring the signal they may be giving you and how to deal with them from a tools perspective, and here but I've never really read anything about how to stop flaky tests getting added in the firs...

Sometimes with Selenium and libraries or frameworks that use it, we need to wait for an element to stop moving before attempting to click on it (to not enconter StaleElementReferenceExceptions, for example). One approach to wait for the element to stop moving is to query it's location and check this twice, with a small sleep between the calls. If the location of the element is the same both times, then we can consider the element to have stopped moving.
The most appropriate way to implement this will differ with languages and libraries, but WebDriverIO provides a nice way to encapsulate this with Custom Commands
browser.addCommand(
"waitUntilStopMoving",
function () {
browser.waitUntil(
() => {
this.waitForDisplayed();
let initialLocation = this.getLocation();
let initialX = initialLocation.x;
let initialY = initialLocation.y;
browser.pause(200);
let finalLocation = this.getLocation();
let finalX = finalLocation.x;
let finalY = finalLocation.y;
return initialX === finalX && initialY === finalY;
},
{
timeout: 5000,
timeoutMsg: "expected element to have stopped moving",
}
);
},
true
);
If we add the custom command to before in the wdio.config.js
/**
* Gets executed before test execution begins. At this point you can access to all global
* variables like `browser`. It is the perfect place to define custom commands.
* @param {Array.<Object>} capabilities list of capabilities details
* @param {Array.<String>} specs List of spec file paths that are to be run
*/
before: function (capabilities, specs) {
We can then call the custom command quite simply before clicking the element
$('div.confirm').waitUntilStopMoving();
$('div.confirm').click();