intelement 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1 @@
1
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ gem 'capybara', '2.0.3'
4
+ gem 'cucumber', '1.2.5'
5
+
6
+ gemspec
@@ -0,0 +1,55 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ intelement (0.0.1)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ builder (3.2.2)
10
+ capybara (2.0.3)
11
+ mime-types (>= 1.16)
12
+ nokogiri (>= 1.3.3)
13
+ rack (>= 1.0.0)
14
+ rack-test (>= 0.5.4)
15
+ selenium-webdriver (~> 2.0)
16
+ xpath (~> 1.0.0)
17
+ childprocess (0.3.9)
18
+ ffi (~> 1.0, >= 1.0.11)
19
+ cucumber (1.2.5)
20
+ builder (>= 2.1.2)
21
+ diff-lcs (>= 1.1.3)
22
+ gherkin (~> 2.11.7)
23
+ multi_json (~> 1.3)
24
+ diff-lcs (1.2.4)
25
+ ffi (1.9.0)
26
+ gherkin (2.11.8)
27
+ multi_json (~> 1.3)
28
+ mime-types (1.24)
29
+ mini_portile (0.5.1)
30
+ multi_json (1.7.9)
31
+ nokogiri (1.6.0)
32
+ mini_portile (~> 0.5.0)
33
+ rack (1.5.2)
34
+ rack-test (0.6.2)
35
+ rack (>= 1.0)
36
+ rake (10.0.4)
37
+ rubyzip (0.9.9)
38
+ selenium-webdriver (2.35.0)
39
+ childprocess (>= 0.2.5)
40
+ multi_json (~> 1.0)
41
+ rubyzip
42
+ websocket (~> 1.0.4)
43
+ websocket (1.0.7)
44
+ xpath (1.0.0)
45
+ nokogiri (~> 1.3)
46
+
47
+ PLATFORMS
48
+ ruby
49
+
50
+ DEPENDENCIES
51
+ bundler (~> 1.3)
52
+ capybara (= 2.0.3)
53
+ cucumber (= 1.2.5)
54
+ intelement!
55
+ rake
@@ -0,0 +1 @@
1
+ Copyright (c) 2013 lavanya
@@ -0,0 +1,63 @@
1
+ # ElementHelper
2
+
3
+ Gem is a wrapper around Capybara.
4
+ It provides users with a method that can does a lot of element related operations like check the presence of an element on page, get value of the element, get the attribute value, find multiple elements with same selector, find all their values, etc.
5
+
6
+ ## Installation
7
+
8
+ Add this line to your application's Gemfile:
9
+
10
+ gem 'intelement'
11
+
12
+ And then execute:
13
+
14
+ $ bundle
15
+
16
+ Or install it yourself as:
17
+
18
+ $ gem install intelement
19
+
20
+ ## Usage
21
+
22
+ # Require ElementHelper in your file
23
+ $ require 'intelement'
24
+
25
+ # To visit the page, specify the URL
26
+ $ ElementHelper.visit_page("http://www.flipkart.com/")
27
+ Driver to be used can also be specified. By default it is selenium.
28
+ $ ElementHelper.visit_page("http://www.flipkart.com/", :selenium)
29
+
30
+ # To check whether an element for a given CSS selector or XPath is present (returns a boolean)
31
+ $ ElementHelper.get_element_details(".btn.btn-blue.btn-cart")
32
+ $ ElementHelper.get_element_details("//div[@class='btn btn-blue btn-cart']")
33
+
34
+ # To get the value of a particular element, we need to specify the type of element
35
+ $ ElementHelper.get_element_details(".fk-cstext", :text)
36
+ The options supported for this are :text, :link and :button. (:text and :link do .text after finding the element while :button does a .value)
37
+
38
+ # When multiple elements are obtained using a specified selector, to get the value of an element at a particular index
39
+ $ ElementHelper.get_element_details("//a[@href='/']", :text, 1)
40
+
41
+ # When attribute value of a particular element is to be found
42
+ $ ElementHelper.get_element_details("//a[@href='/s/help']", "class")
43
+ Above, "class" is the attribute whoes value we require
44
+
45
+ # When multiple elements are obtained using a specified selector, to get the attribute value of an element at a particular index
46
+ $ ElementHelper.get_element_details("//a[@class='fk-footer-unit fk-footer-link']", "href", 0)
47
+
48
+ # When multiple elements are obtained using a specified selector, to get the values of all element
49
+ $ ElementHelper.get_element_details("//a[@class='fk-footer-unit fk-footer-link']", :text, :all)
50
+
51
+ # When multiple elements are obtained using a specified selector, to get the attribute values of all element
52
+ $ ElementHelper.get_element_details("//a[@class='fk-footer-unit fk-footer-link']", "href", :all)
53
+
54
+ # To get the number of elements with the specified Xpath or CSS selector
55
+ $ ElementHelper.get_element_details("//a[@href='/']", nil, :all)
56
+
57
+ ## Contributing
58
+
59
+ 1. Fork it
60
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
61
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
62
+ 4. Push to the branch (`git push origin my-new-feature`)
63
+ 5. Create new Pull Request
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'intelement/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "intelement"
8
+ spec.version = IntelementVersion::VERSION
9
+ spec.authors = ["bitweft"]
10
+ spec.email = ["bitweft@gmail.com"]
11
+ spec.description = %q{A gem that provides an intelligent way to get various different element details}
12
+ spec.summary = %q{An intelligent wrapper over capybara that provides one generic method to get the details of a web element}
13
+ spec.homepage = "http://www.bitweft.com"
14
+ spec.license = "To be decided"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
@@ -0,0 +1,116 @@
1
+ require_relative 'intelement/version'
2
+ require 'capybara'
3
+
4
+ module Intelement
5
+
6
+ class << self
7
+ include Capybara::DSL
8
+ attr_reader :uri, :driver
9
+
10
+ def visit_page uri=nil, driver=:selenium
11
+ @uri = uri
12
+ @driver = driver
13
+ Capybara.default_driver = @driver
14
+ Capybara.visit uri
15
+ end
16
+
17
+ def is_element_present? locator
18
+ selector = get_selector locator
19
+ has_selector?(selector, locator)
20
+ end
21
+
22
+ def get_element_value locator, type=:text
23
+ selector = get_selector locator
24
+ case type
25
+ when :text, :link
26
+ find(selector, locator).text
27
+ when :button
28
+ find(selector, locator).value
29
+ end
30
+ end
31
+
32
+ def get_all_elements locator
33
+ selector = get_selector locator
34
+ all(selector, locator)
35
+ end
36
+
37
+ def get_element_value_of_index locator, type=:text, index=0
38
+ selector = get_selector locator
39
+ case type
40
+ when :text, :link
41
+ all(selector, locator)[index].text
42
+ when :button
43
+ all(selector, locator)[index].value
44
+ end
45
+ end
46
+
47
+ def get_attribute_value locator, attribute_name
48
+ selector = get_selector locator
49
+ find(selector, locator)[attribute_name]
50
+ end
51
+
52
+ def get_attribute_value_of_index locator, attribute_name, index=0
53
+ selector = get_selector locator
54
+ all(selector, locator)[index][attribute_name]
55
+ end
56
+
57
+ def get_all_elements_value locator, type=:text
58
+ selector = get_selector locator
59
+ all_elements = all(selector, locator)
60
+ element_values = case type
61
+ when :text, :link
62
+ all_elements.map do |element|
63
+ element.text
64
+ end
65
+ when :button
66
+ all_elements.map do |element|
67
+ element.value
68
+ end
69
+ end
70
+ element_values
71
+ end
72
+
73
+ def get_all_element_attribute_value locator, attribute_name
74
+ selector = get_selector locator
75
+ all_elements = all(selector, locator)
76
+ attribute_values = all_elements.map do |element|
77
+ element[attribute_name]
78
+ end
79
+ attribute_values
80
+ end
81
+
82
+ def get_element_details locator, value_to_get=nil, index=nil
83
+ if value_to_get.nil? && index.nil?
84
+ is_element_present? locator
85
+ elsif value_to_get.nil? && index.eql?(:all)
86
+ get_all_elements locator
87
+ elsif index.nil?
88
+ if value_to_get.class.eql?(String)
89
+ get_attribute_value locator, value_to_get
90
+ else
91
+ get_element_value locator, value_to_get
92
+ end
93
+ else
94
+ if index.class.eql?(Symbol)
95
+ if value_to_get.class.eql?(String)
96
+ get_all_element_attribute_value locator, value_to_get
97
+ else
98
+ get_all_elements_value locator, value_to_get
99
+ end
100
+ else
101
+ if value_to_get.class.eql?(String)
102
+ get_attribute_value_of_index locator, value_to_get, index
103
+ else
104
+ get_element_value_of_index locator, value_to_get, index
105
+ end
106
+ end
107
+ end
108
+ end
109
+
110
+ def get_selector locator
111
+ locator[0] == '/' ? :xpath : :css
112
+ end
113
+
114
+ end
115
+ end
116
+
@@ -0,0 +1,3 @@
1
+ module IntelementVersion
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,105 @@
1
+ require_relative '../../test_helper'
2
+
3
+ describe Intelement, "When the presence of an element on a page is requested by using a css selector" do
4
+ it "should return true when the element is present" do
5
+ Intelement.visit_page("http://www.flipkart.com/")
6
+ Intelement.is_element_present?(".btn.btn-blue.btn-cart").must_equal true
7
+ end
8
+
9
+ it "should return false when the element is not present" do
10
+ Intelement.visit_page("http://www.flipkart.com/")
11
+ Intelement.is_element_present?("#invalid_element_css_selector").must_equal false
12
+ end
13
+ end
14
+
15
+ describe Intelement, "When the presence of an element on a page is requested by using an xpath selector" do
16
+ it "should return true when the element is present" do
17
+ Intelement.visit_page("http://www.flipkart.com/")
18
+ Intelement.is_element_present?("//div[@class='btn btn-blue btn-cart']").must_equal true
19
+ end
20
+
21
+ it "should return false when the element is not present" do
22
+ Intelement.visit_page("http://www.flipkart.com/")
23
+ Intelement.is_element_present?("//div[@class='invalid_element_xpath_selector']").must_equal false
24
+ end
25
+ end
26
+
27
+ describe Intelement, "When the value of a single element is requested for" do
28
+ it "should return the appropriate value" do
29
+ Intelement.visit_page("http://www.dell.com/")
30
+ Intelement.get_element_value("//button[@type='submit']").must_equal "Search"
31
+ end
32
+
33
+ it "should return the appropriate value" do
34
+ Intelement.visit_page("http://www.flipkart.com/")
35
+ Intelement.get_element_value(".fk-cstext", :text).must_equal "24x7 Customer Support"
36
+ end
37
+
38
+ it "should return the appropriate value" do
39
+ Intelement.visit_page("http://www.flipkart.com/")
40
+ Intelement.get_element_value("//a[@href='/s/help']", :link).must_equal "FAQ"
41
+ end
42
+
43
+ it "should return the appropriate value" do
44
+ Intelement.visit_page("http://www.w3schools.com/html/html_forms.asp")
45
+ Intelement.get_element_value("//input[@type='submit']", :button).must_equal "Submit"
46
+ end
47
+ end
48
+
49
+ describe Intelement, "When all the matches for elements with a particular selector is requested for" do
50
+ it "should give an array of all the matches" do
51
+ Intelement.visit_page("http://www.flipkart.com/")
52
+ Intelement.get_all_elements("//a[@href='/']").size.must_equal 2
53
+ end
54
+ end
55
+
56
+ describe Intelement, "When we need an elements value with a particular selector that returns multiple candidates" do
57
+ it "should give us the value of the element we requested for" do
58
+ Intelement.visit_page("http://www.flipkart.com/")
59
+ Intelement.get_element_value_of_index("//a[@href='/']", :text, 1).must_equal "Home"
60
+ end
61
+ end
62
+
63
+
64
+ describe Intelement, "When we need to get the attribute value of an element" do
65
+ it "should give us the requested attribute value" do
66
+ Intelement.visit_page("http://www.flipkart.com/")
67
+ Intelement.get_attribute_value("//a[@href='/s/help']", "class").must_equal "fk-footer-unit fk-footer-link"
68
+ end
69
+ end
70
+
71
+ describe Intelement, "When we need to get the attribute value of an element from multiple candidates" do
72
+ it "should give us the requested attribute value for the requested element" do
73
+ Intelement.visit_page("http://www.flipkart.com/")
74
+ Intelement.get_attribute_value_of_index("//a[@class='fk-footer-unit fk-footer-link']", "href", 0).must_equal "http://www.flipkart.com/s/help/payments"
75
+ end
76
+ end
77
+
78
+ describe Intelement, "When requested for all the values" do
79
+ it "should give a map of all the values" do
80
+ Intelement.visit_page("http://www.flipkart.com/")
81
+ Intelement.get_all_elements_value("//a[@class='fk-footer-unit fk-footer-link']", :text).must_equal ["Payments", "Saved Cards", "Shipping", "Cancellation & Returns", "FAQ", "Contact Us", "About Us", "Careers", "Blog", "Press", "Slash N", "Download Manager", "MP3 FAQ", "MP3 App", "eBooks FAQ", "eBooks App", "Online Shopping", "Affiliate", "Wallet", "e-Gift Voucher", "Flipkart lite", "Store Directory"]
82
+ end
83
+ end
84
+
85
+ describe Intelement, "When requested for all the values of attributes" do
86
+ it "should give a map of all the attribute values" do
87
+ Intelement.visit_page("http://www.flipkart.com/")
88
+ Intelement.get_all_element_attribute_value("//a[@class='fk-footer-unit fk-footer-link']", "href").must_equal ["http://www.flipkart.com/s/help/payments", "http://www.flipkart.com/help/savedcard_how", "http://www.flipkart.com/s/help/shipping", "http://www.flipkart.com/s/help/cancellation-returns", "http://www.flipkart.com/s/help", "http://www.flipkart.com/s/contact", "http://www.flipkart.com/about-us", "http://www.flipkart.com/s/careers", "http://www.flipkart.com/ol?link=http%3A%2F%2Fblog.flipkart.com%2F", "http://www.flipkart.com/s/press", "http://slashn.flipkart.net/", "http://www.flipkart.com/help/downloadmanager", "http://www.flipkart.com/s/mp3help", "http://www.flipkart.com/help/flytemp3app", "http://www.flipkart.com/help/flyteeBookfaq", "http://www.flipkart.com/ebooks/apps", "http://www.flipkart.com/", "http://www.flipkart.com/affiliate/", "http://www.flipkart.com/help/wallet", "http://www.flipkart.com/buy-gift-voucher", "http://www.flipkart.com/m?sitevariant=mobile", "http://www.flipkart.com/store-directory"]
89
+ end
90
+ end
91
+
92
+
93
+ describe Intelement, "When we need a single method to get the value or attribute or presence of an element" do
94
+ it "should give us the correct result" do
95
+ Intelement.visit_page("http://www.flipkart.com/")
96
+ Intelement.get_element_details(".btn.btn-blue.btn-cart").must_equal true
97
+ Intelement.get_element_details(".fk-cstext", :text).must_equal "24x7 Customer Support"
98
+ Intelement.get_element_details("//a[@href='/']", :text, 1).must_equal "Home"
99
+ Intelement.get_element_details("//a[@href='/s/help']", "class").must_equal "fk-footer-unit fk-footer-link"
100
+ Intelement.get_element_details("//a[@class='fk-footer-unit fk-footer-link']", "href", 0).must_equal "http://www.flipkart.com/s/help/payments" # get particular element attribute
101
+ Intelement.get_element_details("//a[@class='fk-footer-unit fk-footer-link']", :text, :all).must_equal ["Payments", "Saved Cards", "Shipping", "Cancellation & Returns", "FAQ", "Contact Us", "About Us", "Careers", "Blog", "Press", "Slash N", "Download Manager", "MP3 FAQ", "MP3 App", "eBooks FAQ", "eBooks App", "Online Shopping", "Affiliate", "Wallet", "e-Gift Voucher", "Flipkart lite", "Store Directory"]
102
+ Intelement.get_element_details("//a[@class='fk-footer-unit fk-footer-link']", "href", :all).must_equal ["http://www.flipkart.com/s/help/payments", "http://www.flipkart.com/help/savedcard_how", "http://www.flipkart.com/s/help/shipping", "http://www.flipkart.com/s/help/cancellation-returns", "http://www.flipkart.com/s/help", "http://www.flipkart.com/s/contact", "http://www.flipkart.com/about-us", "http://www.flipkart.com/s/careers", "http://www.flipkart.com/ol?link=http%3A%2F%2Fblog.flipkart.com%2F", "http://www.flipkart.com/s/press", "http://slashn.flipkart.net/", "http://www.flipkart.com/help/downloadmanager", "http://www.flipkart.com/s/mp3help", "http://www.flipkart.com/help/flytemp3app", "http://www.flipkart.com/help/flyteeBookfaq", "http://www.flipkart.com/ebooks/apps", "http://www.flipkart.com/", "http://www.flipkart.com/affiliate/", "http://www.flipkart.com/help/wallet", "http://www.flipkart.com/buy-gift-voucher", "http://www.flipkart.com/m?sitevariant=mobile", "http://www.flipkart.com/store-directory"]
103
+ Intelement.get_element_details("//a[@href='/']", nil, :all).size.must_equal 2
104
+ end
105
+ end
@@ -0,0 +1,7 @@
1
+ require_relative '../../test_helper'
2
+
3
+ describe IntelementVersion do
4
+ it "must have a defined version" do
5
+ IntelementVersion::VERSION.wont_be_nil
6
+ end
7
+ end
@@ -0,0 +1,3 @@
1
+ require 'minitest/autorun'
2
+ require 'minitest/pride'
3
+ require File.expand_path('../../lib/intelement.rb', __FILE__)
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: intelement
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - bitweft
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-08-23 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: A gem that provides an intelligent way to get various different element
47
+ details
48
+ email:
49
+ - bitweft@gmail.com
50
+ executables: []
51
+ extensions: []
52
+ extra_rdoc_files: []
53
+ files:
54
+ - .gitignore
55
+ - Gemfile
56
+ - Gemfile.lock
57
+ - LICENSE.txt
58
+ - README.md
59
+ - Rakefile
60
+ - intelement.gemspec
61
+ - lib/intelement.rb
62
+ - lib/intelement/version.rb
63
+ - test/lib/intelement/intelement.rb
64
+ - test/lib/intelement/version_test.rb
65
+ - test/test_helper.rb
66
+ homepage: http://www.bitweft.com
67
+ licenses:
68
+ - To be decided
69
+ post_install_message:
70
+ rdoc_options: []
71
+ require_paths:
72
+ - lib
73
+ required_ruby_version: !ruby/object:Gem::Requirement
74
+ none: false
75
+ requirements:
76
+ - - ! '>='
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ required_rubygems_version: !ruby/object:Gem::Requirement
80
+ none: false
81
+ requirements:
82
+ - - ! '>='
83
+ - !ruby/object:Gem::Version
84
+ version: '0'
85
+ requirements: []
86
+ rubyforge_project:
87
+ rubygems_version: 1.8.23
88
+ signing_key:
89
+ specification_version: 3
90
+ summary: An intelligent wrapper over capybara that provides one generic method to
91
+ get the details of a web element
92
+ test_files:
93
+ - test/lib/intelement/intelement.rb
94
+ - test/lib/intelement/version_test.rb
95
+ - test/test_helper.rb