selenium_fury 0.5.5 → 0.5.6

Sign up to get free protection for your applications and to get access to all the features.
Files changed (47) hide show
  1. data/Changelog.md +29 -0
  2. data/Gemfile +2 -1
  3. data/Gemfile.lock +31 -18
  4. data/Rakefile +2 -2
  5. data/features/custom_generator.feature +2 -1
  6. data/features/generate_page_object.feature +2 -1
  7. data/features/step_definitions/validate_page_object_steps.rb +4 -4
  8. data/features/validate_page_object.feature +2 -1
  9. data/lib/selenium_fury/common/page_parser.rb +49 -0
  10. data/lib/selenium_fury/common/selenium_api_chooser.rb +22 -0
  11. data/lib/selenium_fury/selenium_client/create_selenium_client_driver.rb +38 -0
  12. data/lib/selenium_fury/selenium_client/custom_generator.rb +65 -0
  13. data/lib/selenium_fury/selenium_client/locator_finder.rb +54 -0
  14. data/lib/selenium_fury/selenium_client/page_generator.rb +63 -0
  15. data/lib/selenium_fury/selenium_client/page_validator.rb +117 -0
  16. data/lib/selenium_fury/selenium_web_driver/create_selenium_web_driver.rb +35 -0
  17. data/lib/selenium_fury/selenium_web_driver/element_finder.rb +54 -0
  18. data/lib/selenium_fury/selenium_web_driver/page_generator.rb +47 -0
  19. data/lib/selenium_fury/selenium_web_driver/page_object.rb +26 -0
  20. data/lib/selenium_fury/selenium_web_driver/page_object_components.rb +67 -0
  21. data/lib/selenium_fury/selenium_web_driver/page_validator.rb +44 -0
  22. data/lib/selenium_fury.rb +24 -8
  23. data/selenium_fury.gemspec +46 -24
  24. data/spec/common/page_parser_spec.rb +9 -0
  25. data/spec/common/selenium_api_chooser_spec.rb +44 -0
  26. data/spec/{advanced_search.rb → selenium_client/advanced_search.rb} +1 -1
  27. data/spec/{advanced_search_custom_generator_configuration.rb → selenium_client/advanced_search_custom_generator_configuration.rb} +0 -0
  28. data/spec/{advanced_search_spec.rb → selenium_client/advanced_search_spec.rb} +0 -0
  29. data/spec/{custom_generators_spec.rb → selenium_client/custom_generators_spec.rb} +2 -1
  30. data/spec/selenium_client/locator_finder_spec.rb +20 -0
  31. data/spec/{page_generator_spec.rb → selenium_client/page_generator_spec.rb} +3 -2
  32. data/spec/selenium_client/page_validator_spec.rb +31 -0
  33. data/spec/selenium_web_driver/advanced_search.rb +47 -0
  34. data/spec/selenium_web_driver/element_finder_spec.rb +23 -0
  35. data/spec/selenium_web_driver/inquiry_side_bar.rb +7 -0
  36. data/spec/selenium_web_driver/page_generator_spec.rb +9 -0
  37. data/spec/selenium_web_driver/page_object_spec.rb +60 -0
  38. data/spec/selenium_web_driver/page_validator_spec.rb +21 -0
  39. data/spec/selenium_web_driver/property_page.rb +3 -0
  40. data/spec/spec_helper.rb +8 -4
  41. metadata +93 -59
  42. data/CHANGELOG +0 -5
  43. data/lib/selenium_fury/create_browser_driver.rb +0 -19
  44. data/lib/selenium_fury/custom_generator.rb +0 -62
  45. data/lib/selenium_fury/page_generator.rb +0 -130
  46. data/lib/selenium_fury/page_validator.rb +0 -113
  47. data/spec/page_validator_spec.rb +0 -9
@@ -0,0 +1,60 @@
1
+ require 'spec_helper'
2
+ describe PageObject do
3
+
4
+ it "should go to the home page using launch site" do
5
+ launch_web_driver "http://www.scottcsims.com"
6
+ driver.current_url.should == "http://scottcsims.com/wordpress/"
7
+ driver.navigate.to "http://www.scottcsims"
8
+ driver.title.should == "Scott Sims"
9
+ end
10
+
11
+ it 'should return a web_driver element when an attribute is accessed' do
12
+ launch_web_driver "http://www.homeaway.com/vacation-rental/p254680"
13
+ inquiry_side_bar = InquirySideBar.new(driver)
14
+ inquiry_side_bar.first_name.class.should == Selenium::WebDriver::Element
15
+ inquiry_side_bar.first_name.send_keys Faker::Name.first_name
16
+ end
17
+
18
+ it "should raise an exception when the element can't be found'" do
19
+ launch_web_driver "http://www.homeaway.com/vacation-rental/p254680"
20
+ inquiry_side_bar = InquirySideBar.new(driver)
21
+ InquirySideBar.element(:not_a_element, {:id =>"not a element"})
22
+ begin
23
+ inquiry_side_bar.not_a_element
24
+ rescue Exception => e
25
+ e.message.should == "Could not find element not_a_element"
26
+ end
27
+ e.should_not be_nil, "we were expecting an exception"
28
+ end
29
+
30
+ it "should have a property page that contains a inquiry sidebar" do
31
+ launch_web_driver "http://www.homeaway.com/vacation-rental/p254680"
32
+ property_page = PropertyPage.new(driver)
33
+ property_page.should_not be_nil
34
+ property_page.inquiry_side_bar.should_not be_nil
35
+ property_page.inquiry_side_bar.first_name.class.should == Selenium::WebDriver::Element
36
+ end
37
+
38
+ it "should have an error if a non page object class is passed to page method" do
39
+ property_page = PropertyPage.new()
40
+ PropertyPage.page(:not_a_page, String)
41
+ begin
42
+ property_page.not_a_page
43
+ rescue Exception => e
44
+ e.message.should == "String does not inherit from PageObject"
45
+ end
46
+ e.should_not be_nil, "we were expecting and exception"
47
+ end
48
+ it "should use elements on the HomeAway advanced search page" do
49
+ launch_web_driver("http://www.homeaway.com/searchForm")
50
+ advanced_search = AdvancedSearchWebDriver.new(driver)
51
+ advanced_search.amenity0_0.click
52
+ advanced_search.adv_search_form.submit
53
+ end
54
+
55
+ it "should use methods on the HomeAway advanced search page" do
56
+ launch_web_driver("http://www.homeaway.com/searchForm")
57
+ advanced_search = AdvancedSearchWebDriver.new(driver)
58
+ advanced_search.click_one_item
59
+ end
60
+ end
@@ -0,0 +1,21 @@
1
+ require "spec_helper"
2
+ describe SeleniumFury::SeleniumWebDriver::PageValidator do
3
+
4
+ it "should have a missing element exception" do
5
+ class MissingElement < PageObject
6
+ element :not_a_element1, {:id=>"not a element1"}
7
+ end
8
+ launch_web_driver("http://www.homeaway.com/searchForm")
9
+ begin
10
+ web_driver_validate(MissingElement)
11
+ rescue Exception => e
12
+ e.message.should == "Found Missing Elements"
13
+ end
14
+ end
15
+
16
+ it "should validate elements" do
17
+ launch_web_driver("http://www.homeaway.com/searchForm")
18
+ web_driver_validate(AdvancedSearchWebDriver)
19
+ end
20
+
21
+ end
@@ -0,0 +1,3 @@
1
+ class PropertyPage < PageObject
2
+ page :inquiry_side_bar, InquirySideBar
3
+ end
data/spec/spec_helper.rb CHANGED
@@ -5,13 +5,17 @@ require 'rubygems'
5
5
  require 'bundler'
6
6
  Bundler.setup
7
7
  require "selenium_fury"
8
+ require "faker"
8
9
  require "rspec"
9
- require "advanced_search_custom_generator_configuration"
10
- require "advanced_search"
11
-
10
+ require "selenium_client/advanced_search_custom_generator_configuration"
11
+ require "selenium_client/advanced_search"
12
+ require "selenium_web_driver/inquiry_side_bar"
13
+ require "selenium_web_driver/property_page"
14
+ require "selenium_web_driver/advanced_search"
12
15
 
13
16
  RSpec.configure do |config|
14
17
  config.after(:each) do
15
- browser.close_current_browser_session if defined?(browser) && !browser.nil?
18
+ browser.close_current_browser_session if !browser.nil?
19
+ driver.quit if !driver.nil?
16
20
  end
17
21
  end
metadata CHANGED
@@ -1,26 +1,25 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: selenium_fury
3
3
  version: !ruby/object:Gem::Version
4
- hash: 1
4
+ hash: 7
5
5
  prerelease:
6
6
  segments:
7
7
  - 0
8
8
  - 5
9
- - 5
10
- version: 0.5.5
9
+ - 6
10
+ version: 0.5.6
11
11
  platform: ruby
12
12
  authors:
13
13
  - Scott Sims
14
+ - Tim Tischler
14
15
  autorequire:
15
16
  bindir: bin
16
17
  cert_chain: []
17
18
 
18
- date: 2011-07-27 00:00:00 Z
19
+ date: 2011-10-27 00:00:00 Z
19
20
  dependencies:
20
21
  - !ruby/object:Gem::Dependency
21
- prerelease: false
22
- type: :runtime
23
- requirement: &id001 !ruby/object:Gem::Requirement
22
+ version_requirements: &id001 !ruby/object:Gem::Requirement
24
23
  none: false
25
24
  requirements:
26
25
  - - ">="
@@ -29,12 +28,12 @@ dependencies:
29
28
  segments:
30
29
  - 0
31
30
  version: "0"
32
- name: selenium-client
33
- version_requirements: *id001
34
- - !ruby/object:Gem::Dependency
31
+ requirement: *id001
35
32
  prerelease: false
33
+ name: selenium-webdriver
36
34
  type: :runtime
37
- requirement: &id002 !ruby/object:Gem::Requirement
35
+ - !ruby/object:Gem::Dependency
36
+ version_requirements: &id002 !ruby/object:Gem::Requirement
38
37
  none: false
39
38
  requirements:
40
39
  - - ">="
@@ -43,12 +42,12 @@ dependencies:
43
42
  segments:
44
43
  - 0
45
44
  version: "0"
45
+ requirement: *id002
46
+ prerelease: false
46
47
  name: nokogiri
47
- version_requirements: *id002
48
+ type: :runtime
48
49
  - !ruby/object:Gem::Dependency
49
- prerelease: false
50
- type: :development
51
- requirement: &id003 !ruby/object:Gem::Requirement
50
+ version_requirements: &id003 !ruby/object:Gem::Requirement
52
51
  none: false
53
52
  requirements:
54
53
  - - ">="
@@ -57,12 +56,12 @@ dependencies:
57
56
  segments:
58
57
  - 0
59
58
  version: "0"
60
- name: yard
61
- version_requirements: *id003
62
- - !ruby/object:Gem::Dependency
59
+ requirement: *id003
63
60
  prerelease: false
61
+ name: yard
64
62
  type: :development
65
- requirement: &id004 !ruby/object:Gem::Requirement
63
+ - !ruby/object:Gem::Dependency
64
+ version_requirements: &id004 !ruby/object:Gem::Requirement
66
65
  none: false
67
66
  requirements:
68
67
  - - ">="
@@ -71,12 +70,12 @@ dependencies:
71
70
  segments:
72
71
  - 0
73
72
  version: "0"
74
- name: jeweler
75
- version_requirements: *id004
76
- - !ruby/object:Gem::Dependency
73
+ requirement: *id004
77
74
  prerelease: false
75
+ name: jeweler
78
76
  type: :development
79
- requirement: &id005 !ruby/object:Gem::Requirement
77
+ - !ruby/object:Gem::Dependency
78
+ version_requirements: &id005 !ruby/object:Gem::Requirement
80
79
  none: false
81
80
  requirements:
82
81
  - - ">="
@@ -85,12 +84,12 @@ dependencies:
85
84
  segments:
86
85
  - 0
87
86
  version: "0"
88
- name: rdiscount
89
- version_requirements: *id005
90
- - !ruby/object:Gem::Dependency
87
+ requirement: *id005
91
88
  prerelease: false
89
+ name: rdiscount
92
90
  type: :development
93
- requirement: &id006 !ruby/object:Gem::Requirement
91
+ - !ruby/object:Gem::Dependency
92
+ version_requirements: &id006 !ruby/object:Gem::Requirement
94
93
  none: false
95
94
  requirements:
96
95
  - - ">="
@@ -99,12 +98,12 @@ dependencies:
99
98
  segments:
100
99
  - 0
101
100
  version: "0"
102
- name: rspec
103
- version_requirements: *id006
104
- - !ruby/object:Gem::Dependency
101
+ requirement: *id006
105
102
  prerelease: false
103
+ name: rspec
106
104
  type: :development
107
- requirement: &id007 !ruby/object:Gem::Requirement
105
+ - !ruby/object:Gem::Dependency
106
+ version_requirements: &id007 !ruby/object:Gem::Requirement
108
107
  none: false
109
108
  requirements:
110
109
  - - ">="
@@ -113,12 +112,12 @@ dependencies:
113
112
  segments:
114
113
  - 0
115
114
  version: "0"
116
- name: cucumber
117
- version_requirements: *id007
118
- - !ruby/object:Gem::Dependency
115
+ requirement: *id007
119
116
  prerelease: false
117
+ name: cucumber
120
118
  type: :development
121
- requirement: &id008 !ruby/object:Gem::Requirement
119
+ - !ruby/object:Gem::Dependency
120
+ version_requirements: &id008 !ruby/object:Gem::Requirement
122
121
  none: false
123
122
  requirements:
124
123
  - - ">="
@@ -127,12 +126,12 @@ dependencies:
127
126
  segments:
128
127
  - 0
129
128
  version: "0"
130
- name: bundler
131
- version_requirements: *id008
132
- - !ruby/object:Gem::Dependency
129
+ requirement: *id008
133
130
  prerelease: false
131
+ name: bundler
134
132
  type: :development
135
- requirement: &id009 !ruby/object:Gem::Requirement
133
+ - !ruby/object:Gem::Dependency
134
+ version_requirements: &id009 !ruby/object:Gem::Requirement
136
135
  none: false
137
136
  requirements:
138
137
  - - ">="
@@ -141,12 +140,12 @@ dependencies:
141
140
  segments:
142
141
  - 0
143
142
  version: "0"
144
- name: builder
145
- version_requirements: *id009
146
- - !ruby/object:Gem::Dependency
143
+ requirement: *id009
147
144
  prerelease: false
145
+ name: builder
148
146
  type: :development
149
- requirement: &id010 !ruby/object:Gem::Requirement
147
+ - !ruby/object:Gem::Dependency
148
+ version_requirements: &id010 !ruby/object:Gem::Requirement
150
149
  none: false
151
150
  requirements:
152
151
  - - ">="
@@ -155,12 +154,26 @@ dependencies:
155
154
  segments:
156
155
  - 0
157
156
  version: "0"
157
+ requirement: *id010
158
+ prerelease: false
158
159
  name: rake
159
- version_requirements: *id010
160
+ type: :development
160
161
  - !ruby/object:Gem::Dependency
162
+ version_requirements: &id011 !ruby/object:Gem::Requirement
163
+ none: false
164
+ requirements:
165
+ - - ">="
166
+ - !ruby/object:Gem::Version
167
+ hash: 3
168
+ segments:
169
+ - 0
170
+ version: "0"
171
+ requirement: *id011
161
172
  prerelease: false
162
- type: :runtime
163
- requirement: &id011 !ruby/object:Gem::Requirement
173
+ name: faker
174
+ type: :development
175
+ - !ruby/object:Gem::Dependency
176
+ version_requirements: &id012 !ruby/object:Gem::Requirement
164
177
  none: false
165
178
  requirements:
166
179
  - - ">="
@@ -169,8 +182,10 @@ dependencies:
169
182
  segments:
170
183
  - 0
171
184
  version: "0"
185
+ requirement: *id012
186
+ prerelease: false
172
187
  name: nokogiri
173
- version_requirements: *id011
188
+ type: :runtime
174
189
  description: Generate and validate page objects with this page object factory for Selenium.
175
190
  email: ssims98@gmail.com
176
191
  executables: []
@@ -182,7 +197,7 @@ extra_rdoc_files:
182
197
  - README.md
183
198
  files:
184
199
  - .rvmrc
185
- - CHANGELOG
200
+ - Changelog.md
186
201
  - Gemfile
187
202
  - Gemfile.lock
188
203
  - LICENSE
@@ -197,17 +212,36 @@ files:
197
212
  - features/support/hooks.rb
198
213
  - features/validate_page_object.feature
199
214
  - lib/selenium_fury.rb
200
- - lib/selenium_fury/create_browser_driver.rb
201
- - lib/selenium_fury/custom_generator.rb
202
- - lib/selenium_fury/page_generator.rb
203
- - lib/selenium_fury/page_validator.rb
215
+ - lib/selenium_fury/common/page_parser.rb
216
+ - lib/selenium_fury/common/selenium_api_chooser.rb
217
+ - lib/selenium_fury/selenium_client/create_selenium_client_driver.rb
218
+ - lib/selenium_fury/selenium_client/custom_generator.rb
219
+ - lib/selenium_fury/selenium_client/locator_finder.rb
220
+ - lib/selenium_fury/selenium_client/page_generator.rb
221
+ - lib/selenium_fury/selenium_client/page_validator.rb
222
+ - lib/selenium_fury/selenium_web_driver/create_selenium_web_driver.rb
223
+ - lib/selenium_fury/selenium_web_driver/element_finder.rb
224
+ - lib/selenium_fury/selenium_web_driver/page_generator.rb
225
+ - lib/selenium_fury/selenium_web_driver/page_object.rb
226
+ - lib/selenium_fury/selenium_web_driver/page_object_components.rb
227
+ - lib/selenium_fury/selenium_web_driver/page_validator.rb
204
228
  - selenium_fury.gemspec
205
- - spec/advanced_search.rb
206
- - spec/advanced_search_custom_generator_configuration.rb
207
- - spec/advanced_search_spec.rb
208
- - spec/custom_generators_spec.rb
209
- - spec/page_generator_spec.rb
210
- - spec/page_validator_spec.rb
229
+ - spec/common/page_parser_spec.rb
230
+ - spec/common/selenium_api_chooser_spec.rb
231
+ - spec/selenium_client/advanced_search.rb
232
+ - spec/selenium_client/advanced_search_custom_generator_configuration.rb
233
+ - spec/selenium_client/advanced_search_spec.rb
234
+ - spec/selenium_client/custom_generators_spec.rb
235
+ - spec/selenium_client/locator_finder_spec.rb
236
+ - spec/selenium_client/page_generator_spec.rb
237
+ - spec/selenium_client/page_validator_spec.rb
238
+ - spec/selenium_web_driver/advanced_search.rb
239
+ - spec/selenium_web_driver/element_finder_spec.rb
240
+ - spec/selenium_web_driver/inquiry_side_bar.rb
241
+ - spec/selenium_web_driver/page_generator_spec.rb
242
+ - spec/selenium_web_driver/page_object_spec.rb
243
+ - spec/selenium_web_driver/page_validator_spec.rb
244
+ - spec/selenium_web_driver/property_page.rb
211
245
  - spec/spec_helper.rb
212
246
  homepage: https://github.com/scottcsims/SeleniumFury
213
247
  licenses: []
@@ -238,7 +272,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
238
272
  requirements: []
239
273
 
240
274
  rubyforge_project:
241
- rubygems_version: 1.8.5
275
+ rubygems_version: 1.8.10
242
276
  signing_key:
243
277
  specification_version: 3
244
278
  summary: Selenium Fury allows an automated tester to quickly build page files to use in the page object pattern. Each page file represents a page under test with attributes of all the locators selenium needs to run tests on the page and methods that represent actions that can be performed on the page. You use the generator to build the page files. After the page has been updated you can use the validator to go through all of the selenium locators you are using in your page file and return a list of the locators that it could not find. If there are missing locators you can then rerun the generator to generate new selenium locators for your page. http://www.scottcsims.com
data/CHANGELOG DELETED
@@ -1,5 +0,0 @@
1
- v0.5.2 Adding the custom generator and cucumber features to the project. Tests now execute against HomeAway.
2
- v0.5.1 Adding ability to name attributes in order of id, name, title.
3
- v0.5. Adding project files and connecting to a public facing site.
4
- v0.2. Adding Validator Code
5
- v0.1. Adding Generator Code
@@ -1,19 +0,0 @@
1
- module CreateBrowserDriver
2
-
3
- # @return [Selenium::Client::Driver]
4
- def browser
5
- return @browser
6
- end
7
-
8
- # @param url [string]
9
- # @return [Selenium::Client::Driver]
10
- def create_selenium_driver(url)
11
-
12
- @browser = Selenium::Client::Driver.new(
13
- :host => ENV['HOST'] || "localhost",
14
- :port => 4444,
15
- :browser => ENV['SELENIUM_RC_BROWSER'] || "*firefox",
16
- :url => url,
17
- :timeout_in_second => 60)
18
- end
19
- end
@@ -1,62 +0,0 @@
1
- #/* Copyright (c) 2010 HomeAway, Inc.
2
- # * All rights reserved. http://www.homeaway.com
3
- # *
4
- # * Licensed under the Apache License, Version 2.0 (the "License");
5
- # * you may not use this file except in compliance with the License.
6
- # * You may obtain a copy of the License at
7
- # *
8
- # * http://www.apache.org/licenses/LICENSE-2.0
9
- # *
10
- # * Unless required by applicable law or agreed to in writing, software
11
- # * distributed under the License is distributed on an "AS IS" BASIS,
12
- # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # * See the License for the specific language governing permissions and
14
- # * limitations under the License.
15
- # */
16
-
17
- module CustomGenerator
18
- # Use a custom configuration object to define a location strategy for a locators name and value
19
- # @param [Hash] options the options to use with a custom generator.
20
- # @option opts [CustomConfiguration] :custom_configuration a class that explains how to parse the page
21
- # @option opts [String] :browser the selenium driver
22
- # @option opts [String] :html html from the page under test
23
- def custom_generator(options)
24
- custom_configuration = options[:custom_configuration]
25
- browser = options[:browser]
26
- if !browser.nil?
27
- html = browser.get_html_source
28
- else
29
- html = options[:html]
30
- end
31
- doc = Nokogiri::HTML(html)
32
- html_menu_elements ={}
33
- doc.css(custom_configuration.selector).each do |nokogiri_element|
34
- custom_configuration.nokogiri_element = nokogiri_element
35
- puts "Html element is #{nokogiri_element}" if $DEBUG
36
- generated_name = clean_attribute_name(custom_configuration.name)
37
- puts "generated name is #{generated_name}" if $DEBUG
38
- generated_value = custom_configuration.value
39
- puts "generated value is #{generated_value}" if $DEBUG
40
- puts "@#{generated_name} = \"#{generated_value}\"" if $DEBUG
41
- html_menu_elements[generated_name]= generated_value
42
- end
43
- merge_and_print_elements [html_menu_elements]
44
- return html_menu_elements
45
- end
46
- # Parse the html attribute to a ruby variable
47
- # @return [String]
48
- # @param attribute_name [String] the html id,name, or title of an element
49
- def clean_attribute_name(attribute_name)
50
- attribute_name.gsub!('input-', '')
51
- attribute_name.gsub!('select-', '')
52
- attribute_name.gsub!(/([A-Z]+)/, '_\1')
53
- attribute_name.gsub!('\\', '')
54
- attribute_name.gsub!(' ', '_')
55
- attribute_name.gsub!('.', '_')
56
- attribute_name.gsub!('-', '_')
57
- attribute_name.gsub!('__', '_')
58
- attribute_name.gsub!(/^_/, '')
59
- attribute_name = attribute_name.to_s.downcase
60
- return attribute_name
61
- end
62
- end
@@ -1,130 +0,0 @@
1
- #/* Copyright (c) 2010 HomeAway, Inc.
2
- # * All rights reserved. http://www.homeaway.com
3
- # *
4
- # * Licensed under the Apache License, Version 2.0 (the "License");
5
- # * you may not use this file except in compliance with the License.
6
- # * You may obtain a copy of the License at
7
- # *
8
- # * http://www.apache.org/licenses/LICENSE-2.0
9
- # *
10
- # * Unless required by applicable law or agreed to in writing, software
11
- # * distributed under the License is distributed on an "AS IS" BASIS,
12
- # * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
- # * See the License for the specific language governing permissions and
14
- # * limitations under the License.
15
- # */
16
- module PageGenerator
17
-
18
- # @param html_element [String] a html element that could contain a id, name, or title.
19
- def get_name_and_value(html_element)
20
- if html_element.get_attribute("id") != nil
21
- attribute_name = html_element.get_attribute("id")
22
- attribute_value = html_element.get_attribute("id")
23
- elsif html_element.get_attribute("name") != nil
24
- attribute_name = html_element.get_attribute("name")
25
- attribute_value = html_element.get_attribute("name")
26
- elsif html_element.get_attribute("title") != nil
27
- attribute_name = html_element.get_attribute("title")
28
- attribute_value = html_element.get_attribute("title")
29
- end
30
- return attribute_name, attribute_value
31
- end
32
-
33
- # @param [Hash] opts the opts to use with a custom generator.
34
- # @option opts [String] :locator_type css or xpath
35
- # @option opts [String] :locator the html id,name, or title to be used as a selenium locator
36
- # @option opts [String] :html html from the page under test
37
- # @return [Hash] a list of variable names and locator values that can be used in a test
38
- def generate_instance_variables_from_html(opts)
39
- if opts.kind_of?(Hash)
40
- @html = opts[:html]
41
- @locator_type = opts[:locator_type]
42
- @locator = opts[:locator]
43
- end
44
- doc = Nokogiri::HTML(@html)
45
- html_elements = {}
46
- if (@locator_type=="css")
47
- doc.css(@locator).each do |html_element|
48
- attribute_name, attribute_value = get_name_and_value(html_element)
49
- if !attribute_name.nil?
50
- attribute_name.gsub!('input-', '')
51
- attribute_name.gsub!('select-', '')
52
- attribute_name.gsub!(/([A-Z]+)/, '_\1')
53
- attribute_name.gsub!('\\', '')
54
- attribute_name.gsub!(' ', '_')
55
- attribute_name.gsub!('.', '_')
56
- attribute_name.gsub!('-', '_')
57
- attribute_name.gsub!('__', '_')
58
- attribute_name = attribute_name.to_s.downcase
59
- puts "@#{attribute_name} = \"#{attribute_value}\"" if $debug
60
- html_elements[attribute_name]= attribute_value
61
- end
62
- end
63
- end
64
- return html_elements
65
- end
66
-
67
- # Print the ruby class using the html elements we found from the page.
68
- # @param page_elements_types [Array,Hash] an array of hashes for each type of found html element.
69
- def merge_and_print_elements(page_elements_types)
70
- html_elements={}
71
- page_elements_types.each do |element_type|
72
- html_elements.merge!(element_type)
73
- end
74
- $stdout.puts "found (#{html_elements.length} elements)"
75
- $stdout.puts "class YourPageFile"
76
- $stdout.puts "\tdef initialize *browser\n\t\t@browser = *browser"
77
- html_elements.keys.sort.each do |key|
78
- $stdout.puts "\t\t@#{key} = \"#{html_elements[key]}\""
79
- end
80
- $stdout.puts "\tend"
81
- count=1
82
- html_elements.keys.sort.each do |key|
83
- if count == 1
84
- $stdout.print "\tattr_accessor :browser, "
85
- end
86
- if count % 5 == 0
87
- $stdout.print "\n\t"
88
- end
89
- if count != html_elements.length
90
- $stdout.print ":#{key}, "
91
- else
92
- $stdout.print ":#{key}"
93
- end
94
- count = count + 1
95
- end
96
- $stdout.puts "\n\nend"
97
-
98
- end
99
-
100
-
101
- def get_source_and_print_elements(browser)
102
- html =browser.get_html_source
103
- return print_elements(html)
104
- end
105
-
106
- # @param html [String] html source from your page
107
- # @return [Hash,Array] an array of hashes for each type of found html element.
108
- def print_elements(html)
109
- html_elements_select=generate_instance_variables_from_html(:html =>html, :locator_type => "css", :locator => "select")
110
- html_elements_text_area=generate_instance_variables_from_html(:html =>html, :locator_type => "css", :locator => "textarea")
111
- html_elements_form = generate_instance_variables_from_html(:html =>html, :locator_type => "css", :locator => "form")
112
- html_elements_buttons = generate_instance_variables_from_html(:html =>html, :locator_type => "css", :locator => "input[type='button']")
113
- html_elements_file = generate_instance_variables_from_html(:html =>html, :locator_type => "css", :locator => "input[type='file']")
114
- html_elements_check_boxes = generate_instance_variables_from_html(:html =>html, :locator_type => "css", :locator => "input[type='checkbox']")
115
- html_elements_password = generate_instance_variables_from_html(:html =>html, :locator_type => "css", :locator => "input[type='password']")
116
- html_elements_radio = generate_instance_variables_from_html(:html =>html, :locator_type => "css", :locator => "input[type='radio']")
117
- html_elements_reset = generate_instance_variables_from_html(:html =>html, :locator_type => "css", :locator => "input[type='reset']")
118
- html_elements_image = generate_instance_variables_from_html(:html =>html, :locator_type => "css", :locator => "input[type='image']")
119
- html_elements_submit = generate_instance_variables_from_html(:html =>html, :locator_type => "css", :locator => "input[type='submit']")
120
- html_elements_text = generate_instance_variables_from_html(:html =>html, :locator_type => "css", :locator => "input[type='text']")
121
-
122
- elements = [html_elements_check_boxes, html_elements_select, html_elements_text, html_elements_buttons, html_elements_file,
123
- html_elements_password, html_elements_text_area, html_elements_image, html_elements_radio, html_elements_reset,
124
- html_elements_form, html_elements_submit]
125
- merge_and_print_elements(elements)
126
- return elements
127
- end
128
-
129
-
130
- end