环境准备
- 安装 Appium Desktop
- 启动 Appium Desktop Server
- yarn add webdriverio
- 使用chrome开发者工具获取元素选择器
- 编写测试代码
示例
- 自动打开chrome
- 打开指定网页
- 执行指定动作
const webdriverio = require('webdriverio')
let options = {
port: 4723,
desiredCapabilities: {
'platformName': 'Android',
'deviceName': 'Redmi',
'browserName': 'Chrome',
'fullReset': false,
'noReset': true
}
}
const isPc = false
if (isPc) {
options = {
desiredCapabilities: {
'browserName': 'Chrome',
'fullReset': false,
'noReset': true,
chromeOptions: {
mobileEmulation: { 'deviceName': 'iPhone 6/7/8' },
args: [],
},
}
}
}
const driver = webdriverio.remote(options)
process.on('exit', () => {
console.log(`程序已退出`)
})
process.on('SIGINT', () => {
driver.end()
})
async function main () {
try {
await driver.init()
.url('https://www.baidu.com')
.setValue('#index-kw', '测试')
.click('#index-bn')
.saveScreenshot('./snapshot.png')
.pause(4000)
} catch (e) {
console.error(e)
} finally {
driver.end()
}
}
main().then(() => {
})