mac上使用nodejs appium控制chrome浏览器
1.先安装相关依赖
npm i appium -g
npm i appium-chromium-driver webdriverio appium-chromedriver -D
去https://googlechromelabs.github.io/chrome-for-testing/下载chromedriver
放在项目根目录,然后启动 appium
,运行以下代码即可
2.测试
const wdio = require('webdriverio');
const capabilities = {
platformName: 'mac',
browserName: 'chrome',
'appium:automationName': 'Chromium',
'appium:executable': './chromedriver',
'appium:newCommandTimeout': 0,
// 'goog:chromeOptions': {
// debuggerAddress: 'localhost:9223'
// }
};
const wdOpts = {
hostname: process.env.APPIUM_HOST || 'localhost',
port: parseInt(process.env.APPIUM_PORT, 10) || 4723,
logLevel: 'info',
capabilities
};
async function runTest() {
const driver = await wdio.remote(wdOpts);
try {
// 访问百度
await driver.navigateTo('https://www.baidu.com');
// 等待搜索框加载
const searchBox = await driver.$('#kw');
await searchBox.waitForExist({ timeout: 5000 });
// 输入搜索内容
await searchBox.setValue('Appium 自动化测试');
// 点击搜索按钮
const searchBtn = await driver.$('#su');
await searchBtn.click();
// 等待搜索结果加载
await driver.pause(2000);
// 获取页面标题
const title = await driver.getTitle();
console.log('页面标题:', title);
} catch (error) {
console.error('测试过程中发生错误:', error);
} finally {
// 关闭浏览器
// await driver.deleteSession();
}
}
runTest();
License:
CC BY 4.0