运行以下Appium JAVA代码后:
package com.appium;
io.appium.java_client.AppiumDriver;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.junit.*;
import org.openqa.selenium.remote.RemoteWebDriver;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import org.testng.annotations.Test;
public class NewTest {
AndroidDriver driver;
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
// creating a class object
BasicTest testcases = new BasicTest();
// call launch app method
testcases.launchMilk();
// call add name method
testcases.addName();
}
//@Test
public void launchMilk() throws InterruptedException, MalformedURLException
{
File app=new File("D:\\Radio\\Radio_1.5.1700010675.apk");
DesiredCapabilities capabilities = new DesiredCapabilities();
//Name of mobile web browser to automate. Should be an empty string if automating an app instead.
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
//which mobile OS to use: Android, iOS or FirefoxOS
capabilities.setCapability("platformName", "Android");
//Mobile OS version – in this case 4.4 since my device is running Android 4.4.2
capabilities.setCapability(CapabilityType.VERSION, "4.4.2");
//device name – since this is an actual device name is found using ADB
capabilities.setCapability("deviceName", "4d000614aef030c3");
//the absolute local path to the APK
capabilities.setCapability("app", app.getAbsolutePath());
//Java package of the tested Android app
capabilities.setCapability("appPackage", "com.samsung.mdl.radio");
// activity name for the Android activity you want to run from your package. This need to be preceded by a . (example: .MainActivity)
capabilities.setCapability("appActivity", ".MainActivity");
// constructor to initialize driver object
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
Thread.sleep(4000);
System.out.println("App Launched");
//locating the 'I Agree' button
WebElement StartUp = driver.findElementById("dialog_positive_button");
//Clicking the I Agree button
StartUp.click();
System.out.println("I agree button clicked - wait for the screen to load");
Thread.sleep(2000);
System.out.println("Intro Movie Playing");
Thread.sleep(58000);
System.out.println("App Launched");
//List<WebElement> addContactButton = driver.findElementsByClassName("android.widget.ImageView");
WebElement globalMenu = driver.findElementById("nav_drawer_icon");
//System.out.println(addContactButton);
globalMenu.click();
}
public void addName() throws InterruptedException {
System.out.println("Test success");
// write all your tests here
//driver.clickOnView(driver.getView(com.appium.R.id.dialog_positive_button));
}
}
测试的第一步成功运行。该应用程序启动。但执行此行后:
System.out.println("Intro Movie Playing");
Thread.sleep(58000);
System.out.println("App Launched");
应用程序找不到下一个元素,测试失败:
我在控制台中看到以下错误消息:
App Launched
I agree button clicked - wait for the screen to load
Intro Movie Playing
Exception in thread "main" org.openqa.selenium.remote.SessionNotFoundException:
Command duration or timeout: 14 milliseconds
Build info: version: '2.46.0', revision: '87c69e2', time: '2015-06-04 16:17:10'
System info: host: 'SCS02-LT-005', ip: '105.160.8.32', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_45'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities [{app=D:\Radio\Radio_1.5.1700010675.apk, appPackage=com.samsung.mdl.radio, networkConnectionEnabled=true, warnings={}, databaseEnabled=false, version=4.4.2, deviceName=4d000614aef030c3, platform=LINUX, appActivity=.MainActivity, desired={app=D:\Radio\Radio_1.5.1700010675.apk, appPackage=com.samsung.mdl.radio, appActivity=.MainActivity, browserName=, platformName=Android, version=4.4.2, deviceName=4d000614aef030c3}, platformVersion=4.4.2, webStorageEnabled=false, locationContextEnabled=false, browserName=, takesScreenshot=true, javascriptEnabled=true, platformName=Android}]
Session ID: b3d9c76a-5924-4a92-a906-39889e77d37c
*** Element info: {Using=id, value=nav_drawer_icon}
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:204)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:162)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:605)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:27)
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:358)
at org.openqa.selenium.remote.RemoteWebDriver.findElementById(RemoteWebDriver.java:399)
at io.appium.java_client.DefaultGenericMobileDriver.findElementById(DefaultGenericMobileDriver.java:47)
at io.appium.java_client.AppiumDriver.findElementById(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElementById(AndroidDriver.java:1)
at com.appium.BasicTest.launchMilk(BasicTest.java:82)
at com.appium.NewTest.main(NewTest.java:31)
为什么我看到这个错误?为什么代码在测试中没有检测到下一个元素“nav_drawer_icon”?此错误阻止我连续运行多个测试。我正在使用UiAutomator在Android应用程序中找到元素。
提前致谢。
此致
帕
答案 0 :(得分:0)
我认为你非常接近appium命令超时60秒。请尝试使用--command-timeout所需的功能来增加此超时。