app_prism 0.0.1 → 0.0.2

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
  SHA256:
3
- metadata.gz: f8feeb72fe184d3a1cd955a3d809c6a6b8441d2f3f65068884b7332ab384f78b
4
- data.tar.gz: 96ecc539069f6b86c65cf5b68c06f0820c62c80c7da1a599b10d32a1761a8852
3
+ metadata.gz: a637fdfa42e3ae87953af717e2cf00d1392600c6b9af3ce4003ecb91d1132fab
4
+ data.tar.gz: 4f9b6e76bc6aaff052e14c8105827ac91ac7871b8787810df867a84016b47d46
5
5
  SHA512:
6
- metadata.gz: 453f20eac5d9911eb5eb882e793c4cb7b664f784612f797e9c04ae321355f6c4c4d92eb5778688ee9d48ccc340713b35f60e648dcf14c0771b5315da0b7473cc
7
- data.tar.gz: 40e2a00a6344385febf0548652752e9253696992af5d52d3101ab15852dd8e940112d0808d2c6ad69f6a7f0b1a543131bb9b48921b04c2a2b4bd5d765413191a
6
+ metadata.gz: 2f6d23f287a6f61fa16c43ba64469b3d68f835d4378bca05f1ce1870176478b6e51cf58c92c57a6203b11a1683345fda9ab6e79a7100cad69500e798805c6a25
7
+ data.tar.gz: 738632e6ee607e78d793155335ae3335bd05309553574220424b5137a227140cf671bce46654c78f5ef71c7eacce31dabb3ad54957baeeac7037493d4a8bb5e1
File without changes
@@ -0,0 +1,201 @@
1
+ module AppPrism
2
+ module Elements
3
+ class Element
4
+
5
+ attr_reader :locator, :platform
6
+
7
+ def initialize(identifiers, driver)
8
+ if identifiers.is_a?(Selenium::WebDriver::Element)
9
+ @element = identifiers
10
+ else
11
+ if identifiers.keys.include?(:android) || identifiers.keys.include?(:ios)
12
+ @locator = identifiers[:android] if android?
13
+ @locator = identifiers[:ios] if ios?
14
+ else
15
+ @locator = identifiers
16
+ end
17
+ end
18
+ @platform = driver
19
+ end
20
+
21
+ def element
22
+ @element || @platform.find_element(@locator)
23
+ rescue RuntimeError
24
+ nil
25
+ end
26
+
27
+ # def scroll(direction)
28
+ # @platform.scroll(@locator, direction)
29
+ # end
30
+
31
+ # def fast_scroll(direction)
32
+ # @platform.fast_scroll(@locator, direction)
33
+ # end
34
+
35
+ # def scroll_list_down
36
+ # @platform.scroll_list_down @locator
37
+ # end
38
+
39
+ # def swipe direction
40
+ # @platform.send("swipe_#{direction}", @locator)
41
+ # end
42
+
43
+ # def drag_and_drop(direction)
44
+ # @platform.send("drag_and_drop_#{direction}", @locator, parent_locator)
45
+ # end
46
+
47
+ # def drag_and_drop_to(element_to)
48
+ # @platform.drag_and_drop_to(@locator, element_to.locator)
49
+ # end
50
+
51
+ # def swipe_left
52
+ # @platform.swipe_left(@locator)
53
+ # end
54
+
55
+ # def left_to_edge
56
+ # @platform.left_to_edge(@locator)
57
+ # end
58
+
59
+ # def swipe_right
60
+ # @platform.swipe_right(@locator)
61
+ # end
62
+
63
+ # def swipe_down
64
+ # @platform.swipe_down(@locator)
65
+ # end
66
+
67
+ # def swipe_up
68
+ # @platform.swipe_up(@locator)
69
+ # end
70
+
71
+ def nested_element(identifiers)
72
+ nested_elt = element.find_element(identifiers[:android]) if android?
73
+ nested_elt = element.find_element(identifiers[:ios]) if ios?
74
+ self.class.new(nested_elt, @platform)
75
+ end
76
+
77
+ def when_visible(wait_time = default_wait_time)
78
+ wait_for(wait_time: wait_time) { visible? }
79
+ self
80
+ rescue Timeout::Error
81
+ false
82
+ end
83
+
84
+ def when_not_visible(wait_time = default_wait_time)
85
+ wait_for(wait_time: wait_time) { !visible? }
86
+ rescue Selenium::WebDriver::Error::NoSuchElementError,Timeout::Error,RuntimeError
87
+ return
88
+ end
89
+
90
+ def default_wait_time
91
+ AppPrism::DEFAULT_WAIT_TIME || 30
92
+ end
93
+
94
+ def wait_for(wait_time: default_wait_time)
95
+ start_time = Time.now
96
+ loop do
97
+ return true if yield
98
+ break unless (Time.now - start_time) < wait_time
99
+ sleep 0.5
100
+ end
101
+ raise Timeout::Error, "Timed out while waiting for locator:\"#{@locator}\""
102
+ end
103
+
104
+ def send_keys(text)
105
+ element.send_keys text
106
+ end
107
+
108
+ def clear
109
+ element.send_keys ''
110
+ end
111
+
112
+ def touch
113
+ when_visible.click
114
+ end
115
+
116
+ def click
117
+ element.click
118
+ end
119
+
120
+ # def long_press
121
+ # when_visible
122
+ # @platform.long_touch(@locator)
123
+ # end
124
+
125
+ def text
126
+ element.text
127
+ end
128
+
129
+ # def value
130
+ # get_parameter 'value'
131
+ # end
132
+
133
+ def parent
134
+ loc = if @locator.has_key? :xpath
135
+ @locator[:xpath] + '/..'
136
+ elsif @locator.has_key? :id
137
+ "//*[@id=\"#{@locator[:id]}\"]/.."
138
+ else
139
+ raise 'Something wrong happened'
140
+ end
141
+ @platform.find_element(loc)
142
+ end
143
+
144
+ def visible?
145
+ element.nil? ? false : element.displayed?
146
+ # element ? element.displayed? : false
147
+ end
148
+
149
+ # def active?
150
+ # get_parameter 'enabled'
151
+ # end
152
+
153
+ # def scroll_into_view
154
+ # raise 'Does not work yet'
155
+ # end
156
+
157
+ def size
158
+ element.size
159
+ # rect = get_element['rect']
160
+ # height = rect['height']
161
+ # width = rect['width']
162
+ # {height: height, width: width}
163
+ end
164
+
165
+ # def empty?
166
+ # get_element.nil?
167
+ # end
168
+
169
+ #android only
170
+ # def selected?
171
+ # @platform.query(@locator, :selected).first
172
+ # end
173
+
174
+ #android only
175
+ # def checked?
176
+ # @platform.query(@locator, :checked).first
177
+ # end
178
+
179
+ # def count
180
+ # @platform.query(@locator).count
181
+ # end
182
+
183
+ # def get_parameter(param)
184
+ # get_element[param]
185
+ # end
186
+
187
+ require_relative 'nested_elements'
188
+ include NestedElements
189
+
190
+ # private
191
+ # def get_element
192
+ # @platform.query(@locator).first
193
+ # end
194
+ #
195
+ # def parent_locator
196
+ # @locator + ' parent * index:0'
197
+ # end
198
+ end
199
+
200
+ end
201
+ end
@@ -0,0 +1,30 @@
1
+ module AppPrism
2
+ module Elements
3
+ class ElementsCollection
4
+ include Enumerable
5
+
6
+ def initialize(identifiers, element_or_driver)
7
+ if identifiers.keys.include?(:android) || identifiers.keys.include?(:ios)
8
+ @locator = identifiers[:android] if android?
9
+ @locator = identifiers[:ios] if ios?
10
+ else
11
+ @locator = identifiers
12
+ end
13
+
14
+ @element_or_driver = element_or_driver
15
+ @sel_elements = @element_or_driver.find_elements(@locator) #maybe allow it to be empty if elements are not visible yet
16
+ @elements = @sel_elements.map do |elt|
17
+ AppPrism::Elements::Element.new(elt, @element_or_driver)
18
+ end
19
+ end
20
+
21
+ def each(&block)
22
+ @elements.each(&block)
23
+ end
24
+
25
+ def [](index)
26
+ @elements[index]
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'elements_collection'
2
+
3
+ module AppPrism
4
+ module Elements
5
+ module NestedElements
6
+
7
+ def nested_elements(identifiers)
8
+ ElementsCollection.new(identifiers, element)
9
+ end
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,40 @@
1
+ # require_relative 'elements/elements_collection'
2
+ # require_relative 'sections/section_finders'
3
+ # require_relative 'sections/screen_section'
4
+ # require_relative 'sections/sections_collection'
5
+
6
+ module AppPrism
7
+ module Finders
8
+
9
+ def element(name, identifiers)
10
+ define_method("#{name}_element") do
11
+ get_element_for(identifiers)
12
+ end
13
+
14
+ define_method(name) do |&block|
15
+ get_element_for(identifiers).click
16
+ end
17
+
18
+ define_method("#{name}?") do
19
+ get_element_for(identifiers).visible?
20
+ end
21
+
22
+ define_method("#{name}=") do |value|
23
+ get_element_for(identifiers).send_keys(value)
24
+ end
25
+ end
26
+
27
+ def elements(name, identifiers)
28
+ define_method("#{name}_elements") do
29
+ get_elements_for(identifiers)
30
+ end
31
+ end
32
+
33
+ def expected_element(name, wait_time = 0, timeout = AppPrism::DEFAULT_WAIT_TIME)
34
+ define_method("has_expected_element?") do
35
+ sleep wait_time
36
+ self.respond_to?("#{name}_element") && self.send("#{name}_element").when_visible(timeout)
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,47 @@
1
+ # if ENV['PLATFORM'] == 'ios'
2
+ # require 'calabash-cucumber/cucumber'
3
+ # require_relative 'platforms/i_platform'
4
+ # elsif ENV['PLATFORM'] == 'android'
5
+ # require 'calabash-android/cucumber'
6
+ # require_relative 'platforms/a_platform'
7
+ # end
8
+ # require_relative '../app_prism/platforms/rm'
9
+ require_relative '../app_prism/elements/element'
10
+
11
+ module AppPrism
12
+ module HelperMethods
13
+ def android?
14
+ ENV['ANDROID'] == 'true'
15
+ end
16
+
17
+ def ios?
18
+ ENV['IOS'] == 'true'
19
+ end
20
+
21
+ def platform
22
+ @platform ||= AppPrism::Platforms::AppiumPlatform.new(@driver)
23
+ end
24
+
25
+ def get_element_for(identifiers)
26
+ AppPrism::Elements::Element.new(identifiers, platform)
27
+ end
28
+
29
+ def get_elements_for(identifiers)
30
+ AppPrism::Elements::ElementsCollection.new(identifiers, platform)
31
+ end
32
+
33
+ def default_wait_time
34
+ AppPrism::DEFAULT_WAIT_TIME || 30
35
+ end
36
+
37
+ def wait_for(wait_time = default_wait_time)
38
+ start_time = Time.now
39
+ loop do
40
+ return true if yield
41
+ break unless (Time.now - start_time) < wait_time
42
+ sleep 0.5
43
+ end
44
+ raise Timeout::Error, 'Timed out while waiting for block to return true'
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,91 @@
1
+ require_relative 'platform'
2
+
3
+ module AppPrism
4
+ module Platforms
5
+ class AppiumPlatform < Platform
6
+
7
+ def find_element(identifiers)
8
+ @driver.find_element(identifiers)
9
+ rescue Selenium::WebDriver::Error::NoSuchElementError
10
+ raise "Cannot find such element: #{identifiers}"
11
+ end
12
+
13
+ def refresh
14
+ start_x = screen_width/2
15
+ start_y = screen_height/2
16
+ end_y = start_y * 1.7
17
+
18
+ end_x = start_x
19
+ @driver.swipe(start_x: start_x,
20
+ start_y: start_y,
21
+ end_x: end_x,
22
+ end_y: end_y,
23
+ duration: 1000)
24
+ end
25
+
26
+ def find_elements(identifiers)
27
+ @driver.find_elements(identifiers)
28
+ end
29
+
30
+ def screen_width
31
+ @driver.window_size.width
32
+ end
33
+
34
+ def screen_height
35
+ @driver.window_size.height
36
+ end
37
+
38
+ def swipe_up(element)
39
+ start_x = element.location.x + element.size.width/2
40
+ start_y = element.location.y + element.size.height * 0.95
41
+ end_y = element.location.y * 1.05
42
+
43
+ end_x = start_x
44
+ @driver.swipe(start_x: start_x,
45
+ start_y: start_y,
46
+ end_x: end_x,
47
+ end_y: end_y,
48
+ duration: 3000)
49
+ end
50
+
51
+ def swipe_down(element)
52
+ location = element.location
53
+ start_x = location.x + element.size.width/2
54
+ start_y = location.y + element.size.height * 0.05
55
+ end_y = location.y + element.size.height * 0.95
56
+
57
+ end_x = start_x
58
+ @driver.swipe(start_x: start_x,
59
+ start_y: start_y,
60
+ end_x: end_x,
61
+ end_y: end_y,
62
+ duration: 3000)
63
+ end
64
+
65
+ def hide_keyboard
66
+ tries ||= 3
67
+ sleep 1
68
+ @driver.hide_keyboard
69
+ rescue Selenium::WebDriver::Error::UnknownError
70
+ retry unless (tries -= 1).zero?
71
+ end
72
+
73
+ def set_native_context
74
+ @driver.set_context('NATIVE_APP')
75
+ end
76
+
77
+ def set_web_context
78
+ @driver.set_context("WEBVIEW_#{app_package}")
79
+ end
80
+
81
+ def native_context?
82
+ @driver.current_context == 'NATIVE_APP'
83
+ end
84
+
85
+ def app_package
86
+ @driver.caps[:appPackage]
87
+ end
88
+
89
+ end
90
+ end
91
+ end
@@ -0,0 +1,11 @@
1
+ module AppPrism
2
+ module Platforms
3
+ class Platform
4
+ def initialize(driver = nil)
5
+ @driver = driver if driver
6
+ end
7
+
8
+ attr_reader :driver
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,19 @@
1
+ module AppPrism
2
+ module ScreenFactory
3
+ def on_page(page_class, *args)
4
+ page_class = class_from_string(page_class) if page_class.is_a? String
5
+ @current_screen = page_class.new(@browser)
6
+ end
7
+
8
+ alias_method :on, :on_page
9
+
10
+ def class_from_string(class_name)
11
+ parts = class_name.split("::")
12
+ constant = Object
13
+ parts.each do |part|
14
+ constant = constant.const_get(part)
15
+ end
16
+ constant
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,19 @@
1
+ require_relative '../../app_prism/elements/element'
2
+ require_relative '../finders'
3
+
4
+ module AppPrism
5
+ module Sections
6
+ class ScreenSection < Elements::Element
7
+ extend AppPrism::Finders
8
+ extend AppPrism::Sections::SectionFinders
9
+
10
+ def get_element_for(identifiers)
11
+ nested_element(identifiers)
12
+ end
13
+
14
+ def get_elements_for(identifiers)
15
+ nested_elements(identifiers)
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,40 @@
1
+ require_relative '../../app_prism/elements/elements_collection'
2
+ require_relative 'sections_collection'
3
+
4
+ module AppPrism
5
+ module Sections
6
+ module SectionFinders
7
+
8
+ def screen_section(name, section_class, identifiers)
9
+ define_method(name) do
10
+ if android?
11
+ new_identifiers = identifiers[:android].clone
12
+ elsif ios?
13
+ new_identifiers = identifiers[:ios].clone
14
+ else
15
+ raise '' + 'OS is not specified. Please run tests with ANDROID=true or IOS=true' + ''
16
+ end
17
+ section_class.new(new_identifiers, @platform.driver)
18
+ end
19
+
20
+ define_method("#{name}_element") do
21
+ get_element_for(identifiers)
22
+ end
23
+
24
+ define_method("#{name}?") do
25
+ get_element_for(identifiers).visible?
26
+ end
27
+
28
+ end
29
+
30
+ def screen_sections(name, section_class, identifiers)
31
+ define_method(name) do
32
+ sections_ary = AppPrism::Elements::ElementsCollection.new(identifiers, platform).map do |elt|
33
+ section_class.new(elt.element, platform)
34
+ end
35
+ AppPrism::Sections::SectionsCollection[*sections_ary]
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,23 @@
1
+ module AppPrism
2
+ module Sections
3
+ class SectionsCollection < Array
4
+
5
+ # def locator
6
+ # self.first.locator.chomp(' index:0')
7
+ # end
8
+
9
+ def find_by(values_hash)
10
+ find do |section|
11
+ values_hash.all? { |key, value| value === section.public_send(key) }
12
+ end
13
+ end
14
+
15
+ def select_by(values_hash)
16
+ matches = select do |section|
17
+ values_hash.all? { |key, value| value === section.public_send(key) }
18
+ end
19
+ self.class[*matches]
20
+ end
21
+ end
22
+ end
23
+ end
@@ -1,3 +1,3 @@
1
1
  module AppPrism
2
- VERSION = "0.0.1"
2
+ VERSION = "0.0.2"
3
3
  end
data/lib/app_prism.rb CHANGED
@@ -1,6 +1,22 @@
1
1
  require "app_prism/version"
2
2
 
3
3
  module AppPrism
4
- class Error < StandardError; end
5
- # Your code goes here...
4
+ # require_relative 'app_prism/er_methods'
5
+ # require_relative 'app_prism/ers'
6
+ require_relative 'app_prism/sections/section_finders'
7
+ require_relative 'app_prism/sections/screen_section'
8
+ require_relative 'app_prism/sections/sections_collection'
9
+ # require_relative 'app_prism/en_factory'
10
+
11
+ DEFAULT_WAIT_TIME ||= 5
12
+
13
+ def initialize(driver)
14
+ @platform = AppPrism::Platforms::AppiumPlatform.new(driver)
15
+ end
16
+
17
+ def self.included(cls)
18
+ cls.include AppPrism::HelperMethods
19
+ cls.extend AppPrism::Finders
20
+ cls.extend AppPrism::Sections::SectionFinders
21
+ end
6
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: app_prism
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Denys Bazarnyi
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-01-30 00:00:00.000000000 Z
11
+ date: 2019-01-31 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -56,7 +56,7 @@ description: Write a longer description or delete this line.
56
56
  email:
57
57
  - den.bazarny@gmail.com
58
58
  executables:
59
- - appso
59
+ - app_prism
60
60
  extensions: []
61
61
  extra_rdoc_files: []
62
62
  files:
@@ -71,8 +71,19 @@ files:
71
71
  - app_prism.gemspec
72
72
  - bin/console
73
73
  - bin/setup
74
- - exe/appso
74
+ - exe/app_prism
75
75
  - lib/app_prism.rb
76
+ - lib/app_prism/elements/element.rb
77
+ - lib/app_prism/elements/elements_collection.rb
78
+ - lib/app_prism/elements/nested_elements.rb
79
+ - lib/app_prism/finders.rb
80
+ - lib/app_prism/helper_methods.rb
81
+ - lib/app_prism/platforms/appium_platform.rb
82
+ - lib/app_prism/platforms/platform.rb
83
+ - lib/app_prism/screen_factory.rb
84
+ - lib/app_prism/sections/screen_section.rb
85
+ - lib/app_prism/sections/section_finders.rb
86
+ - lib/app_prism/sections/sections_collection.rb
76
87
  - lib/app_prism/version.rb
77
88
  homepage: http://github.com
78
89
  licenses: