ferrum_common 0.0.0 → 0.1.0

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: 3e8ef82a6987019ce9a01dffedf29704905fc53d7f3534cc1654b6a9a7c9220d
4
- data.tar.gz: b9d2a38f62cc43372635cfcf679f95868c735b5ad8fbf2d482ac9b640df2e06a
3
+ metadata.gz: 337f36c693834482dea4e00fcb2d5407b2787df08279bdba679b5e22b3482745
4
+ data.tar.gz: 01bceb7e7fa64fa3082040ecc11b7c83674ace1a2282153132172cc3e6c12a9f
5
5
  SHA512:
6
- metadata.gz: 4a7044da6e7ec7c03d56d5b1b2e99f72988476cd93323af8fc9601db4fe9161b534dd8188f9ea57548676776ab05394f5d5c60d7a43fed6bc26ecbd21a93eb2e
7
- data.tar.gz: 474977a1cb5ec61a2f1ac62a47d275e34e803754c3554a8f5393c7634e0fe23abbf02fae4c7b1b9d9f48763266868fd00ec8e3df139a0ef3297dbc1a0bbeaaba
6
+ metadata.gz: 56f406f33b726a82f4aa2f7e8b1c4e39b53ddc14dedea932f550bb1e4d7586d09d835915fd2a219327c7fdfeae34e245ae4721b9f06eb66a2cbec1b743b1f977
7
+ data.tar.gz: afbcaa65ed2a25940d44290e3e82f4269eb5b74c6cdc80f6d402da7e7d87ff14fd3f704243d7e080f51c9014e74abba580ecc11df39a4c5a6838e9cf98bf5dd2
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |spec|
2
2
  spec.name = "ferrum_common"
3
- spec.version = "0.0.0"
3
+ spec.version = "0.1.0"
4
4
  spec.summary = "[WIP] common useful extensions for ferrum or cuprite"
5
5
 
6
6
  spec.author = "Victor Maslov aka Nakilon"
@@ -9,7 +9,8 @@ Gem::Specification.new do |spec|
9
9
  spec.metadata = {"source_code_uri" => "https://github.com/nakilon/ferrum_common"}
10
10
 
11
11
  spec.add_dependency "ferrum"
12
- spec.required_ruby_version = ">=2.5"
12
+ spec.add_dependency "browser_reposition"
13
+ spec.required_ruby_version = ">=2.5" # why?
13
14
 
14
15
  spec.files = %w{ LICENSE ferrum_common.gemspec lib/ferrum_common.rb }
15
16
  end
data/lib/ferrum_common.rb CHANGED
@@ -1,28 +1,60 @@
1
1
  require "ferrum"
2
2
  module FerrumCommon
3
- def self.new **_
4
- Ferrum::Browser.new(**_).tap do |browser|
5
- require "browser_reposition"
6
- browser.extend(BrowserReposition).reposition
7
- browser.define_singleton_method :redo_until_true do |timeout, msg = nil, &block|
8
- Timeout.timeout timeout do
9
- begin
10
- block.call
11
- rescue Ferrum::NodeNotFoundError
12
- redo
13
- end or (sleep timeout*0.1; redo)
14
- end
15
- rescue Timeout::Error
16
- browser.mhtml path: "temp.mhtml"
17
- $!.backtrace.reject!{ |_| _[/\/gems\/concurrent-ruby-/] }
18
- $!.backtrace.reject!{ |_| _[/\/gems\/ferrum-/] }
19
- raise Timeout::Error, "#{$!.to_s} in redo_until_true #{" (#{msg})" if msg}"
3
+
4
+ module Common
5
+
6
+ def self.mhtml browser, timeout, mtd, msg = nil
7
+ Timeout.timeout(timeout){ yield }
8
+ rescue Timeout::Error
9
+ browser.mhtml path: "temp.mhtml"
10
+ $!.backtrace.reject!{ |_| _[/\/gems\/concurrent-ruby-/] }
11
+ $!.backtrace.reject!{ |_| _[/\/gems\/ferrum-/] }
12
+ raise Timeout::Error, "#{$!.to_s} after #{timeout} sec in #{mtd}#{" (#{msg.respond_to?(:call) ? msg.call : msg})" if msg}"
13
+ end
14
+
15
+ def until_true timeout, msg = nil
16
+ Module.nesting.first.mhtml self, timeout, __method__, msg do
17
+ begin
18
+ yield
19
+ rescue Ferrum::NodeNotFoundError
20
+ redo
21
+ end or (sleep timeout*0.1; redo)
20
22
  end
21
- browser.define_singleton_method :abort do |msg|
22
- Browser.mhtml path: "temp.mhtml"
23
- puts Thread.current.backtrace
24
- abort msg
23
+ end
24
+
25
+ def until_one type, selector, timeout
26
+ t = nil
27
+ Module.nesting.first.mhtml self, timeout, __method__, ->{ "expected exactly one node for #{type} #{selector.inspect}, got #{t ? t.size : "none"}" } do
28
+ t = begin
29
+ public_method(type).call selector
30
+ rescue Ferrum::NodeNotFoundError
31
+ sleep timeout * 0.1
32
+ redo
33
+ end
34
+ unless 1 == t.size
35
+ sleep timeout * 0.1
36
+ redo
37
+ end
25
38
  end
39
+ t.first
40
+ end
41
+
42
+ def abort msg_or_cause
43
+ # puts (msg_or_cause.respond_to?(:backtrace) ? msg_or_cause : Thread.current).backtrace
44
+ puts (msg_or_cause.respond_to?(:full_message) ? msg_or_cause.full_message : Thread.current.backtrace)
45
+ mhtml path: "temp.mhtml"
46
+ puts "dumped to ./temp.mhtml"
47
+ Kernel.abort msg_or_cause.to_s
26
48
  end
49
+
50
+ end
51
+ Ferrum::Page.include Common
52
+ Ferrum::Frame.include Common
53
+
54
+ require "browser_reposition"
55
+ Ferrum::Browser.include Common, BrowserReposition
56
+ def self.new **_
57
+ Ferrum::Browser.new(**_).tap(&:reposition)
27
58
  end
59
+
28
60
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ferrum_common
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Victor Maslov aka Nakilon
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2023-01-07 00:00:00.000000000 Z
11
+ date: 2023-02-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: ferrum
@@ -24,6 +24,20 @@ dependencies:
24
24
  - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: browser_reposition
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
27
41
  description:
28
42
  email: nakilon@gmail.com
29
43
  executables: []