1

我有一个这样写的功能测试:

confirmation_page = visit(session, "/confirm/#{id}")
confirmation_page
    |> click(link("Decline"))
confirmation_page
    |> assert_text("You have declined.")

但是测试总是失败,因为在控制器中,单击此页面时,我正在这样做:

 conn
    |> put_flash(:info, "You have declined.")
    |> redirect(to: Routes.group_path(conn, :show, group.slug))

所以 flash 出现在重定向页面上,而不是原始页面上。如何在新页面上等待重定向和断言?

4

2 回答 2

1
  1. 您可以简单地提供一个睡眠计时器,就像:time.sleep(1000)在元素上断言之前一样。

  2. 您可以像等待页面刷新一样重试。您可以使用Wallaby.retry/2重试,直到在特定 url 上刷新窗口。我们可以使用Wallaby.current_url/1. 代码看起来像这样。

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 
于 2020-08-04T05:44:54.830 回答
0

尝试使用 assert_has(session, css("element ID or class", text: "You have declined."))

断言已内置重试,因此它将等待此元素出现,因此我认为它将解决您的问题。

于 2020-08-24T09:42:07.137 回答