tooth 0.2.1 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (2) hide show
  1. data/lib/tooth/page_object.rb +78 -0
  2. metadata +2 -1
@@ -0,0 +1,78 @@
1
+ module Tooth
2
+ module PageObject
3
+ def within scope_locator, &block
4
+ within_scope_klass = Class.new
5
+ within_scope_klass.extend PageObject
6
+ within_scope_klass.instance_variable_set :@page_element, page_element
7
+ within_scope_klass.element_with_finders = -> { element_with_finders.find(scope_locator) }
8
+ within_scope_klass.class_eval &block
9
+ end
10
+
11
+ def link name, locator
12
+ page_element[name] = ->(*args){ element_with_finders.find_link(locator_string(locator, args)) }
13
+ end
14
+
15
+ def field name, locator
16
+ page_element[name] = ->(*args){ element_with_finders.find_field(locator_string(locator, args)) }
17
+ end
18
+
19
+ def button name, locator
20
+ page_element[name] = ->(*args){ element_with_finders.find_button(locator_string(locator, args)) }
21
+ end
22
+
23
+ def element name, locator, options = {}
24
+ page_element[name] = ->(*args){
25
+ element_with_finders.find(locator_string(locator, args), options)
26
+ }
27
+ end
28
+
29
+ def component name, component_class, locator, options = {}
30
+ page_element[name] = ->(*args){
31
+ component_element = find(locator_string(locator, args), options)
32
+ component_class.tap do |cmp|
33
+ cmp.element_with_finders = component_element
34
+ end
35
+ }
36
+ end
37
+
38
+ # convenience methods
39
+ def shows? &block
40
+ instance_eval &block
41
+ end
42
+ alias :scenario :shows?
43
+
44
+ def not_shows? &block
45
+ instance_eval &block
46
+ rescue Capybara::ElementNotFound
47
+ else
48
+ raise 'Element is shown'
49
+ end
50
+ protected
51
+ # currently this is not thread safe solution, because we do not create instance of component, but reuse component class.
52
+ # here we trade usability of the framework for the thread safeness. Need to think about this issue and better solutions.
53
+ attr_writer :element_with_finders
54
+
55
+ private
56
+ def locator_string(locator, args)
57
+ locator.kind_of?(Proc) ? locator.call(*args) : locator
58
+ end
59
+
60
+ def page_element; @page_element ||= {} end
61
+
62
+ def element_with_finders
63
+ if @element_with_finders
64
+ @element_with_finders.kind_of?(Proc) ? @element_with_finders.call : @element_with_finders
65
+ else
66
+ Capybara.current_session
67
+ end
68
+ end
69
+
70
+ def method_missing(meth, *args, &block)
71
+ if( element_finder = page_element[meth.to_sym])
72
+ element_finder.call(*args)
73
+ else
74
+ element_with_finders.send(meth, *args, &block)
75
+ end
76
+ end
77
+ end
78
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tooth
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.2.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -66,6 +66,7 @@ executables: []
66
66
  extensions: []
67
67
  extra_rdoc_files: []
68
68
  files:
69
+ - lib/tooth/page_object.rb
69
70
  - lib/tooth.rb
70
71
  homepage: https://github.com/kliuchnikau/tooth
71
72
  licenses: []