avatar

sunday

Sunday's Blog

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

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

Posted 2025-03-4 Updated 2025-03- 4
By sunday
3~5 min read

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
Share

Further Reading

OLDER

2024年终总结

NEWER

解决nextjs15使用useLocalStorage报错的问题

Recently Updated

  • nextjs15使用better-sqlite3的连接报错问题
  • nextjs + clerk + supabase + realtime 实时监听数据库更改
  • 解决nextjs15使用useLocalStorage报错的问题
  • mac上使用nodejs appium控制chrome浏览器
  • 2024年终总结

Trending Tags

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

Contents

©2025 sunday. Some rights reserved.

Using the Halo theme Chirpy