Selenium tests failing randomly on mobile version of Bootstrap-based eCommerce site

I’m having trouble with our Selenium tests for a mobile eCommerce site. The site uses Bootstrap and SAP. We’re running tests on BrowserStack but they keep failing at different spots each time.

I’ve tried adding fluent waits up to 50 seconds for each element, but it’s not helping. The errors are things like ‘Element not clickable’ or ‘Element not found’.

I know Bootstrap sites can be tricky because they’re async, while Selenium is sync. But shouldn’t the waits fix that?

We’re using Selenium 4.16 and Java 21. Has anyone dealt with this before? Any ideas on how to make the tests more stable? I’m totally stumped!

Here’s a simple example of what I’m doing:

WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(50));
WebElement button = wait.until(ExpectedConditions.elementToBeClickable(By.id('buyNow')));
button.click();

But even with this, it sometimes can’t find or click the button. Any suggestions?

hey, i’ve dealt with similar issues. have u tried using javascript executor to click elements? sometims that works better than regular selenium clicks for finnicky sites. also, maybe check if any overlays or popups are blocking the elements? those can mess up tests on mobile. good luck!

Ooh, mobile testing can be such a pain! Have you considered that the viewport size might be causing issues? Sometimes elements shift around or get hidden on smaller screens.

Maybe try setting a consistent viewport size at the start of your tests? Something like:

driver.manage().window().setSize(new Dimension(375, 812)); // iPhone X size

Also, I’m curious - are you using any kind of visual testing tools? They can be super helpful for catching layout shifts that might be messing with your selectors.

What about network conditions? Mobile networks can be pretty unpredictable. Have you tried simulating slower connections to see if that affects things?

Keep us posted on what you find out! Mobile testing is always an adventure.

I’ve encountered similar issues with Bootstrap-based sites. One thing that’s helped me is using CSS selectors instead of IDs or XPaths. Bootstrap dynamically generates classes, so targeting those can be more reliable.

Also, consider implementing a custom ExpectedCondition that checks for both element presence and visibility. Sometimes elements are present in the DOM but not actually visible or interactable.

Another approach is to implement a retry mechanism for your test steps. This can help overcome intermittent failures due to timing issues.

Lastly, ensure your mobile emulation settings in BrowserStack match your actual target devices as closely as possible. Discrepancies there can cause unexpected behavior.

Remember, mobile testing often requires a different mindset than desktop. Patience and creativity are key!