1

Right now i'm learning about Appium Espresso using this article : https://appiumpro.com/editions/18-using-espresso-with-appium. But instead of using this :

private By loginScreen = MobileBy.AccessibilityId("Login Screen");
private By username = MobileBy.AccessibilityId("username");
private By password = MobileBy.AccessibilityId("password");
private By loginBtn = MobileBy.AccessibilityId("loginBtn");
private By verificationTextEspresso = By.xpath(
        "//com.facebook.react.views.text.ReactTextView[@text='You are logged in as alice']");
private By verificationTextUiAuto2 = By.xpath(
        "//android.widget.TextView[contains(@text, 'alice')]");
private By logoutBtn = By.xpath("//com.facebook.react.views.text.ReactTextView[@text='Logout']");

public void clickBtnReadyLogin() {
    WebDriverWait wait = new WebDriverWait(ANDROID_DRIVER, 10);
    ExpectedCondition<WebElement> loginScreenReady =
            ExpectedConditions.presenceOfElementLocated(loginScreen);
    wait.until(loginScreenReady).click();
}

public void fillUsername() {
    ANDROID_DRIVER.findElement(username).sendKeys("alice");
}

public void fillPassword() {
    ANDROID_DRIVER.findElement(password).sendKeys("mypassword");
}

public void clickBtnLogin() {
    ANDROID_DRIVER.findElement(loginBtn).click();
}

public String verifyUser() {
    return ANDROID_DRIVER.findElement(verificationTextEspresso).getText();
}

public void clickLogoutBtn() {
    ANDROID_DRIVER.findElement(logoutBtn).click();
}

I try to find the element using WebElementFacade :

@FindBy(xpath = "//*[@text='Login Screen']")
private WebElementFacade loginScreen;
@FindBy(xpath = "//*[@text='Username']")
private WebElementFacade username;
@FindBy(xpath = "//*[@text='Password']")
private WebElementFacade password;
@FindBy(xpath = "//*[@content-desc='loginBtn']")
private WebElementFacade loginBtn;
@FindBy(xpath = "//com.facebook.react.views.text.ReactTextView[@text='You are logged in as alice']")
private WebElementFacade verificationTextEspresso;
@FindBy(xpath = "//*[contains(@text, 'You are logged in as alice')]")
private WebElementFacade verificationTextUiAuto2;
@FindBy(xpath = "//*[contains(@text, 'Logout')]")
private WebElementFacade logoutBtn;

public void clickLoginScreen() {
    WebDriverWait wait = new WebDriverWait(AppiumDriverEspresso.ANDROID_DRIVER, 10);
    ExpectedCondition<WebElement> loginScreenReady =
            ExpectedConditions.presenceOfElementLocated(loginScreen);
    wait.until(loginScreenReady).click();
}

public void fillUsername() {
    username.type("alice");
}

public void fillPassword() {
    password.type("mypassword");
}

public void clickBtnLogin() {
    loginBtn.click();
}

public String verifyUser() {
    return verificationTextEspresso.getText();
}

public void clickLogoutBtn() {
    logoutBtn.click();
}

When I use UiAutomator2, this code works fine, but when using the Espresso driver, I get this error :

"error":"no such element","message":"Could not find element with strategy XPATH and selector //[@text='Username']","stacktrace":"io.appium.espressoserver.lib.handlers.exceptions.NoSuchElementException: Could not find element with strategy XPATH and selector //[@text='Username']\n\tat io.appium.espressoserver.lib.handlers.FindElement.handleInternal(FindElement.kt:38)\n\tat io.appium.espressoserver.lib.handlers.FindElement.handleInternal(FindElement.kt:26)\n\tat io.appium.espressoserver.lib.handlers.RequestHandler$DefaultImpls.handle(RequestHandler.kt:28)\n\tat io.appium.espressoserver.lib.handlers.FindElement.handle(FindElement.kt:26)\n\tat io.appium.espressoserver.lib.handlers.FindElement.handle(FindElement.kt:26)\n\tat io.appium.espressoserver.lib.http.Router.route(Router.kt:220)\n\tat io.appium.espressoserver.lib.http.Server.serve(Server.kt:49)\n\tat fi.iki.elonen.NanoHTTPD$HTTPSession.execute(NanoH... [debug] [W3C] Matched W3C error code 'no such element' to NoSuchElementError

Can anyone help me?

I am using Appium 1.18.0-beta.0 Serenity 1.9.45

1 Answers1

0

You need to change you XPath expression to one of the following:

"//*[text()='Username']"

or

"//*[.='Username']"

You can check the difference between the two here

dmle
  • 3,498
  • 1
  • 14
  • 22