2016-12-08 21 views
1

我是一般的Ruby的新手。我想用硒来学习水豚做我们公司网站的测试。水豚rspec脚本不执行在描述块

在将代码放入描述块之前,我的代码正在工作。但是我需要在登录后等待页面加载。我做了一些研究,发现期望方法是要走的路,但他们只能在我认为的块中工作。现在,当我运行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

所有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 ...

+0

非常感谢!它现在正在运行。但是我得到了'期望'的NoMethodError。我想我补充了所有要求。有任何想法吗?这是行:expect(page).to have_field('phSearchInput') – shen

+0

@shen假设你正在运行一个现代版本的RSpec,听起来你需要在'水豚/水鸟'之前需要'rspec'或'rspec/expectations' rspec',也许也可以配置RSpec。 –

+0

我昨天刚刚安装了所有东西,所以我相信一切都应该是现代的。我没有'rspec'或'rspec/expectation'。我有'水豚/ rspec/matchers',它在'capybara/rspec'之前,但它仍然不起作用。你有什么想法?谢谢! – shen