[Selenium] IOS 之 ios-driver
从 Selenium 的官方文档来看,推荐用户使用 ios-driver 或 appium 而不是官方发布的 iPone Driver. 他们的地址分别是:
http://ios-driver.github.io/ios-driver
http://appium.io/
ios-driver 基于2种不同的框架构建起来,一种是针对原生 app 进行构建,还一种针对 Web 的 app 或者混合式 app 进行构建。鉴于2中不同 app 的设计原理,需要满足不同的开发环境需求。
1. 原生 app
由于使用 UIAutomation 框架,所以需要确保 iOS SDK 的版本大于5.0.检查方法如下:
$ xcodebuild -showsdks
执行结果:
OS X SDKs:
OS X 10.8 -sdk macosx10.8
iOS X SDKs:
iOS X 7.0 -sdk iphoneox7.0
iOS Simulator SDKs:
Simulator -iOS7.0 -sdk iphonesimulator7.0
1.1 ios-driver 的 Web 实例
首先,进入 ios-driver 官网下载 ios-server-0.6.5-jar-with-dependencies.jar
在第一次运行 ios-driver 之前,应确保一下目录和文件的权限更新:
$ sudo chmod a +rw /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk/Applications
$ sudo chmod a +rw /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk/Applications/MobileSafari.app
然后更新 MobileSafari Info.plist 的权限以允许 ios-driver 编辑它。执行命令如下:
$ sudo chmod 666 /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneSimulator.platform/Developer/SDKs/iPhoneSimulator7.0.sdk/Applications/MobileSafari.app/Info.plist
在启动 ios-driver 之前,请确保系统上安装的 Java 版本至少为1.7.0版本:
$ java -version
java version "1.7.0_04"
接着进入刚下载的 ios-server-0.6.5-jar-with-dependencies.jar 的路径,命令如下:
$ java -jar ios-server-0.6.5-jar-with-dependencies.jar -simulators
...... test can access the server at http://0.0.0.0:5555/wd/hub/devices/all ...
默认端口号为5555,可在浏览器中访问如下地址,如果看到类似JSON 对象的信息,则说明前述操作成功:
http://localhost:5555/wd/hub/status
记下来以百度首页为例进行阐述
注意:在 Eclipse 中要添加之前下载的 ios-server-0.6.5-jar-with-dependencies.jar 文件,因为需要如下库的支持;
org.uiautomation.ios.IOSCapabilities
示例代码:
package com.learningselenium.ios;
import junit.framework.TestCase;
import java.net.URL;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.uiautomation.ios.IOSCapabilities;
public class testiOSBaidu extends TestCase{
public void testBaidu() throws Exception{
//设置DesiredCapabilities,其中IOSCapabilities设置为iphone,如果程序运行在iPad模拟器上,可设置为ipad。
//参数Safari 表示待测试程序的bundle name,因为这里是测试 Web 页面,默认是用 Safari 打开
DesiredCapabilities safari = IOSCapabilities.iphone("Safari");
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:5555/wd/hub"), safari );
driver.get("http://www.baidu.com");
driver.close();
}
}
1.2 ios-driver 的 Native app 实例
接下来以苹果官网的示例程序 InternationalMountains 为例,对原生 app 的测试过程进行讲解,示例代码下载地址:
https://developer.apple.com/legacy/library/samplecode/internationalMountains/introduction/intro.html
请确保app 程序和 UIAutomation已关联,方法如下:
在 Xcode 的菜单中选择 Product->Profile,并选择 Automation。该设定会构建 app 并启动 Instruments
将 Instruments 关闭,并在 Xcode 中选择 Window->Organizer->Projects,可看到 InternationalMountains.app 文件所在位置为:
~/Library/Developer/Xcode/DerivedData/InternationalMountainseordguimrxknwoaynobkvpirkacs/Build/Products/Debugiphonesimulator/InternationalMountains.app
接着进入刚下载的 ios-server-0.6.5-jar-with-dependencies.jar 的路径,执行如下命令:
$ java -jar ios-server-0.6.5-jar-with-dependencies.jar -aut
~/Library/Developer/Xcode/DerivedData/InternationalMountainseordguimrxknwoaynobkvpirkacs/Build/Products/Debugiphonesimulator/InternationalMountains.app -port 4444
接下来确认 ios-driver 启动成功并可访问该 app,可通过浏览器访问如下地址,如果看到类似JSON 对象的信息,说明前述操作成功:
http://localhost:4444/wd/htb/status
如果是在模拟器中运行app,则需要在启动 ios-driver 时使用 -simulators 参数,执行命令及打印信息:
$ java -jar ios-server-0.6.5-jar-with-dependencies.jar -aut
~/Library/Developer/Xcode/DerivedData/InternationalMountainseordguimrxknwoaynobkvpirkacs/Build/Products/Debugiphonesimulator/InternationalMountains.app -port 4444 -simulators
... test can access the server at http://0.0.0.0:4444/wd/hub ...
应确保已经通过了 Xcode 启动了模拟器,否则需要通过浏览器查看 ios-driver 的启动状态是否正常
针对 InternationalMountains 的测试用例代码如下:
package com.learningselenium.ios;
import java.net.URL;
import jave.util.List;
import java.io.File;
import junit.framework.TestCase;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.Augmenter;
import org.uiautomation.ios.IOSCapabilities;
public class testiOSInternationalMountains extends TestCase{
public void testInternationalMountains() throws Exception{
DesiredCapabilities nativeAppCap= IOSCapabilities.iphone("InternationalMountains", "1.1");
RemoteWebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), nativeAppCap);
List<WebElement> cells = driver.findElement(By.className("UIATableCell"));
assertEquals(9, cells.size());
WebElement first = cells.get(0); //操作列表中第一个元素
first.click();
//截屏操作和操作页面元素
TaksScreenshot screen = (TaksScreenshot ) new Augmenter().augment(driver);
File ss = new File("screenshot.png");
screen.getScreenshotAs(OutputType.FILE).renameTo(ss);
System.out.println("screenshot take:" + ss.getAbsolutePath());
By selector = By.xpath("//UIAStaticText[contains(@name, ‘climbed’)]");
WebElement text = driver.findElement(selector);
System.out.println(text.getAttribute("name"));
driver.quit();
}
}
如果需要在真实设备上运行app,则需要在启动 ios-driver 时使用 -beta 参数,执行命令:
$ java -jar ios-server-0.6.5-jar-with-dependencies.jar -beta -port 4444
1.3 ios-driver 的源码编译
前置条件为系统已安装 Git,JDK7 和 apache-maven。然后在 /etc/profile 中添加如下命令:
export JAVA_HOME = /Library/Java/JavaVirtualMachines/jdk1.7.0_xx.jdk/Contents/Home
export M2_HOME=/Users/{YourAccountName}/Desktop/apache-maven-3.1.x
export PATH=$JAVA_HOME/bin:$PATH
export M2=¥M2_HOME/bin
export PATH=$M2:$PATH
通过如下地址并使用 Git 获取源码后解包:
https://github.com/ios-driver/ios-driver
在解压后的源码根目录执行如下命令:
sudo mvn clean package
或
sudo mvn clean install
如果编译成成,会看到如下日志:
。。。
[INFO]BUILD SUCCESS
...
如果不需要运行测试用例,则加上如下参数即可
-DskipTests
如果在编译过程中,出现如下错误信息,说明系统中安装了 JDK1.6 和 JDK1.7 两个不同版本:
[warning]...Detected JDK Version:1.6...is not in the allowed range 1.7
解决方案:在用户根目录创建 .mavenrc 文件并添加如下内容:
export JAVA_HOME=/Library/Java/JavaVirtualMachines/jdk1.7.0_xx.jdk/Contents/Home
2. Web app 或混合式 app
针对这种方式的 app,需要用到远程 Webkit 的调试协议,并且 iOS 的版本要求为6+,Safari 的版本要求为6+。如果无法满足以上条件,也可继续测试原生 app,但无法在 Safari 上运行 Web 页面,也不能使用 DOM 选择器来与 UIWebviews 交互。
[Selenium] IOS 之 ios-driver的更多相关文章
- selenium webdriver的各种driver
selenium官方加上第三方宣布支持的驱动有很多种:除了PC端的浏览器之外,还支持iphone.Android的driver:大概记录一下selenium支持的各种driver的用途与说明. sel ...
- iOS:iOS开发非常全的三方库、插件等等
iOS开发非常全的三方库.插件等等 github排名:https://github.com/trending, github搜索:https://github.com/search. 此文章转自git ...
- iOS开发--iOS及Mac开源项目和学习资料
文/零距离仰望星空(简书作者)原文链接:http://www.jianshu.com/p/f6cdbc8192ba著作权归作者所有,转载请联系作者获得授权,并标注“简书作者”. 原文出处:codecl ...
- 演进之美,越来越美:三分钟看尽 iOS 1 ~ iOS 8 的进化史
演进之美,越来越美:三分钟看尽 iOS 1 ~ iOS 8 的进化史 原文出处: 少数派 9 月 18 日苹果就将推出 iOS 8 正式版了,从 2007 年发布第一代 iPhone 时搭载在 iPh ...
- iOS 6 & iOS 7 的适配笔记
iOS 6 & iOS 7 的适配 场景1: 没有NavigationController,同时根视图是UIView- (void)viewWillLayoutSubviews{ if ([[ ...
- iOS开发-iOS 10 由于权限问题导致崩溃的那些坑
iOS开发-iOS 10 由于权限问题导致崩溃的那些坑 6月份的WWDC大会结束有一段时间了,相信很多开发者也是在努力工作的闲时用着Xcode8 Beta版学习着新的特性吧. 使用Xcode8写自己 ...
- fstream之seekp/seekg/ios::ate/ios::app
在程序开发中,IO处理无处不在,经常会在代码中遇到特殊的IO处理需求 1.描述 需求:如果文件不存在则创建,存在则打开,然后先读取文件的末行,然后在文件末尾写入. 代码: #include <i ...
- 【整理】Xcode中的iOS模拟器(iOS Simulator)的介绍和使用心得
[整理]Xcode中的iOS模拟器(iOS Simulator)的介绍和使用心得 iOS模拟器简介 iOS功能简介 iOS模拟器,是在Mac下面开发程序时,开发iOS平台的程序时候,可以使用的辅助工具 ...
- [Android开发学iOS系列] iOS写UI的几种方式
[Android开发学iOS系列] iOS写UI的几种方式 作为一个现代化的平台, iOS的发展也经历了好几个时代. 本文讲讲iOS写UI的几种主要方式和各自的特点. iOS写UI的方式 在iOS中写 ...
- [ios基础]IOS应用程序的生命周期问题
—程序的生命周期 a.程序的生命周期是指应用程序启动到应用程序结束整个阶段的全过程 b.每一个IOS应用程序都包含一个UIApplication对象,IOS系统通过该U ...
随机推荐
- 安装ubuntu 12.04 后遇到的问题
我的笔记本是08年的戴尔,比较老的机子了.给本本安装ubuntu/windows8双系统后,ubuntu系统中出现了一些问题,在网上搜寻许多解决方法,管用的就分享一下,顺便做一下记录,免得下次自己又不 ...
- Oracle 12c安装报错Installation failed to access the temporary location(无法访问临时位置)
报错如图 1.先检查当前windows账户用户名是否为全英文,没有就新建一个,大多数用户败在这一步,而官方也没有解释 如何新建:开始-->控制面板-->用户账户和家庭安全-->用户账 ...
- 详解DNS,你真的懂吗?
what`s this ? 概念 域名系统(英文:DomainNameSystem,缩写:DNS)是互联网的一项服务.它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网.D ...
- Codeforces Gym 100286I iSharp 模拟
原题地址:http://codeforces.com/gym/100286/attachments/download/2013/20082009-acmicpc-northeastern-europe ...
- 7.Java web—tomcat9部署
1)安装 在此之前要安装 好jdk和jre 下载绿色版 http://tomcat.apache.org/ 解压至:D:\Program Files (x86)\tomcat9 环境变更path添加两 ...
- 【String】String.format(format,args...)的使用解析
String.format(format,args...)的使用解析 使用kotlin 中使用示例 ================================================== ...
- 【gradle】mac下 gradle默认本地仓库位置
gradle默认会把包缓存到用户目录的.gradle目录下,如果你打开.gradle\caches\modules-2\files-2.1,你会发现很多的jar包.mac上的话 ,也就是在/Users ...
- Go -- log4go日志
折腾: [已解决]go语言中实现log信息同时输出到文件和控制台(命令行) 期间,已经通过io的MultiWriter搞定了同时输出信息到文件和console,但是不支持level. 所以,再去试试这 ...
- 基于Office 365 无代码工作流分析-表单基本需求分析!
3.2表单的制作 基于下图的需求,我们须要定义例如以下的表单列表:
- HDU oj 开门人与关门人
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1234 #include<stdio.h> #include<string.h> ...