capybara-page-object 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.0
1
+ 0.2.0
@@ -4,14 +4,14 @@
4
4
  # -*- encoding: utf-8 -*-
5
5
 
6
6
  Gem::Specification.new do |s|
7
- s.name = %q{capybara-page-object}
8
- s.version = "0.1.0"
7
+ s.name = "capybara-page-object"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
- s.authors = [%q{Andy Waite}]
12
- s.date = %q{2012-03-25}
13
- s.description = %q{Page Objects for Capybara}
14
- s.email = %q{andy@andywaite.com}
11
+ s.authors = ["Andy Waite"]
12
+ s.date = "2012-03-26"
13
+ s.description = "Page Objects for Capybara"
14
+ s.email = "andy@andywaite.com"
15
15
  s.extra_rdoc_files = [
16
16
  "LICENSE.txt",
17
17
  "README.rdoc"
@@ -25,16 +25,26 @@ Gem::Specification.new do |s|
25
25
  "Rakefile",
26
26
  "VERSION",
27
27
  "capybara-page-object.gemspec",
28
+ "lib/base.rb",
28
29
  "lib/capybara-page-object.rb",
29
- "test/example.html",
30
+ "lib/extractors/common.rb",
31
+ "lib/extractors/page_level.rb",
32
+ "lib/image.rb",
33
+ "lib/page.rb",
34
+ "lib/table.rb",
30
35
  "test/helper.rb",
31
- "test/test_capybara-page-object.rb"
36
+ "test/image.html",
37
+ "test/page.html",
38
+ "test/table.html",
39
+ "test/test_image.rb",
40
+ "test/test_page.rb",
41
+ "test/test_table.rb"
32
42
  ]
33
- s.homepage = %q{http://github.com/andyw8/capybara-page-object}
34
- s.licenses = [%q{MIT}]
35
- s.require_paths = [%q{lib}]
36
- s.rubygems_version = %q{1.8.6}
37
- s.summary = %q{Page Objects for Capybara}
43
+ s.homepage = "http://github.com/andyw8/capybara-page-object"
44
+ s.licenses = ["MIT"]
45
+ s.require_paths = ["lib"]
46
+ s.rubygems_version = "1.8.10"
47
+ s.summary = "Page Objects for Capybara"
38
48
 
39
49
  if s.respond_to? :specification_version then
40
50
  s.specification_version = 3
data/lib/base.rb ADDED
@@ -0,0 +1,23 @@
1
+ module CapybaraPageObject
2
+ class Base
3
+ include Extractors::Common
4
+
5
+ attr_accessor :source
6
+
7
+ def initialize(source=nil)
8
+ source ||= Capybara.current_session
9
+ @source = source
10
+ end
11
+
12
+ def path(*args)
13
+ raise PageObjectMissingPath, "You need to override this"
14
+ end
15
+
16
+ def visit
17
+ source.visit path
18
+ end
19
+ end
20
+
21
+ class MissingPath < RuntimeError
22
+ end
23
+ end
@@ -1,61 +1,15 @@
1
1
  require 'rubygems'
2
2
  require 'capybara'
3
+ require File.dirname(__FILE__) + '/extractors/common'
4
+ require File.dirname(__FILE__) + '/extractors/page_level'
5
+ require File.dirname(__FILE__) + '/page'
6
+ require File.dirname(__FILE__) + '/table'
7
+ require File.dirname(__FILE__) + '/image'
3
8
 
4
9
  module Capybara
5
10
  module Node
6
11
  module Finders
7
- def title
8
- find('title').text
9
- end
10
-
11
- def tables(attr={})
12
- extract('table', attr)
13
- end
14
-
15
- def forms(attr={})
16
- extract('form', attr)
17
- end
18
-
19
- def rows(attr={})
20
- extract('tr', attr)
21
- end
22
-
23
- def headers(attr={})
24
- extract('th', attr)
25
- end
26
-
27
- def meta_description
28
- extract('meta', :by => 'name')['description']['content']
29
- end
30
-
31
- def meta_keywords
32
- extract('meta', :by => 'name')['keywords']['content'].split(',').collect(&:strip)
33
- end
34
-
35
- def data
36
- r = {}
37
- native.attributes.each do |k, v|
38
- next unless k.start_with?('data-')
39
- r[k.gsub('data-', '')] = v.value
40
- end
41
- r
42
-
43
- end
44
-
45
- private
46
-
47
- def extract(element, attr={})
48
- by = attr[:by]
49
- if by
50
- r = {}
51
- all(element).each do |t|
52
- r[t[by]] = t
53
- end
54
- r
55
- else
56
- all(element)
57
- end
58
- end
12
+ include Extractors::PageLevel
59
13
  end
60
14
  end
61
15
  end
@@ -0,0 +1,50 @@
1
+ module Extractors
2
+
3
+ module Common
4
+ def extract(element, attr={}, page_object_class=nil)
5
+ by = attr[:by]
6
+ keys = attr[:keys]
7
+ if keys
8
+ r = []
9
+ all(element).each do |t|
10
+ r << t[keys]
11
+ end
12
+ r
13
+ elsif by
14
+ r = {}
15
+ all(element).each do |t|
16
+ if page_object_class
17
+ r[t[by]] = page_object_class.new(t)
18
+ else
19
+ r[t[by]] = t
20
+ end
21
+ end
22
+ r
23
+ else
24
+ if page_object_class
25
+ all(element).collect{ |e| page_object_class.new(e) }
26
+ else
27
+ all(element).collect
28
+ end
29
+ end
30
+ end
31
+
32
+ def data
33
+ r = {}
34
+ native.attributes.each do |k, v|
35
+ next unless k.start_with?('data-')
36
+ r[k.gsub('data-', '')] = v.value
37
+ end
38
+ r
39
+ end
40
+
41
+ def all(*args)
42
+ source.all(*args)
43
+ end
44
+
45
+ def find(*args)
46
+ source.find(*args)
47
+ end
48
+
49
+ end
50
+ end
@@ -0,0 +1,25 @@
1
+ module Extractors
2
+ module PageLevel
3
+ include Common
4
+
5
+ def title
6
+ find('title').text
7
+ end
8
+
9
+ def tables(attr={})
10
+ extract('table', attr, CapybaraPageObject::Table)
11
+ end
12
+
13
+ def forms(attr={})
14
+ extract('form', attr)
15
+ end
16
+
17
+ def meta_description
18
+ extract('meta', :by => 'name')['description']['content']
19
+ end
20
+
21
+ def meta_keywords
22
+ extract('meta', :by => 'name')['keywords']['content'].split(',').collect(&:strip)
23
+ end
24
+ end
25
+ end
data/lib/image.rb ADDED
@@ -0,0 +1,13 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ module CapybaraPageObject
4
+ class Image < CapybaraPageObject::Base
5
+ def src
6
+ source.find('img')[:src]
7
+ end
8
+
9
+ def alt
10
+ source.find('img')[:alt].strip
11
+ end
12
+ end
13
+ end
data/lib/page.rb ADDED
@@ -0,0 +1,15 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ module CapybaraPageObject
4
+ class Page < CapybaraPageObject::Base
5
+ include Extractors::PageLevel
6
+
7
+ def initialize(source)
8
+ if source
9
+ @source = source
10
+ else
11
+ @source = Capybara.current_session
12
+ end
13
+ end
14
+ end
15
+ end
data/lib/table.rb ADDED
@@ -0,0 +1,13 @@
1
+ require File.dirname(__FILE__) + '/base'
2
+
3
+ module CapybaraPageObject
4
+ class Table < CapybaraPageObject::Base
5
+ def rows(attr={})
6
+ extract('tr', attr)
7
+ end
8
+
9
+ def headers(attr={})
10
+ extract('th', attr)
11
+ end
12
+ end
13
+ end
data/test/helper.rb CHANGED
@@ -16,3 +16,9 @@ require 'capybara-page-object'
16
16
 
17
17
  class Test::Unit::TestCase
18
18
  end
19
+
20
+ def load_sample_into_model(model)
21
+ filename = model.to_s.split('::').last + '.html'
22
+ html = File.open(File.dirname(__FILE__) + '/' + filename).read
23
+ model.new(Capybara.string(html))
24
+ end
data/test/image.html ADDED
@@ -0,0 +1,2 @@
1
+ <img src="image1.jpg" alt="image 1">
2
+ <img src="image2.jpg" alt="image 2">
File without changes
data/test/table.html ADDED
@@ -0,0 +1,10 @@
1
+ <table id="table_1" class="table_1_class">
2
+ <tr id="table_1_tr_1">
3
+ <th id="table_1_th_1">TH1</th>
4
+ <th id="table_1_th_2">TH2</th>
5
+ </tr>
6
+ <tr id="table_1_tr_2">
7
+ <td id="table_1_td_1">TD1</td>
8
+ <td id="table_1_td_2">TD2</td>
9
+ </tr>
10
+ </table>
@@ -0,0 +1,20 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ require 'capybara'
3
+
4
+ class TestImage< Test::Unit::TestCase
5
+ context "" do
6
+ setup do
7
+ @image = load_sample_into_model(CapybaraPageObject::Image)
8
+ end
9
+
10
+ should "src" do
11
+ assert_equal 'image_1.jpg', @image.src
12
+ end
13
+
14
+ should "src" do
15
+ assert_equal 'image 1', @image.alt
16
+ end
17
+
18
+ end
19
+
20
+ end
data/test/test_page.rb ADDED
@@ -0,0 +1,59 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ require 'capybara'
3
+
4
+ class TestCapybaraPageObject < Test::Unit::TestCase
5
+
6
+ context "Using the example.html" do
7
+
8
+ setup do
9
+ @page = load_sample_into_model(CapybaraPageObject::Page)
10
+ end
11
+
12
+ should "page title" do
13
+ assert_equal 'Hello World', @page.title
14
+ end
15
+
16
+ should "tables" do
17
+ tables = @page.tables
18
+ assert_equal CapybaraPageObject::Table, tables[0].class
19
+ assert_equal CapybaraPageObject::Table, tables[0].class
20
+ end
21
+
22
+ should "tables by id" do
23
+ tables = @page.tables(:by => 'id')
24
+ assert_equal CapybaraPageObject::Table, tables['table_1'].class
25
+ assert_equal CapybaraPageObject::Table, tables['table_2'].class
26
+ end
27
+
28
+ should "forms" do
29
+ assert_equal ['form_1', 'form_2'], @page.forms(:keys => 'id')
30
+ end
31
+
32
+ should "meta_description" do
33
+ assert_equal "meta description", @page.meta_description
34
+ end
35
+
36
+ should "meta_keywords" do
37
+ assert_equal ["meta keyword 1", "meta keyword 2"], @page.meta_keywords
38
+ end
39
+
40
+ should "return the elements as a hash" do
41
+ assert_same_elements ['table_1', 'table_2'], @page.tables(:keys => 'id')
42
+ assert_same_elements ['table_1_class', 'table_2_class'], @page.tables(:keys => 'class')
43
+ end
44
+
45
+ should "data" do
46
+ h = {'foo' => 'a', 'bar' => 'b', 'cat' => ''}
47
+ assert_equal h, @page.find('#data').data
48
+ end
49
+
50
+ context "#path" do
51
+ should "raise exception if path not defined" do
52
+ assert_raise CapybaraPageObject::MissingPath do
53
+ @page.path
54
+ end
55
+ end
56
+ end
57
+
58
+ end
59
+ end
@@ -0,0 +1,20 @@
1
+ require File.dirname(__FILE__) + '/helper'
2
+ require 'capybara'
3
+
4
+ class TestTable< Test::Unit::TestCase
5
+ context "" do
6
+ setup do
7
+ @table = load_sample_into_model(CapybaraPageObject::Table)
8
+ end
9
+
10
+ should "rows" do
11
+ assert_same_elements ['table_1_tr_1', 'table_1_tr_2'], @table.rows(:keys => 'id')
12
+ end
13
+
14
+ should "headers" do
15
+ assert_same_elements ["table_1_th_1", "table_1_th_2"], @table.headers(:keys => 'id')
16
+ end
17
+
18
+ end
19
+
20
+ end
metadata CHANGED
@@ -1,79 +1,106 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: capybara-page-object
3
- version: !ruby/object:Gem::Version
4
- version: 0.1.0
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
5
  prerelease:
6
+ segments:
7
+ - 0
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
6
11
  platform: ruby
7
- authors:
12
+ authors:
8
13
  - Andy Waite
9
14
  autorequire:
10
15
  bindir: bin
11
16
  cert_chain: []
12
- date: 2012-03-25 00:00:00.000000000Z
13
- dependencies:
14
- - !ruby/object:Gem::Dependency
15
- name: capybara
16
- requirement: &2159420260 !ruby/object:Gem::Requirement
17
+
18
+ date: 2012-03-26 00:00:00 Z
19
+ dependencies:
20
+ - !ruby/object:Gem::Dependency
21
+ requirement: &id001 !ruby/object:Gem::Requirement
17
22
  none: false
18
- requirements:
19
- - - ! '>='
20
- - !ruby/object:Gem::Version
21
- version: '0'
22
- type: :runtime
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ hash: 3
27
+ segments:
28
+ - 0
29
+ version: "0"
30
+ version_requirements: *id001
31
+ name: capybara
23
32
  prerelease: false
24
- version_requirements: *2159420260
25
- - !ruby/object:Gem::Dependency
26
- name: shoulda
27
- requirement: &2159419360 !ruby/object:Gem::Requirement
33
+ type: :runtime
34
+ - !ruby/object:Gem::Dependency
35
+ requirement: &id002 !ruby/object:Gem::Requirement
28
36
  none: false
29
- requirements:
30
- - - ! '>='
31
- - !ruby/object:Gem::Version
32
- version: '0'
33
- type: :development
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ hash: 3
41
+ segments:
42
+ - 0
43
+ version: "0"
44
+ version_requirements: *id002
45
+ name: shoulda
34
46
  prerelease: false
35
- version_requirements: *2159419360
36
- - !ruby/object:Gem::Dependency
37
- name: bundler
38
- requirement: &2159418440 !ruby/object:Gem::Requirement
47
+ type: :development
48
+ - !ruby/object:Gem::Dependency
49
+ requirement: &id003 !ruby/object:Gem::Requirement
39
50
  none: false
40
- requirements:
51
+ requirements:
41
52
  - - ~>
42
- - !ruby/object:Gem::Version
53
+ - !ruby/object:Gem::Version
54
+ hash: 23
55
+ segments:
56
+ - 1
57
+ - 0
58
+ - 0
43
59
  version: 1.0.0
44
- type: :development
60
+ version_requirements: *id003
61
+ name: bundler
45
62
  prerelease: false
46
- version_requirements: *2159418440
47
- - !ruby/object:Gem::Dependency
48
- name: jeweler
49
- requirement: &2159416500 !ruby/object:Gem::Requirement
63
+ type: :development
64
+ - !ruby/object:Gem::Dependency
65
+ requirement: &id004 !ruby/object:Gem::Requirement
50
66
  none: false
51
- requirements:
67
+ requirements:
52
68
  - - ~>
53
- - !ruby/object:Gem::Version
69
+ - !ruby/object:Gem::Version
70
+ hash: 7
71
+ segments:
72
+ - 1
73
+ - 6
74
+ - 4
54
75
  version: 1.6.4
55
- type: :development
76
+ version_requirements: *id004
77
+ name: jeweler
56
78
  prerelease: false
57
- version_requirements: *2159416500
58
- - !ruby/object:Gem::Dependency
59
- name: rcov
60
- requirement: &2159415820 !ruby/object:Gem::Requirement
61
- none: false
62
- requirements:
63
- - - ! '>='
64
- - !ruby/object:Gem::Version
65
- version: '0'
66
79
  type: :development
80
+ - !ruby/object:Gem::Dependency
81
+ requirement: &id005 !ruby/object:Gem::Requirement
82
+ none: false
83
+ requirements:
84
+ - - ">="
85
+ - !ruby/object:Gem::Version
86
+ hash: 3
87
+ segments:
88
+ - 0
89
+ version: "0"
90
+ version_requirements: *id005
91
+ name: rcov
67
92
  prerelease: false
68
- version_requirements: *2159415820
93
+ type: :development
69
94
  description: Page Objects for Capybara
70
95
  email: andy@andywaite.com
71
96
  executables: []
97
+
72
98
  extensions: []
73
- extra_rdoc_files:
99
+
100
+ extra_rdoc_files:
74
101
  - LICENSE.txt
75
102
  - README.rdoc
76
- files:
103
+ files:
77
104
  - .document
78
105
  - Gemfile
79
106
  - Gemfile.lock
@@ -82,36 +109,52 @@ files:
82
109
  - Rakefile
83
110
  - VERSION
84
111
  - capybara-page-object.gemspec
112
+ - lib/base.rb
85
113
  - lib/capybara-page-object.rb
86
- - test/example.html
114
+ - lib/extractors/common.rb
115
+ - lib/extractors/page_level.rb
116
+ - lib/image.rb
117
+ - lib/page.rb
118
+ - lib/table.rb
87
119
  - test/helper.rb
88
- - test/test_capybara-page-object.rb
120
+ - test/image.html
121
+ - test/page.html
122
+ - test/table.html
123
+ - test/test_image.rb
124
+ - test/test_page.rb
125
+ - test/test_table.rb
89
126
  homepage: http://github.com/andyw8/capybara-page-object
90
- licenses:
127
+ licenses:
91
128
  - MIT
92
129
  post_install_message:
93
130
  rdoc_options: []
94
- require_paths:
131
+
132
+ require_paths:
95
133
  - lib
96
- required_ruby_version: !ruby/object:Gem::Requirement
134
+ required_ruby_version: !ruby/object:Gem::Requirement
97
135
  none: false
98
- requirements:
99
- - - ! '>='
100
- - !ruby/object:Gem::Version
101
- version: '0'
102
- segments:
136
+ requirements:
137
+ - - ">="
138
+ - !ruby/object:Gem::Version
139
+ hash: 3
140
+ segments:
103
141
  - 0
104
- hash: -2260030659624604151
105
- required_rubygems_version: !ruby/object:Gem::Requirement
142
+ version: "0"
143
+ required_rubygems_version: !ruby/object:Gem::Requirement
106
144
  none: false
107
- requirements:
108
- - - ! '>='
109
- - !ruby/object:Gem::Version
110
- version: '0'
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ hash: 3
149
+ segments:
150
+ - 0
151
+ version: "0"
111
152
  requirements: []
153
+
112
154
  rubyforge_project:
113
- rubygems_version: 1.8.6
155
+ rubygems_version: 1.8.10
114
156
  signing_key:
115
157
  specification_version: 3
116
158
  summary: Page Objects for Capybara
117
159
  test_files: []
160
+
@@ -1,52 +0,0 @@
1
- require_relative 'helper'
2
- require 'capybara'
3
-
4
- class TestCapybaraPageObject < Test::Unit::TestCase
5
-
6
- context "Using the example.html" do
7
-
8
- setup do
9
- html = File.open(File.dirname(__FILE__) + '/example.html').read
10
- @doc = Capybara.string(html)
11
- end
12
-
13
- should "page title" do
14
- assert_equal 'Hello World', @doc.title
15
- end
16
-
17
- should "tables" do
18
- assert_same_elements ['table_1', 'table_2'], @doc.tables.collect{ |t| t[:id] }
19
- end
20
-
21
- should "rows" do
22
- assert_same_elements ['table_1_tr_1', 'table_1_tr_2'], @doc.tables.first.rows.collect{ |t| t[:id] }
23
- end
24
-
25
- should "headers" do
26
- assert_same_elements ["table_1_th_1", "table_1_th_2"], @doc.tables.first.headers.collect{ |t| t[:id] }
27
- end
28
-
29
- should "forms" do
30
- assert_equal ['form_1', 'form_2'], @doc.forms.collect{ |f| f[:id] }
31
- end
32
-
33
- should "meta_description" do
34
- assert_equal "meta description", @doc.meta_description
35
- end
36
-
37
- should "meta_keywords" do
38
- assert_equal ["meta keyword 1", "meta keyword 2"], @doc.meta_keywords
39
- end
40
-
41
- should "return the elements as a hash" do
42
- assert_same_elements ['table_1', 'table_2'], @doc.tables(:by => 'id').keys
43
- assert_same_elements ['table_1_class', 'table_2_class'], @doc.tables(:by => 'class').keys
44
- end
45
-
46
- should "data" do
47
- h = {'foo' => 'a', 'bar' => 'b', 'cat' => ''}
48
- assert_equal h, @doc.find('#data').data
49
- end
50
-
51
- end
52
- end