appium / appium/java-client

Element not found exception using id or xpath attribute with @AndroidFindBy

Open
#1,968 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
1.3k
Forks
752
Avg merge
6d 21h
Merged PRs (30d)
8

Description

Description

I am writing a simple login test for my Android app using the Appium-java client.
I am also using Page Object Model and Page Factory class to initialize elements in my test.
The test is failing as it cannot locate the email address input field.
I tried to locate the email field using both id and xpath attribute with @AndroidFindBy field decorator but it [didn't] work.
Looking for help on this issue.

Environment

  • Java client build version: 8.5.1
  • Appium server version or git revision if you use some snapshot: Appium v2.0.0-rc.5
  • Device: Android emulator

Code To Reproduce Issue [ Good To Have ]

BasePage.java:

public class BasePage {
    AppiumDriver driver;
    public static final long WAIT = 30;

    public BasePage(AppiumDriver appiumDriver) {
        PageFactory.initElements(new AppiumFieldDecorator(appiumDriver), this);
        driver = appiumDriver;
    }

    public void click(WebElement element) {
        waitForVisibility(element);
        element.click();
    }

    public void enterText(WebElement element, String text) throws InterruptedException {
        Thread.sleep(20000);
        element.sendKeys(text);
    }
}

LoginPage.java

public class LoginPage extends BasePage {
    public LoginPage(AppiumDriver appiumDriver) {

        super(appiumDriver);
    }

    @AndroidFindBy(id = "loginId")
    WebElement emailAddressInput;

    @AndroidFindBy(id = "button-username")
    @iOSXCUITFindBy(id = "button-username")
    WebElement continueButton;

    @FindBy(id = "password")
    @iOSXCUITFindBy(id = "password")
    WebElement passwordInput;

    @AndroidFindBy(id = "button-password")
    @iOSXCUITFindBy(id = "button-password")
    WebElement signInButton;

    public void enterEmailAddressAndProceed(String email) throws InterruptedException {
        enterText(emailAddressInput, email);
        enterText(passwordInput, "password");
        //click(continueButton);
    }
}

BaseTest.java

public class BaseTest {

    public static AppiumDriver driver;
    public static void androidSetUp() throws MalformedURLException {
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformName", "Android");
        capabilities.setCapability("appium:automationName", "UiAutomator2");
        capabilities.setCapability("appium:platformVersion", "14");
        capabilities.setCapability("appium:deviceName", "Android Emulator");
        capabilities.setCapability("adbExecTimeout", 900000);
        capabilities.setCapability("uiautomator2ServerLaunchTimeout", 50000);
        capabilities.setCapability("appium:appWaitActivity", "com.vimcar.nextlogbook.feature.useronboarding.authentication.presentation.LoginActivity");
        capabilities.setCapability("appium:newCommandTimeout", 120);
        capabilities.setCapability("appium:app", System.getProperty("user.dir")+"/src/test/resources/next-logbook-debug.apk");

        driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
    }

    public static void tearDown() {
        if(null != driver) {
            driver.quit();
        }
    }
}

LoginTest.java

public class LoginTest extends BaseTest {
    LoginPage loginPage;

    @Test
    public void existingUserLoginTest() throws Exception {
        androidSetUp();
        loginPage = new LoginPage(driver);
        loginPage.enterEmailAddressAndProceed("prateek.saxena+demo@avrios.com");
        tearDown();
    }
}

Exception Stacktraces

[AndroidUiautomator2Driver@fdfb (4d103231)] Got response with status 404: {"sessionId":"14ea4cc4-5028-468c-9007-59f3acc79c4a","value":{"error":"no such element","message":"An element could not be located on the page using the given search parameters","stacktrace":"io.appium.uiautomator2.common.exceptions.ElementNotFoundException: An element could not be located on the page using the given search parameters\n\tat io.appium.uiautomator2.handler.FindElement.safeHandle(FindElement.java:63)\n\tat io.appium.uiautomator2.handler.request.SafeRequestHandler.handle(SafeRequestHandler.java:59)\n\tat io.appium.uiautomator2.server.AppiumServlet.handleRequest(AppiumServlet.java:277)\n\tat io.appium.uiautomator2.server.AppiumServlet.handleHttpRequest(AppiumServlet.java:271)\n\tat io.appium.uiautomator2.http.ServerHandler.channelRead(ServerHandler.java:68)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:366)\n\tat io.netty.channel.AbstractChannelHandlerContext.invokeChannelRead(AbstractChannelHandlerContext.java:352)\n\tat io.netty.chann...
[debug] [W3C] Matched W3C error code 'no such element' to NoSuchElementError
[debug] [AndroidUiautomator2Driver@fdfb (4d103231)] Encountered internal error running command: NoSuchElementError: An element could not be located on the page using the given search parameters.

Link To Appium Logs

Appium logs when searching by id:
https://gist.github.com/p-saxena/3aaead9344a9acf220f1868f47527960

Appium logs when searching by XPath:
https://gist.github.com/p-saxena/9daed82487152f6e203c7a1e6aec25c3

Appium inspector screenshot:
appium-inspector

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reviewing the locator declarations and initialization flow in BasePage.java and LoginPage.java, then compare the Android and XPath Appium logs with the inspector screenshot. Verify whether the failure is in the Java client lookup path or in the app's reported element attributes; done means identifying a reproducible cause and documenting the required correction.

Written by the indexing model from the issue text.

Assessment

Tech stack
android, java
Domain
mobile-dev, testing-qa
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.