这里不做appium安装介绍,自行百度了解
项目目录截图:(只需要看Test文件、pom.xml、testng.xml的内容)
首先需要启动appium服务(设置端口启动服务)
pom.xml设置(注意 testng.xml 的设置)
4.0.0
appiumAndroid
appiunTest
pom
1.0-SNAPSHOT
org.apache.maven.plugins
maven-surefire-plugin
testng.xml
io.appium
java-client
7.0.0
junit
junit
RELEASE
compile
org.testng
testng
RELEASE
compile
testng.xml设置(自行学习testng框架)
Appium25
package Test;
import Tools.Module;
import Tools.Screenshot;
import Tools.Verify;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
/**
* @Author HLY
* @Create 2020-01-13 15:14
*/
public class Appium25 {
private AndroidDriver driver;
public Module module=new Module();
public Verify verify=new Verify();
public Screenshot sh=new Screenshot();
@BeforeTest
@Parameters({"port","udid"})
public void before_Test(String port,String udid) throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
//appium的版本
capabilities.setCapability(MobileCapabilityType.APPIUM_VERSION, "v1.15.1");
//android系统的版本
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "9.1.0");
//系统
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");
//
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,"Appium");
capabilities.setCapability(MobileCapabilityType.UDID,udid);
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,"uiAutomator2");
//设备名称 模拟器名称
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, udid);
//设置app的包名
capabilities.setCapability("appPackage", "你要测试的包名");//替换你要测试的包名,找开发要
//设置app的启动activity
capabilities.setCapability("appActivity", "入口activity");//替换你要测试启动activity,找开发要
capabilities.setCapability("newCommandTimeout", 2000);
//此处的地址是appium中的host地址,可以自己修改,只要与appium对应就可以
capabilities.setCapability("noReset", true);
//支持中文输入
capabilities.setCapability("unicodeKeyboard",true);
//重置输入法到原有状态
capabilities.setCapability("resetKeyboard",false);
//systemPort如果您使用的是appium-uiautomator2-driver,请为每个android会话设置一个不同的系统端口,并设置systemPort功能,因为有时如果不使用其他端口,则可能会发生端口冲突,
capabilities.setCapability("systemPort",8201);
//支持自动切换webview
//capabilities.setCapability(MobileCapabilityType.AUTO_WEBVIEW, "true");
try {
this.driver = new AndroidDriver(new URL("http://127.0.0.1:"+port+"/wd/hub"), capabilities);
System.out.println("25启动成功");
}catch (WebDriverException e){
System.out.println(e+":"+"No start appium");
}
}
@Test(groups = {"SDK"})
public void test25(){
try {
module.open_Shooter(driver,"我的",verify);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.findElementByXPath("//*[@text='我的']").click();
//点击登录
driver.findElementById("com.hw.okm:id/iv_my_head").click();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
//点击密码登录
driver.findElementById("com.hw.okm:id/tv_login_phone_change").click();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
//输入用户名
WebElement user = driver.findElementById("your:id/et_login_pwd_account");
user.sendKeys("用户名");
//输入密码
WebElement pw = driver.findElementById("your:id/et_login_pwd_password");
pw.sendKeys("密码");
//登录
driver.findElementById("your:id/btn_login_pwd_next").click();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
WebElement user_name = driver.findElementById("your:id/tv_my_name");
if (user_name.getText().equals("用户名")){
try {
sh.screenshot(driver,"登录截图");
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("25完成");
}
@AfterTest
public void after(){
driver.quit();
}
}
Appium25
package Test;
import Tools.Module;
import Tools.Screenshot;
import Tools.Verify;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.remote.MobileCapabilityType;
import org.openqa.selenium.WebDriverException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.*;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
/**
* @Author HLY
* @Create 2020-01-13 14:03
*/
public class Appium27 {
private AndroidDriver driver;
public Module module=new Module();
public Verify verify=new Verify();
public Screenshot sh=new Screenshot();
@BeforeTest
@Parameters({"port","udid"})
public void before_Test(String port,String udid) throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
//appium的版本
capabilities.setCapability(MobileCapabilityType.APPIUM_VERSION, "v1.15.1");
//android系统的版本
capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "5.1.1");
//系统
capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME,"Android");
//
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,"Appium");
capabilities.setCapability(MobileCapabilityType.UDID,udid);
capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME,"uiAutomator2");
//设备名称 模拟器名称
capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, udid);
//设置app的包名
capabilities.setCapability("appPackage", "com.hw.okm");
//设置app的启动activity
capabilities.setCapability("appActivity", ".activity.SplashActivity");
capabilities.setCapability("newCommandTimeout", 2000);
//此处的地址是appium中的host地址,可以自己修改,只要与appium对应就可以
capabilities.setCapability("noReset", true);
//支持中文输入
capabilities.setCapability("unicodeKeyboard",true);
//重置输入法到原有状态
capabilities.setCapability("resetKeyboard",false);
//systemPort如果您使用的是appium-uiautomator2-driver,请为每个android会话设置一个不同的系统端口,并设置systemPort功能,因为有时如果不使用其他端口,则可能会发生端口冲突,
capabilities.setCapability("systemPort",8203);
//支持自动切换webview
//capabilities.setCapability(MobileCapabilityType.AUTO_WEBVIEW, "true");
try {
this.driver = new AndroidDriver(new URL("http://127.0.0.1:"+port+"/wd/hub"), capabilities);
System.out.println("27启动成功");
}catch (WebDriverException e){
System.out.println(e+":"+"No start appium");
}
}
@Test(groups = {"SDK"})
public void test27(){
try {
module.open_Shooter(driver,"我的",verify);
} catch (InterruptedException e) {
e.printStackTrace();
}
driver.findElementByXPath("//*[@text='我的']").click();
//点击登录
driver.findElementById("your:id/iv_my_head").click();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
//点击密码登录
driver.findElementById("your:id/tv_login_phone_change").click();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
//输入用户名
WebElement user = driver.findElementById("your:id/et_login_pwd_account");
user.sendKeys("用户名");
//输入密码
WebElement pw = driver.findElementById("your:id/et_login_pwd_password");
pw.sendKeys("密码");
//登录
driver.findElementById("your:id/btn_login_pwd_next").click();
driver.manage().timeouts().implicitlyWait(2, TimeUnit.SECONDS);
WebElement user_name = driver.findElementById("your:id/tv_my_name");
if (user_name.getText().equals("用户名")){
try {
sh.screenshot(driver,"登录截图");
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println("27完成");
}
@AfterTest
public void after(){
driver.quit();
}
}
重要:
capabilities.setCapability("systemPort",8203),解决端口号冲突问题8203可以修改;自行看官网讲解;
Appium27 和Appium25 中使用到 Module 、Verify 、Screenshot 、
这三个类是自己封装的一些方法,根据自己的需求修改:test25();test27()中的内容
运行:
选中testng.xml,鼠标右键,然后选择 Run'xxx/xxx/xxx/testng.xml '
结果: