首页 > 解决方案 > Selenium 等到 API 响应

问题描述

我想检查 API 响应,然后单击特定按钮,因此该按钮存在,无需进行可见性检查,但 API 仍在后台检查某些有效性。我的步骤定义是“

@And("^I click on Describe your Shipment button$")
public void i_click_on_describe_your_shipment_button() {
    Button.safeClick(driver, destinationLocation.describeYourShipmentButton());}

我努力了:

driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);

但没有奏效。

唯一对我有用的东西:

Thread.sleep()

我不想使用。有什么建议吗?

标签: javaseleniumcucumber-jvm

解决方案


不知道如何在 Selenium 中解决这个问题,但你可以用Awaitility摆脱那个 Thread.sleep 。

来自他们的 Github 页面的示例:

@Test
public void updatesCustomerStatus() {
    // Publish an asynchronous message to a broker (e.g. RabbitMQ):
    messageBroker.publishMessage(updateCustomerStatusMessage);
    // Awaitility lets you wait until the asynchronous operation completes:
    await().atMost(5, SECONDS).until(customerStatusIsUpdated());
    ...
}

详细信息:https ://github.com/awaitility/awaitility


推荐阅读