[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 交互。

原文地址:https://www.cnblogs.com/feifeidxl/p/4581369.html