capybara-js_finders 0.3.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -76,7 +76,7 @@ assert find_cell(:row => "Andrew Bon", :column => "Email").has_no_content?("john
76
76
  assert find_cell(:row => "John Smith", :column => "January", :text => "28").has_text?("Present at work")
77
77
  ```
78
78
 
79
- #### Multicolumn and multirows support
79
+ #### Multicolumn and multirow support
80
80
 
81
81
  If there are many rows and/or columns matching `:row` and/or `:column` parameter you can wider the search to include all of them
82
82
  by using `:multirow` and/or `:multicolumn` action.
@@ -127,6 +127,39 @@ find_cell(:row => "John Smith", :column => "Permissions", :text => "Moderator")
127
127
  find_cell(:multirow => true, :row => "John Smith", :column => "Permissions", :text => "Moderator") # will find the proper cell
128
128
  ```
129
129
 
130
+ #### Performance
131
+
132
+ Current implementation calculates the position of every th or td element on a page.
133
+ This might be slow especially when there are many such elements on the page. You if you have multiple
134
+ subsequent `find_cell` invocations and you know that the page does not change between them
135
+ you might use `static_page(&block)` method to improve the overall performance. Only first
136
+ call to `find_cell` will calculate cells' positions and the following checks will
137
+ reuse those values.
138
+
139
+ ##### Example:
140
+
141
+ ```ruby
142
+
143
+ click_link("Permissions")
144
+
145
+ static_page do
146
+ find_cell(:row => "John Smith", :column => "Permissions") # execute JS to calculate elements' positions
147
+ find_cell(:row => "Andrew Bon", :column => "Email") # JS is not executed
148
+ end
149
+
150
+ click_link("Posts")
151
+
152
+ static_page do
153
+ find_cell(:row => "Ruby is Awesome", :column => "Published at") # execute JS to calculate elements' positions
154
+ find_cell(:row => "And CoffeScript too", :column => "Published at") # JS is not executed
155
+ end
156
+
157
+ click_link("Visitors")
158
+
159
+ find_cell(:row => "June 2011", :column => "Visitors") # JS script is always executed outside static_page block
160
+ find_cell(:row => "July 2011", :column => "Page views") # JS script is always executed outside static_page block
161
+ ```
162
+
130
163
  ## License
131
164
 
132
165
  MIT License
@@ -8,9 +8,14 @@ Capybara::DSL.module_eval do
8
8
  def execute_script(*args, &block)
9
9
  page.execute_script(*args, &block)
10
10
  end
11
+
11
12
  def find_cell(*params, &block)
12
13
  page.find_cell(*params, &block)
13
14
  end
15
+
16
+ def static_page(&block)
17
+ page.static_page(&block)
18
+ end
14
19
  end
15
20
 
16
21
  require 'capybara'
@@ -19,15 +24,22 @@ Capybara::Node::Base.module_eval do
19
24
  def execute_script(script)
20
25
  driver.execute_script(script)
21
26
  end
27
+
22
28
  def evaluate_script(script)
23
29
  driver.evaluate_script(script)
24
30
  end
25
31
  end
26
32
 
27
- Capybara::Session::NODE_METHODS << :find_cell
28
- Capybara::Session::DSL_METHODS << :find_cell
33
+
34
+ Capybara::Session::NODE_METHODS.concat(Capybara::JsFinders::NEW_METHODS)
35
+ Capybara::Session::DSL_METHODS.concat(Capybara::JsFinders::NEW_METHODS)
36
+
29
37
  Capybara::Session.class_eval do
30
38
  def find_cell(*params, &block)
31
39
  current_node.find_cell(*params, &block)
32
40
  end
41
+
42
+ def static_page(&block)
43
+ current_node.static_page(&block)
44
+ end
33
45
  end
@@ -1,5 +1,5 @@
1
1
  module Capybara
2
2
  module JsFinders
3
- VERSION = "0.3.0"
3
+ VERSION = "0.4.0"
4
4
  end
5
5
  end
@@ -12,6 +12,7 @@ module Capybara
12
12
  TR = "tr".freeze # Top range
13
13
  BR = "br".freeze # Bottom range
14
14
  XpathTrue = "(1=1)"
15
+ NEW_METHODS = [:find_cell, :static_page].freeze
15
16
 
16
17
  # TODO: This script is only prototype compatible. Let it discover jquery or prototype and use proper methods.
17
18
  #
@@ -133,13 +134,28 @@ module Capybara
133
134
  end
134
135
  rows = Array.wrap(rows)
135
136
 
136
- execute_script(SCRIPT)
137
+ if @static_page
138
+ execute_script(SCRIPT) unless @executed
139
+ @executed = true
140
+ else
141
+ execute_script(SCRIPT)
142
+ end
137
143
  xpath = JsFinders.cell_condition(columns, rows)
138
- xpath = ".//td[ #{xpath} ]"
144
+ xpath = ".//td[ #{xpath} ]" # TODO: td or th ?
139
145
  # puts xpath
140
146
  find(:xpath, xpath, options)
141
147
  end
148
+
149
+ def static_page(&block)
150
+ @static_page = true
151
+ return block.call
152
+ ensure
153
+ @executed = false
154
+ @static_page = false
155
+ end
156
+
142
157
  end
158
+
143
159
  end
144
160
 
145
161
  require "capybara-js_finders/capybara_extensions"
@@ -4,6 +4,13 @@
4
4
  </head>
5
5
 
6
6
  <body>
7
+
8
+ <div id="me" style="background-color: purple; width: 100%; height: 50px;">
9
+ <a href="/x" onclick="Element.hide('me'); return false;">Hide me</a>
10
+ </div>
11
+
12
+ <br/>
13
+
7
14
  <table cellpadding="15" cellspacing="15" border="2">
8
15
  <tr>
9
16
  <td>
@@ -93,6 +93,38 @@ module FindCellTests
93
93
  assert ! user.find_cell(:column => "March 2013", :text => "13").has_content?("March")
94
94
  end
95
95
 
96
+ def test_assume_static_page
97
+ user.visit '/'
98
+
99
+ red = user.find(:xpath, XPath::HTML.cell("red").to_s )
100
+ attributes = [Capybara::JsFinders::LR, Capybara::JsFinders::RR, Capybara::JsFinders::TR, Capybara::JsFinders::BR]
101
+ attributes.each {|attr| assert_nil red[attr]}
102
+ positions = nil
103
+
104
+ user.static_page do
105
+ user.find_cell(:row => "OneRow", :column => "OneColumn", :text => "red")
106
+ positions = attributes.map do |attr|
107
+ assert red[attr]
108
+ red[attr]
109
+ end
110
+
111
+ user.click_link("Hide me") # Changes positions of elements
112
+
113
+ user.find_cell(:row => "OneRow", :column => "OneColumn", :text => "red") # Find the cell but without recalculating positions because we are inside static_page block
114
+ assert_equal positions, attributes.map{|attr| red[attr] } # Attributes store the same positions as after first find_cell call
115
+ end
116
+
117
+ user.find_cell(:row => "OneRow", :column => "OneColumn", :text => "red") # Find the cell but recalculates positions because we are outside of static_page block
118
+ assert_not_equal positions, attributes.map{|attr| red[attr] } # Attributes store different positions than after first find_cell call
119
+
120
+ user.visit '/'
121
+ user.static_page do # Make sure that another use of static_page again recalculates positions for first find_cell query
122
+ red = user.find_cell(:row => "OneRow", :column => "OneColumn", :text => "red")
123
+ attributes.map { |attr| assert red[attr] }
124
+ end
125
+ end
126
+
127
+
96
128
  private
97
129
 
98
130
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: capybara-js_finders
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,12 +9,12 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2011-07-27 00:00:00.000000000 +02:00
12
+ date: 2011-07-28 00:00:00.000000000 +02:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: capybara
17
- requirement: &72850780 !ruby/object:Gem::Requirement
17
+ requirement: &71901060 !ruby/object:Gem::Requirement
18
18
  none: false
19
19
  requirements:
20
20
  - - ~>
@@ -22,10 +22,10 @@ dependencies:
22
22
  version: 1.0.0
23
23
  type: :runtime
24
24
  prerelease: false
25
- version_requirements: *72850780
25
+ version_requirements: *71901060
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: rake
28
- requirement: &72850530 !ruby/object:Gem::Requirement
28
+ requirement: &71900800 !ruby/object:Gem::Requirement
29
29
  none: false
30
30
  requirements:
31
31
  - - ~>
@@ -33,10 +33,10 @@ dependencies:
33
33
  version: 0.9.0
34
34
  type: :development
35
35
  prerelease: false
36
- version_requirements: *72850530
36
+ version_requirements: *71900800
37
37
  - !ruby/object:Gem::Dependency
38
38
  name: rdoc
39
- requirement: &72850300 !ruby/object:Gem::Requirement
39
+ requirement: &71900560 !ruby/object:Gem::Requirement
40
40
  none: false
41
41
  requirements:
42
42
  - - ! '>='
@@ -44,10 +44,10 @@ dependencies:
44
44
  version: 2.4.2
45
45
  type: :development
46
46
  prerelease: false
47
- version_requirements: *72850300
47
+ version_requirements: *71900560
48
48
  - !ruby/object:Gem::Dependency
49
49
  name: sinatra
50
- requirement: &72850070 !ruby/object:Gem::Requirement
50
+ requirement: &71900330 !ruby/object:Gem::Requirement
51
51
  none: false
52
52
  requirements:
53
53
  - - ~>
@@ -55,10 +55,10 @@ dependencies:
55
55
  version: 1.2.6
56
56
  type: :development
57
57
  prerelease: false
58
- version_requirements: *72850070
58
+ version_requirements: *71900330
59
59
  - !ruby/object:Gem::Dependency
60
60
  name: erubis
61
- requirement: &72849840 !ruby/object:Gem::Requirement
61
+ requirement: &71900100 !ruby/object:Gem::Requirement
62
62
  none: false
63
63
  requirements:
64
64
  - - ~>
@@ -66,10 +66,10 @@ dependencies:
66
66
  version: 2.6.6
67
67
  type: :development
68
68
  prerelease: false
69
- version_requirements: *72849840
69
+ version_requirements: *71900100
70
70
  - !ruby/object:Gem::Dependency
71
71
  name: bbq
72
- requirement: &72849610 !ruby/object:Gem::Requirement
72
+ requirement: &71899850 !ruby/object:Gem::Requirement
73
73
  none: false
74
74
  requirements:
75
75
  - - ~>
@@ -77,10 +77,10 @@ dependencies:
77
77
  version: 0.0.3
78
78
  type: :development
79
79
  prerelease: false
80
- version_requirements: *72849610
80
+ version_requirements: *71899850
81
81
  - !ruby/object:Gem::Dependency
82
82
  name: redcarpet
83
- requirement: &72849380 !ruby/object:Gem::Requirement
83
+ requirement: &71899490 !ruby/object:Gem::Requirement
84
84
  none: false
85
85
  requirements:
86
86
  - - ~>
@@ -88,10 +88,10 @@ dependencies:
88
88
  version: '1.17'
89
89
  type: :development
90
90
  prerelease: false
91
- version_requirements: *72849380
91
+ version_requirements: *71899490
92
92
  - !ruby/object:Gem::Dependency
93
93
  name: ruby-debug19
94
- requirement: &72849190 !ruby/object:Gem::Requirement
94
+ requirement: &71899180 !ruby/object:Gem::Requirement
95
95
  none: false
96
96
  requirements:
97
97
  - - ! '>='
@@ -99,7 +99,7 @@ dependencies:
99
99
  version: '0'
100
100
  type: :development
101
101
  prerelease: false
102
- version_requirements: *72849190
102
+ version_requirements: *71899180
103
103
  description: ! " Additional finders for capybara that for some reason\n cannot
104
104
  use only xpath for finding nodes but needs to\n execute js for some calculations.\n\n
105
105
  \ Ex: I you want to find a table cell\n that is under or next to other cell
@@ -144,7 +144,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
144
144
  version: '0'
145
145
  segments:
146
146
  - 0
147
- hash: 1067246483
147
+ hash: -1004171375
148
148
  required_rubygems_version: !ruby/object:Gem::Requirement
149
149
  none: false
150
150
  requirements:
@@ -153,7 +153,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
153
153
  version: '0'
154
154
  segments:
155
155
  - 0
156
- hash: 1067246483
156
+ hash: -1004171375
157
157
  requirements: []
158
158
  rubyforge_project: capybara-js_finders
159
159
  rubygems_version: 1.6.2