是否可以在 Device Farm 上的测试用例期间修改网络配置文件?

Is it possible to modify the Network Profile during a test case on Device Farm?

我正在构建一个 运行 在浏览器中的网络应用程序。此 Web 应用程序是一个 PWA,如果设备“离线”,它应该会继续正常工作。

我正在尝试编写 Appium 测试,我想 运行 在 AWS Device Farm 上验证此行为。

AWS Device Farm Docs suggest that it is possible to "Change Network Conditions During Your Test" and points to the use of the CreateNetworkProfile。然而,当我使用这个 API 虽然我可以创建一个新的网络配置文件时,我找不到在测试中途 激活 配置文件的方法。

这里是一个简单测试的例子:

const webdriverio = require('webdriverio')
const assert = require('chai').assert
const {
  CreateNetworkProfileCommand,
  DeviceFarmClient
} = require('@aws-sdk/client-device-farm')

const offlineProfile = new CreateNetworkProfileCommand({
  projectArn:
    'arn:aws:devicefarm:us-west-2:XXXXXXXXXXXX:project:XXXXXXXXXXXXXXXXXXX',
  name: 'offline',
  uplinkBandwidthBits: 0,
  downlinkBandwidthBits: 0
})
const onlineProfile = new CreateNetworkProfileCommand({
  projectArn:
    'arn:aws:devicefarm:us-west-2:XXXXXXXXXXXX:project:XXXXXXXXXXXXXXXXXXX',
  name: 'online',
  uplinkBandwidthBits: 104857600,
  downlinkBandwidthBits: 104857600
})

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')

    const response = await deviceFarmClient.send(offlineProfile)
    console.log(response)
    // At this point I need the device to switch to using the new "offlineProfile" NetworkProfile
    console.log("Network switched to 'offline'")

    await client.url('https://www.bing.com')
    const title2 = await client.getTitle()
    assert.equal(title2, 'Bing')  // This step is expected to fail because the device should be offline

    const response2 = await deviceFarmClient.send(onlineProfile)
    console.log(response2)
    // At this point I need the device to switch to using the new "onlineProfile" NetworkProfile to clean up

    await client.deleteSession()
  })
})

Device Farm 可以做我想做的事吗?如果是,我将如何在测试中切换活动网络配置文件?

我已从 AWS Support 收到对此的答复 - 不幸的是,不支持对网络配置文件进行动态更改(在撰写本文时)。

支持回复:

AWS Device Farm service allows selecting a custom Network Profile only at the time of scheduling the test run and not when tests are in running state. The custom Network Profile created in Device Farm can only be selected to use in "ScheduleRun" API [1] by specifying the "networkProfileArn" parameter. Hence, we have to manage the network configurations through our test scripts or any supported library.

As per the above statement from service team; I am sorry to say but, dynamic Network Profile handling for our custom profile is yet not supported. So, we have an option of using "Mobile JSON Wire Protocol Specification" API provided by Selenium framework and control setting of the network connection of a device.