package test_ios;
import org.testng.annotations.Test;
import java.io.File;
import java.net.URL;
import java.util.List;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.NoSuchElementException;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import io.appium.java_client.MobileBy;
import io.appium.java_client.ios.IOSDriver;
public class iOSTest {
static IOSDriver<WebElement> driver= null;
@Before
public void setUp() throws Exception {
// set up appium
File classpathRoot = new File(System.getProperty("user.dir"));//返回当前用户工作目录
File appDir = new File(classpathRoot, "apps");
File app = new File(appDir, "XX.app");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("platformName", "iOS");// 设备平台
capabilities.setCapability("deviceName", "iPhone 4s");// 设备名称
capabilities.setCapability("platformVersion", "8.4");// 版本
capabilities.setCapability("app", app.getAbsolutePath());// apk路径
capabilities.setCapability("noSign", true);// 禁止重签名
capabilities.setCapability("autoAcceptAlerts", true);// 自动接受提示信息
driver = new IOSDriver<WebElement>(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
}
@Test
public void testCaseMT() throws Exception {
//我的账户login
getElement("xpath", " //UIAApplication[1]/UIAWindow[1]/UIAButton[1]").click();
Thread.sleep(2000);
//输入账户
getElement("classname", "UIATextField").click();
getElement("classname","UIATextField").clear();
getElement("xpath", "//UIAApplication[1]/UIAWindow[2]/UIAImage[1]/UIAKey[30]").click();
getElentBaiduKeyUnm("0").click();
getElentBaiduKeyUnm("1").click();
getElentBaiduKeyUnm("2").click();
getElentBaiduKeyUnm("3").click();
//输入密码
getElement("name", "UIASecureTextField").click();
getElement("name", "more, numbers").click();
getElentSysKeyUnm("1").click();
getElentSysKeyUnm("2").click();
getElentSysKeyUnm("3").click();
//登录
getElement("name","登录").click();
Thread.sleep(5000);
}
@After
public void done()
{
driver.quit();
}
//定位单个元素
public WebElement getElement(String type, String elStr) {
WebElement we = null;
// 先按xpath进行查询,如果没找到,按name查询,最后按classname查询。
try {
if(type.equals("xpath"))
{
we = driver.findElement(MobileBy.xpath(elStr));
System.out.println("成功找到 [ xpath = " + elStr + " ] 的元素。");
}
else if(type.equals("name"))
{
we = driver.findElement(MobileBy.name(elStr));
System.out.println("成功找到 [ name = " + elStr + " ] 的元素。");
}
else if(type.equals("classname"))
{
we = driver.findElement(MobileBy.className(elStr));
System.out.println("成功找到 [ classname = " + elStr + " ] 的元素。");
}
else
{
System.out.println("type类型不存在");
}
} catch (NoSuchElementException e) {
e.printStackTrace();//在命令行打印异常信息及在程序中出错的位置及原因
}
return we;
}
//非系统键盘输入
public WebElement getElentBaiduKeyUnm(String e){
WebElement gk = null;
try {
if (e.equals("0")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAImage[1]/UIAKey[15]");
}
else if (e.equals("1")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAImage[1]/UIAKey[1]");
}
else if (e.equals("2")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAImage[1]/UIAKey[2]");
}
else if (e.equals("3")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAImage[1]/UIAKey[3]");
}
else if (e.equals("4")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAImage[1]/UIAKey[5]");
}
else if (e.equals("5")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAImage[1]/UIAKey[6]");
}
else if (e.equals("6")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAImage[1]/UIAKey[7]");
}
else if (e.equals("7")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAImage[1]/UIAKey[9]");
}
else if (e.equals("8")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAImage[1]/UIAKey[10]");
}
else if (e.equals("9")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAImage[1]/UIAKey[11]");
}
} catch (NoSuchElementException e2) {
// TODO: handle exception
e2.printStackTrace();//在命令行打印异常信息及在程序中出错的位置及原因
}
return gk;
}
//系统自带输入
public WebElement getElentSysKeyUnm(String e){
WebElement gk = null;
try {
if (e.equals("0")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAKeyboard[1]/UIAKey[10]");
}
else if (e.equals("1")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAKeyboard[1]/UIAKey[1]");
}
else if (e.equals("2")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAKeyboard[1]/UIAKey[2]");
}
else if (e.equals("3")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAKeyboard[1]/UIAKey[3]");
}
else if (e.equals("4")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAKeyboard[1]/UIAKey[4]");
}
else if (e.equals("5")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAKeyboard[1]/UIAKey[5]");
}
else if (e.equals("6")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAKeyboard[1]/UIAKey[6]");
}
else if (e.equals("7")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAKeyboard[1]/UIAKey[7]");
}
else if (e.equals("8")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAKeyboard[1]/UIAKey[8]");
}
else if (e.equals("9")) {
gk = driver.findElementByXPath("//UIAApplication[1]/UIAWindow[2]/UIAKeyboard[1]/UIAKey[9]");
}
} catch (NoSuchElementException e2) {
// TODO: handle exception
e2.printStackTrace();//在命令行打印异常信息及在程序中出错的位置及原因
}
return gk;
}
}
稍完整的script
最新推荐文章于 2024-10-07 19:01:55 发布