我刚开始学习安卓自动化测试, 我是按照虫师的文章安装的 java 自动化测试平台。
代码如下:
package com.example.Appdemo1;
//import org.testng.annotations.Test;
//import org.testng.annotations.BeforeClass;
//import org.testng.annotations.AfterClass;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.net.MalformedURLException;
public class App {
// private AppiumDriver driver;
//public void setup() throws Exception
public static void main(String[] args) throws MalformedURLException, InterruptedException{
DesiredCapabilities cap = new DesiredCapabilities();
cap.setCapability(CapabilityType.BROWSER_NAME, "");
cap.setCapability("device", "Android");
cap.setCapability("automationName", "Appium");
cap.setCapability("platformName", "Android"); //指定测试平台
cap.setCapability("deviceName", "8ad9f2f07d93"); //指定测试机的 ID,通过 adb 命令adb devices
获取
cap.setCapability("platformVersion", "6.0.1");
//将上面获取到的包名和 Activity 名设置为值
cap.setCapability("appPackage", "com.miui.calculator");
cap.setCapability("appActivity", "com.miui.calculator.cal.CalculatorActivity");
//A new session could not be created 的解决方法
cap.setCapability("appWaitActivity","com.miui.calculator.cal.CalculatorActivity");
//每次启动时覆盖 session,否则第二次后运行会报错不能新建 session
cap.setCapability("sessionOverride", true);
AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
// AndroidDriver driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), cap);
Thread.sleep(5000);
// 获取 1
driver.findElementById("com.miui.calculator:id/btn_1_s").click();
//获取 +
driver.findElementById("com.miui.calculator:id/btn_plus_s").click();
//获取 2
driver.findElementById("com.miui.calculator:id/btn_2_s").click();
//获取=
driver.findElementById("com.miui.calculator:id/btn_equal_s").click();
// driver.findElement(By.id("com.miui.calculator:id/btn_1_s")).click();
// driver.findElement(By.id("com.miui.calculator:id/btn_plus_s")).click();
// driver.findElement(By.id("com.miui.calculator:id/btn_2_s")).click();
// driver.findElement(By.id("com.miui.calculator:id/btn_equal_s")).click();
//
// driver.findElementById("com.miui.calculator:id/btn_1_s").click();
// driver.findElementById("com.miui.calculator:id/btn_plus_s").click();
// driver.findElementById("com.miui.calculator:id/btn_2_s").click();
// driver.findElementById("com.miui.calculator:id/btn_equal_s").click();
// driver.findElement(By.name("1")).click();
// driver.findElement(By.name("5")).click();
// driver.findElement(By.name("9")).click();
// driver.findElement(By.name("delete")).click();
// driver.findElement(By.name("+")).click();
// driver.findElement(By.name("6")).click();
// driver.findElement(By.name("=")).click();
// Thread.sleep(2000);
// driver.findElement(By.name("1")).click();
// driver.findElement(By.name("+")).click();
// driver.findElement(By.name("2")).click();
// driver.findElement(By.name("=")).click();
// driver.findElement("com.miui.calculator:id/btn_1_s").click();
// //获取 +
// driver.findElementById("com.miui.calculator:id/btn_plus_s").click();
// //获取 2
// driver.findElementById("com.miui.calculator:id/btn_2_s").click();
// //获取=
// driver.findElementById("com.miui.calculator:id/btn_equal_s").click();
driver.quit();
}
}
当运行的时候,出现下面的错误:
九月 07, 2018 9:11:22 上午 org.openqa.selenium.remote.ProtocolHandshake createSession
信息: Detected dialect: OSS
Exception in thread "main" org.openqa.selenium.NoSuchElementException: An element could not be located on the page using the given search parameters. (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 0 milliseconds
For documentation on this error, please visit: http://seleniumhq.org/exceptions/no_such_element.html
Build info: version: '3.8.1', revision: '6e95a6684b', time: '2017-12-01T19:05:32.194Z'
System info: host: '6921Y1O05X5QTK6', ip: '172.31.252.191', os.name: 'Windows 7', os.arch: 'amd64', os.version: '6.1', java.version: '1.8.0_181'
Driver info: io.appium.java_client.android.AndroidDriver
Capabilities {appActivity: com.miui.calculator.cal.Cal..., appPackage: com.miui.calculator, appWaitActivity: com.miui.calculator.cal.Cal..., automationName: Appium, browserName: , databaseEnabled: false, desired: {appActivity: com.miui.calculator.cal.Cal..., appPackage: com.miui.calculator, appWaitActivity: com.miui.calculator.cal.Cal..., automationName: Appium, browserName: , deviceName: 8ad9f2f07d93, platformName: Android, platformVersion: 6.0.1, sessionOverride: true}, deviceName: 8ad9f2f07d93, javascriptEnabled: true, locationContextEnabled: false, networkConnectionEnabled: true, platform: LINUX, platformName: LINUX, platformVersion: 6.0.1, sessionOverride: true, takesScreenshot: true, warnings: {}, webStorageEnabled: false}
Session ID: 2a74d568-53ed-493a-8fb4-b4420b783a82
*** Element info: {Using=name, value=delete}
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:214)
at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:166)
at org.openqa.selenium.remote.http.JsonHttpResponseCodec.reconstructValue(JsonHttpResponseCodec.java:40)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:80)
at org.openqa.selenium.remote.http.AbstractHttpResponseCodec.decode(AbstractHttpResponseCodec.java:44)
at org.openqa.selenium.remote.HttpCommandExecutor.execute(HttpCommandExecutor.java:164)
at io.appium.java_client.remote.AppiumCommandExecutor.execute(AppiumCommandExecutor.java:89)
at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:601)
at io.appium.java_client.DefaultGenericMobileDriver.execute(DefaultGenericMobileDriver.java:42)
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:371)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:62)
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.findElementByName(RemoteWebDriver.java:449)
at io.appium.java_client.DefaultGenericMobileDriver.findElementByName(DefaultGenericMobileDriver.java:110)
at io.appium.java_client.AppiumDriver.findElementByName(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElementByName(AndroidDriver.java:1)
at org.openqa.selenium.By$ByName.findElement(By.java:303)
at org.openqa.selenium.remote.RemoteWebDriver.findElement(RemoteWebDriver.java:363)
at io.appium.java_client.DefaultGenericMobileDriver.findElement(DefaultGenericMobileDriver.java:58)
at io.appium.java_client.AppiumDriver.findElement(AppiumDriver.java:1)
at io.appium.java_client.android.AndroidDriver.findElement(AndroidDriver.java:1)
at com.example.Appdemo1.App.main(App.java:70)
好像这里的点击定位操作不对,请大家多多指教.