selenium-client 1.2.10 → 1.2.11

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.markdown CHANGED
@@ -42,6 +42,8 @@ Features
42
42
  * `click 'the_button_id', :wait_for => :no_text, :text => 'Disappearing Text'`
43
43
  * `click 'the_button_id', :wait_for => :no_text, :element => 'notification_box', :text => 'Disappearing Text'`
44
44
  * `click 'the_button_id', :wait_for => :effects`
45
+ * `click 'the_button_id', :wait_for => :value, :element => 'a_locator', :value => 'some value'`
46
+ * `click 'the_button_id', :wait_for => :no_value, :element => 'a_locator', :value => 'some value' # will wait for the field value of 'a_locator' to not be 'some value'`
45
47
  * `click 'the_button_id', :wait_for => :condition, :javascript => "some arbitrary javascript expression"`
46
48
 
47
49
  Check out the `click`, `go_back` and `wait_for` methods of the [Idiomatic Module](http://selenium-client.rubyforge.org/classes/Selenium/Client/Idiomatic.html)
@@ -76,19 +78,26 @@ Plain API
76
78
  # Sample Ruby script using the Selenium client API
77
79
  #
78
80
  require "rubygems"
79
- gem "selenium-client", ">=1.2.9"
81
+ gem "selenium-client", ">=1.2.11"
80
82
  require "selenium/client"
81
-
83
+
82
84
  begin
83
- @browser = Selenium::Client::Driver.new("localhost", 4444, "*firefox", "http://www.google.com", 10000);
85
+ @browser = Selenium::Client::Driver.new \
86
+ :host => "localhost",
87
+ :port => 4444,
88
+ :browser => "*firefox",
89
+ :url => "http://www.google.com",
90
+ :timeout_in_second => 60
91
+
84
92
  @browser.start_new_browser_session
85
93
  @browser.open "/"
86
- @browser.type "q", "Selenium"
94
+ @browser.type "q", "Selenium seleniumhq.org"
87
95
  @browser.click "btnG", :wait_for => :page
88
- puts @browser.text?("selenium.openqa.org")
96
+ puts @browser.text?("seleniumhq.org")
89
97
  ensure
90
98
  @browser.close_current_browser_session
91
99
  end
100
+
92
101
 
93
102
  Writing Tests
94
103
  =============
@@ -102,70 +111,83 @@ Writing Tests
102
111
  #
103
112
  require "test/unit"
104
113
  require "rubygems"
105
- gem "selenium-client", ">=1.2.9"
114
+ gem "selenium-client", ">=1.2.11"
106
115
  require "selenium/client"
107
-
116
+
108
117
  class ExampleTest < Test::Unit::TestCase
109
118
  attr_reader :browser
110
-
119
+
111
120
  def setup
112
- @browser = Selenium::Client::Driver.new "localhost", 4444, "*firefox", "http://www.google.com", 10000
121
+ @browser = Selenium::Client::Driver.new \
122
+ :host => "localhost",
123
+ :port => 4444,
124
+ :browser => "*firefox",
125
+ :url => "http://www.google.com",
126
+ :timeout_in_second => 60
127
+
113
128
  browser.start_new_browser_session
114
129
  end
115
-
130
+
116
131
  def teardown
117
132
  browser.close_current_browser_session
118
133
  end
119
-
134
+
120
135
  def test_page_search
121
136
  browser.open "/"
122
137
  assert_equal "Google", browser.title
123
- browser.type "q", "Selenium"
138
+ browser.type "q", "Selenium seleniumhq"
124
139
  browser.click "btnG", :wait_for => :page
125
- assert_equal "Selenium - Google Search", browser.title
126
- assert_equal "Selenium", browser.field("q")
127
- assert browser.text?("selenium.openqa.org")
140
+ assert_equal "Selenium seleniumhq - Google Search", browser.title
141
+ assert_equal "Selenium seleniumhq", browser.field("q")
142
+ assert browser.text?("seleniumhq.org")
128
143
  assert browser.element?("link=Cached")
129
144
  end
130
-
145
+
131
146
  end
132
147
 
133
148
  If BDD is more your style, here is how you can achieve the same thing using RSpec:
134
149
 
135
150
  require 'rubygems'
136
- gem "rspec", "=1.1.11"
137
- gem "selenium-client", ">=1.2.9"
151
+ gem "rspec", "=1.1.12"
152
+ gem "selenium-client", ">=1.2.11"
138
153
  require "selenium/client"
139
154
  require "selenium/rspec/spec_helper"
140
-
155
+
141
156
  describe "Google Search" do
142
157
  attr_reader :selenium_driver
143
158
  alias :page :selenium_driver
144
-
145
- before(:all) do
146
- @selenium_driver = Selenium::Client::Driver.new "localhost", 4444, "*firefox", "http://www.google.com", 10000
147
- end
148
-
149
- before(:each) do
150
- selenium_driver.start_new_browser_session
151
- end
152
-
153
- # The system capture need to happen BEFORE closing the Selenium session
154
- append_after(:each) do
155
- @selenium_driver.close_current_browser_session
156
- end
157
-
158
- it "can find Selenium" do
159
- page.open "/"
160
- page.title.should eql("Google")
161
- page.type "q", "Selenium"
162
- page.click "btnG", :wait_for => :page
163
- page.value("q").should eql("Selenium")
164
- page.text?("selenium.openqa.org").should be_true
165
- page.title.should eql("Selenium - Google Search")
166
- end
167
-
168
- end
159
+
160
+ before(:all) do
161
+ @selenium_driver = Selenium::Client::Driver.new \
162
+ :host => "localhost",
163
+ :port => 4444,
164
+ :browser => "*firefox",
165
+ :url => "http://www.google.com",
166
+ :timeout_in_second => 60
167
+ end
168
+
169
+ before(:each) do
170
+ selenium_driver.start_new_browser_session
171
+ end
172
+
173
+ # The system capture need to happen BEFORE closing the Selenium session
174
+ append_after(:each) do
175
+ @selenium_driver.close_current_browser_session
176
+ end
177
+
178
+ it "can find Selenium" do
179
+ page.open "/"
180
+ page.title.should eql("Google")
181
+ page.type "q", "Selenium seleniumhq"
182
+ page.click "btnG", :wait_for => :page
183
+ page.value("q").should eql("Selenium seleniumhq")
184
+ page.text?("seleniumhq.org").should be_true
185
+ page.title.should eql("Selenium seleniumhq - Google Search")
186
+ page.text?("seleniumhq.org").should be_true
187
+ page.element?("link=Cached").should be_true
188
+ end
189
+
190
+ end
169
191
 
170
192
  Start/Stop a Selenium Remote Control Server
171
193
  ===========================================
@@ -0,0 +1,41 @@
1
+ require 'rubygems'
2
+ gem "rspec", "=1.1.12"
3
+ gem "selenium-client", ">=1.2.11"
4
+ require "selenium/client"
5
+ require "selenium/rspec/spec_helper"
6
+
7
+ describe "Google Search" do
8
+ attr_reader :selenium_driver
9
+ alias :page :selenium_driver
10
+
11
+ before(:all) do
12
+ @selenium_driver = Selenium::Client::Driver.new \
13
+ :host => "localhost",
14
+ :port => 4444,
15
+ :browser => "*firefox",
16
+ :url => "http://www.google.com",
17
+ :timeout_in_second => 60
18
+ end
19
+
20
+ before(:each) do
21
+ selenium_driver.start_new_browser_session
22
+ end
23
+
24
+ # The system capture need to happen BEFORE closing the Selenium session
25
+ append_after(:each) do
26
+ @selenium_driver.close_current_browser_session
27
+ end
28
+
29
+ it "can find Selenium" do
30
+ page.open "/"
31
+ page.title.should eql("Google")
32
+ page.type "q", "Selenium seleniumhq"
33
+ page.click "btnG", :wait_for => :page
34
+ page.value("q").should eql("Selenium seleniumhq")
35
+ page.text?("seleniumhq.org").should be_true
36
+ page.title.should eql("Selenium seleniumhq - Google Search")
37
+ page.text?("seleniumhq.org").should be_true
38
+ page.element?("link=Cached").should be_true
39
+ end
40
+
41
+ end
@@ -0,0 +1,25 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Sample Ruby script using the Selenium client API
4
+ #
5
+ require "rubygems"
6
+ gem "selenium-client", ">=1.2.11"
7
+ require "selenium/client"
8
+
9
+ begin
10
+ @browser = Selenium::Client::Driver.new \
11
+ :host => "localhost",
12
+ :port => 4444,
13
+ :browser => "*firefox",
14
+ :url => "http://www.google.com",
15
+ :timeout_in_second => 60
16
+
17
+ @browser.start_new_browser_session
18
+ @browser.open "/"
19
+ @browser.type "q", "Selenium seleniumhq.org"
20
+ @browser.click "btnG", :wait_for => :page
21
+ puts @browser.text?("seleniumhq.org")
22
+ ensure
23
+ @browser.close_current_browser_session
24
+ end
25
+
@@ -0,0 +1,39 @@
1
+ #!/usr/bin/env ruby
2
+ #
3
+ # Sample Test:Unit based test case using the selenium-client API
4
+ #
5
+ require "test/unit"
6
+ require "rubygems"
7
+ gem "selenium-client", ">=1.2.11"
8
+ require "selenium/client"
9
+
10
+ class ExampleTest < Test::Unit::TestCase
11
+ attr_reader :browser
12
+
13
+ def setup
14
+ @browser = Selenium::Client::Driver.new \
15
+ :host => "localhost",
16
+ :port => 4444,
17
+ :browser => "*firefox",
18
+ :url => "http://www.google.com",
19
+ :timeout_in_second => 60
20
+
21
+ browser.start_new_browser_session
22
+ end
23
+
24
+ def teardown
25
+ browser.close_current_browser_session
26
+ end
27
+
28
+ def test_page_search
29
+ browser.open "/"
30
+ assert_equal "Google", browser.title
31
+ browser.type "q", "Selenium seleniumhq"
32
+ browser.click "btnG", :wait_for => :page
33
+ assert_equal "Selenium seleniumhq - Google Search", browser.title
34
+ assert_equal "Selenium seleniumhq", browser.field("q")
35
+ assert browser.text?("seleniumhq.org")
36
+ assert browser.element?("link=Cached")
37
+ end
38
+
39
+ end
@@ -13,6 +13,8 @@ require File.expand_path(File.dirname(__FILE__) + '/command_error')
13
13
  require File.expand_path(File.dirname(__FILE__) + '/protocol_error')
14
14
  require File.expand_path(File.dirname(__FILE__) + '/client/protocol')
15
15
  require File.expand_path(File.dirname(__FILE__) + '/client/generated_driver')
16
+ require File.expand_path(File.dirname(__FILE__) + '/client/javascript_frameworks/jquery')
17
+ require File.expand_path(File.dirname(__FILE__) + '/client/javascript_frameworks/prototype')
16
18
  require File.expand_path(File.dirname(__FILE__) + '/client/extensions')
17
19
  require File.expand_path(File.dirname(__FILE__) + '/client/idiomatic')
18
20
  require File.expand_path(File.dirname(__FILE__) + '/client/base')
@@ -2,26 +2,60 @@ module Selenium
2
2
  module Client
3
3
 
4
4
  # Driver constructor and session management commands
5
- #
6
- # Original code by Aslak Hellesoy and Darren Hobbs
7
5
  module Base
8
6
  include Selenium::Client::Protocol
9
7
  include Selenium::Client::GeneratedDriver
10
8
  include Selenium::Client::Extensions
11
9
  include Selenium::Client::Idiomatic
12
10
 
13
- attr_reader :browser_string
11
+ attr_reader :host, :port, :browser_string, :browser_url,
12
+ :default_timeout_in_seconds, :default_javascript_framework
14
13
 
15
- def initialize(server_host, server_port, browser_string, browser_url, timeout_in_seconds=300)
16
- @server_host = server_host
17
- @server_port = server_port
18
- @browser_string = browser_string
19
- @browser_url = browser_url
20
- @timeout = timeout_in_seconds
14
+ #
15
+ # Create a new client driver
16
+ #
17
+ # Example:
18
+ #
19
+ # Selenium::Client::Driver.new \
20
+ # :host => "localhost",
21
+ # :port => 4444,
22
+ # :browser => "*firefox",
23
+ # :timeout_in_seconds => 10,
24
+ # :url => "http://localhost:3000",
25
+ #
26
+ # You can also set the default javascript framework used for :wait_for
27
+ # AJAX and effects semantics (:prototype is the default value):
28
+ #
29
+ # Selenium::Client::Driver.new \
30
+ # :host => "localhost",
31
+ # :port => 4444,
32
+ # :browser => "*firefox",
33
+ # :timeout_in_seconds => 10,
34
+ # :url => "http://localhost:3000",
35
+ # :javascript_framework => :jquery
36
+ #
37
+ def initialize(*args)
38
+ if args[0].kind_of?(Hash)
39
+ options = args[0]
40
+ @host = options[:host]
41
+ @port = options[:port].to_i
42
+ @browser_string = options[:browser]
43
+ @browser_url = options[:url]
44
+ @default_timeout_in_seconds = (options[:timeout_in_seconds] || 300).to_i
45
+ @default_javascript_framework = options[:javascript_framework] || :prototype
46
+ else
47
+ @host = args[0]
48
+ @port = args[1].to_i
49
+ @browser_string = args[2]
50
+ @browser_url = args[3]
51
+ @default_timeout_in_seconds = (args[4] || 300).to_i
52
+ @default_javascript_framework = :prototype
53
+ end
54
+
21
55
  @extension_js = ""
22
56
  @session_id = nil
23
57
  end
24
-
58
+
25
59
  def session_started?
26
60
  not @session_id.nil?
27
61
  end
@@ -31,7 +65,7 @@ module Selenium
31
65
  @session_id = result
32
66
  # Consistent timeout on the remote control and driver side.
33
67
  # Intuitive and this is what you want 90% of the time
34
- self.remote_control_timeout_in_seconds = @timeout
68
+ self.remote_control_timeout_in_seconds = @default_timeout_in_seconds
35
69
  end
36
70
 
37
71
  def close_current_browser_session
@@ -4,6 +4,7 @@ module Selenium
4
4
  # Client driver providing the complete API to drive a Selenium Remote Control
5
5
  class Driver
6
6
  include Selenium::Client::Base
7
+
7
8
  end
8
9
 
9
10
  end
@@ -8,45 +8,30 @@ module Selenium
8
8
  #
9
9
  # See http://davidvollbracht.com/2008/6/4/30-days-of-tech-day-3-waitforajax for
10
10
  # more background.
11
- def wait_for_ajax(timeout_in_seconds=nil)
12
- wait_for_condition "selenium.browserbot.getCurrentWindow().Ajax.activeRequestCount == 0", timeout_in_seconds
11
+ def wait_for_ajax(options={})
12
+ framework = javascript_framework_for(options[:javascript_framework] || default_javascript_framework)
13
+ wait_for_condition window_script("#{framework.ajax_request_tracker} == 0"),
14
+ options[:timeout_in_seconds]
13
15
  end
14
16
 
15
17
  # Wait for all Prototype effects to be processed (the wait in happenning browser side).
16
18
  #
17
19
  # Credits to http://github.com/brynary/webrat/tree/master
18
- def wait_for_effects(timeout_in_seconds=nil)
19
- wait_for_condition "window.Effect.Queue.size() == 0", timeout_in_seconds
20
+ def wait_for_effects(options={})
21
+ wait_for_condition window_script("Effect.Queue.size() == 0"),
22
+ options[:timeout_in_seconds]
20
23
  end
21
24
 
22
25
  # Wait for an element to be present (the wait in happenning browser side).
23
- def wait_for_element(locator, timeout_in_seconds=nil)
24
- script = <<-EOS
25
- var element;
26
- try {
27
- element = selenium.browserbot.findElement('#{locator}');
28
- } catch(e) {
29
- element = null;
30
- }
31
- element != null;
32
- EOS
33
-
34
- wait_for_condition script, timeout_in_seconds
26
+ def wait_for_element(locator, options={})
27
+ wait_for_condition find_element_script(locator, "element != null"),
28
+ options[:timeout_in_seconds]
35
29
  end
36
30
 
37
31
  # Wait for an element to NOT be present (the wait in happenning browser side).
38
- def wait_for_no_element(locator, timeout_in_seconds=nil)
39
- script = <<-EOS
40
- var element;
41
- try {
42
- element = selenium.browserbot.findElement('#{locator}');
43
- } catch(e) {
44
- element = null;
45
- }
46
- element == null;
47
- EOS
48
-
49
- wait_for_condition script, timeout_in_seconds
32
+ def wait_for_no_element(locator, options={})
33
+ wait_for_condition find_element_script(locator, "element == null"),
34
+ options[:timeout_in_seconds]
50
35
  end
51
36
 
52
37
  # Wait for some text to be present (the wait in happenning browser side).
@@ -56,31 +41,13 @@ module Selenium
56
41
  #
57
42
  # If a non nil locator is provided, the text will be
58
43
  # detected within the innerHTML of the element identified by the locator.
59
- def wait_for_text(text, locator=nil, timeout_in_seconds=nil)
60
- script = case locator
61
- when nil:
62
- <<-EOS
63
- var text;
64
- try {
65
- text = selenium.browserbot.getCurrentWindow().find('#{text}');
66
- } catch(e) {
67
- text = null;
68
- }
69
- text != null;
70
- EOS
71
- else
72
- <<-EOS
73
- var element;
74
- try {
75
- element = selenium.browserbot.getCurrentWindow().findElement('#{locator}');
76
- } catch(e) {
77
- element = null;
78
- }
79
- element != null && element.innerHTML == '#{text}'";
80
- EOS
81
- end
44
+ def wait_for_text(text, options={})
45
+ script = options[:element].nil? ?
46
+ find_text_script(text, "text != null && text != false") :
47
+ find_element_script(options[:element],
48
+ "element != null && element.innerHTML == '#{quote_escaped(text)}'")
82
49
 
83
- wait_for_condition script, timeout_in_seconds
50
+ wait_for_condition script, options[:timeout_in_seconds]
84
51
  end
85
52
 
86
53
  # Wait for some text to NOT be present (the wait in happenning browser side).
@@ -90,46 +57,71 @@ module Selenium
90
57
  #
91
58
  # If a non nil locator is provided, the text will be
92
59
  # detected within the innerHTML of the element identified by the locator.
93
- def wait_for_no_text(original_text, locator=nil, timeout_in_seconds=nil)
94
- script = case locator
95
- when nil:
96
- <<-EOS
97
- var text;
98
- try {
99
- text = selenium.browserbot.getCurrentWindow().find('#{original_text}');
100
- } catch(e) {
101
- text = false;
102
- }
103
- text == false;
104
- EOS
105
- else
106
- <<-EOS
107
- var element;
108
-
109
- try {
110
- element = selenium.browserbot.findElement('#{locator}');
111
- } catch(e) {
112
- element = null;
113
- }
114
- alert(element);
115
- element != null && element.innerHTML != '#{original_text}'";
116
- EOS
117
- end
118
- wait_for_condition script, timeout_in_seconds
60
+ def wait_for_no_text(original_text, options={})
61
+ script = options[:element].nil? ?
62
+ find_text_script(original_text, "(text == false || text == null)") :
63
+ find_element_script(options[:element],
64
+ "(element == null || element.innerHTML != '#{quote_escaped(original_text)}')")
65
+ wait_for_condition script, options[:timeout_in_seconds]
119
66
  end
120
67
 
121
68
  # Wait for a field to get a specific value (the wait in happenning browser side).
122
- def wait_for_field_value(locator, expected_value, timeout_in_seconds=nil)
123
- script = "var element;
124
- try {
125
- element = selenium.browserbot.findElement('#{locator}');
126
- } catch(e) {
127
- element = null;
128
- }
129
- element != null && element.value == '#{expected_value}'";
69
+ def wait_for_field_value(locator, expected_value, options={})
70
+ script = find_element_script(locator,
71
+ "(element != null && element.value == '#{quote_escaped(expected_value)}')")
72
+ wait_for_condition script, options[:timeout_in_seconds]
73
+ end
130
74
 
131
- wait_for_condition script, timeout_in_seconds
132
- end
75
+ def wait_for_no_field_value(locator, expected_value, options={})
76
+ script = find_element_script(locator,
77
+ "(element == null || element.value != '#{quote_escaped(expected_value)}')")
78
+ wait_for_condition script, options[:timeout_in_seconds]
79
+ end
80
+
81
+ def javascript_framework_for(framework_name)
82
+ case framework_name.to_sym
83
+ when :prototype
84
+ JavascriptFrameworks::Prototype
85
+ when :jquery
86
+ JavascriptFrameworks::JQuery
87
+ else
88
+ raise "Unsupported Javascript Framework: #{framework_name}"
89
+ end
90
+ end
91
+
92
+ def find_element_script(locator, return_value)
93
+ script = <<-EOS
94
+ var element;
95
+
96
+ try {
97
+ element = selenium.browserbot.findElement('#{quote_escaped(locator)}');
98
+ } catch(e) {
99
+ element = null;
100
+ }
101
+ #{return_value};
102
+ EOS
103
+ end
104
+
105
+ def find_text_script(text, return_value)
106
+ script = <<-EOS
107
+ var text;
108
+
109
+ try {
110
+ text = selenium.browserbot.getCurrentWindow().find('#{quote_escaped(text)}');
111
+ } catch(e) {
112
+ text = null;
113
+ }
114
+ #{return_value};
115
+ EOS
116
+ end
117
+
118
+ def window_script(expression)
119
+ "selenium.browserbot.getCurrentWindow().#{expression};"
120
+ end
121
+
122
+ def quote_escaped(a_string)
123
+ a_string.gsub(/'/, %q<\\\'>)
124
+ end
133
125
 
134
126
  end
135
127
  end