selenium_fury 0.6.3 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,52 @@
1
+ module SeleniumFury
2
+ module SeleniumWebDriver
3
+ module PageObjectComponents
4
+
5
+ class GenericElement
6
+ include GenericElementHelpers
7
+ include ElementWaitHelpers
8
+
9
+ def initialize(locator, driver=nil, opt={})
10
+ @location = locator
11
+ @driver = driver
12
+ end
13
+
14
+ attr_accessor :location, :driver
15
+ end
16
+
17
+ class CheckboxElement < GenericElement
18
+ include CheckboxElementHelpers
19
+ include SelectableElementHelpers
20
+ end
21
+
22
+ class DropDownElement < GenericElement
23
+ include DropDownHelpers
24
+ include SelectableElementHelpers
25
+ end
26
+
27
+ class ImageElement < GenericElement
28
+ include ImageElementHelpers
29
+ end
30
+
31
+ class LinkElement < GenericElement
32
+ include LinkElementHelpers
33
+ include SelectableElementHelpers
34
+ end
35
+
36
+ class RadioButtonElement < GenericElement
37
+ include SelectableElementHelpers
38
+ end
39
+
40
+ class SubmitElement < GenericElement
41
+ include SelectableElementHelpers
42
+ end
43
+
44
+ class TextElement < GenericElement
45
+ end
46
+
47
+ class TextInputElement < GenericElement
48
+ include TextElementHelpers
49
+ end
50
+ end
51
+ end
52
+ end
@@ -0,0 +1,110 @@
1
+ module GenericElementHelpers
2
+ def el
3
+ raise "Locator at #{location} is not present" unless present?
4
+ @driver.find_element(location)
5
+ end
6
+
7
+ def present?
8
+ list.size > 0
9
+ end
10
+
11
+ def visible?
12
+ el.displayed?
13
+ end
14
+
15
+ def list
16
+ @driver.find_elements(location)
17
+ end
18
+
19
+ def value
20
+ el.attribute('value')
21
+ end
22
+
23
+ # Use any methods from WebDriverElement not present
24
+ def method_missing method_sym, *args
25
+ if el.respond_to?(method_sym)
26
+ el.send(method_sym, *args)
27
+ else
28
+ raise NoMethodError, "No such method #{method_sym} for #{self.class}"
29
+ end
30
+ end
31
+ end
32
+
33
+ module ElementWaitHelpers
34
+ def wait_present(timeout)
35
+ web_driver_wait(timeout) { present? }
36
+ end
37
+
38
+ def wait_visible(timeout)
39
+ web_driver_wait(timeout) { visible? }
40
+ end
41
+
42
+ def wait_not_present(timeout)
43
+ web_driver_wait(timeout) { !present? }
44
+ end
45
+
46
+ def wait_not_visible(timeout)
47
+ web_driver_wait(timeout) { !visible? }
48
+ end
49
+ end
50
+
51
+ module CheckboxElementHelpers
52
+ def checked(be_selected=true)
53
+ select unless be_selected == selected?
54
+ end
55
+ end
56
+
57
+
58
+ module DropDownHelpers
59
+ def selected_option
60
+ Selenium::WebDriver::Support::Select.new(el).first_selected_option.text
61
+ end
62
+
63
+ # how can be :text, :index, :value
64
+ def select_option(how=nil, what=nil)
65
+ raise "Locator at #{location} can not be interacted with" unless visible?
66
+ el.click if how.nil?
67
+ Selenium::WebDriver::Support::Select.new(el).select_by(how, what)
68
+ end
69
+ end
70
+
71
+ module ImageElementHelpers
72
+ def text
73
+ attribute('alt')
74
+ end
75
+
76
+ def source
77
+ attribute('src')
78
+ end
79
+ end
80
+
81
+ module LinkElementHelpers
82
+ def link
83
+ attribute('href')
84
+ end
85
+ end
86
+
87
+
88
+ module SelectableElementHelpers
89
+ def select
90
+ raise "Locator at #{location} can not be interacted with" unless visible?
91
+ el.click
92
+ end
93
+
94
+ def selected?
95
+ el.selected?
96
+ end
97
+ end
98
+
99
+ module TextElementHelpers
100
+ def send_keys(text)
101
+ raise "Locator at #{location} can not be interacted with" unless visible?
102
+ el.clear
103
+ el.send_keys(text)
104
+ end
105
+
106
+ def send_keys!(text)
107
+ raise "Locator at #{location} can not be interacted with" unless visible?
108
+ el.send_keys(text)
109
+ end
110
+ end
@@ -69,7 +69,71 @@ module SeleniumFury
69
69
  page_class.new(driver)
70
70
  end
71
71
  end
72
+
73
+
74
+ def generic_element(element_sym, locator, opt={})
75
+ define_method(element_sym) do
76
+ GenericElement.new(locator, driver, opt)
77
+ end
78
+ elements << element_sym unless elements.include? element_sym
79
+ end
80
+
81
+ def text_element(element_sym, locator, opt={})
82
+ define_method(element_sym) do
83
+ TextElement.new(locator, driver, opt)
84
+ end
85
+ elements << element_sym unless elements.include? element_sym
86
+ end
87
+
88
+ def link_element(element_sym, locator, opt={})
89
+ define_method(element_sym) do
90
+ LinkElement.new(locator, driver, opt)
91
+ end
92
+ elements << element_sym unless elements.include? element_sym
93
+ end
94
+
95
+ def text_input_element(element_sym, locator, opt={})
96
+ define_method(element_sym) do
97
+ TextInputElement.new(locator, driver, opt)
98
+ end
99
+ elements << element_sym unless elements.include? element_sym
100
+ end
101
+
102
+ def drop_down_element(element_sym, locator, opt={})
103
+ define_method(element_sym) do
104
+ DropDownElement.new(locator, driver, opt)
105
+ end
106
+ elements << element_sym unless elements.include? element_sym
107
+ end
108
+
109
+ def submit_element(element_sym, locator, opt={})
110
+ define_method(element_sym) do
111
+ SubmitElement.new(locator, driver, opt)
112
+ end
113
+ elements << element_sym unless elements.include? element_sym
114
+ end
115
+
116
+ def checkbox_element(element_sym, locator, opt={})
117
+ define_method(element_sym) do
118
+ CheckboxElement.new(locator, driver, opt)
119
+ end
120
+ elements << element_sym unless elements.include? element_sym
121
+ end
122
+
123
+ def image_element(element_sym, locator, opt={})
124
+ define_method(element_sym) do
125
+ ImageElement.new(locator, driver, opt)
126
+ end
127
+ elements << element_sym unless elements.include? element_sym
128
+ end
129
+
130
+ def radio_button_element(element_sym, locator, opt={})
131
+ define_method(element_sym) do
132
+ RadioButtonElement.new(locator, driver, opt)
133
+ end
134
+ elements << element_sym unless elements.include? element_sym
135
+ end
72
136
  end
73
137
  end
74
138
  end
75
- end
139
+ end
@@ -23,13 +23,17 @@ module SeleniumFury
23
23
  puts "class #{page_class}"
24
24
  page_object=page_class.new(driver)
25
25
  raise "Could not find web driver elements in #{page_class}" if page_class.elements.nil?
26
- page_class.elements.each do |web_drive_element_name|
27
- puts "\tValidating #{web_drive_element_name}"
26
+ page_class.elements.each do |web_driver_element_name|
27
+ puts "\tValidating #{web_driver_element_name}"
28
28
  begin
29
- page_object.method(web_drive_element_name).call
29
+ if page_object.send(web_driver_element_name).is_a? Selenium::WebDriver::Element
30
+ page_object.method(web_driver_element_name).call
31
+ else
32
+ raise unless page_object.send(web_driver_element_name).present?
33
+ end
30
34
  rescue
31
- puts "\t\t\tCould not find #{web_drive_element_name}"
32
- missing_elements.push(web_drive_element_name)
35
+ puts "\t\t\tCould not find #{web_driver_element_name}"
36
+ missing_elements.push(web_driver_element_name)
33
37
  end
34
38
  end
35
39
  if missing_elements.length > 0
@@ -40,7 +44,6 @@ module SeleniumFury
40
44
  end
41
45
  raise "Found Missing Elements: #{missing_elements.inspect}" if missing_elements.length > 0
42
46
  end
43
-
44
47
  end
45
48
  end
46
49
  end
@@ -1,3 +1,3 @@
1
1
  module SeleniumFury
2
- VERSION = "0.6.3"
2
+ VERSION = "1.0.0"
3
3
  end
data/lib/selenium_fury.rb CHANGED
@@ -31,6 +31,9 @@ require "selenium_fury/selenium_web_driver/page_object"
31
31
  require "selenium_fury/selenium_web_driver/page_generator"
32
32
  require "selenium_fury/selenium_web_driver/page_validator"
33
33
 
34
+ require "selenium_fury/selenium_web_driver/generic_elements/generic_element_helpers"
35
+ require "selenium_fury/selenium_web_driver/generic_elements/generic_element"
36
+
34
37
  include SeleniumFury::SeleniumWebDriver::PageGenerator
35
38
  include SeleniumFury::SeleniumWebDriver::PageValidator
36
39
 
@@ -0,0 +1,160 @@
1
+ require 'spec_helper'
2
+
3
+ describe PageObject do
4
+
5
+ let(:test_page) { TestPage.new(driver) }
6
+
7
+ before(:each) do
8
+ load "test_page/test_page.rb"
9
+ launch_web_driver TEST_PAGE_URL
10
+ end
11
+
12
+ describe SeleniumFury::SeleniumWebDriver::PageObjectComponents::GenericElement do
13
+
14
+ it "should return correct object type" do
15
+ test_page.input_checkbox_element.should be_an SeleniumFury::SeleniumWebDriver::PageObjectComponents::CheckboxElement
16
+ end
17
+
18
+ it "should return correct superclass object type" do
19
+ test_page.input_checkbox_element.should be_an SeleniumFury::SeleniumWebDriver::PageObjectComponents::GenericElement
20
+ end
21
+
22
+ it "should provide location" do
23
+ test_page.form_element.location.should == {id: 'form'}
24
+ end
25
+
26
+ it "should return a web driver element" do
27
+ test_page.form_element.el.should be_an Selenium::WebDriver::Element
28
+ end
29
+
30
+ it "should find visible elements" do
31
+ test_page.form_element.visible?.should be_true
32
+ end
33
+
34
+ it "should return the value" do
35
+ test_page.input_button_element.value.should == 'Click me'
36
+ end
37
+
38
+ it "When there is more than one element with the provided locator, it should return an array of all of them" do
39
+ test_page.listings_element.list.should be_an Array
40
+ test_page.listings_element.list[0].should be_an Selenium::WebDriver::Element
41
+ end
42
+
43
+ describe Selenium::WebDriver::Element
44
+
45
+ it "should return the correct value of a method defined in WebDriver::Element, but not GenericElement class" do
46
+ test_page.input_button_element.tag_name.should == 'input'
47
+ end
48
+
49
+ it "should throw an WebDriver Element error when using a method not defined in either WebDriver::Element or GenericElement classes" do
50
+ expect { test_page.input_checkbox.clickit_good }.to raise_error(NoMethodError)
51
+ end
52
+ end
53
+
54
+
55
+ describe SeleniumFury::SeleniumWebDriver::PageObjectComponents::CheckboxElement do
56
+
57
+ it "should check a checkbox not checked" do
58
+ test_page.input_checkbox_element.selected?.should be_false
59
+ test_page.input_checkbox_element.checked(true)
60
+ test_page.input_checkbox_element.selected?.should be_true
61
+ end
62
+
63
+ it "should leave a checkbox checked when already checked" do
64
+ test_page.input_checkbox_element.click
65
+ test_page.input_checkbox_element.selected?.should be_true
66
+ test_page.input_checkbox_element.checked(true)
67
+ test_page.input_checkbox_element.selected?.should be_true
68
+ end
69
+
70
+ it "should uncheck a checkbox when checked" do
71
+ test_page.input_checkbox_element.click
72
+ test_page.input_checkbox_element.selected?.should be_true
73
+ test_page.input_checkbox_element.checked(false)
74
+ test_page.input_checkbox_element.selected?.should be_false
75
+ end
76
+
77
+ it "should leave a checkbox alone when already checked" do
78
+ test_page.input_checkbox_element.selected?.should be_false
79
+ test_page.input_checkbox_element.checked(false)
80
+ test_page.input_checkbox_element.selected?.should be_false
81
+ end
82
+ end
83
+
84
+
85
+ describe SeleniumFury::SeleniumWebDriver::PageObjectComponents::DropDownElement do
86
+
87
+ it "should select from a dropdown by value" do
88
+ test_page.select_element.select_option(:value, 'mercedes')
89
+ test_page.select_element.selected_option.should == 'Mercedes'
90
+ end
91
+
92
+ it "should select from a dropdown by text" do
93
+ what = 'Mercedes'
94
+ test_page.select_element.select_option(:text, what)
95
+ test_page.select_element.selected_option.should == what
96
+ end
97
+
98
+ it "should select from a dropdown by index" do
99
+ test_page.select_element.select_option(:index, 3)
100
+ test_page.select_element.selected_option.should == 'Audi'
101
+ end
102
+ end
103
+
104
+
105
+ describe SeleniumFury::SeleniumWebDriver::PageObjectComponents::ImageElement do
106
+
107
+ it "should return the alternate text for an image" do
108
+ test_page.input_image_element.text.should == 'input image'
109
+ end
110
+
111
+ it 'should return the source of an image' do
112
+ test_page.input_image_element.attribute('src').include?('test_page/spacer.gif')
113
+ end
114
+ end
115
+
116
+
117
+ describe SeleniumFury::SeleniumWebDriver::PageObjectComponents::LinkElement do
118
+
119
+ it "should return the link location" do
120
+ test_page.link_element.link.should == 'http://news.ycombinator.com/'
121
+ end
122
+ end
123
+
124
+
125
+ describe SelectableElementHelpers do
126
+
127
+ it "should properly submit a form" do
128
+ text = "Hey buddy"
129
+ test_page.input_message_element.send_keys(text)
130
+ test_page.input_msg_button_element.select
131
+ test_page.message_element.text.should == text
132
+ end
133
+
134
+ it "should verify option is not selected" do
135
+ test_page.input_checkbox_element.selected?.should be_false
136
+ end
137
+
138
+ it "should verify option is selected" do
139
+ test_page.input_checkbox_element.select
140
+ test_page.input_checkbox_element.selected?.should be_true
141
+ end
142
+ end
143
+
144
+
145
+ describe TextElementHelpers do
146
+
147
+ it "should clear and write text" do
148
+ text = "Hey buddy"
149
+ test_page.textarea_element.send_keys(text)
150
+ test_page.textarea_element.value.should == text
151
+ end
152
+
153
+ it "should write text without clearing" do
154
+ existing_text = "This is a textarea field.\n "
155
+ new_text = "Hey buddy"
156
+ test_page.textarea_element.send_keys!(new_text)
157
+ test_page.textarea_element.value.should == existing_text+new_text
158
+ end
159
+ end
160
+ end
@@ -42,7 +42,7 @@ describe PageObject do
42
42
  launch_web_driver TEST_PAGE_URL
43
43
  test_page = TestPage.new(driver)
44
44
  test_page.class.elements.should_not be_nil
45
- test_page.class.elements.should have(15).elements
45
+ test_page.class.elements.should have(37).elements
46
46
  test_page.method(test_page.class.elements[0]).call.class.should == Selenium::WebDriver::Element
47
47
  end
48
48
 
Binary file
@@ -8,9 +8,12 @@
8
8
  document.getElementById('message').innerHTML = document.form.msgtext.value
9
9
  }
10
10
  </script>
11
+ <style>p {display:none}</style>
11
12
  </head>
12
13
  <body>
13
14
 
15
+ <p id="not_visible">This should not be visible by default</p>
16
+
14
17
  <div id="message"></div>
15
18
 
16
19
  <ul id="element_list">
@@ -43,7 +46,7 @@
43
46
  <input id="input_hidden" type="hidden"/>
44
47
  <br/>
45
48
  <label for="input_image">Image</label>
46
- <input id="input_image" type="image" alt="input image"/>
49
+ <input id="input_image" type="image" alt="input image" src="spacer.gif"/>
47
50
  <br/>
48
51
  <label for="input_password">Password</label>
49
52
  <input id="input_password" type="password"/>
@@ -17,6 +17,39 @@ class TestPage < PageObject
17
17
 
18
18
  element_list :listings, { css: 'li.listing' }
19
19
 
20
+
21
+ generic_element :form_element, {id: 'form'}
22
+ generic_element :input_file_element, {id: 'input_file'}
23
+ generic_element :input_hidden_element, {id: 'input_hidden'}
24
+ generic_element :message_element, {id: 'message'}
25
+ generic_element :message_text_element, {name: 'msgtext'}
26
+ generic_element :fieldset_element, {css: 'fieldset'}
27
+ generic_element :listings_element, {css: 'li.listing'}
28
+ generic_element :not_visible_element, {id: 'not_visible'}
29
+
30
+ checkbox_element :input_checkbox_element, {id: 'input_checkbox'}
31
+
32
+ drop_down_element :select_element, {id: 'select'}
33
+
34
+ image_element :input_image_element, {id: 'input_image'}
35
+
36
+ link_element :link_element, {id: 'link111111'}
37
+
38
+ radio_button_element :input_radio_element, {id: 'input_radio'}
39
+
40
+ submit_element :input_button_element, {id: 'input_button'}
41
+ submit_element :input_msg_button_element, {id: 'input_msg_button'}
42
+ submit_element :input_reset_element, {id: 'input_reset'}
43
+ submit_element :input_submit_element, {id: 'input_submit'}
44
+
45
+ text_element :input_message_label_element, {css: 'label'}
46
+
47
+ text_input_element :input_message_element, {id: 'input_message'}
48
+ text_input_element :input_password_element, {id: 'input_password'}
49
+ text_input_element :input_text_element, {id: 'input_text'}
50
+ text_input_element :textarea_element, {id: 'textarea'}
51
+
52
+
20
53
  def click_check_box
21
54
  input_checkbox.click
22
55
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: selenium_fury
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.3
4
+ version: 1.0.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-01 00:00:00.000000000 Z
12
+ date: 2013-05-17 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: selenium-webdriver
@@ -133,6 +133,8 @@ files:
133
133
  - lib/selenium_fury/common/selenium_api_chooser.rb
134
134
  - lib/selenium_fury/selenium_web_driver/create_selenium_web_driver.rb
135
135
  - lib/selenium_fury/selenium_web_driver/element_finder.rb
136
+ - lib/selenium_fury/selenium_web_driver/generic_elements/generic_element.rb
137
+ - lib/selenium_fury/selenium_web_driver/generic_elements/generic_element_helpers.rb
136
138
  - lib/selenium_fury/selenium_web_driver/page_generator.rb
137
139
  - lib/selenium_fury/selenium_web_driver/page_object.rb
138
140
  - lib/selenium_fury/selenium_web_driver/page_object_components.rb
@@ -142,10 +144,12 @@ files:
142
144
  - spec/common/page_parser_spec.rb
143
145
  - spec/common/selenium_api_chooser_spec.rb
144
146
  - spec/selenium_web_driver/element_finder_spec.rb
147
+ - spec/selenium_web_driver/generic_elements_spec.rb
145
148
  - spec/selenium_web_driver/page_generator_spec.rb
146
149
  - spec/selenium_web_driver/page_object_spec.rb
147
150
  - spec/selenium_web_driver/page_validator_spec.rb
148
151
  - spec/spec_helper.rb
152
+ - spec/test_page/Spacer.gif
149
153
  - spec/test_page/test_page.html
150
154
  - spec/test_page/test_page.rb
151
155
  - spec/test_page/test_page_custom_generator_configuration.rb
@@ -163,7 +167,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
163
167
  version: '0'
164
168
  segments:
165
169
  - 0
166
- hash: 2135389141801828284
170
+ hash: -4010786722862812326
167
171
  required_rubygems_version: !ruby/object:Gem::Requirement
168
172
  none: false
169
173
  requirements:
@@ -172,7 +176,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
172
176
  version: '0'
173
177
  segments:
174
178
  - 0
175
- hash: 2135389141801828284
179
+ hash: -4010786722862812326
176
180
  requirements: []
177
181
  rubyforge_project:
178
182
  rubygems_version: 1.8.25
@@ -189,10 +193,12 @@ test_files:
189
193
  - spec/common/page_parser_spec.rb
190
194
  - spec/common/selenium_api_chooser_spec.rb
191
195
  - spec/selenium_web_driver/element_finder_spec.rb
196
+ - spec/selenium_web_driver/generic_elements_spec.rb
192
197
  - spec/selenium_web_driver/page_generator_spec.rb
193
198
  - spec/selenium_web_driver/page_object_spec.rb
194
199
  - spec/selenium_web_driver/page_validator_spec.rb
195
200
  - spec/spec_helper.rb
201
+ - spec/test_page/Spacer.gif
196
202
  - spec/test_page/test_page.html
197
203
  - spec/test_page/test_page.rb
198
204
  - spec/test_page/test_page_custom_generator_configuration.rb