If some asynchronous behavior in your application is important enough that a real user should wait for it, then you should tell them. Similarly, your script can wait for that same indication before proceeding.
For example, if a user clicks a button that triggers an API call to create a record, and the user needs to wait for that record to be created, you should show them a message indicating when it completes successfully, e.g., "Record created successfully." Your script can then wait for that same text to appear, just as a user would.
Importantly, it shouldn't matter how your application is implemented. What matters is that your users can use your application—not that it calls certain AngularJS APIs or React APIs, etc.
1. Using Selenium
Selenium includes WebDriverWait
and the expected_conditions
module to help you wait for particular conditions to be met:
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
TIMEOUT = 5
# ...
WebDriverWait(driver, TIMEOUT).until(
EC.text_to_be_present_in_element(
[By.CLASS_NAME, "alert"],
"Record created successfully"))
2. Using Capybara (which uses Selenium)
As you can see above, bare Selenium is complicated and finicky. capybara-py abstracts most of it away:
from capybara.dsl import page
# ...
page.assert_text("Record created successfully")
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…