2011-05-12 10 views
31

मैं रेल 3 एप में पहली बार सेलेनियम का उपयोग करने की कोशिश कर रहा हूं। (मैंने इसे रेल के साथ पहले इस्तेमाल किया है 2)। मैं एक आरवीएम में हूं जिसमें निश्चित रूप से अपना खुद का रत्न है।रेल में काम कर रहे सेलेनियम प्राप्त करने का प्रयास 3 - "वेबमॉक :: नेटकनेक्ट नॉटअलेवॉयरर"

मैं पहली बार, capybara के साथ एक कार्य भाग गया सेलेनियम के बिना:

#./spec/requests/homepage_spec.rb 
require 'spec_helper' 

describe 'home page', :type => :request do 
    it 'should welcome the user' do 
    visit '/' 
    page.should have_content('Buddy') 
    end 
end 

यह ठीक भाग गया। मैंने फिर वर्णन के विकल्पों के लिए "js => true" विकल्प जोड़ा, जिसे सेलेनियम (जहां तक ​​मुझे पता है) का उपयोग करके इसे चलाने के लिए माना जाता है। और यह त्रुटि मिली:

Failure/Error: @page.start_new_browser_session 
WebMock::NetConnectNotAllowedError: 
    Real HTTP connections are disabled. Unregistered request: POST http://localhost:4444/selenium-server/driver/ with body 'cmd=getNewBrowserSession&1=%2Afirefox&2=http%3A%2F%2Flocalhost%3A3000&3=&4=' with headers {'Accept'=>'*/*', 'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8', 'User-Agent'=>'Ruby'} 

    You can stub this request with the following snippet: 

    stub_request(:post, "http://localhost:4444/selenium-server/driver/"). 
    with(:body => "cmd=getNewBrowserSession&1=%2Afirefox&2=http%3A%2F%2Flocalhost%3A3000&3=&4=", 
      :headers => {'Accept'=>'*/*', 'Content-Type'=>'application/x-www-form-urlencoded; charset=utf-8', 'User-Agent'=>'Ruby'}). 
    to_return(:status => 200, :body => "", :headers => {}) 

    ============================================================ 

यह पहली बार कैपिबरा का उपयोग कर रहा है और मुझे लगता है कि मेरी कॉन्फ़िगरेशन किसी भी तरह गलत है। शायद कुछ आवश्यक रत्न याद आ रही है? मैं इस ब्लॉग पोस्ट का अनुसरण कर रहा था: Capybara (and Selenium) with RSpec & Rails 3: quick tutorial

कोई विचार?

source 'http://rubygems.org' 
gem 'rails', '3.0.7' 
gem 'haml' 
gem 'heroku' 
gem "heroku_backup_task" 
gem 'authlogic', '3.0.2' 
gem 'rails3-generators' 
gem 'txtlocal', :git => 'git://github.com/epigenesys/txtlocal.git' 
gem 'chronic' 
gem 'sqlite3-ruby', :require => 'sqlite3' 
gem 'bcrypt-ruby' 

group :development, :test do 
    gem 'rspec' 
    gem 'mocha' 
    gem "rspec-rails", "~> 2.4" 
    gem "webmock" 
    gem 'database_cleaner', '0.5.2' 
    gem 'capybara', :git => 'git://github.com/jnicklas/capybara.git' 
    gem 'selenium-client' 
end 

और मेरे Gemfile.lock:

GIT 
    remote: git://github.com/epigenesys/txtlocal.git 
    revision: 66aab6628d4385eda959e4a8bd083cc2c259fea0 
    specs: 
    txtlocal (0.0.1) 
     json 

GIT 
    remote: git://github.com/jnicklas/capybara.git 
    revision: 9cdf015e28637b7b0d94f5a69f9bc3605455ce22 
    specs: 
    capybara (1.0.0.beta1) 
     mime-types (>= 1.16) 
     nokogiri (>= 1.3.3) 
     rack (>= 1.0.0) 
     rack-test (>= 0.5.4) 
     selenium-webdriver (~> 0.2.0) 
     xpath (~> 0.1.4) 

GEM 
    remote: http://rubygems.org/ 
    specs: 
    abstract (1.0.0) 
    actionmailer (3.0.7) 
     actionpack (= 3.0.7) 
     mail (~> 2.2.15) 
    actionpack (3.0.7) 
     activemodel (= 3.0.7) 
     activesupport (= 3.0.7) 
     builder (~> 2.1.2) 
     erubis (~> 2.6.6) 
     i18n (~> 0.5.0) 
     rack (~> 1.2.1) 
     rack-mount (~> 0.6.14) 
     rack-test (~> 0.5.7) 
     tzinfo (~> 0.3.23) 
    activemodel (3.0.7) 
     activesupport (= 3.0.7) 
     builder (~> 2.1.2) 
     i18n (~> 0.5.0) 
    activerecord (3.0.7) 
     activemodel (= 3.0.7) 
     activesupport (= 3.0.7) 
     arel (~> 2.0.2) 
     tzinfo (~> 0.3.23) 
    activeresource (3.0.7) 
     activemodel (= 3.0.7) 
     activesupport (= 3.0.7) 
    activesupport (3.0.7) 
    addressable (2.2.5) 
    arel (2.0.9) 
    authlogic (3.0.2) 
     activerecord (~> 3.0.7) 
     activerecord (~> 3.0.7) 
    bcrypt-ruby (2.1.4) 
    builder (2.1.2) 
    childprocess (0.1.8) 
     ffi (~> 1.0.6) 
    chronic (0.3.0) 
    configuration (1.2.0) 
    crack (0.1.8) 
    database_cleaner (0.5.2) 
    diff-lcs (1.1.2) 
    erubis (2.6.6) 
     abstract (>= 1.0.0) 
    ffi (1.0.7) 
     rake (>= 0.8.7) 
    haml (3.1.1) 
    heroku (2.1.2) 
     launchy (>= 0.3.2) 
     rest-client (~> 1.6.1) 
     term-ansicolor (~> 1.0.5) 
    heroku_backup_task (0.0.5) 
     heroku (>= 1.13.7) 
     rake 
    i18n (0.5.0) 
    json (1.5.1) 
    json_pure (1.5.1) 
    launchy (0.4.0) 
     configuration (>= 0.0.5) 
     rake (>= 0.8.1) 
    mail (2.2.19) 
     activesupport (>= 2.3.6) 
     i18n (>= 0.4.0) 
     mime-types (~> 1.16) 
     treetop (~> 1.4.8) 
    mime-types (1.16) 
    mocha (0.9.12) 
    nokogiri (1.4.4) 
    polyglot (0.3.1) 
    rack (1.2.2) 
    rack-mount (0.6.14) 
     rack (>= 1.0.0) 
    rack-test (0.5.7) 
     rack (>= 1.0) 
    rails (3.0.7) 
     actionmailer (= 3.0.7) 
     actionpack (= 3.0.7) 
     activerecord (= 3.0.7) 
     activeresource (= 3.0.7) 
     activesupport (= 3.0.7) 
     bundler (~> 1.0) 
     railties (= 3.0.7) 
    rails3-generators (0.17.4) 
     railties (>= 3.0.0) 
    railties (3.0.7) 
     actionpack (= 3.0.7) 
     activesupport (= 3.0.7) 
     rake (>= 0.8.7) 
     thor (~> 0.14.4) 
    rake (0.8.7) 
    rest-client (1.6.1) 
     mime-types (>= 1.16) 
    rspec (2.6.0) 
     rspec-core (~> 2.6.0) 
     rspec-expectations (~> 2.6.0) 
     rspec-mocks (~> 2.6.0) 
    rspec-core (2.6.0) 
    rspec-expectations (2.6.0) 
     diff-lcs (~> 1.1.2) 
    rspec-mocks (2.6.0) 
    rspec-rails (2.6.0) 
     actionpack (~> 3.0) 
     activesupport (~> 3.0) 
     railties (~> 3.0) 
     rspec (~> 2.6.0) 
    rubyzip (0.9.4) 
    selenium-client (1.2.18) 
    selenium-webdriver (0.2.0) 
     childprocess (>= 0.1.7) 
     ffi (>= 1.0.7) 
     json_pure 
     rubyzip 
    sqlite3 (1.3.3) 
    sqlite3-ruby (1.3.3) 
     sqlite3 (>= 1.3.3) 
    term-ansicolor (1.0.5) 
    thor (0.14.6) 
    treetop (1.4.9) 
     polyglot (>= 0.3.1) 
    tzinfo (0.3.27) 
    webmock (1.6.2) 
     addressable (>= 2.2.2) 
     crack (>= 0.1.7) 
    xpath (0.1.4) 
     nokogiri (~> 1.3) 

PLATFORMS 
    ruby 

DEPENDENCIES 
    authlogic (= 3.0.2) 
    bcrypt-ruby 
    capybara! 
    chronic 
    database_cleaner (= 0.5.2) 
    haml 
    heroku 
    heroku_backup_task 
    mocha 
    rails (= 3.0.7) 
    rails3-generators 
    rspec 
    rspec-rails (~> 2.4) 
    selenium-client 
    sqlite3-ruby 
    txtlocal! 
    webmock 

उत्तर

28

क्योंकि आप webmock उपयोग कर रहे हैं यह

पी एस, यहाँ मेरी Gemfile सामग्री है। यह सभी आउटबाउंड HTTP अनुरोधों को अवरुद्ध करता है।

यदि आपको इसकी आवश्यकता नहीं है, तो इसे Gemfile से हटा दें। आप इसे की जरूरत है, तो आप अपनी आवश्यकताओं के अधिक यह कॉन्फ़िगर करने के लिए ठीक आवश्यकता हो सकती है:

https://github.com/bblimke/webmock

+0

आह ... धन्यवाद! –

15

हम इसका उपयोग capybara में सामान्य अनुरोध सक्षम और सेलेनियम के कॉलबैक हर जगह अनुमति देने के लिए, क्योंकि वे अनुरोध के बाद निकाल दिया जाता है समाप्त हो गया है।

# spec/spec_helper.rb 
RSpec.configure do |config| 
    config.before(:all, type: :request) do 
    WebMock.allow_net_connect! 
    end 

    config.after(:all, type: :request) do 
    selenium_requests = %r{/((__.+__)|(hub/session.*))$} 
    WebMock.disable_net_connect! :allow => selenium_requests 
    end 
end 
+0

यह काम किया! धन्यवाद! हालांकि, मुझे यकीन नहीं है कि सेलेनियम_रेक्वेस्ट क्या है। – Rimian

+0

'WebMock.allow_net_connect! 'चाल चल रही थी! –

8

@grosser कोड

RSpec.configure do |config| 
    config.before do 
    WebMock.enable! 
    if Capybara.current_driver != :rack_test 
     selenium_requests = %r{/((__.+__)|(hub/session.*))$} 
     WebMock.disable_net_connect! :allow => selenium_requests 
     WebMock.disable_net_connect! :allow => "127.0.0.1:#{Capybara.current_session.driver.server_port}" # this only works for capybara selenium and capybara-webkit 
    else 
     WebMock.disable_net_connect! 
    end 
    end 

    # for connections where we need to have network access we just tag it network 
    config.before(:each, :network => true) do 
    WebMock.disable! 
    end 
end 
7

WebMock.disable_net_connect! allow_localhost: true

49
इस के साथ

पर सुधार करने के लिए ...

WebMock.disable_net_connect!(:allow_localhost => true) 

आप अपने स्थानीय होस्ट करने के लिए वास्तविक वेब का उपयोग की अनुमति है। यह सही है जब आपको अपने आवेदन के लिए सेलेनियम का उपयोग करने की आवश्यकता होती है, साथ ही, बाहरी संसाधनों का नकल करना।

3

मेरे विशिष्ट मामले में मेरी समस्या evergreen (जावास्क्रिप्ट आरएसपीईसी) में थी।

मैं कहा:

WebMock.disable_net_connect! :allow_localhost => true 

मेरी वातावरण/env करने के लिए * क्योंकि सदाबहार .rb rspec सहायकों लोड नहीं करता है।

+0

यह वह उत्तर था जो मेरे लिए काम करता था! मेरी परिस्थिति यह है कि मैं रेक संपत्तियों को चलाने की कोशिश कर रहा हूं: मेरे परीक्षण सफल होने के बाद संपत्ति को अमेज़ॅन एस 3 में धक्का देने के लिए परिसंपत्ति_सिंक के साथ प्रीकंपाइल करें। वेबमॉक मणि एसेट पुश को रोक रहा था, भले ही मैंने अपना spec_helper.rb 'WebMock.disable_net_connect' के साथ अपडेट किया था! (: Allow => /s3.amazonaws.com/,: allow_localhost => true) 'मुझे वातावरण/test.rb इस लाइन के साथ और फिर यह काम शुरू कर दिया, इस जवाब @carlosveucv के लिए धन्यवाद –

0

मैंने उपरोक्त उत्तरों की कोशिश की और उनमें से कोई भी काम नहीं किया, फिर पाया कि यह मुद्दा मेरा कार्यस्थल का इंटरनेट कनेक्शन था। यह मेरे घर के इंटरनेट कनेक्शन पर काम किया।

1

यहां अच्छे सुझाव।एक अन्य, अधिक परिष्कृत tuneable, समाधान:

WebMock.disable_net_connect!(
    allow: [ 
    'localhost', 
    'dynamo:8000', 
    'pact-broker:81' 
    ] 
) 

निर्माण विशेष रूप से अच्छी तरह से डोकर-रचना कंटेनर श्वेत सूची बनाती के लिए!

संबंधित मुद्दे