简体   繁体   English

如何处理 Wallaby 中的重定向?

[英]How to handle redirects in Wallaby?

I have a feature test written like this:我有一个这样写的功能测试:

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

However the test always fails, because in controller, on click of this page, I am doing this:但是测试总是失败,因为在 controller 中,单击此页面时,我正在这样做:

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

So the flash is coming on the redirected page, and not the original page.所以 flash 出现在重定向页面上,而不是原始页面上。 How can I wait for the redirect and assert on the new page?我如何等待重定向并在新页面上断言?

  1. You can simply provide a sleep timer like :time.sleep(1000) before asserting over element.在断言元素之前,您可以简单地提供一个睡眠计时器,如:time.sleep(1000)

  2. You can retry like waiting for page to be refreshed.您可以像等待页面刷新一样重试。 You can use Wallaby.retry/2 to retry until window is refreshed on specific url. We can get current url of window using Wallaby.current_url/1 .您可以使用Wallaby.retry/2重试,直到 window 在特定 url 上刷新。我们可以使用Wallaby.current_url/1获取 window 的当前 url。 Code would look something like this.代码看起来像这样。

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 

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

The assert has have build in retry so it will wait for this element to show up so I think it will solve your problem.断言已内置重试,因此它将等待此元素出现,因此我认为它会解决您的问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM