使用 AWS 设备农场时如何解决代理问题?

How can i solve proxy problem when using AWS device farm?

我正在尝试执行我们可以在网站下方获取的 aws device farm 示例代码。 https://docs.aws.amazon.com/devicefarm/latest/testgrid/getting-started-local.html

// Import the AWS SDK for Java 2.x Device Farm client:
...

// in your tests ...
public class MyTests {
  // ... When you set up your test suite
  private static RemoteWebDriver driver;

  @Before
  void setUp() {
    String myProjectARN = "...";
    DeviceFarmClient client  = DeviceFarmClient.builder().region(Region.US_WEST_2).build();
    CreateTestGridUrlRequest request = CreateTestGridUrlRequest.builder()
      .expiresInSeconds(300)
      .projectArn(myProjectARN)
      .build();
    CreateTestGridUrlResponse response = client.createTest.GridUrl(request);
    URL testGridUrl = new URL(response.url());
    // You can now pass this URL into RemoteWebDriver.
    WebDriver driver = new RemoteWebDriver(testGridUrl, DesiredCapabilities.firefox());
  }
  
  @After
  void tearDown() {
    // make sure to close your WebDriver:
    driver.quit();
  }

}

执行上面的代码后,出现错误,提示信息是这样的

java.net.UnknownHostException: devicefarm.us-westt-2.amazonaws.com

我猜是因为代理服务器,代码无法解析主机。 我该如何解决这个问题?

谢谢。

请确认哪一行抛出 java.net.UnknownHostException: devicefarm.us-westt-2.amazonaws.com。是 client.createTest.GridUrl(request) 还是 WebDriver driver = new RemoteWebDriver(testGridUrl, DesiredCapabilities.firefox());

如果是client.createTest.GridUrl(请求),请按照https://docs.aws.amazon.com/sdk-for-java/v1/developer-guide/section-client-configuration.html

中提到的代理配置

我现在的设置方法是这样的

@Before
public void setUp() {
    try {
        ProxyConfiguration.Builder proxyConfig = ProxyConfiguration.builder();
        proxyConfig.endpoint(new URI("<YOUR PROXY URL>"));
        proxyConfig.username("<YOUR USER ID>");
        proxyConfig.password("YOUR PASSWORD");
        ApacheHttpClient.Builder httpClientBuilder =
                ApacheHttpClient.builder()
                                .proxyConfiguration(proxyConfig.build());

        String myARN = "<YOUR ARN>";
        DeviceFarmClient client  = DeviceFarmClient.builder()
                .credentialsProvider(DefaultCredentialsProvider.create())
                .region(Region.US_WEST_2)
                .httpClientBuilder(httpClientBuilder)
                .overrideConfiguration(ClientOverrideConfiguration.builder().build())
                .build();
        CreateTestGridUrlRequest request = CreateTestGridUrlRequest.builder()
                .expiresInSeconds(300)        // 5 minutes
                .projectArn(myARN)
                .build();
        URL testGridUrl = null;
        CreateTestGridUrlResponse response = client.createTestGridUrl(request);
        testGridUrl = new URL(response.url());
        driver = new RemoteWebDriver(testGridUrl, DesiredCapabilities.chrome());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

再次感谢。