You need to enable JavaScript to run this app. AWS Device Farm测试用例执行过程中是否可修改并激活Network Profile?
优惠活动
大模型
产品
解决方案
定价
更多

AWS Device Farm测试用例执行过程中是否可修改并激活Network Profile?

结论

你的需求是可以实现的,你之前调用的CreateNetworkProfile仅用于创建网络配置,没有激活配置的作用,需要配合其他API完成切换操作。

具体实现步骤

方案1:使用Device Farm原生网络配置切换

  • 第一步:提前创建好离线、在线两个网络配置,留存两个配置的ARN,不需要每次测试运行时重复创建,避免生成冗余配置。
  • 第二步:测试运行时可从环境变量DEVICEFARM_RUN_ARN拿到当前测试任务的ARN,需要切换网络时调用UpdateRun API,传入当前任务ARN和目标网络配置的ARN即可激活对应配置。
  • 注意:切换配置后建议等待2~3秒,确认网络规则生效后再执行后续测试逻辑,避免时序导致的用例异常。

方案2:浏览器级离线模拟(更轻量)

因为你测试的是浏览器端PWA,不需要依赖Device Farm的全局网络限制,可以直接通过WebDriver的能力设置浏览器离线状态,调用成本更低:

  • WebdriverIO 直接提供了setNetworkConditions方法,直接设置offline: true即可实现浏览器级离线,不需要调用AWS相关API。

修改后代码示例(方案1)

const webdriverio = require('webdriverio')
const assert = require('chai').assert
const {
  CreateNetworkProfileCommand,
  UpdateRunCommand, // 新增UpdateRun命令
  DeviceFarmClient
} = require('@aws-sdk/client-device-farm')

// 提前创建好的两个网络配置ARN,替换成你自己的
const OFFLINE_PROFILE_ARN = 'arn:aws:devicefarm:us-west-2:XXXXXXXXXXXX:networkprofile:XXXXXXXXXXXXXXXXXXX'
const ONLINE_PROFILE_ARN = 'arn:aws:devicefarm:us-west-2:XXXXXXXXXXXX:networkprofile:XXXXXXXXXXXXXXXXXXX'

const credentials = {
  accessKeyId: '',
  secretAccessKey: '',
  sessionToken: ''
}

const deviceFarmClient = new DeviceFarmClient({
  region: 'us-west-2',
  credentials
})

describe('Test Online and Offline', function () {
  it('Should go to Google then go offline then fail to get to Bing', async function () {
    const client = await webdriverio.remote({
      capabilities: {
        automationName: 'XCUITest',
        platformName: process.env.DEVICEFARM_DEVICE_PLATFORM_NAME,
        deviceName: process.env.DEVICEFARM_DEVICE_NAME,
        platformVersion: process.env.DEVICEFARM_DEVICE_OS_VERSION,
        browserName: 'Safari'
      },
      path: '/wd/hub',
      host: process.env.APPIUM_HOST || 'localhost',
      port: process.env.APPIUM_PORT || 4723,
      logLevel: 'info'
    })

    await client.url('https://www.google.com')
    const title = await client.getTitle()
    assert.equal(title, 'Google')
    console.log('Online check completed')

    // 切换到离线配置
    await deviceFarmClient.send(new UpdateRunCommand({
      arn: process.env.DEVICEFARM_RUN_ARN,
      networkProfileArn: OFFLINE_PROFILE_ARN
    }))
    // 等待网络配置生效
    await client.pause(3000)
    console.log("Network switched to 'offline'")

    await client.url('https://www.bing.com')
    const title2 = await client.getTitle()
    assert.equal(title2, 'Bing')  // 该步骤会按预期失败,因为设备已离线

    // 切回在线配置
    await deviceFarmClient.send(new UpdateRunCommand({
      arn: process.env.DEVICEFARM_RUN_ARN,
      networkProfileArn: ONLINE_PROFILE_ARN
    }))
    await client.pause(3000)
    console.log("Network switched back to 'online'")

    await client.deleteSession()
  })
})

内容的提问来源于stack exchange,提问作者stang

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.07 20:21:02