You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Appium+TestNG测试用例元素存在却抛出NoSuchElementException问题求助

Fixing NoSuchElementException in Appium + TestNG Sequential Tests

Hey there, this is a super common pain point when running sequential Appium tests with shared state—let's break this down and get your tests running smoothly!

What's Actually Happening?

The root issue here is residual app state leaking between test cases. Here's the play-by-play:

  • When test case 1 runs, it navigates to its target module and leaves the app stuck on that page after completion.
  • Test case 2 starts up, but the app is still sitting on test case 1's module screen—so when it tries to find elements for its own module, they don't exist (hence the org.openqa.selenium.NoSuchElementException).
  • When you comment out test case 1, test case 2 starts fresh from the app's initial state, so it can navigate to its module and find elements correctly. But test case 3 then inherits test case 2's module state, and the same error pops up again.

Step-by-Step Fixes

1. Reset App State Before Every Test

Use TestNG's @BeforeMethod annotation to guarantee every test starts with a clean slate. You have two solid options here:

  • Reset the app (fast, preserves session):
    @BeforeMethod
    public void resetAppState() {
        // Resets the app to its initial state without restarting the Appium session
        driver.resetApp();
    }
    
  • Restart the app completely (slower but bulletproof):
    @BeforeMethod
    public void restartApp() {
        // Clean up the old session if it exists
        if (driver != null) {
            driver.quit();
        }
        // Re-initialize your Appium driver with your desired capabilities
        driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), desiredCapabilities);
    }
    

2. Explicitly Navigate to Your Target Module

Even with a reset, don't assume the app is in the right place. Add a helper method to navigate to each module explicitly:

private void navigateToModule(String moduleName) {
    // First ensure we're on the home screen
    driver.findElement(By.id("home_screen_button")).click();
    // Tap the target module
    driver.findElement(By.xpath("//android.widget.TextView[@text='" + moduleName + "']")).click();
    // Wait for the module page to fully load
    WebDriverWait wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.id(moduleName + "_header")));
}

@Test
public void testModule1() {
    navigateToModule("Module1");
    // Safely locate and interact with your element
    WebElement targetElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("module1_target_element")));
    String elementValue = targetElement.getText();
    // Add your assertions here
}

@Test
public void testModule2() {
    navigateToModule("Module2");
    WebElement targetElement = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("module2_target_element")));
    String elementValue = targetElement.getText();
}

3. Clean Up After Each Test (Optional but Safe)

Add an @AfterMethod to make sure no leftover state bleeds into the next test:

@AfterMethod
public void cleanUp() {
    // Navigate back to home or reset the app again
    driver.findElement(By.id("back_to_home_button")).click();
    // Or use driver.resetApp(); if you prefer
}

4. Ditch Thread.sleep() for Explicit Waits

Even if your first test works without waits, subsequent tests might face slight delays. Use WebDriverWait to wait for elements to be visible/interactable:

WebDriverWait wait = new WebDriverWait(driver, 15);
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("your_element_id")));

Quick Sanity Checks

  • Double-check locators: Since modules 2-4 work when run first, your locators are probably correct—but it never hurts to confirm them with Appium Inspector.
  • Watch for overlays: Sometimes a popup or toast from a previous test can block elements. Add logic to dismiss unexpected overlays in your @BeforeMethod.

内容的提问来源于stack exchange,提问作者Panshul

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:57:35