[英]How to handle redirects in Wallaby?
我有一個這樣寫的功能測試:
confirmation_page = visit(session, "/confirm/#{id}")
confirmation_page
|> click(link("Decline"))
confirmation_page
|> assert_text("You have declined.")
但是測試總是失敗,因為在 controller 中,單擊此頁面時,我正在這樣做:
conn
|> put_flash(:info, "You have declined.")
|> redirect(to: Routes.group_path(conn, :show, group.slug))
所以 flash 出現在重定向頁面上,而不是原始頁面上。 我如何等待重定向並在新頁面上斷言?
在斷言元素之前,您可以簡單地提供一個睡眠計時器,如:time.sleep(1000)
。
您可以像等待頁面刷新一樣重試。 您可以使用Wallaby.retry/2
重試,直到 window 在特定 url 上刷新。我們可以使用Wallaby.current_url/1
獲取 window 的當前 url。 代碼看起來像這樣。
1 retry = fn -> if Wallaby.current_url == "expected" do
2 ┆ ┆ ┆ assert_text(confirmation_page)
3 ┆ ┆ ┆ else
4 ┆ ┆ ┆ {:error, :url_not_refreshed}
5 ┆ ┆ ┆end
6 ┆ end
7
8 assert {:ok, response} = Wallaby.retry(retry, 5000) ## default timeout option after which it
嘗試使用assert_has(session, css("element ID or class", text: "You have declined."))
斷言已內置重試,因此它將等待此元素出現,因此我認為它會解決您的問題。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.