⚠️ Mobile Automation Fail: Are your Selenium tests ready for a screen rotation?
Ever wonder why your mobile UI tests break when the device flips from portrait to landscape? It's a common pitfall!
📌 Problem Statement
Mobile device screen orientation changes dynamically (portrait/landscape). UI elements, layouts, and validations heavily depend on this state. Unhandled shifts lead to flaky tests in Selenium or Appium.
❌ Challenges:
• Locators or element positions become invalid (e.g., a "Next" button moves).
• Waits misinterpret new layouts, causing StaleElementReferenceException.
• Hard-coded portrait expectations break tests on landscape devices.
💡 Solution & Code Walkthrough
Leverage MobileDriver to programmatically control and query screen orientation. Combine this with robust WebDriverWait conditions to ensure elements are stable post-rotation.
import io.appium.java_client.MobileDriver;
import io.appium.java_client.MobileElement;
import io.appium.java_client.android.orientation.ScreenOrientation; // Or io.appium.java_client.ios.orientation.ScreenOrientation
import java.time.Duration;
import org.openqa.selenium.By; // Essential for element location
import org.openqa.selenium.support.ui.WebDriverWait;
import org.openqa.selenium.support.ui.ExpectedConditions;
public class ScreenOrientationHandler {
private final MobileDriver<MobileElement> driver;
private final WebDriverWait wait;
public ScreenOrientationHandler(MobileDriver<MobileElement> driver) {
this.driver = driver;
this.wait = new WebDriverWait(driver, Duration.ofSeconds(10));
}
/** Locks the device into a specific orientation (e.g., PORTRAIT, LANDSCAPE). */
public void setOrientation(ScreenOrientation orientation) {
driver.rotate(orientation);
}
/** Queries the current screen orientation. */
public ScreenOrientation getCurrentOrientation() {
return driver.getOrientation();
}
/** Waits until a set of elements is visible after an orientation change. */
public void waitUntilElementsAreVisible(By... locators) {
for (By locator : locators) {
wait.until(ExpectedConditions.visibilityOfElementLocated(locator));
}
}
}
🔑 Key Takeaways
• MobileDriver.rotate(orientation) dynamically changes screen orientation.
• MobileDriver.getOrientation() fetches the current device state.
• WebDriverWait with ExpectedConditions is critical for element stability after rotation.
• Always test UI responsiveness in both portrait and landscape modes for complete coverage.
❓ Quick Summary Q&A
Q: Why do mobile tests often fail with orientation changes?
A: UI elements shift, invalidating locators and causing waits to misfire.
Q: How can I change screen orientation in Selenium-Java?
A: Use driver.rotate(ScreenOrientation.LANDSCAPE) or ScreenOrientation.PORTRAIT).
Q: What's the best practice to handle elements after rotation?
A: Implement WebDriverWait with ExpectedConditions.visibilityOfElementLocated to ensure elements are rendered and stable.
TAGS: selenium, java, appium, mobile testing, automation, screen orientation, qa, sdet
────────────────────────────────────────
🚀 Elevate your testing skills! Download our app today:
App Store: ────────────────────────────────────────
📲 𝐅𝐑𝐄𝐄 𝐌𝐎𝐁𝐈𝐋𝐄 𝐀𝐏𝐏 — 𝟔𝟎𝟎+ 𝐒𝐃𝐄𝐓 𝐐&𝐀𝐬
Practice real-world interview scenarios offline on the free QA Automation & SDET Prep app:
🤖 𝐆𝐨𝐨𝐠𝐥𝐞 𝐏𝐥𝐚𝐲 (𝐀𝐧𝐝𝐫𝐨𝐢𝐝):
https://play.google.com/store/apps/details?id=com.app.seleniuminterviewquestions&referrer=utm_source%3Ddevto%26utm_medium%3Darticle%26utm_campaign%3Dselenium_20261031
🍎 𝐀𝐩𝐩 𝐒𝐭𝐨𝐫𝐞 (𝐢𝐎𝐒):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20261031&mt=8
────────────────────────────────────────
Play Store: ────────────────────────────────────────
📲 𝐅𝐑𝐄𝐄 𝐌𝐎𝐁𝐈𝐋𝐄 𝐀𝐏𝐏 — 𝟔𝟎𝟎+ 𝐒𝐃𝐄𝐓 𝐐&𝐀𝐬
Practice real-world interview scenarios offline on the free QA Automation & SDET Prep app:
🤖 𝐆𝐨𝐨𝐠𝐥𝐞 𝐏𝐥𝐚𝐲 (𝐀𝐧𝐝𝐫𝐨𝐢𝐝):
https://play.google.com/store/apps/details?id=com.app.seleniuminterviewquestions&referrer=utm_source%3Ddevto%26utm_medium%3Darticle%26utm_campaign%3Dselenium_20261031
🍎 𝐀𝐩𝐩 𝐒𝐭𝐨𝐫𝐞 (𝐢𝐎𝐒):
https://apps.apple.com/app/id6786760948?pt=128640464&ct=devto_selenium_20261031&mt=8
────────────────────────────────────────
────────────────────────────────────────
Top comments (0)