com.appium.driver包下创建InitDriver.java类:
package com.appium.driver;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.remote.AndroidMobileCapabilityType;
import io.appium.java_client.remote.MobileCapabilityType;
public class InitDriver {
public static AndroidDriver initDriverWebapp() throws MalformedURLException{
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "chinablue");
caps.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome");
caps.setCapability(MobileCapabilityType.UDID, "DU3ADH154V007404");
caps.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, true);
caps.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD, true);
caps.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
caps.setCapability(MobileCapabilityType.PLATFORM_VERSION, "4.4.2");
URL url = new URL("http://127.0.0.1:4723/wd/hub");
AndroidDriver driver = new AndroidDriver(url,caps);
return driver;
}
public static AndroidDriver initDriver() throws MalformedURLException{
File apk_path = new File("apps/zhihu.apk");
DesiredCapabilities caps = new DesiredCapabilities();
// 与appium服务器相关的caps
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "chinablue");
caps.setCapability(MobileCapabilityType.APP, apk_path.getAbsolutePath());
// 手机网页测试
// caps.setCapability(MobileCapabilityType.BROWSER_NAME, "chinablue");
// caps.setCapability(MobileCapabilityType.UDID, "127.0.0.1:62001");
// 服务端等待客户端发送脚本命令时间
caps.setCapability(MobileCapabilityType.NEW_COMMAND_TIMEOUT, 600);
// 与android相关的caps
// 默认就是false
caps.setCapability(AndroidMobileCapabilityType.NO_SIGN, false);
// 支持输入时使用中文
caps.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, true);
caps.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD, true);
// 判断连接的Android设备应答超时,默认5s
caps.setCapability(AndroidMobileCapabilityType.DEVICE_READY_TIMEOUT, 10);
// 如果apk的起始activity和apk开机后稳定显示的activity不是一个时,需要指定activity
// caps.setCapability(AndroidMobileCapabilityType.APP_WAIT_ACTIVITY, "");
// 启动手机已有apk。此时指定apk包名和起始activity即可
// caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, "");
// caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, "");
AndroidDriver driver = new AndroidDriver(
new URL("http://127.0.0.1:4723/wd/hub"),caps);
return driver;
}
public static AndroidDriver initDriverInstalledApp(String appPackage,String appActivity ) throws MalformedURLException{
File apk_path = new File("apps/zhihu.apk");
DesiredCapabilities caps = new DesiredCapabilities();
caps.setCapability(MobileCapabilityType.DEVICE_NAME, "chinablue");
caps.setCapability(MobileCapabilityType.UDID, "127.0.0.1:62001");
caps.setCapability(AndroidMobileCapabilityType.UNICODE_KEYBOARD, true);
caps.setCapability(AndroidMobileCapabilityType.RESET_KEYBOARD, true);
caps.setCapability(AndroidMobileCapabilityType.APP_PACKAGE, appPackage);
caps.setCapability(AndroidMobileCapabilityType.APP_ACTIVITY, appActivity);
AndroidDriver driver = new AndroidDriver(
new URL("http://127.0.0.1:4723/wd/hub"),caps);
return driver;
}
}
com.appium.driver包下创建AppiumUtils.java类:
package com.appium.driver;
import java.io.File;
import java.io.IOException;
import java.time.Duration;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.Point;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.AndroidKeyCode;
public class AppiumUtils {
/**
* 获取输入框的text文本 将光标置于输入框的最后 根据文本长度循环调用物理键盘删除扫除,逐个字符进行删除
*
* @throws InterruptedException
*
* 注意:此方法对于密码输入框此方法无效。因密码输入框的特性是密码值不会写入到text属性中
* 思路:可根据密码长度强制删除密码最长长度次
*/
public static void clearText(AndroidDriver driver, AndroidElement element)
throws InterruptedException {
element.click();
String text = element.getText();
// 将光标置于文本最后
driver.pressKeyCode(AndroidKeyCode.KEYCODE_MOVE_END);
for (int i = 0; i < text.length(); i++) {
driver.pressKeyCode(AndroidKeyCode.BACKSPACE);
Thread.sleep(200);
}
}
/**
* @param driver
* @param element
* @param passwdMaxLength
* @throws InterruptedException
*/
public static void clearWithPwd(AndroidDriver driver, AndroidElement element, int passwdMaxLength)
throws InterruptedException {
element.click();
driver.pressKeyCode(AndroidKeyCode.KEYCODE_MOVE_END);
for (int i = 0; i < passwdMaxLength; i++) {
driver.pressKeyCode(AndroidKeyCode.BACKSPACE);
Thread.sleep(200);
}
}
public static boolean isElementExist(AndroidDriver driver, By by) {
try {
driver.findElement(by);
return true;
} catch (Exception e) {
// TODO: handle exception
return false;
}
}
// public static void swipeToUp(AndroidDriver driver,int during,int num){
// int width = driver.manage().window().getSize().width;
// int height = driver.manage().window().getSize().height;
// for (int i = 0; i < num; i++) {
// driver.swipe(width/2, height*3/4, width/2, height*1/4, during);
//
// }
// }
public static void swipeToLeft(AndroidDriver driver,int during){
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
TouchAction leftSwipe = new TouchAction(driver);
leftSwipe.press(width/4, height/2).waitAction(Duration.ofMillis(during)).moveTo(width*3/4, height/2).release();
leftSwipe.perform();
}
public static void swipeToUp(AndroidDriver driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
TouchAction upSwipe = new TouchAction(driver);
upSwipe.press(width/2, height/4).waitAction(Duration.ofMillis(during)).moveTo(width/2, height*3/4).release();
upSwipe.perform();
}
public static void swipeToDown(AndroidDriver driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
TouchAction downSwipe = new TouchAction(driver);
downSwipe.press(width/2, height*3/4).waitAction(Duration.ofMillis(during)).moveTo(width/2, height/4).release();
downSwipe.perform();
}
public static void swipeToRight(AndroidDriver driver, int during) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
TouchAction rightSwipe = new TouchAction(driver);
rightSwipe.press(width*3/4, height/2).waitAction(Duration.ofMillis(during)).moveTo(width/4, height/2).release();
rightSwipe.perform();
}
enum SwipeDirection{
up,down,right,left;
public static SwipeDirection getSwipeDirection(String direction){
return valueOf(direction);
}
}
public static void swipe(AndroidDriver driver, int during, String direction){
switch (SwipeDirection.getSwipeDirection(direction.toLowerCase())) {
case up:
swipeToUp(driver, during);
break;
case down:
swipeToDown(driver, during);
break;
case right:
swipeToRight(driver, during);
break;
case left:
swipeToLeft(driver, during);
break;
default:
System.out.println("方向参数只能是 up/down/right/left");
break;
}
}
/**
* @param driver
* @param by
* @return
* 功能:定位元素并且获取元素结束点坐标
*/
public static Point getElementCoor(AndroidDriver driver,By by){
AndroidElement element = driver.findElement(by);
// // 获取元素起始点坐标
// int startx = element.getLocation().getX();
// int starty = element.getLocation().getY();
// // 获取元素的宽高
// int width = element.getSize().getWidth();
// int height = element.getSize().getHeight();
// // 计算出元素结束点坐标
// int endx = startx + width;
// int endy = starty + height;
return getElementCoor(element);
}
/**
* @param driver
* @return:返回一个Point对象
*/
public static Point getElementCoor(AndroidElement element){
// 获取元素起始点坐标
int startx = element.getLocation().getX();
int starty = element.getLocation().getY();
// 获取元素的宽高
int width = element.getSize().getWidth();
int height = element.getSize().getHeight();
// 计算出元素结束点坐标
int endx = startx + width;
int endy = starty + height;
return new Point(endx,endy);
}
/**
* 截图方法
* @param driver
* @param filename
* @throws IOException
*/
public void getScreenShotcut(AndroidDriver driver,String filename) throws IOException{
File file = driver.getScreenshotAs(OutputType.FILE);
FileUtils.copyFile(file, new File("images\\"+filename+".png"));
}
}
样例代码:
package com.appium.zhihu;
import java.io.File;
import java.net.MalformedURLException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.ScreenOrientation;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.mobile.NetworkConnection;
import org.openqa.selenium.mobile.NetworkConnection.ConnectionType;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.appium.driver.AppiumUtils;
import com.appium.driver.InitDriver;
import com.sun.javafx.scene.traversal.Direction;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.Activity;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidElement;
import io.appium.java_client.android.AndroidKeyCode;
import io.appium.java_client.functions.ExpectedCondition;
import net.bytebuddy.asm.Advice.This;
public class ZhihuLogin {
public static AndroidDriver driver;
public ZhihuLogin(AndroidDriver driver){
this.driver = driver;
}
public void login(){
// driver.findElement(By.xpath("//*[@text='未登录']")).click();
driver.findElementByAndroidUIAutomator("new UiSelector().text(\"未登录\")").click();
delayTime(200);
// driver.findElement(By.xpath("//android.support.v7.widget.LinearLayoutCompat/android.support.v7.widget.LinearLayoutCompat/android.widget.ImageView[1]")).click();
driver.findElement(By.xpath("//*[@resource-id='com.zhihu.android:id/login_phone']")).click();
delayTime(200);
driver.findElement(By.xpath("//*[@text='密码登录']")).click();
delayTime(200);
driver.findElement(By.xpath("//*[@text='输入手机号或邮箱']")).sendKeys("15099947428");
delayTime(200);
driver.findElement(By.xpath("//android.support.v7.widget.LinearLayoutCompat/android.widget.EditText[2]")).sendKeys("chinablue2018");
delayTime(200);
driver.findElement(By.xpath("//*[@text='登录']")).click();
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 拿到界面上的所有资源
if(driver.getPageSource().contains("已购")){
System.out.println("登陆成功");
}else{
System.out.println("登陆失败");
}
}
public static void logout(){
clickMenu(5);
if(AppiumUtils.isElementExist(driver, By.name("设置"))){
driver.findElement(By.name("设置")).click();
}else{
AppiumUtils.swipe(driver, 500, "down");
driver.findElement(By.name("设置")).click();
}
while(true){
if(AppiumUtils.isElementExist(driver, By.id("com.zhihu.android:id/func_text"))){
driver.findElement(By.id("com.zhihu.android:id/func_text")).click();
driver.findElement(By.name("确定")).click();
break;
}else{
AppiumUtils.swipe(driver, 500, "down");
}
}
}
/**
*
* @param order
*/
public static void clickMenu(int order){
//android.widget.LinearLayout/*[@class='android.support.v7.widget.LinearLayoutCompat'][1]
driver.findElement(By.xpath("//android.widget.HorizontalScrollView/android.widget.LinearLayout/descendant::android.support.v7.widget.LinearLayoutCompat["+order+"]")).click();
}
public static void delayTime(long timeout){
try {
Thread.sleep(timeout);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void attention(){
List titles = driver.findElements(By.xpath("//*[@resource-id='com.zhihu.android:id/title']"));
System.out.println("当前页面文章标题数为:"+titles.size());
for(AndroidElement title: titles){
title.click();
delayTime(5000);
driver.pressKeyCode(4);
}
}
public void loginByUiautomator(){
// 此方法只能用于Android原生APP
driver.findElementByAndroidUIAutomator("new UiSelector().text(\"未登录\")").clear();
}
public void nightMode(){
clickMenu(5);
AndroidElement nightMode = driver.findElement(By.id("com.zhihu.android:id/night_mode_switch"));
String status = nightMode.getAttribute("checked");
System.out.println(status);
AppiumUtils.swipe(driver, 500, "down");
nightMode.click();
status = nightMode.getAttribute("checked");
if(status.equals("true")){
System.out.println("夜间模式打开");
}else{
System.out.println("夜间模式关闭");
}
}
/**
* 获取元素的起始点坐标、结束点坐标、中心点坐标
*/
public void getCoor(){
// int end_x = AppiumUtils.getElementCoor(driver, By.id("")).getX();
// int end_y = AppiumUtils.getElementCoor(driver, By.id("")).getY();
// 上述写法定位元素次数有所增加,故需要优化
Point p = AppiumUtils.getElementCoor(driver, By.id(""));
int endx = p.getX();
int endy = p.getY();
}
public static void tap(){
AndroidElement element = driver.findElement(By.id(""));
element.replaceValue("");
// element.tap();
}
/**
* 打开另一个应用 快手app
*/
public void startKuaiShou(){
Activity activity = new Activity("com.smile.gifmaker", "com.yxcorp.gifshow.HomeActivity");
// activity.setAppWaitPackage("com.smile.gifmaker");
// activity.setAppWaitActivity("com.yxcorp.gifshow.HomeActivity");
// activity.setStopApp(false);
driver.startActivity(activity);
delayTime(5000);
// 获取当前activity,可以判断界面跳转(前提是跳转后界面的activity发生了变化)
String currentActivity = driver.currentActivity();
System.out.println(currentActivity);
}
/**
* 获取网络状态:getNetworkConnection().toString();或getNetworkConnection().value();
* 设置网络状态:setNetworkConnection()
*/
public void networkGetAndSet(){
// new NetworkConnection() {
//
// public ConnectionType setNetworkConnection(ConnectionType type) {
// // TODO Auto-generated method stub
// return null;
// }
//
// public ConnectionType getNetworkConnection() {
// // TODO Auto-generated method stub
// return null;
// }
// };
NetworkConnection mobileDriver = (NetworkConnection)driver;
String networkInfo = mobileDriver.getNetworkConnection().toString();
System.out.println(networkInfo);
}
public static void getOrientation(){
ScreenOrientation orientation = driver.getOrientation();
// 获取屏幕方向信息
System.out.println(orientation.value());
delayTime(5000);
// 设置屏幕方向,如果app本身不支持横竖屏切换,那么会报错
driver.rotate(ScreenOrientation.LANDSCAPE);
System.out.println(orientation.value());
}
/**
* qq应该
* com.tencent.mobileqq
* com.tencent.mobileqq.activity.SplashActivity
*/
public void appInstallandRemove(){
if(driver.isAppInstalled("com.tencent.mobileqq")){
driver.removeApp("com.tencent.mobileqq");
}
driver.installApp("D:\\eclipse\\workspace0122\\appiumtest\\apps\\qq.apk");
System.out.println(driver.isAppInstalled("com.tencent.mobileqq"));
}
/**
* 显示等待的两个使用场景
* 场景1:等待某个元素出现
* 场景2:等待属性值出现
*/
public void waitUtilElement(){
// 场景1:等待某个元素在30s内出现,不出现就抛出异常
WebDriverWait wait = new WebDriverWait(driver, 30);
AndroidElement element = (AndroidElement) wait.until(
ExpectedConditions.presenceOfElementLocated(By.id("")));
// 场景2:自定义显示等待 getText
WebDriverWait wait1 = new WebDriverWait(driver, 60);
wait1.until(new ExpectedCondition() {
public Boolean apply(WebDriver arg0) {
// TODO Auto-generated method stub
return driver.findElement(
By.id("")).getText().contains("注册或登陆");
}
});
// 场景3:自定义显示等待 getAttribute
WebDriverWait wait2 = new WebDriverWait(driver, 60);
wait2.until(new ExpectedCondition() {
public Boolean apply(WebDriver arg0) {
// TODO Auto-generated method stub
return driver.findElement(
By.id("")).getAttribute("checked").equals("true");
}
});
// 场景4:自定义显示等待 getAttribute 判断元素是否被置灰
WebDriverWait wait3 = new WebDriverWait(driver, 60);
wait3.until(new ExpectedCondition() {
public Boolean apply(WebDriver arg0) {
// TODO Auto-generated method stub
return driver.findElement(
By.id("")).getAttribute("enabled").equals("false");
}
});
}
/**
* 如果手机锁屏,对屏幕进行解锁
*/
public void lockToUnlock(){
if(driver.isLocked()){
driver.unlockDevice();
}
// 长按某个键
driver.longPressKeyCode(3);
}
public void touchaction(){
AndroidElement element = driver.findElement(By.id(""));
TouchAction touch = new TouchAction(driver);
// 长按元素
touch.longPress(element).release().perform();
// 长按某个坐标点
touch.longPress(300, 500).release().perform();
// 在元素的某一个点上长按
touch.longPress(element, 20, 20).release().perform();
touch.press(element).release().perform();
touch.press(300, 500).release().perform();
//实现拖拽操作 实现手势解锁
}
public void changeLanguageHit(){
driver.pressKeyCode(AndroidKeyCode.HOME);
driver.findElement(By.name("设置")).click();
AppiumUtils.isElementExist(driver, By.xpath(""));
}
/**
* 1. 通过id定位当前屏幕的所有设置,并且获取这个设置的文本存入到list。如果这个list大小小于15,那就向上滑动
* 2. 再次获取当前屏幕的所有设置,并且将所有设置文本存入list,并且判重。再次判断list大小是否大于15,如果大于,直接list.get(索引值).click()完成点击
*
*/
public void helpClick(){
driver.pressKeyCode(AndroidKeyCode.HOME);
driver.findElement(By.name("设置")).click();
List settingTexts = new ArrayList();
List settings = new ArrayList();
while(true){
List titles = driver.findElements(By.id("android:id/title"));
for(AndroidElement title:titles){
String text = title.getText();
if(!settingTexts.contains(text)){
settingTexts.add(text);
settings.add(title);
}
}
System.out.println("*********");
System.out.println(settingTexts.size());
System.out.println("*********");
if(settingTexts.size()>33){
System.out.println("--------------");
for(String a:settingTexts){
System.out.println(a);
}
System.out.println("--------------");
settings.get(30).click();
break;
}else{
AppiumUtils.swipe(driver, 1000, "down");
}
delayTime(3000);
}
}
/**
* 点击倒数第七个
* 先滑动到底部,获取最后一屏的所有title,然后点击倒数第7个
* 如何判断滑动到最后一屏?
* 答:每次滑动前后的界面是否一致,如何一致则表示滑动到底部了,
* 其中每个设置的文本作为判断滑动屏幕是否一样的依据
*/
public void settingInputWayClick(){
driver.pressKeyCode(AndroidKeyCode.HOME);
driver.findElement(By.xpath("//*[@resource-id='com.huawei.android.launcher:id/hotseat']/android.view.View/android.view.View/android.widget.TextView[4]")).click();
ArrayList oldTexts = new ArrayList();
ArrayList newTexts = new ArrayList();
// ArrayList androidelement = new ArrayList();
List titles = driver.findElements(By.id("android:id/title"));
for(AndroidElement title:titles){
String text = title.getText();
oldTexts.add(text);
}
AppiumUtils.swipe(driver, 500, "down");
for(AndroidElement title:titles){
String text = title.getText();
newTexts.add(text);
}
}
/**
* 拖拽操作:知乎app拖拽到快手app的位置
*/
public void drag(){
driver.pressKeyCode(AndroidKeyCode.HOME);
TouchAction touch = new TouchAction(driver);
AndroidElement zhihu = driver.findElement(By.name("知乎"));
AndroidElement contact = driver.findElement(By.name("快手"));
touch.longPress(zhihu).moveTo(contact).release().perform();
}
public void shoushisuo(){
driver.pressKeyCode(AndroidKeyCode.HOME);
driver.findElement(By.name("设置")).click();;
driver.findElement(By.name("安全")).click();;
driver.findElement(By.name("屏幕锁定")).click();;
driver.findElement(By.name("图案")).click();
AndroidElement element = driver.findElement(By.id("com.android.settings:id/lockPattern"));
Point location = element.getLocation();
int startx = location.getX();
int starty = location.getY();
Dimension size = element.getSize();
int height = size.getHeight();
int width = size.getWidth();
// 将9个点加入到集合中
// 绘制图案、注意绝对坐标和相对坐标. movoTo(1,1)
TouchAction touch = new TouchAction(driver);
touch.press(300,500).moveTo(20,30).moveTo(100, 20).release().perform();
}
public static void main(String[] args) {
AndroidDriver driver = null;
try {
driver = InitDriver.initDriver();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ZhihuLogin zhihu = new ZhihuLogin(driver);
// 此后所有的find查找元素 隐式等待时间都是10s
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
delayTime(15000);
zhihu.login();
// zhihu.settingInputWayClick();
// zhihu.shoushisuo();
// delayTime(5000);
// zhihu.appInstallandRemove();
// zhihu.getOrientation();
// zhihu.delayTime(6000);
// zhihu.nightMode();
// for (int i = 0; i < 3; i++) {
// AppiumUtils.swipe(driver, 300,"down");
// zhihu.delayTime(2000);
// }
// zhihu.clickMenu(1);
// zhihu.delayTime(6000);
// logout();
// for (int i = 0; i < 3; i++) {
// AppiumUtils.swipe(driver, 300,"down");
// zhihu.delayTime(2000);
// }
//
// AppiumUtils.swipe(driver, 300,"left");
// for (int i = 0; i < 3; i++) {
// AppiumUtils.swipe(driver, 300,"down");
// zhihu.delayTime(2000);
// }
//
// AppiumUtils.swipe(driver, 300,"right");
// for (int i = 0; i < 3; i++) {
// AppiumUtils.swipe(driver, 300,"down");
// zhihu.delayTime(2000);
// }
// while(true){
// AppiumUtils.swipeToDown(driver, 100);
// zhihu.delayTime(100);
// }
//
// for (int i = 0; i < 8; i++) {
// AppiumUtils.swipeToUp(driver, 200);
// zhihu.delayTime(3000);
// }
// zhihu.attention();
driver.quit();
}
}