好了,上一篇主要讲的比较简单,运用了系统自身带有的插件,但是局限性比较大。
第一,只能支持ios,第二,必须使用JS脚本。
实际中,我们运用更加强大的,也是现在炙手可热的Appium,首先这个工具,支持了ios,andriod,以及web,而且选择的语言也是众多,ruby,python,JAVA等等,可以去它的官网获取具体的说明,网址:appium.io
好啦,让我们赶紧下载吧,由于我们不是专业测试的啊,所以,我选择了更为简单的图形化界面,大概安装万就是这个样子
看上去比较简单吧,功能比较强大,我们其实,它运用的json write 协议,进行和里设备的socket通信,而主要的封装API,我们需要去下载另外一个,他们封装的一个api,叫做selenium,这个有各种语言的封装的库,所以在这里我们就下载了JAVA的selenium库,来吧,让我们看一下JAVA脚本吧
package test.java.com.saucelabs.appium;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.MobileBy;
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.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.openqa.selenium.*;
import org.openqa.selenium.remote.CapabilityType;
import org.openqa.selenium.remote.DesiredCapabilities;
import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class aaa {
/**
* 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
*/
private AppiumDriver driver;
private List<Integer> values;
private static final int MINIMUM = 0;
private static final int MAXIMUM = 100;
@Before
public void setUp() throws Exception {
// set up appium
File appDir = new File("/Users/dh/appium/sample-code/apps/TestApp/build/Release-iphoneos");
File app = new File(appDir, "TestApp.app");
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability(CapabilityType.BROWSER_NAME, "");
capabilities.setCapability("platformVersion", "7.0");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("deviceName", "iPhone");
capabilities.setCapability("app", app.getAbsolutePath());
driver = new AppiumDriver(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) {
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 testSlierMore() throws Exception {
WebElement slider = driver.findElement(By.xpath("//UIASlider[1]"));
assertEquals("50%", slider.getAttribute("value"));
Point sliderLocation = getCenter(slider);
driver.swipe(sliderLocation.getX(), sliderLocation.getY(), sliderLocation.getX()+100, sliderLocation.getY(), 1000);
assertEquals("100%", slider.getAttribute("value"));
}
@Test
public void testBasicAlert() throws Exception {
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 {
WebElement button = driver.findElement(By.xpath("//UIAButton[1]"));
assertEquals("ComputeSumButton", 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();
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();
WebElement text = 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 = 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));
assertEquals("TextField1", 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]"));
assertEquals("50%", slider.getAttribute("value"));
Point sliderLocation = getCenter(slider);
driver.swipe(sliderLocation.getX(), sliderLocation.getY(), sliderLocation.getX()-100, sliderLocation.getY(), 1000);
assertEquals("0%", slider.getAttribute("value"));
}
@Test
public void testLocation() throws Exception {
WebElement button = driver.findElement(By.xpath("//UIAButton[1]"));
Point location = button.getLocation();
assertEquals(94, location.getX());
assertEquals(122, location.getY());
}
@Test
public void testSessions() throws Exception {
// HttpGet request = new HttpGet("http://localhost:4723/wd/hub/sessions");
// 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());
}
}
可能这里案例有些多了,有几个比较重要,就是before里面的内容,这里设置了连接的socket的端口,和IP,所以我们要在设置server的是否要保持一直,还有一个APP路径,这个很关键,设置错了可能找到app就无法进行安装了,其他的就是配置一些平台啊,系统号啊,但是要和图形化界面一致,不然对不上就无法连接了。看看图形化界面里面的配置吧。
DeviceId就是里设备的UDID,首先可以点击appium工具右上角lanuch,这时可以看下appium是否正常,如果正常,他会返回0。
注意java脚本,我们需要引入3个库:java-client,seleium.以及协议gjson,当然如果你编译代码的时候,如果没有就会出错,记住,每次运行脚本的是否,必须吧appium启动起来,然后,就坐等一个个case运行就可以了,让我们梳理一下流程吧,
你的java脚本,调用java-client和平台建立连接,采用seleium封装的api获取界面上的各种元素,然后采用json write协议进行传输,当与appium建立连接后,元素操作转化为xcode自带的automation操作,运行真机或者模拟器,其实本质上来说还是automation,但是中间介入了中转平台,就是appium,对于语言和系统做出了兼容而已,不过对于实际的话,对于公司还是效果明显。