Midscenejs_iOS测试

3 阅读1分钟
  1. 创建目录A,然后安装npx install -g appium-webdriveragent,
  2. 在A目录下打开node_modules,然后打开appium-webdriveragent
  3. 执行命令: xcodebuild -project WebDriverAgent.xcodeproj -scheme WebDriverAgentRunner -destination "id=udid" test 或者使用Xcode进行Product -> test 启动WebDriverAgentRunner
  4. 若访问localhost:8100/status未返回 WebDriverAgent信息, 则terminal执行:iproxy 8100 81005. 然后访问localhost:8100/status返回信息:
    "productBundleIdentifier" : "com.facebook.WebDriverAgentRunner" 则表示成功
  5. 执行npx @midscene/ios@1 connect, 则提示设备连接成功;
  6. 执行Midscenejs demo脚本,demo_ios.ts:
import {
  IOSAgent,
  IOSDevice,
  agentFromWebDriverAgent,
} from '@midscene/ios';

import 'dotenv/config';

const sleep = (ms) => new Promise((r) => setTimeout(r, ms));
Promise.resolve(
  (async () => {
    // Method 1: Create device and agent directly
    const page = new IOSDevice({
      wdaPort: 8100,
      wdaHost: 'localhost',
    });

    // 👀 Initialize Midscene agent
    const agent = new IOSAgent(page, {
      aiActionContext:
        'If any location, permission, user agreement, etc. popup appears, click agree. If login page appears, close it.',
    });
    await page.connect();

    // Method 2: Or use convenience function (recommended)
    // const agent = await agentFromWebDriverAgent({
    //   wdaPort: 8100,
    //   wdaHost: 'localhost',
    //   aiActionContext: 'If any location, permission, user agreement, etc. popup appears, click agree. If login page appears, close it.',
    // });

    // 👀 Launch Safari browser with graphical interface
    await agent.launch('com.apple.mobilesafari');
    await sleep(3000);

    // Navigate to eBay using AI to interact with Safari
    await agent.aiAct('tap on the address bar');
    await sleep(1000);
    await agent.aiAct('type "https://ebay.com"');
    await sleep(1000);
    await agent.aiAct('tap the Go button or press Enter');
    await sleep(5000);

    // 👀 Enter keywords and perform search
    await agent.aiAct('Search for "Headphones"');

    // 👀 Wait for loading to complete
    await agent.aiWaitFor('At least one headphone product is displayed on the page');
    // Or you can use a simple sleep:
    // await sleep(5000);

    // 👀 Understand page content and extract data
    const items = await agent.aiQuery(
      '{itemTitle: string, price: Number}[], find product titles and prices in the list',
    );
    console.log('Headphone product information', items);

    // 👀 Use AI assertion
    await agent.aiAssert('Multiple headphone products are displayed on the interface');

    await page.destroy();
  })(),
);

8.在A目录下创建.env文件添加midscene model, 然后创建脚本demo_ios.ts, 脚本执行命令npx tsx demo_ios.ts, 此时则打开iphone safari 浏览器,执行成功;