简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM