0

I'm testing a mobile app using appium + selenium + java

I have an element that I'm trying to find (in order to click on later) by using an xpath.

Here is the xpath: //android.widget.TextView[@text='Mon email']

When I use the Appium inspector in order to find the element, it works (and I can "tap" on it using said Appium inspector).

XPath used in the Appium inspector

However, when I try to find the same element using my java program, I get a "No such element" error even though the app open as it should (and in far less than 10 seconds):

org.openqa.selenium.TimeoutException: Expected condition failed: waiting for presence of element located by: By.xpath: //android.widget.TextView[@text='Mon email'] (tried for 10 second(s) with 500 milliseconds interval)
    at org.openqa.selenium.support.ui.WebDriverWait.timeoutException(WebDriverWait.java:95)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:272)
    at ECM.MyStepdefs.iClickOnTextFromClass(MyStepdefs.java:92)
    at ✽.I click on text "Mon email" from class "android.widget.TextView"(file:/C:/Users/Blop/src/test/resources/ECM/login.feature:5)
Caused by: org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters.
For documentation on this error, please visit: https://www.seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.141.59', revision: 'e82be7d358', time: '2018-11-14T08:17:03'
System info: host: 'BLOP', ip: '194.170.0.1', os.name: 'Windows 10', os.arch: 'amd64', os.version: '10.0', java.version: '1.8.0_191'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities {appActivity: com.something.something.MainA..., appPackage: com.something.something, databaseEnabled: false, desired: {appActivity: com.something.something.MainA..., appPackage: com.something.something, deviceName: My Phone, platformName: android}, deviceManufacturer: samsung, deviceModel: SM-J600FN, deviceName: 785112random74852, deviceScreenSize: 720x1480, deviceUDID: 84337random41285, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: true, platform: LINUX, platformName: Android, platformVersion: 9, takesScreenshot: true, warnings: {}, webStorageEnabled: false}
Session ID: 18e2ca78-1253-1234-1234-12354564865215
*** Element info: {Using=xpath, value=//android.widget.TextView[@text='Mon email']}
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.createException(W3CHttpResponseCodec.java:187)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:122)
    at org.openqa.selenium.remote.http.W3CHttpResponseCodec.decode(W3CHttpResponseCodec.java:49)
    at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:158)
    at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:239)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:552)
    at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:41)
    at io.appium.java_client.AppiumDriver.execute(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.execute(AndroidDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:323)
    at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:61)
    at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
    at org.openqa.selenium.remote.RemoteWebDriver.findElementByXPath(RemoteWebDriver.java:428)
    at io.appium.java_client.DefaultGenericMobileDriver.findElementByXPath(DefaultGenericMobileDriver.java:151)
    at io.appium.java_client.AppiumDriver.findElementByXPath(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.findElementByXPath(AndroidDriver.java:1)
    at org.openqa.selenium.By$ByXPath.findElement(By.java:353)
    at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:315)
    at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:57)
    at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
    at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
    at org.openqa.selenium.support.ui.ExpectedConditions$6.apply(ExpectedConditions.java:182)
    at org.openqa.selenium.support.ui.ExpectedConditions$6.apply(ExpectedConditions.java:179)
    at org.openqa.selenium.support.ui.FluentWait.until(FluentWait.java:249)
    at ECM.MyStepdefs.iClickOnTextFromClass(MyStepdefs.java:92)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at cucumber.runtime.Utils$1.call(Utils.java:26)
    at cucumber.runtime.Timeout.timeout(Timeout.java:16)
    at cucumber.runtime.Utils.invoke(Utils.java:20)
    at cucumber.runtime.java.JavaStepDefinition.execute(JavaStepDefinition.java:57)
    at cucumber.runner.PickleStepDefinitionMatch.runStep(PickleStepDefinitionMatch.java:50)
    at cucumber.runner.TestStep.executeStep(TestStep.java:65)
    at cucumber.runner.TestStep.run(TestStep.java:50)
    at cucumber.runner.PickleStepTestStep.run(PickleStepTestStep.java:43)
    at cucumber.runner.TestCase.run(TestCase.java:46)
    at cucumber.runner.Runner.runPickle(Runner.java:49)
    at cucumber.runtime.Runtime$1.run(Runtime.java:82)
    at cucumber.runtime.Runtime$SameThreadExecutorService.execute(Runtime.java:217)
    at cucumber.runtime.Runtime.run(Runtime.java:79)
    at cucumber.api.cli.Main.run(Main.java:26)
    at cucumber.api.cli.Main.main(Main.java:8)

Here is the code I'm using:

    AppiumDriver<MobileElement> driver = null;

    @Before
    public void setDriver() {
        //Set the Desired Capabilities
        DesiredCapabilities caps = new DesiredCapabilities();
        caps.setCapability("deviceName", "My Phone");
        caps.setCapability("platformName", "Android");
        caps.setCapability("appPackage", "com.something.something");
        caps.setCapability("appActivity", "com.something.something.MainActivity");

        //Instantiate Appium Driver
        try {
            driver = new AndroidDriver<>(new URL("http://0.0.0.0:4723/wd/hub"), caps);

        } catch (MalformedURLException e) {
            System.out.println(e.getMessage());
        }
    }

    @When("I click on text {string} from class {string}")
    public void iClickOnTextFromClass(String text, String myClass) {
        WebDriverWait wait = new WebDriverWait(driver,10);
        wait.until(ExpectedConditions.presenceOfElementLocated(By.xpath("//"+myClass+"[@text='"+text+"']")));
        MobileElement element = driver.findElement(By.xpath("//"+myClass+"[@text='"+text+"']"));

        element.click();
    }

[...]

        iClickOnTextFromClass("Mon email", "android.widget.TextView");

For what it's worth, my code is perfectly working when I'm using it on "Google Play" :

        caps.setCapability("appPackage", "com.android.vending");
        caps.setCapability("appActivity", "com.google.android.finsky.activities.MainActivity");

[...]

        iClickOnTextFromClass("Meilleurs classements", "android.widget.TextView");

I tried to change my xpath to //*[contains(@text, 'email')] but it didn't solve the issue (still not working on my app using java, but working on "google play" using java and working on my app using "Appium Inspector").


So, any idea on how I can solve this so I'm finally able to find my element using java (for automated tests)?


In case it matters, here is (the relevant part of) my pom:


    <dependencies>
        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>4.3.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>io.cucumber</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>4.3.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.3.2</version>
        </dependency>

        <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.141.59</version>
        </dependency>

        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>7.3.0</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.7.0</version>
                <configuration>
                    <encoding>UTF-8</encoding>
                    <source>1.8</source>
                    <target>1.8</target>
                    <compilerArgument>-Werror</compilerArgument>
                </configuration>
            </plugin>
        </plugins>
    </build>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
1

I'm not sure exactly why, but I updated my version of Appium to the v1.15.1 and now things are working. I guess there was some incompatibility of versions somewhere.

Anyway, things are working now, which is great!

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.