2017-06-17 30 views
0

我不知道爲什麼,但是在完成第一個it塊之後,chrome呈現空白頁。Rspec功能測試 - 渲染空白頁的問題

create_account_spec.rb:

require 'feature_helper' 
require 'rails_helper' 

describe 'Create users' , :chrome do 
    let(:company) { build(:company, :auto_company) } 
    let(:financer) { build(:account, :auto_financer) } 
    let(:code_1) { build(:account, :auto_code_1) } 
    let(:user)  { build(:account, :auto_user) } 

    before :all do 
     visit '/' 
     company = build(:company, :auto_company) 

     GUI.login_as :auto_admin 

    # Create Company 
    find('#employer_model_screen a').send_keys :enter 
    fill_in 'name',   with: company.name 
    fill_in 'code_1',   with: company.code1 
    fill_in 'code_2',   with: company.code2 
    fill_in 'code_3',   with: company.code3 
    fill_in 'code_4',   with: company.code4 
    select 'Ukraine',  from: 'country_code' 
    fill_in 'master_counter', with: company.master_counter 
    fill_in 'storage',  with: company.storage 
    find('input[value="CREATE"]').send_keys :enter 

    GUI.sign_out 
    end 

    describe 'Check company created', :chrome do 
    before :all do 
     GUI.login_as :auto_admin 
     find('#employer_model_screen a').send_keys :enter 
    end 

    after :all do 
     GUI.sign_out 
     visit '/' 
    end 
    it { expect(first('td.name').text).to eq company.name } 
    it { expect(first('td.workflow-type').text).to eq company.workflow_type } 
    it { expect(first('td.code-1').text).to   eq company.code1 } 
    it { expect(first('td.code-2').text).to   eq company.code2 } 
    it { expect(first('td.code-3').text).to   eq company.code3 } 
    it { expect(first('td.code-4').text).to   eq company.code4 } 
    it { expect(first('td.country-code').text).to eq company.country_code } 
    it { expect(first('td.business-model').text).to eq 'R1' } 
    it { expect(first('td.master-counter').text).to eq company.master_counter.to_s } 
    it { expect(first('td.storage').text).to  eq company.storage.to_s } 
    end 

    context 'financer account' do 
    describe 'create financer account', :chrome do 
     include_examples 'login as', :auto_admin 

     it 'fill financer fields' do 
     find('#employer_model_screen a').send_keys :enter 
     find('a', text: company.name).send_keys :enter 
     fill_in 'emp_id',  with: financer.employee_id 
     fill_in 'first_name', with: financer.first_name 
     fill_in 'last_name', with: financer.last_name 
     fill_in 'designation', with: financer.designation 
     fill_in 'department', with: financer.department 
     fill_in 'email',  with: financer.email 
     find('#create_code2').send_keys :enter 
     end 

     it { expect(first('td.emp_id').text).to  eq financer.employee_id } 
     it { expect(first('td.name').text).to  eq financer.first_name + ' ' + financer.last_name } 
     it { expect(first('td.designation').text).to eq financer.designation } 
     it { expect(first('td.department').text).to eq financer.department } 
     it { expect(first('td.code_type').text).to eq 'financer' } 
     it { expect(first('td.email').text).to  eq financer.email } 
     it { expect(first('td.status').text).to  eq financer.status } 
     it { expect(first('.btn-primary').text).to eq 'ACTIVATE' } 

     include_examples 'sign out' 
    end 

    describe 'sign up as financer account', :chrome do 
     include_examples 'sign up as', :auto_financer, code: :code2 
     include_examples 'check sign up successfully' 
     include_examples 'check welcome message' 
     include_examples 'sign out' 
    end 

    describe 'login as financer', :chrome do 
     it 'fill email' do 
     fill_in 'email', with: financer.email 
     sleep(0.5) 
     end 

     include_examples 'check plan', disabled: :individual, checked: :company 

     it 'fill password & Go' do 
     fill_in 'password', with: financer.password 
     find('[value=Go]').send_keys :enter 
     end 
     include_examples 'check sign in successfully' 
     include_examples 'check navigation items', 'Home', 'Submit', 'Storage (0)', 
         'My Claims', 'Employees\' Claim (0)', 'Sign Out', 
         'Employee Accounts', 'Expense Setup', 'Reports' 
     include_examples 'sign out' 
    end 
    end 

    context 'code_1 account' do 
    describe 'create code_1 account', :chrome do 
     include_examples 'login as', :auto_financer 
     it 'fill code_1 fields' do 
     find('#employee_accounts_screen a').send_keys :enter 
     fill_in 'emp_id',  with: code_1.employee_id 
     fill_in 'first_name', with: code_1.first_name 
     fill_in 'last_name', with: code_1.last_name 
     fill_in 'designation', with: code_1.designation 
     fill_in 'department', with: code_1.department 
     fill_in 'email',  with: code_1.email 
     find('select option[value=User]').select_option 
     find('input[value="CREATE"]').send_keys :enter 
     end 

     # [Question] User are created in the middle of table list (at second position - little weird) 
     # I have automated it for 1-st position (like when creating financer account) 
     # If it is correct behavior I will change logic 
     it { expect(all('td.role')[1].text).to   eq 'User' } 
     it { expect(all('td.emp_id')[1].text).to   eq code_1.employee_id } 
     it { expect(all('td.name')[1].text).to   eq code_1.first_name + ' ' + code_1.last_name } 
     it { expect(all('td.designation')[1].text).to eq code_1.designation } 
     it { expect(all('td.department')[1].text).to  eq code_1.department } 
     it { expect(all('td.email')[1].text).to   eq code_1.email } 

     include_examples 'sign out' 
    end 

    describe 'sign up as code_1 account', :chrome do 
     include_examples 'sign up as', :auto_code_1, code: :code1 
     include_examples 'check sign up successfully' 
     include_examples 'check welcome message' 
     include_examples 'sign out' 
    end 

    describe 'login as code_1', :chrome do 
     it 'fill email' do 
     fill_in 'email', with: code_1.email 
     sleep(0.5) 
     end 

     include_examples 'check plan', disabled: :individual, checked: :company 

     it 'fill password & Go' do 
     fill_in 'password', with: code_1.password 
     find('[value=Go]').send_keys :enter 
     end 
     include_examples 'check sign in successfully' 
     include_examples 'check navigation items', 'Home', 'Submit', 'Storage (0)', 
         'My Claims', 'Employees\' Claim (0)', 'Sign Out' 
     include_examples 'sign out' 
    end 
    end 

    context 'user account' do 
    describe 'sign up as user account ', :chrome do 
     include_examples 'sign up as', :auto_user 
     include_examples 'check sign up successfully' 
     include_examples 'check welcome message' 
     include_examples 'sign out' 
    end 

    describe 'login as user', :chrome do 
     it 'fill email' do 
     fill_in 'email', with: user.email 
     sleep(0.5) 
     end 

     include_examples 'check plan', disabled: :company, checked: :individual 

     it 'fill password & Go' do 
     fill_in 'password', with: user.password 
     find('[value=Go]').send_keys :enter 
     end 
     include_examples 'check sign in successfully' 
     include_examples 'check navigation items', 'Home', 'Submit', 
         'Storage (0)', 'My Claims', 'Sign Out' 
     include_examples 'sign out' 
    end 
    end 

    context 'duplicate user' do 
    describe 'sign up as duplicate user', :chrome do 
     include_examples 'sign up as', :auto_user 
     include_examples 'check review errors message' 
     it 'check warning message ' do 
     expect(find('.user-form')).to be_visible 
     end 
    end 
    end 
end 

feature_helper:

require 'capybara/rspec' 
require 'capybara/poltergeist' 

require './spec/support/helpers/feature_helper' 
require './spec/support/shared_examples/sign_in_examples' 
require './spec/support/shared_examples/sign_up_examples' 
require './spec/support/shared_examples/main_page_examples' 

# Configure Capybara 
Capybara.register_driver :chrome do |app| 
    Capybara::Selenium::Driver.new(app, :browser => :chrome) 
end 
Capybara.default_driver = :poltergeist 
Capybara.javascript_driver = :poltergeist 
Capybara.current_driver = :chrome 
Capybara.app_host = 'http://0.0.0.0:3000/' 
Capybara.exact = true 

RSpec.configure do |config| 
    config.expect_with :rspec do |expectations| 
    expectations.include_chain_clauses_in_custom_matcher_descriptions = true 
    config.include Capybara::DSL, :type => :request 
    end 

    config.mock_with :rspec do |mocks| 
    mocks.verify_partial_doubles = true 
    end 
end 

支持/ feature_helper.rb

require 'capybara/dsl' 

module GUI 
    extend Capybara::DSL 

    class << self 
    def login_as(user) 
     binding.pry 
     user = FactoryGirl.build(:account, user) 
     find('a', text: 'Sign In').send_keys :enter 
     fill_in 'email', with: user.email 
     fill_in 'password', with: user.password 
     find('[value=Go]').send_keys :enter 
    end 

    def sign_out 
     binding.pry 
     find('a', text: 'Sign Out').send_keys :enter 
    end 
    end 
end 

的Gemfile: 組:發展,:測試做 寶石「dotenv - ''' 寶石'撬滑軌 寶石‘byebug’ 寶石‘factory_girl_rails’ 寶石‘RSpec的護欄’ 寶石‘chromedriver輔助’ 結束

group :test do 
    gem 'capybara' 
    gem 'selenium-webdriver' 
    gem 'poltergeist' 
    gem 'codeclimate-test-reporter', require: false 
    gem 'database_cleaner' 
    gem 'simplecov', require: false # Test Coverage analytics 
    gem 'shoulda-matchers', '~> 3.1' 
    gem 'webmock' # Mocking http request 
    gem 'terminal-notifier-guard' # for OSX 
    gem 'rspec-steps' 
end 

如果我寫的所有在itexpect它工作正常。

在此先感謝。

回答

1

對於這個特定問題的問題是您的visit調用位置不正確,但更大的總體問題是您正在編寫功能測試,如單元測試。

每個it塊都是一個單獨的測試,並假設您使用的是標準的Capybara rspec集成,最後做的一件事就是訪問about:blank。這將發生在after :all塊之後。因此,您正在測試的頁面的visit應發生在before :all塊或單獨的測試中。此外,由於數據庫是/應在每次測試之間重置(it/scenario區塊),因此退出通常不是必需的。

before :all do 
    visit '/' # Alternatively you could move this to your login_as method 
    GUI.login_as :auto_admin 
    find('#employer_model_screen a').send_keys :enter 
end 

# Not generally necessary for tests of the type you show 
# after :all do 
# GUI.sign_out 
# visit '/' 
# end 

現在,更大的問題是,你正在測試一個整體「功能」,但測試在每個測試一個視覺元素。這意味着要查看是否顯示公司名稱,必須創建公司,創建用戶,讓瀏覽器訪問頁面,然後登錄,然後檢查名稱。接下來,它想要檢查國家/地區類型,以便再次創建公司,創建用戶,瀏覽器訪問同一頁面,然後登錄,然後檢查國家/地區類型。如果你繼續寫這樣的功能測試,他們很快就要花幾個小時,如果沒有幾天運行。如果你檢查的目的是讓所有的公司的信息顯示,那麼它應該是一個it/scenario

it "shows the company info" do 
    expect(page).to have_css('td.name'.text: company.name) 
    expect(page).to have_css('td.workflow-type', text: company.workflow_type) 
    ... 
end 

還要注意使用水豚提供的匹配,而不是.texteq,這將使你的測試一旦你開始擁有一個jax /異步操作和頁面更新,就會更加穩定,因爲它提供了對檢查的等待/重試行爲。 (您應該儘可能遠離all/first,因爲它們對動態頁面有嚴格限制)