chemlab 0.8.0 → 0.9.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: b8768461aba3f55a80a1ef51e13b894d8f876343abb1a402c7de72e4fbfa4a9e
4
- data.tar.gz: a3a48670a730d1d873ce671d94de87198478e54bfa2f29665aa2c05bf686f328
3
+ metadata.gz: 5511ef5fd5d6d4aba05a6ad59ba41d25d75c019b5314a2b079feada7cba946db
4
+ data.tar.gz: 9bbfbf9bf30ddd61c2f6caf5ede340be131217c2cf3e8e0db462abcad21b30c6
5
5
  SHA512:
6
- metadata.gz: 9f6b5cd6c624455c9204da1815b891f65a97725309acf4157b280df795c56f072aa905ae3d48c75a3c42c7414bee7a3408d379dcfdbbc406eb1d849f6c4da3ed
7
- data.tar.gz: 5cc500d4395d6470dc19cb4e250721d600d34e2f0f561410de26437490cf83d2529122c19d7da64ed7f17fd135f3fbfd93cfd01e90a253faae8b54956a7b2092
6
+ metadata.gz: 88ade200b47c7f01db6a4ca215da19a3c877cc9aede859ece778591f039d597def1f2d986146677ec5143d9e1df585ac23b3627e9f6dc06de1bab0061cb3f47e
7
+ data.tar.gz: f1b87c97400fee349d7eac0e94576eaec15ccc9694e0a92826e02865e79135f6a812b9dce3491961fa4fdcf2f1d9fd1a43621e10aacf73881875176ca6dda25c
@@ -17,6 +17,8 @@ module Chemlab
17
17
  default_value = nil
18
18
  default_value = yield if block_given?
19
19
 
20
+ attr_writer name
21
+
20
22
  define_method(name) do
21
23
  instance_variable_get("@#{name}") ||
22
24
  instance_variable_set(
@@ -46,18 +46,18 @@ module Chemlab
46
46
  public_elements << { type: watir_method, name: name, args: args }
47
47
 
48
48
  # @return [Watir::Element] the raw Watir element
49
- define_method("#{name}_element") do
50
- find_element(watir_method, name, args.first, &block)
49
+ define_method("#{name}_element") do |**find_args|
50
+ find_element(watir_method, name, args.first, **find_args, &block)
51
51
  end
52
52
 
53
53
  # @return [Boolean] true if the element is present
54
- define_method("#{name}?") do
55
- public_send("#{name}_element").present?
54
+ define_method("#{name}?") do |wait: nil, wait_until: nil|
55
+ public_send("#{name}_element", wait: wait, wait_until: wait_until).present?
56
56
  end
57
57
 
58
58
  # === GETTER / CLICKER === #
59
- define_method(name) do
60
- element = public_send("#{name}_element")
59
+ define_method(name) do |wait: nil, wait_until: :exists?|
60
+ element = public_send("#{name}_element", wait: wait, wait_until: wait_until)
61
61
  if Element::CLICKABLES.include? watir_method
62
62
  element.wait_until(&:present?).click
63
63
  elsif Element::INPUTS.include? watir_method
@@ -132,12 +132,59 @@ module Chemlab
132
132
  # @api private
133
133
  # @example
134
134
  # #find_element(:text_field, :username) #=>
135
- def find_element(watir_method, name, locator = nil, &block)
135
+ def find_element(watir_method, name, locator = nil, **args, &block)
136
136
  locator = { css: %([data-qa-selector="#{name}"]) } if locator.nil?
137
137
 
138
+ # extract extraneous arguments from the locator and insert them into args
139
+ chemlab_options, locator = extract_chemlab_options(**locator)
140
+ args.merge!(chemlab_options)
141
+
142
+ old_timeout = Chemlab.configuration.default_timeout
143
+ args[:wait] ||= old_timeout
144
+
138
145
  return instance_exec(&block) if block_given?
139
146
 
140
- Chemlab.configuration.browser.session.engine.public_send(watir_method, locator).wait_until(&:exist?)
147
+ element = Chemlab.configuration.browser.session.engine.public_send(watir_method, locator)
148
+
149
+ return element if args[:wait_until].nil?
150
+
151
+ begin
152
+ # change timeout temporarily to the wait specified
153
+ change_timeout(args[:wait])
154
+
155
+ # perform wait(s) on the element
156
+ element.wait_until do |conditions|
157
+ break conditions.public_send(args[:wait_until]) unless args[:wait_until].respond_to?(:each)
158
+
159
+ args[:wait_until].each { |condition| conditions.public_send(condition) }
160
+ end
161
+
162
+ element
163
+ ensure
164
+ # reset timeout
165
+ change_timeout(old_timeout)
166
+ end
167
+ end
168
+
169
+ # Extract Chemlab specific options from an array
170
+ # @param [Array<Hash>] args given arguments
171
+ # @return [Array<Array<Hash>>] first array returned are the chemlab options, second is the resulting array
172
+ # @example
173
+ # arr = { id: 'test', foo: 'bar', wait: 10 }
174
+ # a, b = extract_chemlab_options!(**arr)
175
+ # a #=> { wait: 10 }
176
+ # b #=> { id: 'test', foo: 'bar' }
177
+ def extract_chemlab_options(**args)
178
+ [args.keys.each_with_object({}) do |arg, chemlab_opts|
179
+ chemlab_opts[arg] = args.delete(arg) if %i[wait wait_until].include?(arg)
180
+ end, args]
181
+ end
182
+
183
+ # Set / Reset default Chemlab timeout
184
+ # @param [Integer] timeout the timeout to set (defaults to +Chemlab.configuration.default_timeout)
185
+ # @return [Integer] the new timeout
186
+ def change_timeout(timeout = Chemlab.configuration.default_timeout)
187
+ Chemlab.configuration.default_timeout = timeout
141
188
  end
142
189
 
143
190
  protected
@@ -4,6 +4,8 @@ module Chemlab
4
4
  # Chemlab Configuration
5
5
  class Configuration
6
6
  include Runtime::Logger
7
+ include Attributable
8
+ extend Forwardable
7
9
 
8
10
  # Chemlab Terminal Banner
9
11
  BANNER = <<~'BANNER'
@@ -59,13 +61,13 @@ module Chemlab
59
61
  CONF
60
62
  end
61
63
 
62
- # Add a chemlab configuration
63
- def self.add_config(name)
64
- attr_accessor name
65
- end
64
+ attribute :base_url
65
+ attribute :hide_banner
66
66
 
67
- add_config :base_url
68
- add_config :hide_banner
67
+ # Delegate `default_timeout` to Watir.default_timeout
68
+ # Chemlab.configuration.default_timeout #=> Watir.default_timeout
69
+ # Chemlab.configuration.default_timeout = Watir.default_timeout = 30
70
+ def_delegators Watir, :default_timeout, :default_timeout=
69
71
 
70
72
  attr_reader :browser, :libraries
71
73
 
@@ -45,29 +45,21 @@ module Chemlab
45
45
  #
46
46
  # Chemlab::Runtime::Browser.navigate_to(ThePage) #=> Navigates to https://example.com/path
47
47
  def self.navigate_to(page_class)
48
- unless page_class&.name.respond_to?(:root_module)
48
+ unless page_class&.name.respond_to?(:root_module) && page_class.name.root_module.respond_to?(:base_url)
49
49
  return Chemlab.configuration.browser.navigate_to(Chemlab.configuration.base_url + page_class.path)
50
50
  end
51
51
 
52
- # workaround for file:// protocol. URI.join() does not work with URI.join('file:///Users', '/user')
53
- uri = if URI(page_class.name.root_module.base_url).scheme == 'file'
54
- URI(File.join(page_class.name.root_module.base_url, page_class.path))
55
- else
56
- URI.join(page_class.name.root_module.base_url, page_class.path)
57
- end
58
-
59
- Chemlab.configuration.browser.navigate_to(uri)
52
+ Chemlab.configuration.browser.navigate_to(File.join(page_class.name.root_module.base_url, page_class.path))
60
53
  end
61
54
 
62
- # Navigate to a URI or Path
63
- # @param [URI,String] uri_or_path the URI or path
64
- # @return [URI,String] the URI or Path that was navigated to
55
+ # Navigate to a Path
56
+ # @param [String] path the path to navigate to, relative to the +base_url+
57
+ # @return [String] the path that was navigated to
65
58
  # @example
66
59
  # Chemlab.configuration.browser.navigate_to('/path') #=> /path
67
- # Chemlab.configuration.browser.navigate_to(URI('https://example.com/path')) #=> URI('https://example.com/path')
68
- def navigate_to(uri_or_path)
60
+ def navigate_to(path)
69
61
  @session ||= Chemlab.configuration.browser.session
70
- @session.engine.goto(uri_or_path.to_s)
62
+ @session.engine.goto(path.to_s)
71
63
  end
72
64
 
73
65
  # The options used to create the browser session
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Chemlab
4
- VERSION = '0.8.0'
4
+ VERSION = '0.9.2'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chemlab
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - GitLab Quality
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-09-15 00:00:00.000000000 Z
11
+ date: 2021-10-13 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: climate_control