Rajiv
Rajiv

Reputation: 1

Android application Automation Script: Through Appium, Selenium WebDriver, Java: NoSuchElementFoundException

I am trying to open a mobile .apk file in my android device then select a country "USA" and click on "Save" button.

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.By;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.Test;

import io.appium.java_client.android.AndroidDriver;

public class DemoC {

AndroidDriver driver =null;
DesiredCapabilities capabilities;
File app = new     File("C:\\Users\\310250972\\workspace\\DreamMapper_2.5_Alpha_July_5_65.apk");

@Test(priority=1)
public void invokeApp() throws MalformedURLException
{
capabilities = new DesiredCapabilities();
capabilities.setCapability("automationName","Appium" );
capabilities.setCapability("platformName","Android" );
capabilities.setCapability("platformVersion","6.0.1");
capabilities.setCapability("deviceName","Galaxy S6" );

capabilities.setCapability("app", app.getAbsolutePath());

//capabilities.setCapability("appPackage","com.philips.sleepmapper.root");
//capabilities.setCapability("appactivity","com.philips.sleepmapper.activity.SplashScreenActivity");

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

}

@Test(priority=2)
public void selectCountry() throws InterruptedException{
Thread.sleep(20000);
//driver.findElement(By.xpath("//android.widget.TextView[Contain]"))
driver.findElement(By.xpath("//android.widget.TextView[contains(@text='United States')]")).click();
Thread.sleep(10000);
driver.findElement(By.xpath("//android.widget.TextView[contains(@text='Save')]")).click();
//  driver.findElement(By.name("Save")).click();
}

}[I want to select "United States" and click on "Save" button][1]

// On execution, test method "selectCountry()" get failed. Nosuchelementfoundexception

Upvotes: 0

Views: 247

Answers (2)

Daniel
Daniel

Reputation: 64

Avoid using Thread.sleep() as much as possible because it tells to wait for an exact amount of time which is bad when testing with test scenarios that has factors like different internet speed or different file size. Use WebDriverWait instead.

WebDriverWait wait = new WebDriverWait(driver, 60);
wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//android.widget.TextView[contains(@text='United States')]").cick();

wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//android.widget.TextView[contains(@text='Save')]")).click();

WebDriverWait tells appium to wait for a certain amount of time(in this code its 60 seconds) or for the ExpectedConditions to be met.

Upvotes: 0

Lasse
Lasse

Reputation: 880

When you use Xpath query with contains, you will need to use a comma separator rather than equals.

So, your steps should look like this:

driver.findElement(By.xpath("//android.widget.TextView[contains(@text, 'United States')]")).click();
Thread.sleep(10000);
driver.findElement(By.xpath("//android.widget.TextView[contains(@text, 'Save')]")).click();

If this doesn't help, you could try using driver.getPageSource(); to make sure that the elements are actually there with the expected @text values. The command returns all the existing elements and can be used to verify that elements exist with correct attributes.

Upvotes: 1

Related Questions