稍完整的script

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;
    } 
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值