avatar

sunday

Sunday's Blog

  • 首页
主页 mac上使用nodejs appium控制chrome浏览器
文章

mac上使用nodejs appium控制chrome浏览器

发表于 2025-03-4 更新于 2025-03- 4
作者 sunday 已删除用户
3~5 分钟 阅读

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();

许可协议:  CC BY 4.0
分享

相关文章

下一篇

2024年终总结

上一篇

解决nextjs15使用useLocalStorage报错的问题

最近更新

  • ios18 swiftUI 开发的一些问题
  • Cursor IDE中开发IOS应用——支持热更新
  • nginx + acme 不占用80端口申请证书
  • 免费CDN 阿里云ESA 加速国内网站
  • nextjs15使用ai sdk的一些问题

热门标签

nginx acme 强制跳转HTTPS nodejs 代理 mac 神器 vue3 工具 docker

目录

©2025 sunday. 保留部分权利。

使用 Halo 主题 Chirpy