How to use start method of Selenium Package

Best Selenium code snippet using Selenium.start

selenium_session_spec.rb

Source:selenium_session_spec.rb Github

copy

Full Screen

1require File.expand_path("#{File.dirname(__FILE__)}/../../unit_spec_helper")2module JsTestCore3 module Resources4 describe SeleniumSession do5 attr_reader :request, :driver, :session_id, :selenium_browser_start_command6 after do7 Models::SeleniumSession.send(:instances).clear8 end9 describe "POST /selenium_sessions" do10 before do11 @session_id = FakeSeleniumDriver::SESSION_ID12 @driver = FakeSeleniumDriver.new13 stub.proxy(Selenium::Client::Driver).new do14 driver15 end16 end17 it "responds with a 200 and the session_id" do18 Models::SeleniumSession.find(session_id).should be_nil19 response = post(SeleniumSession.path("/"), {:selenium_browser_start_command => selenium_browser_start_command})20 body = "session_id=#{session_id}"21 response.should be_http(22 200,23 {'Content-Length' => body.length.to_s},24 body25 )26 end27 it "creates and starts a Models::SeleniumSession" do28 mock.proxy(Models::SeleniumSession).new({29 :selenium_host => 'localhost',30 :selenium_port => 4444,31 :selenium_browser_start_command => "*firefox",32 :spec_url => 'http://0.0.0.0:8080/specs'33 }) do |selenium_session|34 mock.strong(selenium_session).start35 end36 response = post(SeleniumSession.path("/"), {:selenium_browser_start_command => "*firefox"})37 end38 describe "when a selenium_host parameter is passed into the request" do39 it "creates and starts the Models::SeleniumSession with the given selenium_host" do40 mock.proxy(Models::SeleniumSession).new({41 :selenium_host => 'another-machine',42 :selenium_port => 4444,43 :selenium_browser_start_command => "*firefox",44 :spec_url => 'http://0.0.0.0:8080/specs'45 }) do |selenium_session|46 mock.strong(selenium_session).start47 end48 response = post(SeleniumSession.path("/"), {49 :selenium_browser_start_command => "*firefox",50 :selenium_host => "another-machine"51 })52 end53 end54 describe "when a selenium_host parameter is not passed into the request" do55 it "creates and starts the Models::SeleniumSession with localhost" do56 mock.proxy(Models::SeleniumSession).new({57 :selenium_host => 'localhost',58 :selenium_port => 4444,59 :selenium_browser_start_command => "*firefox",60 :spec_url => 'http://0.0.0.0:8080/specs'61 }) do |selenium_session|62 mock.strong(selenium_session).start63 end64 response = post(SeleniumSession.path("/"), {65 :selenium_browser_start_command => "*firefox",66 :selenium_host => ""67 })68 end69 end70 describe "when a selenium_port parameter is passed into the request" do71 it "creates and starts the Models::SeleniumSession with the given selenium_port" do72 mock.proxy(Models::SeleniumSession).new({73 :selenium_host => 'localhost',74 :selenium_port => 4000,75 :selenium_browser_start_command => "*firefox",76 :spec_url => 'http://0.0.0.0:8080/specs'77 }) do |selenium_session|78 mock.strong(selenium_session).start79 end80 response = post(SeleniumSession.path("/"), {81 :selenium_browser_start_command => "*firefox",82 :selenium_port => "4000"83 })84 end85 end86 describe "when a selenium_port parameter is not passed into the request" do87 it "creates and starts the Models::SeleniumSession on port 4444" do88 mock.proxy(Models::SeleniumSession).new({89 :selenium_host => 'localhost',90 :selenium_port => 4444,91 :selenium_browser_start_command => "*firefox",92 :spec_url => 'http://0.0.0.0:8080/specs'93 }) do |selenium_session|94 mock.strong(selenium_session).start95 end96 response = post(SeleniumSession.path("/"), {97 :selenium_browser_start_command => "*firefox",98 :selenium_port => ""99 })100 end101 end102 describe "when a spec_url is passed into the request" do103 it "creates and starts the Models::SeleniumSession with the given spec_url" do104 mock.proxy(Models::SeleniumSession).new({105 :selenium_host => 'localhost',106 :selenium_port => 4444,107 :selenium_browser_start_command => "*firefox",108 :spec_url => 'http://another-host:8080/specs/subdir'109 }) do |selenium_session|110 mock.strong(selenium_session).start111 end112 response = post(SeleniumSession.path("/"), {113 :selenium_browser_start_command => "*firefox",114 :spec_url => "http://another-host:8080/specs/subdir"115 })116 end117 end118 describe "when a spec_url is not passed into the request" do119 it "creates and starts the Models::SeleniumSession with a spec_url of http://0.0.0.0:8080/specs" do120 mock.proxy(Models::SeleniumSession).new({121 :selenium_host => 'localhost',122 :selenium_port => 4444,123 :selenium_browser_start_command => "*firefox",124 :spec_url => 'http://0.0.0.0:8080/specs'125 }) do |selenium_session|126 mock.strong(selenium_session).start127 end128 response = post(SeleniumSession.path("/"), {129 :selenium_browser_start_command => "*firefox",130 :spec_url => ""131 })132 end133 end134 end135 def self.before_with_selenium_browser_start_command(selenium_browser_start_command)136 before do137 @session_id = FakeSeleniumDriver::SESSION_ID138 @selenium_browser_start_command = selenium_browser_start_command139 end140 end141 describe "POST /selenium_sessions/firefox" do142 it "creates a selenium_session whose #driver started with '*firefox'" do143 mock.proxy(Models::SeleniumSession).new({144 :selenium_host => 'localhost',145 :selenium_port => 4444,146 :selenium_browser_start_command => "*firefox",147 :spec_url => 'http://0.0.0.0:8080/specs'148 }) do |selenium_session|149 mock.strong(selenium_session).start150 end151 response = post(SeleniumSession.path("/firefox"))152 body = "session_id=#{session_id}"153 response.should be_http(154 200,155 {'Content-Length' => body.length.to_s},156 body157 )158 end159 end160 describe "POST /selenium_sessions/iexplore" do161 it "creates a selenium_session whose #driver started with '*iexplore'" do162 mock.proxy(Models::SeleniumSession).new({163 :selenium_host => 'localhost',164 :selenium_port => 4444,165 :selenium_browser_start_command => "*iexplore",166 :spec_url => 'http://0.0.0.0:8080/specs'167 }) do |selenium_session|168 mock.strong(selenium_session).start169 end170 response = post(SeleniumSession.path("/iexplore"))171 body = "session_id=#{session_id}"172 response.should be_http(173 200,174 {'Content-Length' => body.length.to_s},175 body176 )177 end178 end179 describe "GET /sessions/:session_id" do180 context "when there is no Runner with the :session_id" do181 it "responds with a 404" do182 session_id = "invalid_session_id"183 response = get(SeleniumSession.path(session_id))184 response.body.should include("Could not find session #{session_id}")185 response.status.should == 404186 end187 end188 context "when there is a Runner with the :session_id" do189 attr_reader :driver, :session_id, :session_runner190 before do191 @driver = FakeSeleniumDriver.new192 @session_id = FakeSeleniumDriver::SESSION_ID193 stub(Selenium::Client::Driver).new('localhost', 4444, '*firefox', 'http://0.0.0.0:8080') do194 driver195 end196 post(SeleniumSession.path('firefox'))197 @session_runner = Models::SeleniumSession.find(session_id)198 session_runner.should be_running199 end200 context "when a Runner with the :session_id is running" do201 it "responds with a 200 and status=running" do202 response = get(SeleniumSession.path(session_id))203 body = "status=#{SeleniumSession::RUNNING}"204 response.should be_http(200, {'Content-Length' => body.length.to_s}, body)205 end206 end207 context "when a Runner with the :session_id has completed" do208 context "when the session has a status of 'success'" do209 before do210 session_runner.finish("")211 session_runner.should be_successful212 end213 it "responds with a 200 and status=success" do214 response = get(SeleniumSession.path(session_id))215 body = "status=#{SeleniumSession::SUCCESSFUL_COMPLETION}"216 response.should be_http(200, {'Content-Length' => body.length.to_s}, body)217 end218 end219 context "when the session has a status of 'failure'" do220 attr_reader :reason221 before do222 @reason = "Failure stuff"223 session_runner.finish(reason)224 session_runner.should be_failed225 end226 it "responds with a 200 and status=failure and reason" do227 response = get(SeleniumSession.path(session_id))228 body = "status=#{SeleniumSession::FAILURE_COMPLETION}&reason=#{reason}"229 response.should be_http(200, {'Content-Length' => body.length.to_s}, body)230 end231 end232 end233 end234 end235 describe "finish" do236 attr_reader :stdout237 before do238 @stdout = StringIO.new239 SeleniumSession.const_set(:STDOUT, stdout)240 end241 after do242 SeleniumSession.__send__(:remove_const, :STDOUT)243 end244 describe "POST /selenium_sessions/finish" do245 attr_reader :selenium_session246 context "when passed a :session_id parameter" do247 context "when :session_id does not match a registered SeleniumSession" do248 it "returns the text and writes the text to stdout" do249 Models::SeleniumSession.find(1).should be_nil250 text = "The text in the POST body"251 response = post(SeleniumSession.path("/finish", :session_id => 1), :text => text)252 response.should be_http(253 200,254 {},255 text256 )257 stdout.string.should == "#{text}\n"258 end259 end260 context "when :session_id matches a registered SeleniumSession" do261 before do262 @driver = FakeSeleniumDriver.new263 @selenium_session = Models::SeleniumSession.new(:spec_url => "http://localhost:8080/specs")264 stub(selenium_session).driver {driver}265 driver.start266 @session_id = driver.session_id267 Models::SeleniumSession.register(selenium_session)268 end269 it "finishes the SeleniumSession" do270 text = "The text in the POST body"271 mock.proxy(selenium_session).finish(text)272 selenium_session.should be_running273 response = post(SeleniumSession.path("/finish", :session_id => session_id), :text => text)274 response.should be_http(275 200,276 {},277 text278 )279 selenium_session.should_not be_running280 end281 end282 end283 context "when the session_id cookie is set" do284 context "when :session_id does not match a registered SeleniumSession" do285 it "returns the text and writes the text to stdout" do286 Models::SeleniumSession.find(1).should be_nil287 text = "The text in the POST body"288 response = post(SeleniumSession.path("/finish"), {:text => text}, {:cookie => "session_id=1"})289 response.should be_http(290 200,291 {},292 text293 )294 stdout.string.should == "#{text}\n"295 end296 end297 context "when :session_id matches a registered SeleniumSession" do298 attr_reader :selenium_session299 before do300 @driver = FakeSeleniumDriver.new301 @selenium_session = Models::SeleniumSession.new(:spec_url => "http://localhost:8080/specs")302 stub(selenium_session).driver {driver}303 driver.start304 @session_id = driver.session_id305 Models::SeleniumSession.register(selenium_session)306 end307 it "finishes the SeleniumSession" do308 text = "The text in the POST body"309 mock.proxy(selenium_session).finish(text)310 selenium_session.should be_running311 response = post(SeleniumSession.path("/finish"), {:text => text}, {:cookie => "session_id=#{session_id}"})312 response.should be_http(313 200,314 {},315 text316 )317 selenium_session.should_not be_running...

Full Screen

Full Screen

Capfile

Source:Capfile Github

copy

Full Screen

...26 27 if cloud.farms.length == 028 puts <<-EOS29 +++++++++++++++++++++++++++++++++++++++++++++++30 Error: No remote controls started!31 32 Check above output for errors. You probably hit your EC2 instance limit.33 +++++++++++++++++++++++++++++++++++++++++++++++ 34 EOS35 exit 136 elsif cloud.farms.length != remote_control_farms_count37 puts <<-EOS38 +++++++++++++++++++++++++++++++++++++++++++++++39 Warning: started #{cloud.farms.length} remote controls, requested #{remote_control_farms_count}.40 41 Check above output for errors. You probably hit your EC2 instance limit during rc:boot.42 +++++++++++++++++++++++++++++++++++++++++++++++43 EOS44 end45 ec2_client.authorize_port 2246 ec2_client.authorize_port 444447 ec2_client.authorize_port 590048 ec2_client.authorize_port 600049 boundaries = remote_control_port_range.split(/\s*-\s*/)50 Range.new(boundaries.first, boundaries.last).each do |port|51 ec2_client.authorize_port port52 end53 end54 desc "Shutdown EC2 Instance used to run Selenium Hub."55 task :shutdown do56 rc.shutdown57 hub.shutdown58 end59 desc "Start Selenium Grid Hub."60 task :start do61 hub.start62 rc.vnc.start63 TCPSocket.wait_for_service :host => cloud.hub.public_dns, :port => 4444, :timeout => launch_timeout64 rc.start65 end66 desc "Stop Selenium Grid Hub."67 task :stop do68 rc.stop69 hub.stop70 end71 desc "Display information about current Selenium Grid"72 task :info do73 template = ERB.new <<-EOS74 75EC2 Selenium Grid:76==================77 Hub: 78 ----79<% if cloud.hub %> 80 - Public DNS: <%= cloud.hub.public_dns %>81 - Private DNS: <%= cloud.hub.private_dns %>82 - Console: <%= cloud.hub.console_url %>83<% end %> 84 Remote Control Farms:85 ---------------------86<% cloud.farms.each_with_index do |farm, index| %>87 * Farm #<%= index %>88 - Public DNS: <%= farm.public_dns %>89 - Private DNS: <%= farm.private_dns %>90 - Port Range: <%= remote_control_port_range %> 91<% end %>92 EOS93 94 puts template.result(binding)95 end96 task :refresh_status do97 SeleniumGrid::AWS::Cloud.update do |cloud|98 cloud.hub.refresh_status99 end100 end101 102end103###### Hub Recipes ######104namespace :hub do105 desc "Boot a new EC2 Instance to Run Selenium Grid Hub."106 task :boot do107 SeleniumGrid::AWS::Cloud.update do |cloud|108 puts "Starting a new EC2 Instance..."109 cloud.hub = SeleniumGrid::AWS::Hub.boot_and_acquire_dns ami, :keypair => keypair_name110 puts "Started new Hub at #{cloud.hub.public_dns}"111 end112 set :cloud, SeleniumGrid::AWS::Cloud.load113 end114 desc "Shutdown EC2 Instance used to run Selenium Hub."115 task :shutdown do116 SeleniumGrid::AWS::Cloud.update do |cloud|117 puts "Shutting down EC2 Instance #{cloud.hub.public_dns}..."118 cloud.hub.shutdown119 cloud.hub = nil120 end121 set :cloud, SeleniumGrid::AWS::Cloud.load122 end123 desc "(Re)start Selenium Grid Hub."124 task :restart do125 stop rescue nil126 start127 end128 desc "Start Selenium Grid Hub."129 task :start do130 cloud.hub.run "nohup rake hub:start BACKGROUND=true", 131 :pwd => selenium_grid_path,132 :su => "grid",133 :keypair => keypair134 end135 desc("Stop Selenium Grid Hub.")136 task :stop do137 cloud.hub.run 'rake hub:stop', 138 :pwd => selenium_grid_path,139 :su => "grid",140 :keypair => keypair 141 end142 desc "Open Selenium Grid Hub Console in a browser."143 task :console do144 launcher = PLATFORM["darwin"] ? "open" : "firefox "145 system "open #{cloud.hub.console_url}"146 end147 desc "View Selenium Grid Hub logs."148 task :logs do149 cloud.hub.run 'tail -200f #{selenium_grid_path}/log/hub.log', :keypair => keypair150 end151 152end153###### Remote Control Recipes ######154namespace :rc do155 desc "Boot a new EC2 Instance to run a collection of Selenium Grid Remote Controls."156 task :boot do157 SeleniumGrid::AWS::Cloud.update do |cloud|158 puts "Starting a new EC2 Instance..."159 begin160 new_farm = SeleniumGrid::AWS::Server.boot_and_acquire_dns ami, :keypair => keypair_name161 cloud.farms << new_farm162 puts "Started new Remote Control farm at #{new_farm.public_dns}"163 rescue164 puts "Failed to boot new Remote Control farm."165 end166 end167 set :cloud, SeleniumGrid::AWS::Cloud.load168 end169 desc "Shutdown all EC2 Instances used to run Selenium Grid Remote Controls."170 task :shutdown do171 SeleniumGrid::AWS::Cloud.update do |cloud|172 cloud.farms.each do |farm|173 puts "Shutting down EC2 Instance #{farm.public_dns}..."174 farm.shutdown175 end176 cloud.farms = []177 end178 set :cloud, SeleniumGrid::AWS::Cloud.load179 end180 namespace :vnc do181 182 desc "Start VNC server on all Remote Control Farms"183 task :start do184 cloud.farms.each do |farm|185 farm.run "nohup vncserver :0", :keypair => keypair, :su => "grid"186 end187 ec2_client.authorize_port 5900188 ec2_client.authorize_port 6000189 end190 191 end192 193 desc "(Re)start Remote Controls for all farms."194 task :restart do195 stop196 start197 end198 desc "(Re)start Remote Controls for all farms."199 task :restart do200 stop rescue nil201 start202 end203 204 desc "Start Remote Controls for all farms."205 task :start do206 cloud.farms.each do |farm|207 farm.run "nohup rake rc:start_all HUB_URL=#{cloud.hub.private_url} HOST=#{farm.private_dns} PORTS=#{remote_control_port_range} BACKGROUND=true", 208 :display => ":0",209 :path => "/usr/lib/firefox", 210 :pwd => selenium_grid_path,211 :su => "grid", 212 :keypair => keypair213 end214 end215 desc "Stop Remote Controls for all farms."216 task :stop do217 cloud.farms.each do |farm|218 farm.run "rake rc:stop_all PORTS=#{remote_control_port_range}", 219 :pwd => selenium_grid_path,220 :su => "grid",221 :keypair => keypair222 end223 end224 225 desc "Visualize what is happening in Remote Control Farms with VNC"226 task :view, :roles => :rc do227 cloud.farms.each do |farm|228 if File.exists?('/Applications/Chicken of the VNC.app/Contents/MacOS/Chicken of the VNC')229 system "'/Applications/Chicken of the VNC.app/Contents/MacOS/Chicken of the VNC' --Display 0 --PasswordFile vnc_password #{farm.public_dns} &"230 else231 system "vncviewer -passwd vnc_password #{farm.public_dns}:0 &"232 end233 end234 end235end236###### Settings Recipes ######237namespace :ec2 do238 239 desc "Check EC2 related configuration."240 task :check_settings do241 puts "Validating environment variables..."242 %w(EC2_HOME EC2_PRIVATE_KEY EC2_CERT EC2_KEYPAIR).each do |variable|243 raise <<-EOS unless ENV[variable]244 245 +++++++++++++++++++++++++++++++++++++++++++++++246 You must set #{variable}. 247 248 Read http://docs.amazonwebservices.com/AWSEC2/2007-08-29/GettingStartedGuide/?ref=get-started 249 for more details.250 251 ++++++++++++++++++++++++++++++++++++++++++++++++252 EOS253 end254 %w(EC2_PRIVATE_KEY EC2_CERT EC2_KEYPAIR).each do |variable|255 unless File.exists?(ENV[variable])256 raise "#{variable} environment variable does not point to a valid path: '#{ENV[variable]}'" 257 end258 unless File.file?(ENV[variable])259 raise "#{variable} environment variable does not point to a valid file: '#{ENV[variable]}'" 260 end261 end262 puts "EC2 Version: #{ec2_client.version}"...

Full Screen

Full Screen

server.rb

Source:server.rb Github

copy

Full Screen

...7 #8 # Usage:9 #10 # server = Selenium::Server.new('/path/to/selenium-server-standalone.jar')11 # server.start12 #13 # Automatically download the given version:14 #15 # server = Selenium::Server.get '2.6.0'16 # server.start17 #18 # or the latest version:19 #20 # server = Selenium::Server.get :latest21 # server.start22 #23 # Run the server in the background:24 #25 # server = Selenium::Server.new(jar, :background => true)26 # server.start27 #28 # Add additional arguments:29 #30 # server = Selenium::Server.new(jar)31 # server << ["--additional", "args"]32 # server.start33 #34 class Server35 class Error < StandardError; end36 CL_RESET = WebDriver::Platform.windows? ? '' : "\r\e[0K"37 def self.get(required_version, opts = {})38 new(download(required_version), opts)39 end40 #41 # Download the given version of the selenium-server-standalone jar.42 #43 def self.download(required_version)44 required_version = latest if required_version == :latest45 download_file_name = "selenium-server-standalone-#{required_version}.jar"46 if File.exists? download_file_name47 return download_file_name48 end49 begin50 open(download_file_name, "wb") do |destination|51 net_http.start("selenium.googlecode.com") do |http|52 resp = http.request_get("/files/#{download_file_name}") do |response|53 total = response.content_length54 progress = 055 segment_count = 056 response.read_body do |segment|57 progress += segment.length58 segment_count += 159 if segment_count % 15 == 060 percent = (progress.to_f / total.to_f) * 10061 print "#{CL_RESET}Downloading #{download_file_name}: #{percent.to_i}% (#{progress} / #{total})"62 segment_count = 063 end64 destination.write(segment)65 end66 end67 unless resp.kind_of? Net::HTTPSuccess68 raise Error, "#{resp.code} for #{download_file_name}"69 end70 end71 end72 rescue73 FileUtils.rm download_file_name if File.exists? download_file_name74 raise75 end76 download_file_name77 end78 #79 # Ask Google Code what the latest selenium-server-standalone version is.80 #81 def self.latest82 net_http.start("code.google.com") do |http|83 resp = http.get("/p/selenium/downloads/list")84 resp.body.to_s[/selenium-server-standalone-(\d+.\d+.\d+).jar/, 1]85 end86 end87 #88 # The server port89 #90 attr_accessor :port91 #92 # The server timeout93 #94 attr_accessor :timeout95 #96 # Whether to launch the server in the background97 #98 attr_accessor :background99 #100 # Path to log file, or 'true' for stdout.101 #102 attr_accessor :log103 #104 # @param [String] jar Path to the server jar.105 # @param [Hash] opts the options to create the server process with106 #107 # @option opts [Integer] :port Port the server should listen on (default: 4444).108 # @option opts [Integer] :timeout Seconds to wait for server launch/shutdown (default: 30)109 # @option opts [true,false] :background Run the server in the background (default: false)110 # @option opts [true,false,String] :log Either a path to a log file,111 # or true to pass server log to stdout.112 # @raise [Errno::ENOENT] if the jar file does not exist113 #114 def initialize(jar, opts = {})115 raise Errno::ENOENT, jar unless File.exist?(jar)116 @jar = jar117 @host = "127.0.0.1"118 @port = opts.fetch(:port, 4444)119 @timeout = opts.fetch(:timeout, 30)120 @background = opts.fetch(:background, false)121 @log = opts[:log]122 @additional_args = []123 end124 def start125 process.start126 poll_for_service127 process.wait unless @background128 end129 def stop130 begin131 Net::HTTP.get(@host, "/selenium-server/driver/?cmd=shutDownSeleniumServer", @port)132 rescue Errno::ECONNREFUSED133 end134 stop_process if @process135 poll_for_shutdown136 @log_file.close if @log_file137 end138 def webdriver_url139 "http://#{@host}:#{@port}/wd/hub"140 end141 def <<(arg)142 if arg.kind_of?(Array)143 @additional_args += arg144 else145 @additional_args << arg.to_s146 end147 end148 private149 def self.net_http150 http_proxy = ENV['http_proxy'] || ENV['HTTP_PROXY']151 if http_proxy152 http_proxy = "http://#{http_proxy}" unless http_proxy.start_with?("http://")153 uri = URI.parse(http_proxy)154 Net::HTTP::Proxy(uri.host, uri.port)155 else156 Net::HTTP157 end158 end159 def stop_process160 return unless @process.alive?161 begin162 @process.poll_for_exit(5)163 rescue ChildProcess::TimeoutError164 @process.stop165 end166 rescue Errno::ECHILD...

Full Screen

Full Screen

selenium.rb

Source:selenium.rb Github

copy

Full Screen

...4require "webrat/selenium/selenium_session"5require "webrat/selenium/matchers"6module Webrat7 def self.with_selenium_server #:nodoc:8 start_selenium_server9 yield10 stop_selenium_server11 end12 def self.start_selenium_server #:nodoc:13 unless Webrat.configuration.selenium_server_address14 remote_control = ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", Webrat.configuration.selenium_server_port, 5)15 remote_control.jar_file = File.expand_path(__FILE__ + "../../../../vendor/selenium-server.jar")16 remote_control.start :background => true17 end18 TCPSocket.wait_for_service :host => (Webrat.configuration.selenium_server_address || "0.0.0.0"), :port => Webrat.configuration.selenium_server_port19 end20 def self.stop_selenium_server #:nodoc:21 ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0", Webrat.configuration.selenium_server_port, 5).stop unless Webrat.configuration.selenium_server_address22 end23 def self.pid_file24 if File.exists?('config.ru')25 prepare_pid_file(Dir.pwd, 'rack.pid')26 else27 prepare_pid_file("#{RAILS_ROOT}/tmp/pids", "mongrel_selenium.pid")28 end29 end30 # Start the appserver for the underlying framework to test31 #32 # Sinatra: requires a config.ru in the root of your sinatra app to use this33 # Merb: Attempts to use bin/merb and falls back to the system merb34 # Rails: Calls mongrel_rails to startup the appserver35 def self.start_app_server36 case Webrat.configuration.application_framework37 when :sinatra38 fork do39 File.open('rack.pid', 'w') { |fp| fp.write Process.pid }40 exec 'rackup', File.expand_path(Dir.pwd + '/config.ru'), '-p', Webrat.configuration.application_port.to_s41 end42 when :merb43 cmd = 'merb'44 if File.exist?('bin/merb')45 cmd = 'bin/merb'46 end47 system("#{cmd} -d -p #{Webrat.configuration.application_port} -e #{Webrat.configuration.application_environment}")48 else # rails49 system("mongrel_rails start -d --chdir='#{RAILS_ROOT}' --port=#{Webrat.configuration.application_port} --environment=#{Webrat.configuration.application_environment} --pid #{pid_file} &")50 end51 TCPSocket.wait_for_service :host => Webrat.configuration.application_address, :port => Webrat.configuration.application_port.to_i52 end53 # Stop the appserver for the underlying framework under test54 #55 # Sinatra: Reads and kills the pid from the pid file created on startup56 # Merb: Reads and kills the pid from the pid file created on startup57 # Rails: Calls mongrel_rails stop to kill the appserver58 def self.stop_app_server 59 case Webrat.configuration.application_framework60 when :sinatra61 pid = File.read('rack.pid')62 system("kill -9 #{pid}")63 FileUtils.rm_f 'rack.pid'64 when :merb65 pid = File.read("log/merb.#{Webrat.configuration.application_port}.pid")66 system("kill -9 #{pid}")67 FileUtils.rm_f "log/merb.#{Webrat.configuration.application_port}.pid"68 else # rails69 system "mongrel_rails stop -c #{RAILS_ROOT} --pid #{pid_file}"70 end71 end72 def self.prepare_pid_file(file_path, pid_file_name)73 FileUtils.mkdir_p File.expand_path(file_path)74 File.expand_path("#{file_path}/#{pid_file_name}")75 end76 # To use Webrat's Selenium support, you'll need the selenium-client gem installed.77 # Activate it with (for example, in your <tt>env.rb</tt>):78 #79 # require "webrat"80 #81 # Webrat.configure do |config|82 # config.mode = :selenium83 # end84 #85 # == Dropping down to the selenium-client API86 #87 # If you ever need to do something with Selenium not provided in the Webrat API,88 # you can always drop down to the selenium-client API using the <tt>selenium</tt> method.89 # For example:90 #91 # When "I drag the photo to the left" do92 # selenium.dragdrop("id=photo_123", "+350, 0")93 # end94 #95 # == Choosing the underlying framework to test96 #97 # Webrat assumes you're using rails by default but it can also work with sinatra98 # and merb. To take advantage of this you can use the configuration block to99 # set the application_framework variable.100 # require "webrat"101 #102 # Webrat.configure do |config|103 # config.mode = :selenium104 # config.application_port = 4567105 # config.application_framework = :sinatra # could also be :merb106 # end107 #108 # == Auto-starting of the appserver and java server109 #110 # Webrat will automatically start the Selenium Java server process and an instance111 # of Mongrel when a test is run. The Mongrel will run in the "selenium" environment112 # instead of "test", so ensure you've got that defined, and will run on port113 # Webrat.configuration.application_port.114 #115 # == Waiting116 #117 # In order to make writing Selenium tests as easy as possible, Webrat will automatically118 # wait for the correct elements to exist on the page when trying to manipulate them119 # with methods like <tt>fill_in</tt>, etc. In general, this means you should be able to write120 # your Webrat::Selenium tests ignoring the concurrency issues that can plague in-browser121 # testing, so long as you're using the Webrat API.122 module Selenium123 module Methods124 def response...

Full Screen

Full Screen

selenium_spec.rb

Source:selenium_spec.rb Github

copy

Full Screen

...3require "action_controller/integration"4require "webrat/selenium"5RAILS_ROOT = "/"6describe Webrat, "Selenium" do7 describe "start_app_server" do8 after(:each) { Webrat.configuration.application_framework = :rails }9 describe "ruby on rails" do10 it "should start the app server with correct config options" do11 pid_file = "file"12 Webrat.should_receive(:prepare_pid_file).with("#{RAILS_ROOT}/tmp/pids","mongrel_selenium.pid").and_return pid_file13 Webrat.should_receive(:system).with("mongrel_rails start -d --chdir='#{RAILS_ROOT}' --port=#{Webrat.configuration.application_port} --environment=#{Webrat.configuration.application_environment} --pid #{pid_file} &")14 TCPSocket.should_receive(:wait_for_service).with(:host => Webrat.configuration.application_address, :port => Webrat.configuration.application_port.to_i)15 Webrat.start_app_server16 end17 end18 describe "merb" do19 it "should start the app server with correct config options" do20 Webrat.configuration.application_framework = :merb21 Webrat.should_receive(:system).with("merb -d -p #{Webrat.configuration.application_port} -e #{Webrat.configuration.application_environment}")22 TCPSocket.should_receive(:wait_for_service).with(:host => Webrat.configuration.application_address, :port => Webrat.configuration.application_port.to_i)23 Webrat.start_app_server24 end25 end26 describe "sinatra" do27 it "should start the app server with correct config options" do28 rackup_file = File.expand_path(Dir.pwd + '/config.ru')29 Webrat.configuration.application_framework = :sinatra30 Webrat.should_receive(:fork)31# Kernel.should_receive(:exec).with(['rackup', rackup_file, '-p', Webrat.configuration.application_port])32 TCPSocket.should_receive(:wait_for_service).with(:host => Webrat.configuration.application_address, :port => Webrat.configuration.application_port.to_i)33 Webrat.start_app_server34 FileUtils.rm_f 'rack.pid'35 end36 end37 end38 describe "stop_app_server" do39 after(:each) { Webrat.configuration.application_framework = :rails }40 describe "ruby on rails" do41 it "should stop the app server with correct config options" do42 pid_file = RAILS_ROOT+'/tmp/pids/mongrel_selenium.pid'43 Webrat.should_receive(:system).with("mongrel_rails stop -c #{RAILS_ROOT} --pid #{pid_file}")44 Webrat.stop_app_server45 end46 end47 describe "merb" do48 it "should stop the app server with correct config options" do49 Webrat.configuration.application_framework = :merb50 File.should_receive(:read).with('log/merb.3001.pid').and_return('666')51 Webrat.should_receive(:system).with("kill -9 666")52 Webrat.stop_app_server53 end54 end55 describe "sinatra" do56 it "should stop the app server with correct config options" do57 Webrat.configuration.application_framework = :sinatra58 File.should_receive(:read).with('rack.pid').and_return('666')59 Webrat.should_receive(:system).with("kill -9 666")60 Webrat.stop_app_server61 FileUtils.rm_f 'rack.pid'62 end63 end64 end65 it 'prepare_pid_file' do66 File.should_receive(:expand_path).with('path').and_return('full_path')67 FileUtils.should_receive(:mkdir_p).with 'full_path'68 File.should_receive(:expand_path).with('path/name')69 Webrat.prepare_pid_file 'path', 'name'70 end71 describe "start_selenium_server" do72 it "should not start the local selenium server if the selenium_server_address is set" do73 Webrat.configuration.selenium_server_address = 'foo address'74 ::Selenium::RemoteControl::RemoteControl.should_not_receive(:new)75 TCPSocket.should_receive(:wait_for_service).with(:host => Webrat.configuration.selenium_server_address, :port => Webrat.configuration.selenium_server_port)76 Webrat.start_selenium_server77 end78 it "should start the local selenium server if the selenium_server_address is set" do79 remote_control = mock "selenium remote control"80 ::Selenium::RemoteControl::RemoteControl.should_receive(:new).with("0.0.0.0", Webrat.configuration.selenium_server_port, 5).and_return remote_control81 remote_control.should_receive(:jar_file=).with(/selenium-server\.jar/)82 remote_control.should_receive(:start).with(:background => true)83 TCPSocket.should_receive(:wait_for_service).with(:host => "0.0.0.0", :port => Webrat.configuration.selenium_server_port)84 Webrat.start_selenium_server85 end86 end87 describe "stop_selenium_server" do88 it "should not attempt to stop the server if the selenium_server_address is set" do89 Webrat.configuration.selenium_server_address = 'foo address'90 ::Selenium::RemoteControl::RemoteControl.should_not_receive(:new)91 Webrat.stop_selenium_server92 end93 it "should stop the local server is the selenium_server_address is nil" do94 remote_control = mock "selenium remote control"95 ::Selenium::RemoteControl::RemoteControl.should_receive(:new).with("0.0.0.0", Webrat.configuration.selenium_server_port, 5).and_return remote_control96 remote_control.should_receive(:stop)97 Webrat.stop_selenium_server98 end...

Full Screen

Full Screen

server_task.rb

Source:server_task.rb Github

copy

Full Screen

...4 module Rake5 class MissingJarFileError < StandardError6 end7 #8 # Defines rake tasks for starting, stopping and restarting the Selenium server.9 #10 # Usage:11 #12 # require 'selenium/rake/server_task'13 #14 # Selenium::Rake::ServerTask.new do |t|15 # t.jar = "/path/to/selenium-server-standalone.jar"16 # t.port = 444417 # t.opts = %w[-some options]18 # end19 #20 # Alternatively, you can have the task download a specific version of the server:21 #22 # Selenium::Rake::ServerTask.new(:server) do |t|23 # t.version = '2.6.0'24 # end25 #26 # or the latest version27 #28 # Selenium::Rake::ServerTask.new(:server) do |t|29 # t.version = :latest30 # end31 #32 #33 # Tasks defined:34 #35 # rake selenium:server:start36 # rake selenium:server:stop37 # rake selenium:server:restart38 #39 class ServerTask40 include ::Rake::DSL if defined?(::Rake::DSL)41 #42 # Path to the selenium server jar43 #44 attr_accessor :jar45 #46 # Port to use for the server.47 # Default: 444448 #49 #50 attr_accessor :port51 #52 # Timeout in seconds for the server to start/stop.53 # Default: 3054 #55 attr_accessor :timeout56 #57 # Whether we should detach from the server process.58 # Default: true59 #60 attr_accessor :background61 alias_method :background?, :background62 #63 # Configure logging. Pass a log file path or a boolean.64 # Default: true65 #66 # true - log to stdout/stderr67 # false - no logging68 # String - log to the specified file69 #70 attr_accessor :log71 #72 # Add additional options passed to the server jar.73 #74 attr_accessor :opts75 #76 # Specify the version of the server jar to download77 #78 attr_accessor :version79 def initialize(prefix = "selenium:server")80 @jar = nil81 @prefix = prefix82 @port = 444483 @timeout = 3084 @background = true85 @log = true86 @opts = []87 @version = nil88 yield self if block_given?89 if @version90 @jar = Selenium::Server.download(@version)91 end92 unless @jar93 raise MissingJarFileError, "must provide path to the selenium server jar"94 end95 @server = Selenium::Server.new(@jar, :port => @port,96 :timeout => @timeout,97 :background => @background,98 :log => @log )99 @server << @opts100 define_start_task101 define_stop_task102 define_restart_task103 end104 private105 def define_start_task106 desc "Start the Selenium server"107 task "#{@prefix}:start" do108 @server.start109 end110 end111 def define_stop_task112 desc 'Stop the Selenium server'113 task "#{@prefix}:stop" do114 @server.stop115 end116 end117 def define_restart_task118 desc 'Restart the Selenium server'119 task "#{@prefix}:restart" do120 @server.stop121 @server.start122 end123 end124 end # ServerTask125 end # Rake126end # Selenium...

Full Screen

Full Screen

selenium_rc_server.rb

Source:selenium_rc_server.rb Github

copy

Full Screen

...6 new.boot7 end8 def boot9 return if selenium_grid?10 start11 wait12 stop_at_exit13 end14 def start15 silence_stream(STDOUT) do16 remote_control.start :background => true17 end18 end19 def stop_at_exit20 at_exit do21 stop22 end23 end24 def remote_control25 return @remote_control if @remote_control26 @remote_control = ::Selenium::RemoteControl::RemoteControl.new("0.0.0.0",27 Webrat.configuration.selenium_server_port,28 :timeout => Webrat.configuration.selenium_browser_startup_timeout)29 @remote_control.jar_file = jar_path30 return @remote_control31 end32 def jar_path33 File.expand_path(__FILE__ + "../../../../../vendor/selenium-server.jar")34 end35 def selenium_grid?36 Webrat.configuration.selenium_server_address37 end38 def wait39 $stderr.print "==> Waiting for Selenium RC server on port #{Webrat.configuration.selenium_server_port}... "40 wait_for_socket41 $stderr.print "Ready!\n"42 rescue SocketError...

Full Screen

Full Screen

selenium_webdriver_phantomjs_monkey_patch.rb

Source:selenium_webdriver_phantomjs_monkey_patch.rb Github

copy

Full Screen

...16 true17 end18end19##20# Don't start new PhantomJS process21#22Selenium::WebDriver::PhantomJS::Service.class_eval do23 def start(args = [])24 require 'selenium/webdriver/common'25 if @process && @process.alive?26 raise "already started: #{@uri.inspect} #{@executable.inspect}"27 end28 puts "Starting monkey-patched PhantomJS Selenium Webdriver"29 # @process = create_process(args)30 # @process.start31 socket_poller = Selenium::WebDriver::SocketPoller.new Selenium::WebDriver::Platform.localhost, @uri.port, Selenium::WebDriver::PhantomJS::Service::START_TIMEOUT32 unless socket_poller.connected?33 raise Selenium::WebDriver::Error::WebDriverError, "unable to connect to phantomjs @ #{@uri} after #{Selenium::WebDriver::PhantomJS::Service::START_TIMEOUT} seconds"34 end35 Selenium::WebDriver::Platform.exit_hook { stop } # make sure we don't leave the server running36 end37end...

Full Screen

Full Screen

start

Using AI Code Generation

copy

Full Screen

1driver.find_element(:name, 'q').send_keys "Selenium"2driver.find_element(:name, 'q').submit3driver.find_element(:name, 'q').send_keys "Selenium"4driver.find_element(:name, 'q').submit5driver.find_element(:name, 'q').send_keys "Selenium"6driver.find_element(:name, 'q').submit7driver.find_element(:name, 'q').send_keys "Selenium"8driver.find_element(:name, 'q').submit9driver.find_element(:name, 'q').send_keys "Selenium"10driver.find_element(:name, 'q').submit11driver.find_element(:name, 'q').send_keys "Selenium"12driver.find_element(:name, 'q').submit13driver.find_element(:name, 'q').send_keys "Selenium"14driver.find_element(:name, 'q').submit

Full Screen

Full Screen

start

Using AI Code Generation

copy

Full Screen

1search_box = driver.find_element(name: 'q')2search_box = driver.find_element(name: 'q')3search_box = driver.find_element(name: 'q')4search_box = driver.find_element(name: 'q')5search_box = driver.find_element(name: 'q')6search_box = driver.find_element(name: 'q')7search_box = driver.find_element(name: 'q')

Full Screen

Full Screen

start

Using AI Code Generation

copy

Full Screen

1driver.find_element(:name, 'q').send_keys 'selenium webdriver'2driver.find_element(:name, 'btnK').click3driver.find_element(:name, 'q').send_keys 'selenium webdriver'4driver.find_element(:name, 'btnK').click5driver.find_element(:link_text, 'Selenium WebDriver').click6driver.find_element(:name, 'q').send_keys 'selenium webdriver'7driver.find_element(:name, 'btnK').click8driver.find_element(:link_text, 'Selenium WebDriver').click

Full Screen

Full Screen

start

Using AI Code Generation

copy

Full Screen

1driver.find_element(:name, 'q').send_keys "Hello World!"2driver.find_element(:name, 'btnG').click3driver.find_element(:name, 'q').send_keys "Hello World!"4driver.find_element(:name, 'btnG').click5driver.find_element(:name, 'q').send_keys "Hello World!"6driver.find_element(:name, 'btnG').click7driver.find_element(:name, 'q').send_keys "Hello World!"8driver.find_element(:name, 'btnG').click9search_box = driver.find_element(name: 'q')

Full Screen

Full Screen

start

Using AI Code Generation

copy

Full Screen

1driver.find_element(:name, 'q').send_keys 'selenium webdriver'2driver.find_element(:name, 'btnK').click3driver.find_element(:name, 'q').send_keys 'selenium webdriver'4driver.find_element(:name, 'btnK').click5driver.find_element(:link_text, 'Selenium WebDriver').click6driver.find_element(:name, 'q').send_keys 'selenium webdriver'7driver.find_element(:name, 'btnK').click8driver.find_element(:link_text, 'Selenium WebDriver').click

Full Screen

Full Screen

start

Using AI Code Generation

copy

Full Screen

1driver.find_element(:name, 'q').send_keys 'selenium webdriver'2driver.find_element(:name, 'btnK').click3driver.find_element(:name, 'q').send_keys 'selenium webdriver'4driver.find_element(:name, 'btnK').click5driver.find_element(:link_text, 'Selenium WebDriver').click6driver.find_element(:name, 'q').send_keys 'selenium webdriver'7driver.find_element(:name, 'btnK').click8driver.find_element(:link_text, 'Selenium WebDriver').click

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Selenium automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful