selenium_dsl 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,65 @@
1
+ require 'selenium/webdriver'
2
+ require 'selenium_dsl/proxy'
3
+
4
+ module SeleniumDSL::SeleniumWebdriver
5
+ class Script < SeleniumDSL::Proxy
6
+ attr_reader :driver
7
+
8
+ attr_accessor :timeout_in_seconds
9
+
10
+ def initialize driver
11
+ super driver, [:open, :select, :type]
12
+
13
+ @driver = driver
14
+
15
+ @timeout_in_seconds = 60
16
+ end
17
+
18
+ def condition block
19
+ lambda do |type, query|
20
+ element = find_element(type, query)
21
+
22
+ block.call(element)
23
+ end
24
+ end
25
+
26
+ def wait_for_condition(type, query, condition)
27
+ wait = ::Selenium::WebDriver::Wait.new(:timeout => @timeout_in_seconds) # seconds
28
+
29
+ wait.until { condition.call(type, query) }
30
+ end
31
+
32
+ def is_text_present type, query, value
33
+ !!(find_element(type, query).text =~ /#{value}/i)
34
+ end
35
+
36
+ def click type, query
37
+ find_element(type, query).click
38
+ end
39
+
40
+ def select_box type, query, value
41
+ select_box = find_element(type, query)
42
+
43
+ options = select_box.find_elements(:tag_name => "option")
44
+
45
+ option = nil
46
+
47
+ if value.kind_of? Fixnum # get n-th element
48
+ option = options[value]
49
+ else
50
+ options.each do |current_option|
51
+ if current_option.text == value
52
+ option = current_option
53
+ break
54
+ end
55
+ end
56
+ end
57
+
58
+ option.click if option
59
+ end
60
+
61
+ def input type, query, value
62
+ find_element(type, query).send_keys(value)
63
+ end
64
+ end
65
+ end
@@ -1,3 +1,3 @@
1
1
  module SeleniumDSL
2
- VERSION = "1.0.0"
2
+ VERSION = "1.1.0"
3
3
  end
@@ -0,0 +1,39 @@
1
+ require 'watir-webdriver'
2
+ require 'selenium_dsl/proxy'
3
+ require 'selenium_dsl/watir_webdriver/script'
4
+
5
+ module SeleniumDSL::WatirWebdriver
6
+ class DSL
7
+ attr_reader :script, :driver
8
+
9
+ attr_writer :timeout_in_seconds
10
+
11
+ def initialize(browser, capabilities={})
12
+ @driver = Watir::Browser.new browser.to_sym
13
+
14
+ @driver.capabilities.merge(capabilities) unless capabilities.empty?
15
+
16
+ @script = SeleniumDSL::WatirWebdriver::Script.new @driver
17
+ end
18
+
19
+ def timeout_in_seconds= timeout_in_seconds
20
+ #@driver.manage.timeouts.implicit_wait = timeout_in_seconds.to_i
21
+ end
22
+
23
+ def start
24
+ end
25
+
26
+ def stop
27
+ @driver.close
28
+ end
29
+
30
+ def watir_webdriver(&block)
31
+ @script.instance_eval(&block)
32
+
33
+ @script
34
+ end
35
+
36
+ end
37
+
38
+ end
39
+
@@ -0,0 +1,18 @@
1
+ require 'selenium_dsl/proxy'
2
+
3
+ module SeleniumDSL::WatirWebdriver
4
+ class Script < SeleniumDSL::Proxy
5
+ attr_reader :driver
6
+
7
+ attr_accessor :timeout_in_seconds
8
+
9
+ def initialize driver
10
+ super driver, [:open, :select, :type]
11
+
12
+ @driver = driver
13
+
14
+ @timeout_in_seconds = 60
15
+ end
16
+
17
+ end
18
+ end
data/selenium_dsl.gemspec CHANGED
@@ -15,7 +15,8 @@ Gem::Specification.new do |spec|
15
15
  spec.require_paths = ["lib"]
16
16
  spec.version = SeleniumDSL::VERSION
17
17
 
18
- spec.add_runtime_dependency "selenium-client", [">= 0"]
18
+ spec.add_runtime_dependency "selenium-webdriver", [">= 0"]
19
+ spec.add_runtime_dependency "selenium", [">= 0"]
19
20
  spec.add_development_dependency "gemspec_deps_gen", [">= 0"]
20
21
  spec.add_development_dependency "gemcutter", [">= 0"]
21
22
 
@@ -1,54 +1,201 @@
1
- # access_webapp_spec.rb
1
+ # encoding: utf-8
2
2
 
3
3
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
4
4
 
5
5
  describe "access wikipedia" do
6
6
 
7
- it_should_behave_like :SeleniumTest do
8
- before :all do
9
- config = load_configuration
7
+ it "should submit the request with selenium-client" do
8
+ start_selenium_server
10
9
 
11
- puts "Application: #{config['webapp_url']}"
12
- puts "Selenium: #{config['selenium_server_address']}:#{config['selenium_server_port']}"
10
+ config = load_configuration
13
11
 
14
- init_selenium(config['selenium_server_address'], config['selenium_server_port'].to_i,
15
- config['selenium_browser_key'], config['webapp_url'], config['timeout_in_seconds'])
12
+ puts "Application: #{config[:webapp_url]}"
13
+ puts "Selenium: #{config[:selenium_host]}:#{config[:selenium_port]}"
14
+
15
+ driver = SeleniumDSL::SeleniumClient::DSL.new(
16
+ config[:selenium_host], config[:selenium_port], config[:browser], config[:webapp_url])
17
+
18
+ driver.timeout_in_seconds = config['timeout_in_seconds']
19
+
20
+ driver.start
21
+
22
+ driver.selenium_client do
23
+ open "/"
24
+
25
+ is_text_present("The Free Encyclopedia").should be_true
26
+
27
+ type "searchInput", "iphone"
28
+
29
+ click "go", :wait => true
30
+
31
+ is_text_present("iPhone").should be_true
16
32
  end
17
33
 
18
- it "should submit the request" do
19
- selenium do
20
- open "/"
34
+ driver.stop
35
+
36
+ stop_selenium_server
37
+ end
38
+
39
+ it "should submit the request with selenium webdriver" do
40
+ start_selenium_server
41
+
42
+ config = load_configuration
43
+
44
+ puts "Application: #{config[:webapp_url]}"
45
+ puts "Selenium: #{config[:selenium_host]}:#{config[:selenium_port]}"
46
+
47
+ driver = SeleniumDSL::SeleniumWebdriver::DSL.new(
48
+ config[:selenium_host], config[:selenium_port], config[:browser])
49
+
50
+ driver.timeout_in_seconds = config[:timeout_in_seconds]
21
51
 
22
- is_text_present("The Free Encyclopedia").should be_true
52
+ driver.start
23
53
 
24
- type "searchInput", "iphone"
54
+ driver.selenium_webdriver do
55
+ get config[:webapp_url]
25
56
 
26
- click "go", :wait => true
57
+ expect(find_element(:id, 'www-wikipedia-org')).not_to be_nil
27
58
 
28
- is_text_present("iPhone").should be_true
29
- end
59
+ find_element(:id, 'searchInput').send_keys("iphone")
30
60
 
61
+ find_element(:name, 'go').click
62
+
63
+ disabled_condition = condition(lambda { |element| element.attribute("disabled").nil? ? true : element.attribute("disabled") })
64
+
65
+ wait_for_condition :id, 'content', disabled_condition
66
+
67
+ expect(find_element(:id, 'content').text).to match /iPhone/
31
68
  end
69
+
70
+ driver.stop
71
+
72
+ stop_selenium_server
32
73
  end
33
74
 
34
- private
75
+ it "should submit the request with watir webdriver" do
76
+ config = load_configuration
35
77
 
36
- def load_configuration
37
- config = {
38
- 'application_address' => 'www.wikipedia.org',
39
- 'application_port' => '80',
40
- 'application_name' => '',
41
- 'selenium_server_address' => 'localhost',
42
- 'selenium_server_port' => '4444',
43
- 'selenium_browser_key' => '*firefox',
44
- 'timeout_in_seconds' => 40
45
- }
78
+ driver = SeleniumDSL::WatirWebdriver::DSL.new(config[:browser])
79
+
80
+ driver.start
81
+
82
+ driver.watir_webdriver do
83
+ goto config[:webapp_url]
46
84
 
47
- webapp_url = "http://#{config['application_address']}:#{config['application_port']}/#{config['application_name']}"
85
+ expect(element(:id => 'www-wikipedia-org')).not_to be_nil
48
86
 
49
- config['webapp_url'] = webapp_url
87
+ text_field(:id => 'searchInput').set("iphone")
50
88
 
51
- config
89
+ button(:name => 'go').click
90
+
91
+ # todo: 1. how to wait?
92
+ # todo 2. how to use selenium?
93
+ # todo 3. how to use remote selenium?
94
+
95
+ #wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
96
+ #wait.until {
97
+ # driver.find_element(:id, 'content')
98
+ #}
99
+
100
+ expect(element(:id, 'content').text).to match /iPhone/
101
+
102
+ expect(url).to eq "http://en.wikipedia.org/wiki/Iphone"
103
+ end
104
+
105
+ driver.stop
106
+ end
107
+
108
+ #it "should submit the request with capybara and webkit" do
109
+ # config = load_configuration
110
+ #
111
+ # driver = SeleniumDSL::Capybara::DSL.new(
112
+ # config[:selenium_host], config[:selenium_port], config[:browser], config[:webapp_url], :webkit)
113
+ #
114
+ # driver.timeout_in_seconds = config['timeout_in_seconds']
115
+ #
116
+ # driver.start
117
+ #
118
+ # driver.capybara do
119
+ # visit '/'
120
+ #
121
+ # text.should =~ /The Free Encyclopedia/
122
+ #
123
+ # expect(find('#www-wikipedia-org')).not_to be_nil
124
+ #
125
+ # fill_in 'searchInput', :with => 'iphone.com'
126
+ # click_button ' → '
127
+ #
128
+ # ## todo: how to wait?
129
+ # ##wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
130
+ # ##wait.until {
131
+ # ## driver.find_element(:id, 'content')
132
+ # ##}
133
+ #
134
+ # expect(find('#content').text).to match /iPhone/
135
+ #
136
+ # # "http://en.wikipedia.org/wiki/Special:Search?search=iphone.com&go=Go"
137
+ # #expect(page.current_url).to eq "http://en.wikipedia.org/wiki/Iphone"
138
+ #
139
+ # # save_and_open_page
140
+ # end
141
+ #
142
+ # driver.stop
143
+ #end
144
+ #
145
+ #it "should submit the request with capybara and selenium" do
146
+ # start_selenium_server(:port => 4444)
147
+ #
148
+ # config = load_configuration
149
+ #
150
+ # driver = SeleniumDSL::Capybara::DSL.new(
151
+ # config[:selenium_host], config[:selenium_port], config[:browser], config[:webapp_url], :selenium)
152
+ #
153
+ # driver.timeout_in_seconds = config['timeout_in_seconds']
154
+ #
155
+ # driver.start
156
+ #
157
+ # driver.capybara do
158
+ # visit '/'
159
+ #
160
+ # text.should =~ /The Free Encyclopedia/
161
+ #
162
+ # expect(find('#www-wikipedia-org')).not_to be_nil
163
+ #
164
+ # fill_in 'searchInput', :with => 'iphone.com'
165
+ # click_button ' → '
166
+ #
167
+ # ## todo: how to wait?
168
+ # # todo: how to execute remote selenium?
169
+ #
170
+ # ##wait = Selenium::WebDriver::Wait.new(:timeout => 10) # seconds
171
+ # ##wait.until {
172
+ # ## driver.find_element(:id, 'content')
173
+ # ##}
174
+ #
175
+ # expect(find('#content').text).to match /iPhone/
176
+ #
177
+ # # "http://en.wikipedia.org/wiki/Special:Search?search=iphone.com&go=Go"
178
+ # #expect(page.current_url).to eq "http://en.wikipedia.org/wiki/Iphone"
179
+ #
180
+ # # save_and_open_page
181
+ # end
182
+ #
183
+ # driver.stop
184
+ #
185
+ # stop_selenium_server
186
+ #end
187
+
188
+ private
189
+
190
+ def load_configuration
191
+ {
192
+ :webapp_url => "http://www.wikipedia.org",
193
+
194
+ :selenium_host => "localhost",
195
+ :selenium_port => "4444",
196
+ :browser => 'firefox',
197
+ :timeout_in_seconds => 40
198
+ }
52
199
  end
53
200
 
54
201
  end
data/spec/spec_helper.rb CHANGED
@@ -4,10 +4,14 @@ require 'rubygems' unless Object.const_defined?(:Gem)
4
4
  $:.unshift File.dirname(__FILE__) + '/../lib'
5
5
 
6
6
  require "selenium_dsl"
7
+ require 'selenium/server'
7
8
 
8
- SeleniumDSL::Script.send :include, RSpec::Matchers
9
+ SeleniumDSL::SeleniumClient::Script.send :include, RSpec::Matchers
10
+ SeleniumDSL::SeleniumWebdriver::Script.send :include, RSpec::Matchers
11
+ SeleniumDSL::WatirWebdriver::Script.send :include, RSpec::Matchers
12
+ #SeleniumDSL::Capybara::Script.send :include, RSpec::Matchers
9
13
 
10
- RSpec.configuration.include(SeleniumDSL::DSL)
14
+ #RSpec.configuration.include(SeleniumDSL::DSL)
11
15
 
12
16
  module Selenium::Client::Base
13
17
  alias originalInitialize initialize
@@ -20,11 +24,58 @@ module Selenium::Client::Base
20
24
  end
21
25
 
22
26
  share_examples_for :SeleniumTest do
23
- before :each do
24
- start_new_session
27
+ before :all do
28
+ start_selenium_server
25
29
  end
26
30
 
27
- after :each do
28
- close_session
31
+ after :all do
32
+ stop_selenium_server
29
33
  end
30
- end
34
+
35
+ end
36
+
37
+ #def selenium_already_started?
38
+ # system "lsof -i :4444"
39
+ #end
40
+ #
41
+ #def download_selenium_server url, file_name
42
+ # system "mkdir #{File.dirname(file_name)}"
43
+ # system "wget #{url} -O #{file_name}"
44
+ #end
45
+ #
46
+ #def start_selenium_server opts = {}
47
+ # unless selenium_already_started?
48
+ # require 'selenium'
49
+ #
50
+ # version = Selenium::Starter::SELENIUM_SERVER_VERSION
51
+ # url = "http://selenium.googlecode.com/files/selenium-server-standalone-#{version}.jar"
52
+ # file_name = "#{ENV['HOME']}/.selenium/assets/selenium-#{version}/selenium-server-standalone-#{version}.jar"
53
+ #
54
+ # unless File.exist? file_name
55
+ # download_selenium_server url, file_name
56
+ # end
57
+ #
58
+ # opts[:background] = true
59
+ # @server = Selenium::Server.new(file_name, opts)
60
+ # @server.start
61
+ # end
62
+ #end
63
+
64
+ def start_selenium_server
65
+ require 'selenium'
66
+
67
+ version = Selenium::Starter::SELENIUM_SERVER_VERSION
68
+ selenium_server_standalone_jar = "#{ENV['HOME']}/.selenium/assets/selenium-#{version}/selenium-server-standalone-#{version}.jar"
69
+
70
+ if File.exist? selenium_server_standalone_jar
71
+ @server = Selenium::Server.new(selenium_server_standalone_jar, :background => true)
72
+ @server.start
73
+ else
74
+ puts "Selenium gem is not installed.Run: selenium install"
75
+ end
76
+ end
77
+
78
+ def stop_selenium_server
79
+ @server.stop if @server
80
+ @server = nil
81
+ end