安装流程
在安装之前需要先安装好Xcode和Xcode的conmmand line tools。参照之前通过node官网下载的node.js环境,在通过npm或者cnpm执行install命令的时候会出现权限的问题,就算是添加sudo也不能解决,所以经过尝试用了nvm的安装方法,具体命令如下:
#下载nvm
curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.33.5/install.sh | bash
source ~/.zshrc
#安装node.js
nvm install --lts # nvm会自动选择lts版本作为默认版本
node --version
#下载cnpm方便安装
npm install -g cnpm --registry=https://registry.npm.taobao.org
#下载appium-doctor来查看环境部署是否成功
cnpm install -g appium-doctor
#下载appium
cnpm install -g appium
#后台启动appium
appium &
注意,此处还是会遇到npm 执行的问题,同样不能解决,找了很久也找不到原因,可是用cnpm就可以完美解决了
js的解析
前期配置文件初始化阶段
//在Login.test.js文件中
import wd from 'wd';
import * as fs from 'fs';
import path from 'path';
import { iosCaps, serverConfig } from '../helps/caps';
import { weekUserName,weekPassword } from '../helps/weekMessage';
const {assert} = chai;
describe('Basic IOS interactions', function () {
let driver;
before(async function () {
// Connect to Appium server
driver = await wd.promiseChainRemote(serverConfig);
// Start the session
await driver.init({
...iosCaps,
app: path.resolve(__dirname, "..", "apps", "LoginTest.zip")
});
});
//其中iosCaps是相应的配置信息。app是测试的软件的ipa文件的打包文件
iosCaps的信息如下:
const iosCaps = {
platformName: 'iOS',
automationName: 'XCUITest',
deviceName: 'new_iPhone',
platformVersion: '11.4',
};
其中platformName 为测试平台的系统 iOS,automationName自动化测试测的驱动名字,这里使用XCUITest,deviceName为使用的设备名,用来对应相应的虚拟器驱动
执行测试任务
由于iOS与Andriod 的界面格式差别,iOS的获取当前界面需要自己创建标识来做区分,还有相应的element获取需要使用各种方法结合,不仅仅只是根据id来查找。下面会有详细的例子
it('Simulated login user', async function () {
//截屏并转为base64编码,文件写入
let obj=await driver.takeScreenshot();
let dataBuffer = new Buffer(obj, 'base64');
console.log(dataBuffer)
fs.writeFile('./1.png',dataBuffer,function(err){
if(err) console.log('写文件操作失败');
else console.log('写文件操作成功');
});
//根据编辑框的id得到用户名那个编辑框的句柄
const userNameElement = await driver.elementById('userName');
//根据编辑框的id得到输入密码那个编辑框的句柄
const passWordElement = await driver.elementById('password');
for( let i=0;i<weekUserName.length;i++ ){
let result ='';
for(let j=0;j<weekPassword.length;j++){
//输入用户名密码
//根据句柄来给编辑框复赋值
await userNameElement.sendKeys(weekUserName[I]);
await passWordElement.sendKeys(weekPassword[j]);
//点击登陆按钮
//根据登陆按钮的Xpath路径得到按钮的实例数组
const onLogInButton = await driver.elementsByXPath("//XCUIElementTypeButton");
await onLogInButton[0].click();
//根据className来拿到XCUIElementTypeNavigationBar,注意此处XCUIElementTypeNavigationBar只能有一个
const currentPage = await driver.elementByClassName('XCUIElementTypeNavigationBar');
//得到对应的属性的值
const current = await currentPage.getAttribute('name')
//根据页面名来判断是否跳转
if(current == 'Login'){
//不跳转,弹出提示框,根据xpath得到提示框的句柄,此处的下下标是在界面中第几个XCUIElementTypeStaticText,需要打开appium的inspector来判断
const alertElement = await driver.elementsByXPath("//XCUIElementTypeStaticText");
//得到提示框提示的内容
const alertText = await alertElement[2].getAttribute('name');
//根据id得到点击按钮的句柄,点击提示框的确定按钮
const closeDialogButton = await driver.waitForElementByAccessibilityId(`OK`);
await closeDialogButton.click();
let flag = 0; // 当用户名不存在时的时候就不再验证用户名及密码
//判断提示的内容并做相应的处理
switch(alertText){
case '密码不能为空':result = '密码不为空';break;
case '用户名不存在':result = '用户名不存在';flag = 1;break;
case '用户名不能为空':result = '用户名不能为空';flag = 1;break;
case '密码错误':result = '密码错误';break;
}
if(flag == 1)
break;
}else if (current == 'secess'){
//页面跳转,得到跳转后的句柄
const secessElement = await driver.elementById('登陆成功');
const secessText = await secessElement.getAttribute('name');
//验证得到的信息是否正确
assert.equal(secessText, '登陆成功');
result = '登陆成功,发现存在弱口令匹配用户名和密码';
//重新返回页面方便进一步的测试
const backButton = await driver.elementById('Back');
await backButton.click();
break;
}
}
}
});
结束操作
after(async function () {
await driver.quit();
});
模拟器的创建和开启
在实际的生产环境中需要通过程序来启动模拟器,则需要一些获取虚拟器的信息还有创建开启的模拟器
获取设备列表
xcrun simctl list
获取到的信息如下:
其中包含三部分
##设备相应的类型
== Device Types ==
iPhone 4s (com.apple.CoreSimulator.SimDeviceType.iPhone-4s)
iPhone 5 (com.apple.CoreSimulator.SimDeviceType.iPhone-5)
iPhone 5s (com.apple.CoreSimulator.SimDeviceType.iPhone-5s)
iPhone 6 (com.apple.CoreSimulator.SimDeviceType.iPhone-6)
iPhone 6 Plus (com.apple.CoreSimulator.SimDeviceType.iPhone-6-Plus)
iPhone 6s (com.apple.CoreSimulator.SimDeviceType.iPhone-6s)
iPhone 6s Plus (com.apple.CoreSimulator.SimDeviceType.iPhone-6s-Plus)
iPhone 7 (com.apple.CoreSimulator.SimDeviceType.iPhone-7)
iPhone 7 Plus (com.apple.CoreSimulator.SimDeviceType.iPhone-7-Plus)
iPhone 8 (com.apple.CoreSimulator.SimDeviceType.iPhone-8)
iPhone 8 Plus (com.apple.CoreSimulator.SimDeviceType.iPhone-8-Plus)
iPhone SE (com.apple.CoreSimulator.SimDeviceType.iPhone-SE)
iPhone X (com.apple.CoreSimulator.SimDeviceType.iPhone-X)
iPad 2 (com.apple.CoreSimulator.SimDeviceType.iPad-2)
iPad Retina (com.apple.CoreSimulator.SimDeviceType.iPad-Retina)
iPad Air (com.apple.CoreSimulator.SimDeviceType.iPad-Air)
iPad Air 2 (com.apple.CoreSimulator.SimDeviceType.iPad-Air-2)
iPad (5th generation) (com.apple.CoreSimulator.SimDeviceType.iPad--5th-generation-)
iPad Pro (9.7-inch) (com.apple.CoreSimulator.SimDeviceType.iPad-Pro--9-7-inch-)
iPad Pro (12.9-inch) (com.apple.CoreSimulator.SimDeviceType.iPad-Pro)
iPad Pro (12.9-inch) (2nd generation) (com.apple.CoreSimulator.SimDeviceType.iPad-Pro--12-9-inch---2nd-generation-)
iPad Pro (10.5-inch) (com.apple.CoreSimulator.SimDeviceType.iPad-Pro--10-5-inch-)
iPad (6th generation) (com.apple.CoreSimulator.SimDeviceType.iPad--6th-generation-)
Apple TV (com.apple.CoreSimulator.SimDeviceType.Apple-TV-1080p)
Apple TV 4K (com.apple.CoreSimulator.SimDeviceType.Apple-TV-4K-4K)
Apple TV 4K (at 1080p) (com.apple.CoreSimulator.SimDeviceType.Apple-TV-4K-1080p)
Apple Watch - 38mm (com.apple.CoreSimulator.SimDeviceType.Apple-Watch-38mm)
Apple Watch - 42mm (com.apple.CoreSimulator.SimDeviceType.Apple-Watch-42mm)
Apple Watch Series 2 - 38mm (com.apple.CoreSimulator.SimDeviceType.Apple-Watch-Series-2-38mm)
Apple Watch Series 2 - 42mm (com.apple.CoreSimulator.SimDeviceType.Apple-Watch-Series-2-42mm)
Apple Watch Series 3 - 38mm (com.apple.CoreSimulator.SimDeviceType.Apple-Watch-Series-3-38mm)
Apple Watch Series 3 - 42mm (com.apple.CoreSimulator.SimDeviceType.Apple-Watch-Series-3-42mm)
设备的系统版本
== Runtimes ==
iOS 11.4 (11.4 - 15F79) - com.apple.CoreSimulator.SimRuntime.iOS-11-4
tvOS 11.4 (11.4 - 15L576) - com.apple.CoreSimulator.SimRuntime.tvOS-11-4
watchOS 4.3 (4.3 - 15T212) - com.apple.CoreSimulator.SimRuntime.watchOS-4-3
已有设备(包含设备名,uuid,和当前状态Shutdown为停机状态,Booted为启动中)
== Devices ==
-- iOS 11.4 --
iPhone 5s (94F02B76-3A97-46AC-8A42-C591874FFC97) (Shutdown)
iPhone 6 (DBDE455E-8E6F-427F-B694-B6CE6775336F) (Shutdown)
iPhone 6 Plus (93B103F0-0B32-4EB8-AECB-8F582FC42A9D) (Shutdown)
iPhone 6s (6BA22CD6-C101-439C-BA14-CCED0174C349) (Shutdown)
iPhone 6s Plus (88550DD7-1546-43FC-B198-41BE190D78A2) (Shutdown)
iPhone 7 (40E663E2-A37C-4F07-B54F-FB65B09087AA) (Shutdown)
iPhone 7 Plus (A3AEA9AC-F6A9-41CD-8098-B8F5E1383FBF) (Shutdown)
iPhone 8 (C3F5BE6E-FDC6-4EF9-B7C6-5E87ABE098CC) (Shutdown)
iPhone 8 Plus (772FEA3C-8197-498B-9F4A-9A68AB679B5E) (Shutdown)
iPhone SE (4A257C21-7D3F-4600-BE0D-FA03FB346C87) (Shutdown)
iPhone X (1940A427-BBA3-49C5-B004-28B51D049C32) (Shutdown)
new_iPhone (FCFC0238-D4E2-4073-9189-8A4C143220C3) (Shutdown)
iPad Air (AD50B8F7-50C3-4FBC-9DC7-0734FB58A595) (Shutdown)
iPad Air 2 (FFD0189D-0A09-46F5-88A3-2B9EB11252F4) (Shutdown)
iPad (5th generation) (76118A95-6557-499A-91D4-34D3B453318F) (Shutdown)
iPad Pro (9.7-inch) (DD67073B-0F56-43D4-B64A-C4CB3A8B6C3D) (Shutdown)
iPad Pro (12.9-inch) (37B1B39E-2240-4D1A-B3AD-0DADE0F23E59) (Shutdown)
iPad Pro (12.9-inch) (2nd generation) (43D13B49-D1D6-4E10-9CD2-8B891FA0C477) (Shutdown)
iPad Pro (10.5-inch) (0D54B97A-88C5-41DF-B7FB-436F9638479B) (Shutdown)
-- tvOS 11.4 --
Apple TV (12F32E6B-E142-4E52-8A8D-C1AD1843924E) (Shutdown)
Apple TV 4K (9B6C900A-739D-44D0-91CC-BF5D3AD8E225) (Shutdown)
Apple TV 4K (at 1080p) (23AB194E-70D3-4D60-BFAE-830F70BCC4D9) (Shutdown)
-- watchOS 4.3 --
Apple Watch - 38mm (B9B7EECA-A19B-4726-9AFA-CDD835F860B5) (Shutdown)
Apple Watch - 42mm (0BB2ED34-67BF-42DB-AE57-D6284DB35A9F) (Shutdown)
Apple Watch Series 2 - 38mm (E189E183-9800-4A1C-8D89-DB653E3D968B) (Shutdown)
Apple Watch Series 2 - 42mm (B3B45670-F683-4CAD-8E24-4DE3F0529FF0) (Shutdown)
Apple Watch Series 3 - 38mm (07BA27F7-1478-4BE8-9F04-6662C15833AA) (Shutdown)
Apple Watch Series 3 - 42mm (7945674E-97C1-42A7-8CC2-9677A1408BEC) (Shutdown)
== Device Pairs ==
E014BE3D-4852-4EEB-8723-5E06B33B9DAF (active, disconnected)
Watch: Apple Watch Series 2 - 38mm (E189E183-9800-4A1C-8D89-DB653E3D968B) (Shutdown)
Phone: iPhone 7 (40E663E2-A37C-4F07-B54F-FB65B09087AA) (Shutdown)
09144A5B-3D70-4726-9D96-9AC8BD282348 (active, disconnected)
Watch: Apple Watch Series 2 - 42mm (B3B45670-F683-4CAD-8E24-4DE3F0529FF0) (Shutdown)
Phone: iPhone 7 Plus (A3AEA9AC-F6A9-41CD-8098-B8F5E1383FBF) (Shutdown)
64389547-414F-4D57-A82A-A133B6DDB667 (active, disconnected)
Watch: Apple Watch Series 3 - 38mm (07BA27F7-1478-4BE8-9F04-6662C15833AA) (Shutdown)
Phone: iPhone 8 (C3F5BE6E-FDC6-4EF9-B7C6-5E87ABE098CC) (Shutdown)
BDE87505-570B-48FB-9A4E-7074A9F990E7 (active, disconnected)
Watch: Apple Watch Series 3 - 42mm (7945674E-97C1-42A7-8CC2-9677A1408BEC) (Shutdown)
Phone: iPhone 8 Plus (772FEA3C-8197-498B-9F4A-9A68AB679B5E) (Shutdown)
####新建模拟器
xcrun simctl create "名称" "设备类型" "系统版本"
可以同时启动多台相同主机,只要设备名称不同就可以创建。通过在
例:
xcrun simctl create "my_iPhone_X" "com.apple.CoreSimulator.SimDeviceType.iPhone-X" "com.apple.CoreSimulator.SimRuntime.iOS-11-4"
通过appium指定相应的deviceName名程,就可以在调试的同时启动相应的模拟器
就算是没有启动状态,通过npm test 执行的时候appium会自动启动相应名称的设备
安装其他的系统版本
从Xcode菜单栏里打开Xcode -> Preferences -> Components -> Simulators,下载对应版本的模拟器。
参考
https://blog.csdn.net/TuGeLe/article/details/80848300
https://www.jianshu.com/p/ae8846736dba
http://appium.io
https://www.jianshu.com/p/bb1ba50700bc