算法: visit '/user-6ren">
gpt4 book ai didi

ruby-on-rails - capybara 登录测试

转载 作者:太空宇宙 更新时间:2023-11-03 16:44:53 25 4
gpt4 key购买 nike

我想测试一个页面的登录是否有效,所以我创建了两个测试,如果 url 更改和内容更改。

这是我的观点:

<%= f.email_field :email, :autofocus => true, placeholder: "Email" %>
<%= f.password_field :password, placeholder: "Password" %>
<%= f.submit "Login" %>

算法:

visit '/users/sign_in'
fill_in 'Email', with: email
fill_in 'Password', with: password
click_on 'Login'

测试:

 expect(page).to have_content('Welcome')

结果:预期在“登录”中找到文本“欢迎”

expect(current_path).to eql('/home')

结果:预期:“/home”得到:“/”

所有的测试都失败了,我不知道为什么,我使用了有效的登录,我可以手动使用登录成功。

最佳答案

“预期在“登录”中找到文本“欢迎””,这些错误通常发生在页面加载不正常但测试得到执行时。所以你应该在你的测试中应用等待。以下代码应该适合您:

visit '/users/sign_in'
page.should have_content("Wait for some text which is diaplayed on the page") //waits for the sign in page to load
fill_in 'Email', with: email
fill_in 'Password', with: password
click_on 'Login'
page.should have_no_content("Wait for the text which is available in the sign in page but not on next page") //waits for the sign in page to disappear and open welcome page.
page.should have_content("Welcome")

注意:除了等待一些文本,您还可以等待 css 选择器

希望这有帮助:)

关于ruby-on-rails - capybara 登录测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35361373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com