您的位置:首页 > 移动开发

appium-mac-idea-ios 安装和初级使用记录-直至跑通!

2017-01-23 10:58 871 查看


一.用命令行做了一部分准备工作

1. 爬墙 

因为后续安装过程中可能会碰到墙的问题,所以首先得解决爬墙的问题。

我的方便,公司提供代理。

2. Java环境 

java -version

Java version "1.8.0_05"

Java(TM) SE Runtime Environment (build 1.8.0_05-b13)

Java HotSpot(TM) 64-Bit Server VM (build 25.5-b02, mixed mode)

3. Git 自行搜索安装 

git --version

git version 1.8.5.2 (Apple Git-48)

4. ruby 

ruby -v

ruby 2.0.0p451 (2014-02-24 revision 45167) [universal.x86_64-darwin13]

5. brew 

brew -v

Homebrew 0.9.5

说下brew的安装,brew是Mac OS不可或缺的套件管理器

执行下面命令
ruby -e"$(curl -fsSLhttps://raw.githubusercontent.com/Homebrew/install/master/install)"

6. node 

有了brew安装node就方便了

brew install node

7.后续需要用到pom,maven环境需要配好


二.环境准备好后,直接下载的app dmg

1.首先去Appium官网上下载appium.dmg。官网地址:http://appium.io/downloads.html

2.安装完成后运行监测 





3.发现android home 没配 强迫症要把环境配好 

4.下载android studio 它自己就把安卓sdk下载好了

5.找到sdk位置 在/etc/profile 中配置android home

export ANDROID_HOME=/Users/gloria/Library/Android/sdk

export M2_HOME=/Users/gloria/jobs/apache-maven-3.3.9

export PATH=$PATH:$M2_HOME/bin:$ANDROID_HOME/tools:$ANDROID_HOME/platform-tools

6.后再运行检测 

提示xcode已经安装 但是自己没找到 又下了一个xcode 4g...( 注意 8x以上的xcode, appium1.5.3不支持 下7x,踩了个坑)

再运行检测看起来没什么问题了。


三.下载官方demo 编译

下载appium的测试例子:

git clone https://github.com/appium/sample-code
使用terminal命令行形式运行例子

a. 启动appium;

b. 输入下面的命令编译生成示例程序:

cd sample-code/sample-code/apps/TestApp

xcodebuild -sdk iphonesimulator

c.目录下通过如下命令编译生成示例程序:xcodebuild -sdk iphonesimulator 当看到BUILD SUCCEEDED则说明编译成功。这行命令在TestApp项目底下创建了一个build/Release-iphonesimulator目录,并且声称一个可以透过Appium服务器交流的.app封装包,该包为appium gui中App Path的路径。

d.直接执行启动

运行以上命令,出现下图说明正常。




然后启动 发现报错 不支持xcode8x 

此处发现报错 不支持xcode8x 再次下载7x 后启动成功



四.设置配置

配置如下





注意地址 型号和版本 错了会起不来报错

设置里修改部分配置





*成功 *

出图





上面按钮可定位 recode可录制 (但是录制的脚本基本跑不成 问题还挺多)

-------------此处appium的安装启动就完毕了--------------

--------下面是工程时间----------
1.新建自己的测试工程

1.步骤: New Project -- Maven -- 输入Project name -- Project location -- Next -- Next

2.加入pom依赖
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>LATEST</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>1.0.2</version>
</dependency>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>commons-lang</groupId>
<artifactId>commons-lang</artifactId>
<version>2.6</version>
<scope>test</scope>
</dependency>
<!-- Includes the Sauce JUnit helper libraries -->
<dependency>
<groupId>com.saucelabs</groupId>
<artifactId>sauce_junit</artifactId>
<version>1.0.18</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.2.4</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>1.5</source>
<target>1.5</target>
</configuration>
</plugin>
</plugins>
</build>

<repositories>
<repository>
<id>saucelabs-repository</id>
<url>https://repository-saucelabs.forge.cloudbees.com/release</url>
<releases>
<enabled>true</enabled>
</releases>
<snapshots>
<enabled>true</enabled>
</snapshots>
</repository>
</repositories>

(注意 pom有两个配置需要修改,先这么配)

配置好 ,建立新的文件 贴进代码
/**
* Created by gloria on 17/1/22.
*/

import io.appium.java_client.AppiumDriver;

import io.appium.java_client.MobileElement;
import org.openqa.selenium.By;
import org.openqa.selenium.DeviceRotation;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

public class apptest1 {
public static void main(String[] args) throws MalformedURLException {
WebDriver wd;
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("appium-version", "1.0");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "9.3");
capabilities.setCapability("deviceName", "iPhone 6s Plus");
capabilities.setCapability("app", "/Users/gloria/jobs/appiumtest/sample-code/sample-code/apps/TestApp/build/release-iphonesimulator/TestApp.app");
wd = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities) {
public void rotate(DeviceRotation deviceRotation) {

}

public DeviceRotation rotation() {
return null;
}

public MobileElement scrollTo(String s) {
return null;
}

public MobileElement scrollToExact(String s) {
return null;
}
};
wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
wd.findElement(By.name("TextField1")).sendKeys("4");
wd.findElement(By.name("(null)")).sendKeys("5");
wd.switchTo().alert().accept();
wd.findElement(By.name("show alert")).click();
wd.findElement(By.name("(null)")).click();
wd.findElement(By.name("ComputeSumButton")).click();
wd.findElement(By.name("locationStatus")).click();
// wd.shake();
//            wd.findElement(By.name("DisabledButton")).click();
//            (JavascriptExecutor)wd.executeScript("mobile: swipe", new HashMap<String, Double>() {{
//                put("touchCount", (double) 1); put("startX", (double) 125);
//                put("startY", (double) 389); put("endX", (double) 283);
//                put("endY", 390); put("duration", 1.3539453125); }});
wd.findElement(By.name("show alert")).click();
wd.findElement(By.name("contact alert")).click();
wd.close();
}

}

发现报错 1 版本不一致 修改pom
<dependency>
<groupId>org.seleniumhq.selenium</groupId>
<artifactId>selenium-java</artifactId>
<version>2.42.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>2.2.0</version>
</dependency>


能正常启动模拟器了 但是启动后 还是没有正常跑testcase
2.再报错






搜搜搜 更新appium-client 3.2.0解决
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>3.2.0</version>
</dependency>

3.然后遇到第三个错误。。。。。。 by.name报错了(此处表示无语)






搜了半天 发现是Appium1.5.0的问题,于是,到github去搜了一下,果然,已经有人提交了issue

npm install appium on windows not support driver.findElement(By.name("Add Contact")) 

by.name的方法在1.5.0中被移除了。。。移除了。。。移除。。了。。除。。了
4.然后改用xpath的定位方式 启动报了第四个错(。。。)






搜搜搜 搜搜搜 增加两行 貌似解决了

capabilities.setCapability("appPackage", "com.company.AppName");

capabilities.setCapability("appActivity", "com.company.AppName.AppMainActivity");
5.然后再跑 果然又有新的错(啊啊啊啊.........)






此问题报错位置在wd.close();代码释放的最后一行(至少到了最后一行)

搜搜搜 搜搜搜 搜不着。。。- -!

再搜搜搜 搜webdriver api,发现

quit()详细信息: “Quits this driver, closing every associated window”,执行这个方法后,driver会关闭所有关联窗口

close()详细信息:Close the current window, quitting the browser if it's the last window currently open ,此方法是关闭当前窗口,或最后打开的窗口

1. webDriver.Close() - Close the browser window that the driver has focus of //关闭当前焦点所在的窗口

2. webDriver.Quit() - Calls dispose //调用dispose方法

3. webDriver.Dispose() Closes all browser windows and safely ends the session 关闭所有窗口,并且安全关闭session

猜想因为我代码里弹出了个弹窗,close可能关闭不了弹窗才报这个错

遂把close 改成quit 问题解决

至此 终于一个完整的小case可以跑过了。

最终的代码被注释掉大部分。没有打开 但是至少。。。跑通了

代码如下:
/**
* Created by gloria on 17/1/22.
*/

import io.appium.java_client.AppiumDriver;

import io.appium.java_client.MobileElement;
import org.openqa.selenium.By;
import org.openqa.selenium.DeviceRotation;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import java.util.concurrent.TimeUnit;

public class apptest1 {
public static void main(String[] args) throws MalformedURLException {
WebDriver wd;
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("appium-version", "1.0");
capabilities.setCapability("platformName", "iOS");
capabilities.setCapability("platformVersion", "9.3");
capabilities.setCapability("deviceName", "iPhone 6s Plus");
capabilities.setCapability("appPackage", "com.company.AppName");
capabilities.setCapability("appActivity", "com.company.AppName.AppMainActivity");
//            capabilities.setCapability("locationServicesAuthorized", true);
//            capabilities.setCapability("waitForAppScript", "$.delay(5000); $.acceptAlert(); true;");
capabilities.setCapability("app", "/Users/gloria/jobs/appiumtest/sample-code/sample-code/apps/TestApp/build/release-iphonesimulator/TestApp.app");
wd = new AppiumDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities) {
public void rotate(DeviceRotation deviceRotation) {

}

public DeviceRotation rotation() {
return null;
}

public MobileElement scrollTo(String s) {
return null;
}

public MobileElement scrollToExact(String s) {
return null;
}
};
wd.findElement(By.xpath("//UIAApplication[1]/UIAWindow[2]/UIAButton[2]\n")).click();

wd.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);

wd.quit();
//wd.findElement(By.xpath("//UIAApplication[1]/UIAWindow[2]/UIAButton[3]\n")).click();

//            wd.findElement(By.name("TextField1")).sendKeys("4");
//            wd.findElement(By.name("(null)")).sendKeys("5");
//            wd.switchTo().alert().accept();
//            wd.findElement(By.name("show alert")).click();
//            wd.findElement(By.name("(null)")).click();
//            wd.findElement(By.name("ComputeSumButton")).click();
//            wd.findElement(By.name("locationStatus")).click();
//wd.shake();
//            wd.findElement(By.name("DisabledButton")).click();
//            (JavascriptExecutor)wd.executeScript("mobile: swipe", new HashMap<String, Double>() {{
//                put("touchCount", (double) 1); put("startX", (double) 125);
//                put("startY", (double) 389); put("endX", (double) 283);
//                put("endY", 390); put("duration", 1.3539453125); }});
//            wd.findElement(By.name("show alert")).click();
//            wd.findElement(By.name("contact alert")).click();

}

}


班门弄斧了 写一个mac+java+idea+iOS的 能跑通的实践记录。。。。。

后面还请大家多多指导:)

----------------------------------------------实践补充-------------------------------------------------------
设置自动关闭alerts的配置如下:
capabilities.setCapability("autoAcceptAlerts",true);
apabilities.setCapability("waitForAppScript", "$.delay(6000); $.acceptAlert(); true;");

遍历整页的元素写法,找不到元素时可用,一点点查
System.out.print(wd.getPageSource());打印全部页面元素

swipe 的方式

使用1.5版本相比1.4 swipe的方式从‘xy起始坐标’变成了‘xy起坐标,xy偏移量’。。。。。(又踩到了坑)

所以滑动的方法需要修改:

传入driver,during是滑动时间,num是次数

左滑代码:(由于是左滑,偏移量为负)
public static void swipeToLeft(AppiumDriver<WebElement> driver, int during, int num) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
System.out.println(width);
System.out.println(height);
for (int i = 0; i < num; i++) {
driver.swipe(width*6/7, height / 2, -(width*4/7), 0, during);
}

}


滑动代码亲测可用(适用版本1.5.3)
import io.appium.java_client.AppiumDriver;
import org.openqa.selenium.WebElement;

/**
* Created by gloria on 17/2/7.
*/
public class SwipUtils {

/**
* 向左滑动
*
* @param driver
* @param during
* @param num
*/public static void swipeToLeft(AppiumDriver<WebElement> driver, int during, int num) { int width = driver.manage().window().getSize().width; int height = driver.manage().window().getSize().height; System.out.println(width); System.out.println(height); for (int i = 0; i < num; i++) { driver.swipe(width*6/7, height / 2, -(width*4/7), 0, during); } }/**
* 上滑
*
* @param driver
* @param during
* @param num
*/
public static void swipeToUp(AppiumDriver<WebElement> driver,int during, int num) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
for (int i = 0; i < num; i++) {
driver.swipe(width / 2, height * 3 / 4, 0, -(height*2/ 4), during);

}
}

/**
* 下拉
*
* @param driver
* @param during
* @param num
*/
public static void swipeToDown(AppiumDriver<WebElement> driver,int during, int num) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
System.out.println(width);
System.out.println(height);
for (int i = 0; i < num; i++) {
driver.swipe(width / 2, height / 4, 0, height * 3 / 4, during);

}
}

/**
* 向右滑动
*
* @param driver
* @param during
* @param num
*/
public static void swipeToRight(AppiumDriver<WebElement> driver,int during, int num) {
int width = driver.manage().window().getSize().width;
int height = driver.manage().window().getSize().height;
for (int i = 0; i < num; i++) {
driver.swipe(width / 4, height / 2, width * 3 / 4, 0 , during);

}
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: