рдПрдХ рдкрд░реАрдХреНрд╖рдг рд╕реНрд╡рдЪрд╛рд▓рди рдЙрдкрдХрд░рдг рдХреЗ рд░реВрдк рдореЗрдВ рд╡рд╛рдЯрд░ рд╡реЗрдмрд░рд╛рдЗрд╡рд░

WebDriver рдХреЗ рдмрд╛рд░реЗ рдореЗрдВ рдПрдХ рд╣рд╛рд▓рд┐рдпрд╛ рд▓реЗрдЦ рдиреЗ рдореБрдЭреЗ рдЙрди рдЬрдЯрд┐рд▓ рдХреЗ рдмрд╛рд░реЗ рдореЗрдВ рдпрд╛рдж рджрд┐рд▓рд╛рдпрд╛ рдЬреЛ рд╣рдо рд╡реЗрдм рдЕрдиреБрдкреНрд░рдпреЛрдЧреЛрдВ рдХреЗ рдкрд░реАрдХреНрд╖рдг рдХреЛ рд╕реНрд╡рдЪрд╛рд▓рд┐рдд рдХрд░рдиреЗ рдХреЗ рд▓рд┐рдП рдЙрдкрдпреЛрдЧ рдХрд░рддреЗ рд╣реИрдВред

рддреЛ, рдкрд░реАрдХреНрд╖рдг RSpec + Watir WebDriver рдмрдВрдбрд▓ (рд░реЗрд▓ рдЕрдиреБрдкреНрд░рдпреЛрдЧреЛрдВ рдХреЗ рд▓рд┐рдП рдХреИрдкрд┐рдмрд╛рд░рд╛ рдХреА рдУрд░ рджреЗрдЦрдирд╛ рдЕрдзрд┐рдХ рдЙрдкрдпреБрдХреНрдд рд╣реЛрдЧрд╛) рдкрд░ рдЖрдзрд╛рд░рд┐рдд рд╣реИрдВред рдмреБрдВрдбрд▓рд░ рдФрд░ рд╕реНрдерд╛рдиреАрдп рд╡реЗрдмрдбреНрд░рд╛рдЗрд╡рд░реНрд╕ рдХреЗ рд╕рдорд░реНрдерди рдХреЗ рд╕рд╛рде, рдкрд░реАрдХреНрд╖рдХ рдХреЗ рдХрд╛рд░реНрдпрд╕реНрдерд▓ рдкрд░ рдПрдХ рдЖрд╕рд╛рди рдЗрдВрд╕реНрдЯреЙрд▓реЗрд╢рди рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИ (рд░реВрдмреА 1.9, рд░реВрдмреАрдЬ, рдмреБрдВрдбрд▓рд░ bundle install , рдФрд░ bundle install рд░рди рдХрд░реЗрдВ)ред рд░рддреНрди рд╕реНрд░реЛрдд:

 source :rubygems gem "watir-webdriver", "~>0.6.1" gem "rspec-core", "~> 2.0" gem "rspec-expectations", "~> 2.0" gem "rr", "~> 1.0" gem "ci_reporter", "~> 1.7" 


Ci_reporter рд░рддреНрди рдХреЗ рдХрд╛рд░рдг, рдЬреЗрдирдХрд┐рдВрд╕ CI рдХреЗ рд╕рд╛рде рдПрдХреАрдХрд░рдг рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИ, рдФрд░ рд╕рдорд╛рдирд╛рдВрддрд░_tests рдордгрд┐ рдФрд░ рд╕реЗрд▓реЗрдирд┐рдпрдо рдЧреНрд░рд┐рдб рдХреЗ рдХрд╛рд░рдг, рдкрд░реАрдХреНрд╖рдгреЛрдВ рдХрд╛ рд╕рдорд╛рдирд╛рдВрддрд░рдХрд░рдг рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИ (рдЖрдЬ рд╕рдорд╛рдирд╛рдВрддрд░ рдкрд░реАрдХреНрд╖рдгреЛрдВ рдХрд╛ рдЙрдкрдпреЛрдЧ рдЙрддреНрдкрд╛рджрди рдореЗрдВ рдирд╣реАрдВ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИ)ред

рдпрд╣рд╛рдБ рдПрдХ рдкрд░реАрдХреНрд╖рдг рдЙрджрд╛рд╣рд░рдг рд╣реИ:

 describe "FirstSite" do before(:all) do site! "first" end # returns true on success; otherwise false. def login_as(data) browser.rel_goto "/" browser.title.should include 'example.com' browser.text_field(:id => 'login').set data[:login] browser.text_field(:id => 'password').set data[:password] submit_button = browser.button(:id => 'submit') submit_button.click browser.url =~ %r{/welcome$} end def logout browser.rel_goto "/" browser.button(:id => 'logout').click end describe :login do auth = fixture :auth describe :successful do after(:each) do logout end auth.keys.select { |key| key.kind_of?(Symbol) }.each do |key| next if key == :wrong it "Logging in as #{key} should be OK" do login_as(auth[key]).should be_true end end end describe :wrong do it "Logging in with wrong credentials should fail" do login_as(auth[:wrong]).should_not be_true end end end end 


рдкрд░реАрдХреНрд╖рдг рдФрд░ рдкрд░реАрдХреНрд╖рдг рд╡рд┐рдзрд┐ рдХреЗ рддрд╣рдд рд╕рд╛рдЗрдЯреЛрдВ рдХреЛ рд╕реЗрдЯрд┐рдВрдЧреНрд╕ рдореЗрдВ рдкрд░рд┐рднрд╛рд╖рд┐рдд рдХрд┐рдпрд╛ рдЧрдпрд╛ рд╣реИред
 --- production: sites: first: "http://staging.example.com" webdriver: hub: "http://192.168.13.6:8888/wd/hub" test: sites: first: "http://staging.example.com" webdriver: hub: "http://192.168.13.6:8888/wd/hub" development: sites: first: "localhost:5678" webdriver: local: true 


рд╡рд╛реЕрдЯрд░_рдПрдПрдирд╡реА рдФрд░ рд╡рд╛реЕрдЯрд░_рдмреЛрд▓реНрд╡реЗрд╕рд░ рдХреЗ рдкрд░реНрдпрд╛рд╡рд░рдг рдЪрд░ рдХреЛ рд╕реЗрдЯ рдХрд░рдХреЗ, рд╣рдо рдПрдХ рд╡рд┐рд╢реЗрд╖ рдЯреЗрд╕реНрдЯ рд░рди рдХреЗ рд▓рд┐рдП рдкрд░реАрдХреНрд╖рдг рд╡рд╛рддрд╛рд╡рд░рдг рдХреЛ рдкрд░рд┐рднрд╛рд╖рд┐рдд рдХрд░рддреЗ рд╣реИрдВред

рд╕рднреА рдЕрдиреБрдХреВрд▓рди рджреЛ рдлрд╝рд╛рдЗрд▓реЛрдВ рджреНрд╡рд╛рд░рд╛ рдХрд┐рдпрд╛ рдЬрд╛рддрд╛ рд╣реИред .rspec:

--format nested
--color
--require ./spec_helper.rb


spec_helper.rb:

 require "rubygems" require "bundler/setup" require "yaml" require "watir-webdriver" require "rspec/core" $LOAD_PATH << File.join(File.dirname(__FILE__), 'lib') #   . module RegruHelpers include Selenium def site!(site) RSpec.configuration.site = RSpec.configuration.sites[site] end def fixture(name) RegruHelpers.symbolized YAML.load_file( File.join(File.dirname(__FILE__), 'fixtures', "#{name}.yml") ) end def screenshot!(text = '') directory = File.join(File.dirname(__FILE__), 'spec', 'screenshots') file_name = "#{example.metadata.full_description}-#{text}.png" file_path =File.join(directory, file_name) browser.screenshot.save file_path end def save_html!(text = '') directory = File.join(File.dirname(__FILE__), 'spec', 'html') file_name = "#{example.metadata.full_description}-#{text}.html" file_path =File.join(directory, file_name) File.open(file_path, "w") do |f| f.print browser.html end end Watir::Browser.class_eval do def rel_goto url goto "#{RSpec.configuration.site}#{url}" end end def browser RSpec.configuration.browser ||= begin local = RSpec.configuration.watir_settings[:webdriver][:local] if local Watir::Browser.new(RSpec.configuration.browser_type) else capabilities = WebDriver::Remote::Capabilities.send( RSpec.configuration.browser_type, :javascript_enabled => true ) Watir::Browser.new( :remote, :url => RSpec.configuration.watir_settings[:webdriver][:hub], :desired_capabilities => capabilities ) end end end end RSpec.configure do |config| config.mock_with :rr config.add_setting :watir_settings config.add_setting :browser_type config.add_setting :sites config.add_setting :site config.add_setting :browser config.include RegruHelpers config.extend RegruHelpers environment = ENV['WATIR_ENV'] || 'test' browser_type = (ENV['WATIR_BROWSER'] || 'chrome').to_sym watir_settings = RegruHelpers.symbolized YAML.load_file(File.join(File.dirname(__FILE__), 'settings.yml'))[environment] RSpec.configuration.watir_settings = watir_settings RSpec.configuration.sites = watir_settings[:sites] RSpec.configuration.site = watir_settings[:sites]['first'] RSpec.configuration.browser_type = browser_type config.after(:suite) do unless RSpec.configuration.browser.nil? RSpec.configuration.browser.close RSpec.configuration.browser = nil end end config.after(:each) do if example.exception screenshot!('after') save_html!('after') end end def self.symbolized(hash) r = hash.dup hash.each_pair do |k, v| if k.kind_of?(String) if v.kind_of?(Hash) v = symbolized v end r[k.to_sym] = v end end r end end 

Source: https://habr.com/ru/post/In153049/


All Articles