crabfarm 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b162bd44fc267ef4526bef924629cec5757e252a
4
- data.tar.gz: 75dd81f5efd9266a0ee97d426965e6c44e1f63c2
3
+ metadata.gz: 591615890c50a205b34d45a201987d95f4ae3fae
4
+ data.tar.gz: f67f0538057b4708da26bd00a52ef4bcfaa7bd34
5
5
  SHA512:
6
- metadata.gz: de642539e7d2b4f0fe156490090222c5cd64c68763f0d6d9c68fb2b896f5c667c30e05b05d545dff9bbfb53c3de700c22e1eddc84c813f499b98dcdcee82930f
7
- data.tar.gz: 1fef6418933fc2f6d697214408a294fc0350ab65439a6480ae1dc8deb2df8d9f26b0c1a1dee59cf8415f68aa28e04b7de3aa76ccb7fea29b43b4c7c18fad5094
6
+ metadata.gz: ee2b73e89eef91889cdeb754dfe527fcbae9e4f2491e1d3c71169d5bcee7e2f0e42c1797a2299a756af24c87a74756f85c298354e009622395a0d218e83f5212
7
+ data.tar.gz: 4a0aee39da4bb582b025d417da92a57ba9269f1f5baf287c121369106494e4248cd761a121f662ea09bbe0acaa38d000a37d2e4049bcffed05f871cb1cc6a662
@@ -11,7 +11,7 @@ module Crabfarm
11
11
  def initialize(_message, _ctx)
12
12
  super _message
13
13
  @ctx = _ctx
14
- @source = _ctx.root_context.page_source rescue nil # cache page source for future reference
14
+ @source = _ctx.root.page_source rescue nil # cache page source for future reference
15
15
  end
16
16
  end
17
17
 
@@ -7,33 +7,48 @@ module Crabfarm
7
7
 
8
8
  TIMEOUT = 10.0 # Default timeout for waiting operations
9
9
 
10
+ attr_accessor :elements, :parent
11
+
12
+ def_delegators :elements, :length, :count, :empty?
13
+
10
14
  def initialize(_elements, _parent)
11
15
  @elements = _elements
12
16
  @parent = _parent
13
17
  end
14
18
 
15
- # return the context's root context
16
- def root_context
17
- return @parent.root_context if @parent
18
- self
19
+ def root
20
+ @parent.root
19
21
  end
20
22
 
21
- # return the context's parent context
22
- def parent_context
23
- @parent
23
+ def each
24
+ elements.each { |el| yield child_context [el] }
24
25
  end
25
26
 
26
- # forward read-only array methods to context
27
- def_delegators :context, :each, :[], :length, :count, :empty?, :first, :last
28
-
29
- # yield individual SearchContext for each element contained in this result
30
- def explode(&_block)
31
- return enum_for(__method__) if _block.nil?
32
- context.each do |el|
33
- _block.call SearchContext.new([el], self)
27
+ def [](*args)
28
+ if args[0].is_a? String or args[0].is_a? Symbol
29
+ attribute args[0]
30
+ else
31
+ child_context Array(elements.send(:[],*args))
34
32
  end
35
33
  end
36
34
 
35
+ def first
36
+ if elements.first.nil? then nil else child_context [elements.first] end
37
+ end
38
+
39
+ def last
40
+ if elements.last.nil? then nil else child_context [elements.last] end
41
+ end
42
+
43
+ def element!
44
+ raise EmptySetError.new("This set is empty", self) if empty?
45
+ elements.first
46
+ end
47
+
48
+ def classes
49
+ wrap_errors { (element!['class'] || '').split(' ') }
50
+ end
51
+
37
52
  # searches for elements that match a given selector
38
53
  def search(_selector=nil, _options={})
39
54
  _options[:css] = _selector if _selector
@@ -46,34 +61,35 @@ module Crabfarm
46
61
  timeout = TIMEOUT if timeout.nil?
47
62
 
48
63
  # use a selenium timeout
49
- wait = Selenium::WebDriver::Wait.new(timeout: timeout)
50
- wait.until do
51
- new_elements = search_elements _options
52
-
53
- # test wait condition
54
- ok = case wait_mode
55
- when :present then (new_elements.length > 0)
56
- when :visible then (new_elements.length > 0 and new_elements.first.displayed?)
57
- when :enabled then (new_elements.length > 0 and new_elements.first.displayed? and new_elements.first.enabled?)
58
- when :not_present then (new_elements.length == 0)
59
- when :not_visible then (not new_elements.any? { |e| e.displayed? })
60
- else
61
- raise SetupError.new "Invalid wait mode '#{wait_mode}'"
64
+ wrap_errors do
65
+ wait = Selenium::WebDriver::Wait.new(timeout: timeout)
66
+ wait.until do
67
+ new_elements = search_elements _options
68
+
69
+ # test wait condition
70
+ ok = case wait_mode
71
+ when :present then (new_elements.length > 0)
72
+ when :visible then (new_elements.length > 0 and new_elements.first.displayed?)
73
+ when :enabled then (new_elements.length > 0 and new_elements.first.displayed? and new_elements.first.enabled?)
74
+ when :not_present then (new_elements.length == 0)
75
+ when :not_visible then (not new_elements.any? { |e| e.displayed? })
76
+ else
77
+ raise SetupError.new "Invalid wait mode '#{wait_mode}'"
78
+ end
79
+
80
+ child_context new_elements if ok
62
81
  end
63
-
64
- SearchContext.new new_elements, self if ok
65
82
  end
66
83
  else
67
- SearchContext.new search_elements(_options), self
84
+ child_context search_elements(_options)
68
85
  end
69
86
  end
70
87
 
71
88
  # clears and sends_keys to this context main element
72
89
  def fill(_value)
73
- raise EmptySetError.new('Cannot call \'fill\' on an empty set', self) if empty?
74
90
  wrap_errors do
75
- context.first.clear
76
- context.first.send_keys _value
91
+ element!.clear
92
+ element!.send_keys _value
77
93
  end
78
94
  end
79
95
 
@@ -83,10 +99,9 @@ module Crabfarm
83
99
  m = /^(.*)_all$/.match _method.to_s
84
100
  if m then
85
101
  return [] if empty?
86
- context.map { |e| e.send(m[1], *_args, &_block) }
102
+ elements.map { |e| e.send(m[1], *_args, &_block) }
87
103
  else
88
- raise EmptySetError.new("Cannot call '#{_method}' on an empty set", self) if empty?
89
- context.first.send(_method, *_args, &_block)
104
+ element!.send(_method, *_args, &_block)
90
105
  end
91
106
  end
92
107
  end
@@ -96,16 +111,19 @@ module Crabfarm
96
111
  m = /^.*_all$/.match _method.to_s
97
112
  if m then
98
113
  return true if empty?
99
- context.first.respond_to? m[1], _include_all
114
+ elements.first.respond_to? m[1], _include_all
100
115
  else
101
116
  return true if empty?
102
- context.first.respond_to? _method, _include_all
117
+ elements.first.respond_to? _method, _include_all
103
118
  end
104
119
  end
105
120
 
106
121
  private
107
122
 
108
- # wrap every selenium errors that happen inside block.
123
+ def child_context(_elements)
124
+ SearchContext.new _elements, self
125
+ end
126
+
109
127
  def wrap_errors
110
128
  begin
111
129
  yield
@@ -114,20 +132,14 @@ module Crabfarm
114
132
  end
115
133
  end
116
134
 
117
- # base filtering method, expands current context
118
135
  def search_elements(_options)
119
136
  wrap_errors do
120
- context.inject([]) do |r, element|
137
+ elements.inject([]) do |r, element|
121
138
  r + element.find_elements(_options)
122
139
  end
123
140
  end
124
141
  end
125
142
 
126
- # returns the current context
127
- def context
128
- @elements
129
- end
130
-
131
143
  end
132
144
  end
133
145
  end
@@ -2,16 +2,23 @@ module Crabfarm
2
2
  module Dsl
3
3
  module Surfer
4
4
  class SurfContext < SearchContext
5
- extend Forwardable
6
5
 
7
6
  def_delegators :@bucket, :parse, :setup
8
7
  def_delegators 'driver.navigate', :back, :forward, :refresh
9
8
 
10
9
  def initialize(_bucket)
11
- super nil, nil
10
+ super nil, self
12
11
  @bucket = _bucket
13
12
  end
14
13
 
14
+ def root
15
+ self
16
+ end
17
+
18
+ def elements
19
+ [driver]
20
+ end
21
+
15
22
  def driver
16
23
  @bucket.original
17
24
  end
@@ -45,13 +52,6 @@ module Crabfarm
45
52
  end
46
53
  end
47
54
  end
48
-
49
- private
50
-
51
- def context
52
- [driver]
53
- end
54
-
55
55
  end
56
56
  end
57
57
  end
@@ -1,3 +1,3 @@
1
1
  module Crabfarm
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crabfarm
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.5
4
+ version: 0.0.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ignacio Baixas
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-01-05 00:00:00.000000000 Z
11
+ date: 2015-01-08 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: jbuilder