bbc-selenium-webdriver 1.17.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (168) hide show
  1. data/CHANGES +473 -0
  2. data/README +31 -0
  3. data/lib/selenium-client.rb +2 -0
  4. data/lib/selenium-webdriver.rb +1 -0
  5. data/lib/selenium/client.rb +37 -0
  6. data/lib/selenium/client/base.rb +118 -0
  7. data/lib/selenium/client/driver.rb +10 -0
  8. data/lib/selenium/client/errors.rb +9 -0
  9. data/lib/selenium/client/extensions.rb +118 -0
  10. data/lib/selenium/client/idiomatic.rb +488 -0
  11. data/lib/selenium/client/javascript_expression_builder.rb +116 -0
  12. data/lib/selenium/client/javascript_frameworks/jquery.rb +13 -0
  13. data/lib/selenium/client/javascript_frameworks/prototype.rb +13 -0
  14. data/lib/selenium/client/legacy_driver.rb +1720 -0
  15. data/lib/selenium/client/protocol.rb +104 -0
  16. data/lib/selenium/client/selenium_helper.rb +34 -0
  17. data/lib/selenium/rake/server_task.rb +157 -0
  18. data/lib/selenium/server.rb +223 -0
  19. data/lib/selenium/webdriver.rb +64 -0
  20. data/lib/selenium/webdriver/android.rb +9 -0
  21. data/lib/selenium/webdriver/android/bridge.rb +51 -0
  22. data/lib/selenium/webdriver/chrome.rb +26 -0
  23. data/lib/selenium/webdriver/chrome/bridge.rb +106 -0
  24. data/lib/selenium/webdriver/chrome/profile.rb +83 -0
  25. data/lib/selenium/webdriver/chrome/service.rb +74 -0
  26. data/lib/selenium/webdriver/common.rb +30 -0
  27. data/lib/selenium/webdriver/common/action_builder.rb +352 -0
  28. data/lib/selenium/webdriver/common/alert.rb +26 -0
  29. data/lib/selenium/webdriver/common/bridge_helper.rb +67 -0
  30. data/lib/selenium/webdriver/common/core_ext/base64.rb +9 -0
  31. data/lib/selenium/webdriver/common/core_ext/dir.rb +42 -0
  32. data/lib/selenium/webdriver/common/core_ext/string.rb +5 -0
  33. data/lib/selenium/webdriver/common/driver.rb +301 -0
  34. data/lib/selenium/webdriver/common/driver_extensions/has_input_devices.rb +39 -0
  35. data/lib/selenium/webdriver/common/driver_extensions/rotatable.rb +44 -0
  36. data/lib/selenium/webdriver/common/driver_extensions/takes_screenshot.rb +43 -0
  37. data/lib/selenium/webdriver/common/driver_extensions/uploads_files.rb +46 -0
  38. data/lib/selenium/webdriver/common/element.rb +262 -0
  39. data/lib/selenium/webdriver/common/error.rb +212 -0
  40. data/lib/selenium/webdriver/common/file_reaper.rb +47 -0
  41. data/lib/selenium/webdriver/common/keyboard.rb +52 -0
  42. data/lib/selenium/webdriver/common/keys.rb +109 -0
  43. data/lib/selenium/webdriver/common/mouse.rb +65 -0
  44. data/lib/selenium/webdriver/common/navigation.rb +43 -0
  45. data/lib/selenium/webdriver/common/options.rb +128 -0
  46. data/lib/selenium/webdriver/common/platform.rb +166 -0
  47. data/lib/selenium/webdriver/common/port_prober.rb +25 -0
  48. data/lib/selenium/webdriver/common/profile_helper.rb +67 -0
  49. data/lib/selenium/webdriver/common/proxy.rb +120 -0
  50. data/lib/selenium/webdriver/common/search_context.rb +92 -0
  51. data/lib/selenium/webdriver/common/socket_poller.rb +100 -0
  52. data/lib/selenium/webdriver/common/target_locator.rb +81 -0
  53. data/lib/selenium/webdriver/common/timeouts.rb +29 -0
  54. data/lib/selenium/webdriver/common/wait.rb +60 -0
  55. data/lib/selenium/webdriver/common/window.rb +96 -0
  56. data/lib/selenium/webdriver/common/zipper.rb +82 -0
  57. data/lib/selenium/webdriver/firefox.rb +37 -0
  58. data/lib/selenium/webdriver/firefox/binary.rb +161 -0
  59. data/lib/selenium/webdriver/firefox/bridge.rb +58 -0
  60. data/lib/selenium/webdriver/firefox/extension.rb +52 -0
  61. data/lib/selenium/webdriver/firefox/extension/webdriver.xpi +0 -0
  62. data/lib/selenium/webdriver/firefox/launcher.rb +104 -0
  63. data/lib/selenium/webdriver/firefox/native/linux/amd64/x_ignore_nofocus.so +0 -0
  64. data/lib/selenium/webdriver/firefox/native/linux/x86/x_ignore_nofocus.so +0 -0
  65. data/lib/selenium/webdriver/firefox/profile.rb +315 -0
  66. data/lib/selenium/webdriver/firefox/profiles_ini.rb +62 -0
  67. data/lib/selenium/webdriver/firefox/socket_lock.rb +61 -0
  68. data/lib/selenium/webdriver/firefox/util.rb +29 -0
  69. data/lib/selenium/webdriver/ie.rb +19 -0
  70. data/lib/selenium/webdriver/ie/bridge.rb +60 -0
  71. data/lib/selenium/webdriver/ie/native/win32/IEDriver.dll +0 -0
  72. data/lib/selenium/webdriver/ie/native/x64/IEDriver.dll +0 -0
  73. data/lib/selenium/webdriver/ie/server.rb +64 -0
  74. data/lib/selenium/webdriver/iphone.rb +9 -0
  75. data/lib/selenium/webdriver/iphone/bridge.rb +50 -0
  76. data/lib/selenium/webdriver/opera.rb +24 -0
  77. data/lib/selenium/webdriver/opera/bridge.rb +113 -0
  78. data/lib/selenium/webdriver/opera/service.rb +49 -0
  79. data/lib/selenium/webdriver/remote.rb +21 -0
  80. data/lib/selenium/webdriver/remote/bridge.rb +473 -0
  81. data/lib/selenium/webdriver/remote/capabilities.rb +241 -0
  82. data/lib/selenium/webdriver/remote/commands.rb +181 -0
  83. data/lib/selenium/webdriver/remote/http/common.rb +74 -0
  84. data/lib/selenium/webdriver/remote/http/curb.rb +79 -0
  85. data/lib/selenium/webdriver/remote/http/default.rb +102 -0
  86. data/lib/selenium/webdriver/remote/http/persistent.rb +38 -0
  87. data/lib/selenium/webdriver/remote/response.rb +99 -0
  88. data/lib/selenium/webdriver/remote/server_error.rb +17 -0
  89. data/lib/selenium/webdriver/support.rb +4 -0
  90. data/lib/selenium/webdriver/support/abstract_event_listener.rb +28 -0
  91. data/lib/selenium/webdriver/support/block_event_listener.rb +17 -0
  92. data/lib/selenium/webdriver/support/event_firing_bridge.rb +112 -0
  93. data/lib/selenium/webdriver/support/select.rb +293 -0
  94. data/spec/integration/selenium/client/api/backward_compatible_api_spec.rb +22 -0
  95. data/spec/integration/selenium/client/api/browser_xpath_library_spec.rb +17 -0
  96. data/spec/integration/selenium/client/api/click_spec.rb +39 -0
  97. data/spec/integration/selenium/client/api/cookie_spec.rb +39 -0
  98. data/spec/integration/selenium/client/api/element_spec.rb +14 -0
  99. data/spec/integration/selenium/client/api/highlight_located_element_spec.rb +19 -0
  100. data/spec/integration/selenium/client/api/retrieve_last_remote_control_logs_spec.rb +33 -0
  101. data/spec/integration/selenium/client/api/screenshot_spec.rb +29 -0
  102. data/spec/integration/selenium/client/api/select_window_spec.rb +46 -0
  103. data/spec/integration/selenium/client/api/start_stop_spec.rb +9 -0
  104. data/spec/integration/selenium/client/api/wait_for_ajax_spec.rb +27 -0
  105. data/spec/integration/selenium/client/api/wait_for_element_spec.rb +56 -0
  106. data/spec/integration/selenium/client/api/wait_for_field_value_spec.rb +52 -0
  107. data/spec/integration/selenium/client/api/wait_for_text_spec.rb +98 -0
  108. data/spec/integration/selenium/client/sample-app/public/jquery-1.3.2.js +4376 -0
  109. data/spec/integration/selenium/client/sample-app/public/jquery.html +55 -0
  110. data/spec/integration/selenium/client/sample-app/public/prototype-1.6.0.3.js +4320 -0
  111. data/spec/integration/selenium/client/sample-app/public/prototype.html +59 -0
  112. data/spec/integration/selenium/client/sample-app/sample_app.rb +32 -0
  113. data/spec/integration/selenium/client/spec_helper.rb +125 -0
  114. data/spec/integration/selenium/webdriver/chrome/driver_spec.rb +28 -0
  115. data/spec/integration/selenium/webdriver/chrome/profile_spec.rb +42 -0
  116. data/spec/integration/selenium/webdriver/driver_spec.rb +253 -0
  117. data/spec/integration/selenium/webdriver/element_spec.rb +185 -0
  118. data/spec/integration/selenium/webdriver/error_spec.rb +30 -0
  119. data/spec/integration/selenium/webdriver/firefox/driver_spec.rb +21 -0
  120. data/spec/integration/selenium/webdriver/firefox/profile_spec.rb +141 -0
  121. data/spec/integration/selenium/webdriver/keyboard_spec.rb +57 -0
  122. data/spec/integration/selenium/webdriver/mouse_spec.rb +55 -0
  123. data/spec/integration/selenium/webdriver/navigation_spec.rb +44 -0
  124. data/spec/integration/selenium/webdriver/opera/driver_spec.rb +47 -0
  125. data/spec/integration/selenium/webdriver/options_spec.rb +49 -0
  126. data/spec/integration/selenium/webdriver/remote/element_spec.rb +24 -0
  127. data/spec/integration/selenium/webdriver/spec_helper.rb +44 -0
  128. data/spec/integration/selenium/webdriver/spec_support.rb +13 -0
  129. data/spec/integration/selenium/webdriver/spec_support/guards.rb +86 -0
  130. data/spec/integration/selenium/webdriver/spec_support/helpers.rb +46 -0
  131. data/spec/integration/selenium/webdriver/spec_support/jruby_test_environment.rb +29 -0
  132. data/spec/integration/selenium/webdriver/spec_support/rack_server.rb +123 -0
  133. data/spec/integration/selenium/webdriver/spec_support/test_environment.rb +199 -0
  134. data/spec/integration/selenium/webdriver/target_locator_spec.rb +170 -0
  135. data/spec/integration/selenium/webdriver/timeout_spec.rb +56 -0
  136. data/spec/integration/selenium/webdriver/window_spec.rb +56 -0
  137. data/spec/integration/selenium/webdriver/zipper_spec.rb +66 -0
  138. data/spec/unit/selenium/client/base_spec.rb +239 -0
  139. data/spec/unit/selenium/client/extensions_spec.rb +174 -0
  140. data/spec/unit/selenium/client/idiomatic_spec.rb +500 -0
  141. data/spec/unit/selenium/client/javascript_expression_builder_spec.rb +79 -0
  142. data/spec/unit/selenium/client/javascript_frameworks/jquery_spec.rb +10 -0
  143. data/spec/unit/selenium/client/javascript_frameworks/prototype_spec.rb +10 -0
  144. data/spec/unit/selenium/client/protocol_spec.rb +124 -0
  145. data/spec/unit/selenium/client/selenium_helper_spec.rb +56 -0
  146. data/spec/unit/selenium/client/spec_helper.rb +24 -0
  147. data/spec/unit/selenium/rake/task_spec.rb +79 -0
  148. data/spec/unit/selenium/server_spec.rb +131 -0
  149. data/spec/unit/selenium/webdriver/action_builder_spec.rb +90 -0
  150. data/spec/unit/selenium/webdriver/android/bridge_spec.rb +31 -0
  151. data/spec/unit/selenium/webdriver/chrome/bridge_spec.rb +94 -0
  152. data/spec/unit/selenium/webdriver/chrome/profile_spec.rb +57 -0
  153. data/spec/unit/selenium/webdriver/chrome/service_spec.rb +44 -0
  154. data/spec/unit/selenium/webdriver/error_spec.rb +41 -0
  155. data/spec/unit/selenium/webdriver/firefox/bridge_spec.rb +17 -0
  156. data/spec/unit/selenium/webdriver/iphone/bridge_spec.rb +30 -0
  157. data/spec/unit/selenium/webdriver/proxy_spec.rb +87 -0
  158. data/spec/unit/selenium/webdriver/remote/bridge_spec.rb +16 -0
  159. data/spec/unit/selenium/webdriver/remote/capabilities_spec.rb +111 -0
  160. data/spec/unit/selenium/webdriver/remote/http/common_spec.rb +24 -0
  161. data/spec/unit/selenium/webdriver/remote/http/default_spec.rb +46 -0
  162. data/spec/unit/selenium/webdriver/search_context_spec.rb +61 -0
  163. data/spec/unit/selenium/webdriver/socket_poller_spec.rb +68 -0
  164. data/spec/unit/selenium/webdriver/spec_helper.rb +6 -0
  165. data/spec/unit/selenium/webdriver/support/event_firing_spec.rb +111 -0
  166. data/spec/unit/selenium/webdriver/support/select_spec.rb +290 -0
  167. data/spec/unit/selenium/webdriver/wait_spec.rb +40 -0
  168. metadata +215 -0
@@ -0,0 +1,59 @@
1
+ <htmL>
2
+ <head>
3
+ <title>Prototype - Selenium Ruby Client Sample App</title>
4
+ <script type="application/x-javascript" src="/prototype-1.6.0.3.js"></script>
5
+ </head>
6
+ <body>
7
+ <h1 id="title">Prototype - Selenium Ruby Client Sample App</h1>
8
+
9
+ <div id="element-present-demo">
10
+ <h2>Element / Text Present</h2>
11
+ <input type="button" id="create-element-button" value="Create Mojo" />
12
+ <input type="button" id="delete-element-button" value="Destroy Mojo" />
13
+
14
+ <script type="text/javascript" language="javascript">
15
+ Event.observe(window, 'load', function() {
16
+
17
+ Event.observe('create-element-button', 'click', function (event) {
18
+ $('element-present-demo').insert('<div id="new-element">We All Need ' + 'Some Mojo</div>');
19
+ });
20
+
21
+ Event.observe('delete-element-button', 'click', function (event) {
22
+ $('new-element').remove();
23
+ });
24
+ });
25
+ </script>
26
+ </div>
27
+
28
+ <div id="regexp-support">
29
+ <div id="dangerous-characters">There are some dangerous characters here: ' / \</div>
30
+ </div>
31
+
32
+ <div id="ajax-demo">
33
+ <h2>AJAX Calculator</h2>
34
+ <form id="calculator-form" action="#" method="post" onSubmit="return false;">
35
+ <input id="calculator-expression" name="calculator-expression"/>
36
+ <input type="submit" id="calculator-button" value="Compute" />
37
+ <br/><br/>
38
+ <div>Result: <input id="calculator-result" disabled /></div>
39
+ </form>
40
+
41
+ <script type="text/javascript" language="javascript">
42
+ Event.observe(window, 'load', function() {
43
+
44
+ Event.observe('calculator-button', 'click', function (event) {
45
+ new Ajax.Request('/compute', {
46
+ method: 'post',
47
+ parameters: $('calculator-form').serialize(true),
48
+ onSuccess: function(transport) {
49
+ $('calculator-result').value = transport.responseText;
50
+ },
51
+ });
52
+ return false;
53
+ });
54
+
55
+ });
56
+ </script>
57
+ </div>
58
+
59
+ </body>
@@ -0,0 +1,32 @@
1
+ require 'rubygems'
2
+ require 'rack'
3
+ require 'webrick'
4
+
5
+ # TODO: Use Selenium::WebDriver::SpecSupport::RackServer?
6
+ class SampleApp
7
+
8
+ def self.start(host, port)
9
+ Rack::Handler::WEBrick.run(new, :Host => host, :Port => port)
10
+ end
11
+
12
+ def initialize
13
+ @public = Rack::File.new(File.expand_path("../public", __FILE__))
14
+ end
15
+
16
+ def call(env)
17
+ req = Rack::Request.new(env)
18
+
19
+ case req.path
20
+ when "/"
21
+ [200, {}, ["Selenium Ruby Client Sample Application"]]
22
+ when "/compute"
23
+ sleep 5
24
+ resp = eval(req.params['calculator-expression']).to_s
25
+ [200, {}, [resp]]
26
+ else
27
+ @public.call(env)
28
+ end
29
+ end
30
+
31
+ end
32
+
@@ -0,0 +1,125 @@
1
+ require 'rubygems'
2
+ require 'selenium/client'
3
+ require 'tmpdir'
4
+ require File.expand_path('../sample-app/sample_app', __FILE__)
5
+
6
+ # for bamboo
7
+ require 'ci/reporter/rspec'
8
+
9
+ class SeleniumClientTestEnvironment
10
+ def initialize
11
+ $stdout.sync = true
12
+ @jar = File.expand_path("../../../../../../build/java/server/test/org/openqa/selenium/server-with-tests-standalone.jar", __FILE__)
13
+
14
+ raise Errno::ENOENT, @jar unless File.exist?(@jar)
15
+ end
16
+
17
+ def run
18
+ start_server
19
+ start_example_app
20
+
21
+ self
22
+ end
23
+
24
+ def stop
25
+ stop_example_app
26
+ stop_server
27
+ end
28
+
29
+ def driver
30
+ @driver ||= new_driver_with_session
31
+ end
32
+
33
+ def in_separate_driver
34
+ @driver.stop if @driver
35
+ begin
36
+ @driver = new_driver_with_session
37
+ yield
38
+ ensure
39
+ @driver.stop
40
+ @driver = new_driver_with_session
41
+ end
42
+ end
43
+
44
+ private
45
+
46
+ def new_driver_with_session
47
+ application_host = ENV['SELENIUM_APPLICATION_HOST'] || "localhost"
48
+ application_port = ENV['SELENIUM_APPLICATION_PORT'] || "4567"
49
+ url = "http://#{application_host}:#{application_port}"
50
+
51
+ driver = Selenium::Client::Driver.new :host => (ENV['SELENIUM_RC_HOST'] || "localhost"),
52
+ :port => (ENV['SELENIUM_RC_PORT'] || 4444),
53
+ :browser => (ENV['SELENIUM_BROWSER'] || "*firefox"),
54
+ :timeout_in_seconds => (ENV['SELENIUM_RC_TIMEOUT'] || 20),
55
+ :url => url
56
+
57
+ driver.start_new_browser_session
58
+ driver.set_context self.class.name
59
+
60
+ driver
61
+ end
62
+
63
+ def start_server
64
+ @server = Selenium::Server.new(@jar, :background => true,
65
+ :timeout => 3*60,
66
+ :port => 4444,
67
+ :log => true)
68
+
69
+ @server << "-singleWindow"
70
+ @server.start
71
+ end
72
+
73
+ def stop_server
74
+ @server && @server.stop
75
+ end
76
+
77
+ def start_example_app
78
+ Thread.abort_on_exception = true
79
+ @example_app = Thread.new { SampleApp.start("127.0.0.1", 4567) }
80
+
81
+ poller = Selenium::WebDriver::SocketPoller.new("127.0.0.1", 4567, 60)
82
+ unless poller.connected?
83
+ raise "timed out waiting for SampleApp to launch"
84
+ end
85
+ end
86
+
87
+ def stop_example_app
88
+ @example_app.kill
89
+ end
90
+ end # SeleniumClientTestEnvironment
91
+
92
+
93
+ RSpec.configure do |config|
94
+
95
+ config.after(:suite) do
96
+ $selenium_client_test_environment && $selenium_client_test_environment.stop
97
+ end
98
+
99
+ def selenium_driver
100
+ test_environment.driver
101
+ end
102
+ alias :page :selenium_driver
103
+
104
+ def test_environment
105
+ $selenium_client_test_environment ||= SeleniumClientTestEnvironment.new.run
106
+ end
107
+
108
+ def in_separate_driver(&blk)
109
+ test_environment.in_separate_driver(&blk)
110
+ end
111
+
112
+ def should_timeout
113
+ begin
114
+ yield
115
+ raise "Should have timed out"
116
+ rescue Timeout::Error => e
117
+ # All good
118
+ rescue Selenium::Client::CommandError => e
119
+ raise unless e.message =~ /ed out after/
120
+ # All good
121
+ end
122
+ end
123
+
124
+ end
125
+
@@ -0,0 +1,28 @@
1
+ module Selenium
2
+ module WebDriver
3
+ module Chrome
4
+
5
+ describe Driver do
6
+ it "should accept an array of custom command line arguments" do
7
+ begin
8
+ driver = Selenium::WebDriver.for :chrome, :args => ["--user-agent=foo;bar"]
9
+ driver.navigate.to url_for("click_jacker.html")
10
+
11
+ ua = driver.execute_script "return window.navigator.userAgent"
12
+ ua.should == "foo;bar"
13
+ ensure
14
+ driver.quit if driver
15
+ end
16
+ end
17
+
18
+ it "should raise ArgumentError if :args is not an Array" do
19
+ lambda {
20
+ Selenium::WebDriver.for(:chrome, :args => "--foo")
21
+ }.should raise_error(ArgumentError)
22
+ end
23
+ end
24
+
25
+ end # Chrome
26
+ end # WebDriver
27
+ end # Selenium
28
+
@@ -0,0 +1,42 @@
1
+ module Selenium
2
+ module WebDriver
3
+ module Chrome
4
+
5
+ describe Profile do
6
+ let(:profile) { Profile.new }
7
+
8
+ it "launches Chrome with a custom profile" do
9
+ profile['autofill.disabled'] = true
10
+
11
+ begin
12
+ driver = WebDriver.for :chrome, :profile => profile
13
+ ensure
14
+ driver.quit if driver
15
+ end
16
+ end
17
+
18
+ it "should be serializable to JSON" do
19
+ profile['foo.boolean'] = true
20
+
21
+ new_profile = Profile.from_json(profile.to_json)
22
+ new_profile['foo.boolean'].should be_true
23
+ end
24
+
25
+ it "adds an extension" do
26
+ ext_path = "/some/path.crx"
27
+
28
+ File.should_receive(:file?).with(ext_path).and_return true
29
+ profile.add_extension(ext_path).should == [ext_path]
30
+ end
31
+
32
+ it "raises an error if the extension doesn't exist" do
33
+ lambda {
34
+ profile.add_extension("/not/likely/to/exist.crx")
35
+ }.should raise_error
36
+ end
37
+ end
38
+
39
+ end # Chrome
40
+ end # WebDriver
41
+ end # Selenium
42
+
@@ -0,0 +1,253 @@
1
+ require File.expand_path("../spec_helper", __FILE__)
2
+
3
+ describe "Driver" do
4
+ it "should get the page title" do
5
+ driver.navigate.to url_for("xhtmlTest.html")
6
+ driver.title.should == "XHTML Test Page"
7
+ end
8
+
9
+ it "should get the page source" do
10
+ driver.navigate.to url_for("xhtmlTest.html")
11
+ driver.page_source.should match(%r[<title>XHTML Test Page</title>]i)
12
+ end
13
+
14
+ it "should refresh the page" do
15
+ driver.navigate.to url_for("javascriptPage.html")
16
+ driver.find_element(:link_text, 'Update a div').click
17
+ driver.find_element(:id, 'dynamo').text.should == "Fish and chips!"
18
+ driver.navigate.refresh
19
+ driver.find_element(:id, 'dynamo').text.should == "What's for dinner?"
20
+ end
21
+
22
+ not_compliant_on({:browser => [:opera, :iphone]}) do
23
+ it "should save a screenshot" do
24
+ driver.navigate.to url_for("xhtmlTest.html")
25
+ path = "screenshot_tmp.png"
26
+
27
+ begin
28
+ driver.save_screenshot path
29
+ File.exist?(path).should be_true # sic
30
+ File.size(path).should > 0
31
+ ensure
32
+ File.delete(path) if File.exist?(path)
33
+ end
34
+ end
35
+
36
+ it "should return a screenshot in the specified format" do
37
+ driver.navigate.to url_for("xhtmlTest.html")
38
+
39
+ ss = driver.screenshot_as(:png)
40
+ ss.should be_kind_of(String)
41
+ ss.size.should > 0
42
+ end
43
+ end
44
+
45
+ it "raises an error when given an unknown format" do
46
+ lambda { driver.screenshot_as(:jpeg) }.should raise_error(WebDriver::Error::UnsupportedOperationError)
47
+ end
48
+
49
+ describe "one element" do
50
+ it "should find by id" do
51
+ driver.navigate.to url_for("xhtmlTest.html")
52
+ element = driver.find_element(:id, "id1")
53
+ element.should be_kind_of(WebDriver::Element)
54
+ element.text.should == "Foo"
55
+ end
56
+
57
+ it "should find by field name" do
58
+ driver.navigate.to url_for("formPage.html")
59
+ driver.find_element(:name, "x").attribute('value').should == "name"
60
+ end
61
+
62
+ it "should find by class name" do
63
+ driver.navigate.to url_for("xhtmlTest.html")
64
+ driver.find_element(:class, "header").text.should == "XHTML Might Be The Future"
65
+ end
66
+
67
+ it "should find by link text" do
68
+ driver.navigate.to url_for("xhtmlTest.html")
69
+ driver.find_element(:link, "Foo").text.should == "Foo"
70
+ end
71
+
72
+ it "should find by xpath" do
73
+ driver.navigate.to url_for("xhtmlTest.html")
74
+ driver.find_element(:xpath, "//h1").text.should == "XHTML Might Be The Future"
75
+ end
76
+
77
+ it "should find by css selector" do
78
+ driver.navigate.to url_for("xhtmlTest.html")
79
+ driver.find_element(:css, "div.content").attribute("class").should == "content"
80
+ end
81
+
82
+ it "should find by tag name" do
83
+ driver.navigate.to url_for("xhtmlTest.html")
84
+ driver.find_element(:tag_name, 'div').attribute("class").should == "navigation"
85
+ end
86
+
87
+ it "should find child element" do
88
+ driver.navigate.to url_for("nestedElements.html")
89
+
90
+ element = driver.find_element(:name, "form2")
91
+ child = element.find_element(:name, "selectomatic")
92
+
93
+ child.attribute("id").should == "2"
94
+ end
95
+
96
+ it "should find child element by tag name" do
97
+ driver.navigate.to url_for("nestedElements.html")
98
+
99
+ element = driver.find_element(:name, "form2")
100
+ child = element.find_element(:tag_name, "select")
101
+
102
+ child.attribute("id").should == "2"
103
+ end
104
+
105
+ it "should raise on nonexistant element" do
106
+ driver.navigate.to url_for("xhtmlTest.html")
107
+ lambda { driver.find_element("nonexistant") }.should raise_error
108
+ end
109
+
110
+ it "should find elements with a hash selector" do
111
+ driver.navigate.to url_for("xhtmlTest.html")
112
+ driver.find_element(:class => "header").text.should == "XHTML Might Be The Future"
113
+ end
114
+
115
+ it "should find elements with the shortcut syntax" do
116
+ driver.navigate.to url_for("xhtmlTest.html")
117
+
118
+ driver[:id1].should be_kind_of(WebDriver::Element)
119
+ driver[:xpath => "//h1"].should be_kind_of(WebDriver::Element)
120
+ end
121
+ end
122
+
123
+ describe "many elements" do
124
+ it "should find by class name" do
125
+ driver.navigate.to url_for("xhtmlTest.html")
126
+ driver.find_elements(:class, "nameC").should have(2).things
127
+ end
128
+
129
+ it "should find by css selector" do
130
+ driver.navigate.to url_for("xhtmlTest.html")
131
+ driver.find_elements(:css, 'p')
132
+ end
133
+
134
+ it "should find children by field name" do
135
+ driver.navigate.to url_for("nestedElements.html")
136
+ element = driver.find_element(:name, "form2")
137
+ children = element.find_elements(:name, "selectomatic")
138
+ children.should have(2).items
139
+ end
140
+ end
141
+
142
+ describe "execute script" do
143
+ it "should return strings" do
144
+ driver.navigate.to url_for("xhtmlTest.html")
145
+ driver.execute_script("return document.title;").should == "XHTML Test Page"
146
+ end
147
+
148
+ it "should return numbers" do
149
+ driver.navigate.to url_for("xhtmlTest.html")
150
+ driver.execute_script("return document.title.length;").should == "XHTML Test Page".length
151
+ end
152
+
153
+ it "should return elements" do
154
+ driver.navigate.to url_for("xhtmlTest.html")
155
+ element = driver.execute_script("return document.getElementById('id1');")
156
+ element.should be_kind_of(WebDriver::Element)
157
+ element.text.should == "Foo"
158
+ end
159
+
160
+ not_compliant_on :browser => [:opera, :android] do
161
+ it "should unwrap elements in deep objects" do
162
+ driver.navigate.to url_for("xhtmlTest.html")
163
+ result = driver.execute_script(<<-SCRIPT)
164
+ var e1 = document.getElementById('id1');
165
+ var body = document.body;
166
+
167
+ return {
168
+ elements: {'body' : body, other: [e1] }
169
+ };
170
+ SCRIPT
171
+
172
+ result.should be_kind_of(Hash)
173
+ result['elements']['body'].should be_kind_of(WebDriver::Element)
174
+ result['elements']['other'].first.should be_kind_of(WebDriver::Element)
175
+ end
176
+ end
177
+
178
+ it "should return booleans" do
179
+ driver.navigate.to url_for("xhtmlTest.html")
180
+ driver.execute_script("return true;").should == true
181
+ end
182
+
183
+ it "should raise if the script is bad" do
184
+ driver.navigate.to url_for("xhtmlTest.html")
185
+ lambda { driver.execute_script("return squiggle();") }.should raise_error
186
+ end
187
+
188
+ it "should return arrays" do
189
+ driver.navigate.to url_for("xhtmlTest.html")
190
+ driver.execute_script('return ["zero", "one", "two"];').should == %w[zero one two]
191
+ end
192
+
193
+ it "should be able to call functions on the page" do
194
+ driver.navigate.to url_for("javascriptPage.html")
195
+ driver.execute_script("displayMessage('I like cheese');")
196
+ driver.find_element(:id, "result").text.strip.should == "I like cheese"
197
+ end
198
+
199
+ it "should be able to pass string arguments" do
200
+ driver.navigate.to url_for("javascriptPage.html")
201
+ driver.execute_script("return arguments[0] == 'fish' ? 'fish' : 'not fish';", "fish").should == "fish"
202
+ end
203
+
204
+ it "should be able to pass boolean arguments" do
205
+ driver.navigate.to url_for("javascriptPage.html")
206
+ driver.execute_script("return arguments[0] == true;", true).should == true
207
+ end
208
+
209
+ it "should be able to pass numeric arguments" do
210
+ driver.navigate.to url_for("javascriptPage.html")
211
+ driver.execute_script("return arguments[0] == 1 ? 1 : 0;", 1).should == 1
212
+ end
213
+
214
+ it "should be able to pass element arguments" do
215
+ driver.navigate.to url_for("javascriptPage.html")
216
+ button = driver.find_element(:id, "plainButton")
217
+ driver.execute_script("arguments[0]['flibble'] = arguments[0].getAttribute('id'); return arguments[0]['flibble'];", button).should == "plainButton"
218
+ end
219
+
220
+ it "should be able to pass in multiple arguments" do
221
+ driver.navigate.to url_for("javascriptPage.html")
222
+ driver.execute_script("return arguments[0] + arguments[1];", "one", "two").should == "onetwo"
223
+ end
224
+ end
225
+
226
+ not_compliant_on :browser => [:opera, :iphone, :android] do
227
+ describe "execute async script" do
228
+ before {
229
+ driver.manage.timeouts.script_timeout = 0
230
+ driver.navigate.to url_for("ajaxy_page.html")
231
+ }
232
+
233
+ it "should be able to return arrays of primitives from async scripts" do
234
+ result = driver.execute_async_script "arguments[arguments.length - 1]([null, 123, 'abc', true, false]);"
235
+ result.should == [nil, 123, 'abc', true, false]
236
+ end
237
+
238
+ it "should be able to pass multiple arguments to async scripts" do
239
+ result = driver.execute_async_script "arguments[arguments.length - 1](arguments[0] + arguments[1]);", 1, 2
240
+ result.should == 3
241
+ end
242
+
243
+ it "times out if the callback is not invoked" do
244
+ lambda {
245
+ # Script is expected to be async and explicitly callback, so this should timeout.
246
+ driver.execute_async_script "return 1 + 2;"
247
+ }.should raise_error(Selenium::WebDriver::Error::ScriptTimeOutError)
248
+ end
249
+ end
250
+ end
251
+
252
+ end
253
+