我是Ruby的新手。我正在尝试用硒学习水豚来测试我们公司的网站。
在将代码放入describe块之前,我的代码正在运行。但是我需要在登录后等待页面加载。我做了一些研究,发现期望方法是要走的路,但我认为它们只能用于块。现在当我运行ruby testscript.rb时,浏览器甚至都没有打开。以下是代码:
require 'timeout'
require 'capybara'
require 'capybara/dsl'
require 'capybara/rspec'
require 'capybara/rspec/matchers'
require 'capybara/session/matchers'
require 'capybara/node/matchers'
Capybara.default_driver = :selenium
session = Capybara::Session.new(:selenium)
describe 'login process', :type => :feature do
it 'signs me in' do
visit 'https://testwebsite.com'
find_by_id('login_form')
fill_in('username', with: 'testuser')
fill_in('password', with: 'testpw')
find_by_id('Login').click
expect(page).to have_content('phSearchInput')
end
end
describe 'search install product', :type => :feature do
it 'searches product' do
fill_in('phSearchInput', with: '6109302')
find_by_id('phSearchButton').click
within "SVMXC__Installed_Product__c_body" do
click_on "6109302"
end
end
end
感谢任何帮助。随意提供替代方案来解决等待页面加载问题。我不想睡觉。谢谢!
答案 0 :(得分:0)
所有describe
都是定义测试,它实际上并不运行它们。要运行测试,您需要使用rspec启动它们,因此您需要运行
ruby testscript.rb
rspec testscript.rb
此外,假设phSearchInput是输入的id,您的等待期望需要
expect(page).to have_field('phSearchInput')
或
expect(page).to have_css('#phSearchInput')
或
expect(page).to have_selector(:id, 'phSearchInput')
have_content
(has_text的别名)检查页面上的可见文本,而不是元素。
最后一条评论 - Capybara会创建自己的默认会话,如果你想使用它,你可以删除你的Session.new行。如果要手动管理会话,则需要在该会话上调用所有Capybaras方法。 session.visit(...)
,session.fill_in(...)
,expect(session).to ...
等