locatine 0.01300 → 0.01309

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: dcac98a7110904e47f05c216b6b78032013f626f
4
- data.tar.gz: d2560254389abffb699a635a40dd211bebcb0abc
3
+ metadata.gz: 36abed5fc9bb50605d737f8f56be664970461ec9
4
+ data.tar.gz: 3c5b291cf577d416c2396643425d1129c9957b50
5
5
  SHA512:
6
- metadata.gz: 58a755f90fa8442934185daf90f33d0655e215c7487d21fb44dbd760ad3004bc222bca599ca77c221342a86dde2e665a0f9fdefc365456f06c5389cb60612a54
7
- data.tar.gz: 8f0cf282edc1cc4652c8badf6d266814bdd22ab47efb88e708ac381eb75100d895e9e1ccc5306628fb1282eaf3927d8273a03006a96bfd66af638700558ee18c
6
+ metadata.gz: a8e84bd41e4c91bc7ad2dfcffd9c1ca92791aa48a3f658b4a21ad73d61215d6b3afd7b7baeeed69b9d45ee1dd0cafca876f9abecc4540b8ffd9ad30d2d448299
7
+ data.tar.gz: 8415caf49216b65470b0668c9f98c470c5140982a3328750a8f68bc9d348b496c4b667058efe852ba18f78b65d7c02934716ba4ea9c262d4b88c3994657fa78d
@@ -56,15 +56,17 @@ async function refreshData(){
56
56
  };
57
57
 
58
58
  async function getSelected(value){
59
- const magic_div = document.getElementById("locatine_magic_div");
60
- const tagName = value.tagName;
61
- const array = Array.prototype.slice.call( document.getElementsByTagName(tagName) );
62
- const index = array.indexOf(value);
63
- if (document.locatine_selected != value) {
64
- document.locatine_selected = value;
65
- await set_value("locatine_confirm", "selected");
66
- magic_div.setAttribute("tag", tagName);
67
- magic_div.setAttribute("index", index);
59
+ if (value) {
60
+ const magic_div = document.getElementById("locatine_magic_div");
61
+ const tagName = value.tagName;
62
+ const array = Array.prototype.slice.call( document.getElementsByTagName(tagName) );
63
+ const index = array.indexOf(value);
64
+ if (document.locatine_selected != value) {
65
+ document.locatine_selected = value;
66
+ await set_value("locatine_confirm", "selected");
67
+ magic_div.setAttribute("tag", tagName);
68
+ magic_div.setAttribute("index", index);
69
+ }
68
70
  }
69
71
  };
70
72
 
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "Locatine app",
3
- "version": "0.01300",
3
+ "version": "0.01309",
4
4
  "description": "Messaging from browser to main app",
5
5
  "devtools_page": "devtools.html",
6
6
  "permissions": ["activeTab", "storage", "contextMenus", "tabs"],
@@ -40,10 +40,36 @@ module Locatine
40
40
 
41
41
  ##
42
42
  # Generating array of hashes representing data of the element
43
- def get_element_info(element, vars)
43
+ def get_element_info(element, vars, depth)
44
44
  attrs = get_dynamic_attributes(element, vars)
45
45
  attrs.push get_dynamic_tag(element, vars)
46
46
  attrs += get_dynamic_text(element, vars)
47
+ attrs += get_dynamic_css(element, vars) if depth.to_i.zero?
48
+ attrs
49
+ end
50
+
51
+ def hash_by_style(style, value, vars)
52
+ value.gsub!(vars[style.to_sym], "\#{#{style}}") if vars[style.to_sym]
53
+ { 'name' => style, 'value' => value, 'type' => 'css' }
54
+ end
55
+
56
+ def get_raw_css(element)
57
+ test_script = 'return typeof(arguments[0])'
58
+ ok = engine.execute_script(test_script, element) == 'object'
59
+ script = 'return getComputedStyle(arguments[0]).cssText'
60
+ return engine.execute_script(script, element) if ok
61
+ end
62
+
63
+ def get_dynamic_css(element, vars)
64
+ attrs = []
65
+ raw = get_raw_css(element)
66
+ if raw
67
+ styles = css_text_to_hash(get_raw_css(element))
68
+ (styles.to_a - @default_styles).to_h.each_pair do |style, value|
69
+ hash = hash_by_style(style, value, vars)
70
+ attrs.push(hash) if hash
71
+ end
72
+ end
47
73
  attrs
48
74
  end
49
75
 
@@ -60,14 +86,13 @@ module Locatine
60
86
  ##
61
87
  # Getting element\\parents information
62
88
  def get_family_info(element, vars)
63
- current_depth = 0
89
+ i = 0
64
90
  attributes = {}
65
- while current_depth != @depth
66
- attributes[current_depth.to_s] = get_element_info(element, vars)
67
- current_depth += 1
91
+ while i != @depth
92
+ attributes[i.to_s] = get_element_info(element, vars, i)
93
+ i += 1
68
94
  element = element.parent
69
- # Sometimes watir is not returning a valid parent that's why:
70
- current_depth = @depth unless element.parent.exists?
95
+ i = @depth unless element.exists?
71
96
  end
72
97
  attributes
73
98
  end
@@ -0,0 +1,45 @@
1
+ module Locatine
2
+ ##
3
+ # Looking for elements by css values
4
+ module FindByCss
5
+ private
6
+
7
+ def css_array_by_data(data, vars)
8
+ q_css = []
9
+ get_trusted(data['0']).each do |hash|
10
+ if hash['type'] == 'css'
11
+ value = process_string(hash['value'], vars) if vars[hash['name']]
12
+ q_css.push("#{hash['name']}: #{value || hash['value']}")
13
+ end
14
+ end
15
+ q_css
16
+ end
17
+
18
+ def return_caught_elements(caught)
19
+ all = []
20
+ caught.each do |i|
21
+ @help_hash[i[0]] ||= engine.elements(tag_name: i[0].downcase).to_a
22
+ elm = @help_hash[i[0]][i[1].to_i]
23
+ all.push(elm) if elm
24
+ end
25
+ all
26
+ end
27
+
28
+ def select_elements_from_raws_by_css(q_css, raws)
29
+ all = []
30
+ @help_hash = {}
31
+ q_css.each do |item|
32
+ caught = (raws.select { |i| i[2].include?(item) })
33
+ all += return_caught_elements(caught)
34
+ end
35
+ all
36
+ end
37
+
38
+ def full_find_by_css(data, vars)
39
+ q_css = css_array_by_data(data, vars)
40
+ script = File.read("#{HOME}/large_scripts/css.js")
41
+ raws = engine.execute_script(script)
42
+ select_elements_from_raws_by_css(q_css, raws)
43
+ end
44
+ end
45
+ end
@@ -35,20 +35,21 @@ module Locatine
35
35
  all = []
36
36
  data.each_pair do |depth, array|
37
37
  get_trusted(array).each do |hash|
38
- all += one_option_array(hash, vars, depth)
38
+ all += one_option_array(hash, vars, depth).to_a
39
39
  end
40
40
  end
41
+ all += full_find_by_css(data, vars)
41
42
  all
42
43
  end
43
44
 
44
45
  def one_option_array(hash, vars, depth)
45
46
  case hash['type']
46
47
  when 'tag'
47
- find_by_tag(hash, vars, depth).to_a
48
+ find_by_tag(hash, vars, depth)
48
49
  when 'text'
49
- find_by_text(hash, vars, depth).to_a
50
+ find_by_text(hash, vars, depth)
50
51
  when 'attribute'
51
- find_by_attribute(hash, vars, depth).to_a
52
+ find_by_attribute(hash, vars, depth)
52
53
  end
53
54
  end
54
55
  end
@@ -27,6 +27,32 @@ module Locatine
27
27
  Watir::Browser.new(:chrome, switches: ["--load-extension=#{HOME}/app"])
28
28
  end
29
29
 
30
+ def import_browser(browser)
31
+ s_class = browser.class.superclass
32
+ b = right_browser unless browser
33
+ b = browser if browser.class == Watir::Browser
34
+ b = Watir::Browser.new(browser) if s_class == Selenium::WebDriver::Driver
35
+ @browser = b
36
+ @default_styles = default_styles.to_a
37
+ end
38
+
39
+ def css_text_to_hash(text)
40
+ almost_hash = []
41
+ array = text[0..-2].split('; ')
42
+ array.each do |item|
43
+ almost_hash.push item.split(': ')
44
+ end
45
+ almost_hash.to_h
46
+ end
47
+
48
+ def default_styles
49
+ css =
50
+ engine.execute_script("const dummy = document.createElement('dummy');
51
+ document.body.appendChild(dummy);
52
+ return getComputedStyle(dummy).cssText;")
53
+ css_text_to_hash(css)
54
+ end
55
+
30
56
  def process_string(str, vars)
31
57
  str = str.to_s
32
58
  thevar = str.match(/\#{([^\#{]*)}/)[1] unless str.match(/\#{(.*)}/).nil?
@@ -0,0 +1,21 @@
1
+ function walk(elm, result) {
2
+ let node;
3
+
4
+ const tagName = elm.tagName;
5
+ const array = Array.prototype.slice.call( document.getElementsByTagName(tagName) );
6
+ const index = array.indexOf(elm);
7
+
8
+ const item = [tagName, index, getComputedStyle(elm).cssText]
9
+ result.push(item)
10
+
11
+ // Handle child elements
12
+ for (node = elm.firstChild; node; node = node.nextSibling) {
13
+ if (node.nodeType === 1) { // 1 == Element
14
+ result = walk(node, result);
15
+ }
16
+ }
17
+ return result
18
+ }
19
+ let array = walk(document.body,[]);
20
+ array.shift();
21
+ return array;
@@ -21,15 +21,19 @@ module Locatine
21
21
  # consider it trusted.
22
22
  #
23
23
  # +scope+ will be used in search (if not provided) defaulkt is "Default"
24
+ #
25
+ # +tolerance+ Shows how similar must be an element found as alternative
26
+ # to the lost one. Default is 33 which means that if less than 33% of
27
+ # metrics of alternative elements are the same as of the lost element
28
+ # will not be returned
24
29
  def initialize(json: './Locatine_files/default.json',
25
30
  depth: 3,
26
31
  browser: nil,
27
32
  learn: ENV['LEARN'].nil? ? false : true,
28
33
  stability_limit: 10,
29
34
  scope: 'Default',
30
- tolerance: 15)
31
- browser ||= right_browser
32
- @browser = browser
35
+ tolerance: 33)
36
+ import_browser browser
33
37
  import_file(json)
34
38
  @depth = depth
35
39
  @learn = learn
@@ -104,5 +108,9 @@ module Locatine
104
108
  def json=(value)
105
109
  import_file(value)
106
110
  end
111
+
112
+ def browser=(value)
113
+ import_browser(value)
114
+ end
107
115
  end
108
116
  end
@@ -10,6 +10,7 @@ require 'locatine/helpers'
10
10
  require 'locatine/file_work'
11
11
  require 'locatine/highlight'
12
12
  require 'locatine/find_logic'
13
+ require 'locatine/find_by_css'
13
14
  require 'locatine/dialog_logic'
14
15
  require 'locatine/find_by_magic'
15
16
  require 'locatine/find_by_guess'
@@ -30,6 +31,7 @@ module Locatine
30
31
  include Locatine::FileWork
31
32
  include Locatine::FindLogic
32
33
  include Locatine::Highlight
34
+ include Locatine::FindByCss
33
35
  include Locatine::FindByMagic
34
36
  include Locatine::DialogLogic
35
37
  include Locatine::FindByGuess
@@ -40,11 +42,11 @@ module Locatine
40
42
 
41
43
  attr_accessor :data,
42
44
  :depth,
43
- :browser,
44
45
  :learn,
45
46
  :stability_limit,
46
47
  :scope,
47
48
  :tolerance
48
- attr_reader :json
49
+ attr_reader :json,
50
+ :browser
49
51
  end
50
52
  end
@@ -1,6 +1,6 @@
1
1
  module Locatine
2
2
  # constants here...
3
- VERSION = '0.01300'.freeze
3
+ VERSION = '0.01309'.freeze
4
4
  NAME = 'locatine'.freeze
5
5
  HOME = if File.readable?("#{Dir.pwd}/lib/#{Locatine::NAME}")
6
6
  "#{Dir.pwd}/lib/#{Locatine::NAME}"
@@ -17,7 +17,7 @@ module Locatine
17
17
  xpath = "[not(@id = 'locatine_magic_div')]"
18
18
  data.each_pair do |_depth, array|
19
19
  get_trusted(array).each do |hash|
20
- xpath = generate_xpath_part(hash, vars) + xpath
20
+ xpath = generate_xpath_part(hash, vars).to_s + xpath
21
21
  end
22
22
  xpath = '/*' + xpath
23
23
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: locatine
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.01300'
4
+ version: '0.01309'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sergei Seleznev
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-03-13 00:00:00.000000000 Z
11
+ date: 2019-03-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -128,12 +128,14 @@ files:
128
128
  - lib/locatine/dialog_actions.rb
129
129
  - lib/locatine/dialog_logic.rb
130
130
  - lib/locatine/file_work.rb
131
+ - lib/locatine/find_by_css.rb
131
132
  - lib/locatine/find_by_guess.rb
132
133
  - lib/locatine/find_by_locator.rb
133
134
  - lib/locatine/find_by_magic.rb
134
135
  - lib/locatine/find_logic.rb
135
136
  - lib/locatine/helpers.rb
136
137
  - lib/locatine/highlight.rb
138
+ - lib/locatine/large_scripts/css.js
137
139
  - lib/locatine/merge.rb
138
140
  - lib/locatine/public.rb
139
141
  - lib/locatine/search.rb