Appium,IOS 模拟器,Java工程搭建

首先进入sample code Test App 有TestApp.xcodeproj文件的工程目录下 下编译出TestApp.app文件

1.新建 java 工程

2.import需要的包

新建class

package com.saucelabs.appium;


import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileBy;
import io.appium.java_client.ios.IOSDriver;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;

/**
 * Simple <a href="https://github.com/appium/appium">Appium</a> test which runs against a local Appium instance deployed
 * with the 'TestApp' iPhone project which is included in the Appium source distribution.
 *
 * @author Ross Rowe
 */
@SuppressWarnings("deprecation")
public class SimpleTest {

    private AppiumDriver driver;

    private List<Integer> values;

    private static final int MINIMUM = 0;
    private static final int MAXIMUM = 10;

    @Before
    public void setUp() throws Exception {
        // set up appium
        File appDir = new File("/Users/huangxiaoshi/Downloads/sample-code-master/sample-code/apps/TestApp/build/release-iphonesimulator");
        File app = new File(appDir, "TestApp.app");
        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability("platformVersion", "6.1");
        capabilities.setCapability("deviceName", "iPhone Simulator");
        capabilities.setCapability("app", app.getAbsolutePath());
        driver = new IOSDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
        values = new ArrayList<Integer>();
    }

    @After
    public void tearDown() throws Exception {
        driver.quit();
    }

    private void populate() {
        //populate text fields with two random number
        List<WebElement> elems = driver.findElements(By.className("UIATextField"));
        Random random = new Random();
        for (WebElement elem : elems) {
            //为两个UIATextField控件生成两个随机数
            int rndNum = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM;
            elem.sendKeys(String.valueOf(rndNum));
            //并记录到数组中
            values.add(rndNum);
        }
    }
//得到控件的中间位置
    private Point getCenter(WebElement element) {

      Point upperLeft = element.getLocation();
      Dimension dimensions = element.getSize();
      return new Point(upperLeft.getX() + dimensions.getWidth()/2, upperLeft.getY() + dimensions.getHeight()/2);
    }

    @Test
    public void testUIComputation() throws Exception {
        // populate text fields with values
        populate();
        // trigger computation by using the button
        //点击控件计算两个数的和
        WebElement button = driver.findElement(By.className("UIAButton"));
        button.click();
        // is sum equal ?
        //判断得到的数是否和正确的和相等
        WebElement texts = driver.findElement(By.className("UIAStaticText"));
        assertEquals(String.valueOf(values.get(0) + values.get(1)), texts.getText());
    }

    @Test
    public void testActive() throws Exception {
         //判断该控件是否展示
        WebElement text = driver.findElement(By.xpath("//UIATextField[1]"));
        assertTrue(text.isDisplayed());
           //判断该控件是否展示   
        WebElement button = driver.findElement(By.xpath("//UIAButton[1]"));
        assertTrue(button.isDisplayed());
    }

    @Test
    public void testBasicAlert() throws Exception {
         //点击alert控件,弹出提示框
        driver.findElement(By.xpath("//UIAButton[2]")).click();
        Alert alert = driver.switchTo().alert();
        //check if title of alert is correct
        //验证提示框文字是否正确
        assertEquals("Cool title this alert is so cool.", alert.getText());
        alert.accept();
    }

    @Test
    public void testBasicButton() throws Exception {
        // 验证求和 button文字显示是否正确
        WebElement button = driver.findElement(By.xpath("//UIAButton[1]"));
        assertEquals("Compute Sum", button.getText());
    }

    @Test
    public void testClear() throws Exception {
         //验证编辑框输入清空后编辑框内容是否为空
        WebElement text = driver.findElement(By.xpath("//UIATextField[1]"));
        text.sendKeys("12");
        text.clear();

        assertEquals("", text.getText());
    }

    @Test
    public void testHideKeyboard() throws Exception {
        //编辑框输入文字
        driver.findElement(By.xpath("//UIATextField[1]")).sendKeys("12");
         //确认键盘是否弹出
        WebElement button = driver.findElement(MobileBy.AccessibilityId("Done"));
        assertTrue(button.isDisplayed());
         //点击隐藏键盘
        button.click();
    }

    @Test
    public void testFindElementByClassName() throws Exception {
        Random random = new Random();
        //通过classname定位控件测试
        WebElement text = driver.findElementByClassName("UIATextField");
        int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM;
        text.sendKeys(String.valueOf(number));

        driver.findElementByClassName("UIAButton").click();

        // is sum equal ?
        //验证是否相等
        WebElement sumLabel = driver.findElementByClassName("UIAStaticText");
        assertEquals(String.valueOf(number), sumLabel.getText());
    }

    @Test
    public void testFindElementsByClassName() throws Exception {
      Random random = new Random();
      //通过classname定位控件测试
      WebElement text = (WebElement) driver.findElementsByClassName("UIATextField").get(1);
      int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM;
      text.sendKeys(String.valueOf(number));

      driver.findElementByClassName("UIAButton").click();

      // is sum equal ?
      WebElement sumLabel = (WebElement) driver.findElementsByClassName("UIAStaticText").get(0);
      assertEquals(String.valueOf(number), sumLabel.getText());
    }

    @Test
    public void testAttribute() throws Exception {
        Random random = new Random();

        WebElement text = driver.findElement(By.xpath("//UIATextField[1]"));
        //编辑框输入文字
        int number = random.nextInt(MAXIMUM - MINIMUM + 1) + MINIMUM;
        text.sendKeys(String.valueOf(number));
        //验证控件的name,label,value等属性是否和设定的相同,name为IntegerA
        assertEquals("IntegerA", text.getAttribute("name"));
        assertEquals("TextField1", text.getAttribute("label"));
        assertEquals(String.valueOf(number), text.getAttribute("value"));
    }

    @Test
    public void testSlider() throws Exception {
        //get the slider
        //进度条控件
        WebElement slider = driver.findElement(By.xpath("//UIASlider[1]"));
        // 查看初始数据是否为50%
        assertEquals("50%", slider.getAttribute("value"));
        Point sliderLocation = getCenter(slider);
        //拖动控件,从中心拖到初始位置
        driver.swipe(sliderLocation.getX(), sliderLocation.getY(), sliderLocation.getX()-100, sliderLocation.getY(), 1000);
         //验证控件是否归0,不归0?
        assertEquals("0%", slider.getAttribute("value"));
    }

    @Test
    public void testLocation() throws Exception {
        //验证控件的位置是否为94,122?
        WebElement button = driver.findElement(By.xpath("//UIAButton[1]"));

        Point location = button.getLocation();

        assertEquals(94, location.getX());
        assertEquals(142, location.getY());
    }

    @Test
    public void testSessions() throws Exception {
         // ?
        HttpGet request = new HttpGet("http://localhost:4723/wd/hub/sessions");
        @SuppressWarnings("resource")
        HttpClient httpClient = new DefaultHttpClient();
        HttpResponse response = httpClient.execute(request);
        HttpEntity entity = response.getEntity();
        JSONObject jsonObject = (JSONObject) new JSONParser().parse(EntityUtils.toString(entity));

        String sessionId = driver.getSessionId().toString();
        assertEquals(jsonObject.get("sessionId"), sessionId);
    }

    @Test
    public void testSize() {
        //验证两个编辑框控件大小是否一致
        Dimension text1 = driver.findElement(By.xpath("//UIATextField[1]")).getSize();
        Dimension text2 = driver.findElement(By.xpath("//UIATextField[2]")).getSize();
        assertEquals(text1.getWidth(), text2.getWidth());
        assertEquals(text1.getHeight(), text2.getHeight());
    }
}

下载并安装Appium.dmg

配置IOS部分:

配置完成后launch ,稍后启动Inspector:

显示界面如下

便可以看到UI的属性等情况

原文地址:https://www.cnblogs.com/melody-emma/p/4791709.html