Skip to content

Instantly share code, notes, and snippets.

@purplejacket
Created January 26, 2011 22:22
Show Gist options
  • Save purplejacket/797615 to your computer and use it in GitHub Desktop.
Save purplejacket/797615 to your computer and use it in GitHub Desktop.
A Selenium "hello world" program to be run in Ruby
require File.dirname(__FILE__) + '/../test_helper'
require "selenium/client"
class HelloTest < ActiveSupport::TestCase
def setup
@verification_errors = []
@selenium = Selenium::Client::Driver.new \
:host => "localhost",
:port => 4444,
:browser => "*safari",
:url => "http://localhost:3000/",
:timeout_in_second => 60
@selenium.start
@selenium.set_context("test_untitled")
end
def test_untitled
@selenium.open "/?network_id=1"
@selenium.click "open"
@selenium.type "email", "advertiser@gmail.com"
@selenium.type "password", "my_password"
@selenium.click "submit", :wait_for => :page
begin
assert @selenium.is_text_present("Results * for selenium rc")
rescue Test::Unit::AssertionFailedError
@verification_errors << $!
end
end
def teardown
# @selenium.stop
assert_equal [], @verification_errors
end
end
# |open|/selenium/setup|
# |open|/?network_id=1|
# |click|open|
# |type|email|advertiser@gmail.com|
# |type|password|my_password|
# |clickAndWait|submit|
# |clickAndWait|view-campaign-link|
# |type|quantity-298486374|400,000|
# |click|//a[@id='update-298486374']/span|
# |pause|5000|
# |assertTextPresent|$1,196.00|
@softr8
Copy link

softr8 commented Jan 27, 2011

Try with this one:
require 'rubygems'
require 'selenium'
require 'selenium/server_manager'
require 'test/unit/testsuite'
require 'test/unit/ui/console/testrunner'

@server_flags = {
  :selenium_port => 4444,
  :timeout => 40000
}

class MinimalTest < Test::Unit::TestCase

  @webpage = nil
  def setup
    client_flags = {
      :browser => '*safari',
      :url => 'http://www.google.com'
    }
    @webpage = $selenium.open(client_flags[:browser], client_flags[:url])
  end
  def teardown
    @webpage.close
  end
  def test_google
    @webpage.text_field(:name, 'q').enter 'selenium'
    @webpage.button(:name, 'btnG').click
  end
end

class MinimalTestSuite
  def MinimalTestSuite.suite
    suite = Test::Unit::TestSuite.new('Minimal Test Suite')
    suite << MinimalTest.suite
  end
end

$selenium = Selenium::Server.new(@server_flags[:selenium_port], @server_flags[:timeout])
$selenium.start
Test::Unit::UI::Console::TestRunner.run(MinimalTestSuite)
$selenium.stop

It's a minimap setup to get everything working, install Selenium gem and you're done.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment