locatine 0.02542 → 0.02878

Sign up to get free protection for your applications and to get access to all the features.
Files changed (56) hide show
  1. checksums.yaml +5 -5
  2. data/README.md +269 -296
  3. data/bin/locatine-daemon.rb +4 -2
  4. data/lib/locatine.rb +14 -2
  5. data/lib/locatine/daemon.rb +48 -56
  6. data/lib/locatine/daemon_helpers/methods.rb +85 -0
  7. data/lib/locatine/element.rb +32 -0
  8. data/lib/locatine/error.rb +14 -0
  9. data/lib/locatine/logger.rb +69 -0
  10. data/lib/locatine/results.rb +128 -0
  11. data/lib/locatine/results_helpers/common.rb +68 -0
  12. data/lib/locatine/results_helpers/find_by_magic.rb +121 -0
  13. data/lib/locatine/results_helpers/guess.rb +76 -0
  14. data/lib/locatine/results_helpers/info_generator.rb +79 -0
  15. data/lib/locatine/{for_search → results_helpers}/xpath_generator.rb +18 -11
  16. data/lib/locatine/scripts/element.js +40 -0
  17. data/lib/locatine/scripts/page.js +54 -0
  18. data/lib/locatine/scripts/parent.js +6 -0
  19. data/lib/locatine/session.rb +107 -0
  20. data/lib/locatine/version.rb +4 -2
  21. metadata +40 -49
  22. data/lib/locatine/app/background.js +0 -8
  23. data/lib/locatine/app/content.css +0 -43
  24. data/lib/locatine/app/content.js +0 -149
  25. data/lib/locatine/app/devtools.html +0 -1
  26. data/lib/locatine/app/devtools.js +0 -3
  27. data/lib/locatine/app/manifest.json +0 -20
  28. data/lib/locatine/app/popup.css +0 -47
  29. data/lib/locatine/app/popup.html +0 -19
  30. data/lib/locatine/app/popup.js +0 -65
  31. data/lib/locatine/daemon_helpers.rb +0 -52
  32. data/lib/locatine/for_search.rb +0 -6
  33. data/lib/locatine/for_search/data_generate.rb +0 -67
  34. data/lib/locatine/for_search/data_logic.rb +0 -98
  35. data/lib/locatine/for_search/defaults.rb +0 -40
  36. data/lib/locatine/for_search/dialog_logic.rb +0 -107
  37. data/lib/locatine/for_search/element_selection.rb +0 -80
  38. data/lib/locatine/for_search/file_work.rb +0 -67
  39. data/lib/locatine/for_search/find_by_guess.rb +0 -67
  40. data/lib/locatine/for_search/find_by_locator.rb +0 -59
  41. data/lib/locatine/for_search/find_by_magic.rb +0 -65
  42. data/lib/locatine/for_search/find_logic.rb +0 -79
  43. data/lib/locatine/for_search/helpers.rb +0 -106
  44. data/lib/locatine/for_search/highlight.rb +0 -41
  45. data/lib/locatine/for_search/listening.rb +0 -48
  46. data/lib/locatine/for_search/merge.rb +0 -40
  47. data/lib/locatine/for_search/name_helper.rb +0 -51
  48. data/lib/locatine/for_search/page_work.rb +0 -126
  49. data/lib/locatine/for_search/public.rb +0 -179
  50. data/lib/locatine/for_search/saying.rb +0 -196
  51. data/lib/locatine/large_scripts/css.js +0 -21
  52. data/lib/locatine/large_scripts/dimensions.js +0 -17
  53. data/lib/locatine/large_scripts/element.js +0 -30
  54. data/lib/locatine/large_scripts/page.js +0 -60
  55. data/lib/locatine/scope.rb +0 -88
  56. data/lib/locatine/search.rb +0 -67
@@ -1,7 +1,9 @@
1
1
  #!Locatine-daemon...
2
+ # frozen_string_literal: true
3
+
2
4
  require 'locatine'
3
- args = Hash[ ARGV.join(' ').scan(/--?([^=\s]+)(?:=(\S+))?/) ]
4
- args = args.inject({}){|memo,(k,v)| memo[k.to_sym] = v; memo}
5
+ args = Hash[ARGV.join(' ').scan(/--?([^=\s]+)(?:=(\S+))?/)]
6
+ args = args.each_with_object({}) { |(k, v), memo| memo[k.to_sym] = v; }
5
7
  args.each_pair do |key, value|
6
8
  Locatine::Daemon.set key, value
7
9
  end
@@ -1,4 +1,16 @@
1
- require 'locatine/search'
2
- require 'locatine/scope'
1
+ # frozen_string_literal: true
2
+
3
+ require 'locatine/daemon_helpers/methods'
4
+ require 'locatine/results_helpers/common'
5
+ require 'locatine/results_helpers/find_by_magic'
6
+ require 'locatine/results_helpers/guess'
7
+ require 'locatine/results_helpers/info_generator'
8
+ require 'locatine/results_helpers/xpath_generator'
9
+
10
+ require 'locatine/logger'
3
11
  require 'locatine/version'
4
12
  require 'locatine/daemon'
13
+ require 'locatine/session'
14
+ require 'locatine/element'
15
+ require 'locatine/results'
16
+ require 'locatine/error'
@@ -1,6 +1,7 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'sinatra/base'
2
4
  require 'json'
3
- require 'locatine/daemon_helpers'
4
5
 
5
6
  module Locatine
6
7
  #
@@ -8,86 +9,77 @@ module Locatine
8
9
  #
9
10
  # run Locatine::Daemon.run!
10
11
  class Daemon < Sinatra::Base
11
- include Locatine::DaemonHelpers
12
- configure do
13
- set :search, nil
14
- end
12
+ include Locatine::DaemonHelpers::Methods
15
13
 
16
- get '/app' do
17
- { app: File.join(Locatine::HOME, 'app').to_s }.to_json
14
+ configure do
15
+ set :sessions, {}
16
+ set :selenium, ENV['SELENIUM'] || 'http://localhost:4444'
17
+ set :headers, 'Content-Type' => 'application/json'
18
+ set :port, 7733
18
19
  end
19
20
 
21
+ # own calls
20
22
  get '/' do
21
- redirect 'https://github.com/sseleznevqa/locatine#using-as-a-daemon'
23
+ redirect 'https://github.com/sseleznevqa/locatine'
22
24
  end
23
25
 
24
- get '/stop' do
26
+ get '/locatine/stop' do
25
27
  Locatine::Daemon.quit!
26
28
  { result: 'dead' }.to_json
27
29
  end
28
30
 
29
- post '/chromedriver' do
30
- Webdrivers::Chromedriver.required_version = params['version']
31
- { version: Webdrivers::Chromedriver.required_version }.to_json
32
- end
33
-
34
- get '/chromedriver' do
35
- { path: Webdrivers::Chromedriver.update }.to_json
31
+ post '/locatine/session/*' do
32
+ session = request.path_info.split('/').last
33
+ result = settings.sessions[session].configure(params)
34
+ { result: result }.to_json
36
35
  end
37
36
 
38
- post '/geckodriver' do
39
- Webdrivers::Geckodriver.required_version = params['version']
40
- { version: Webdrivers::Geckodriver.required_version }.to_json
41
- end
37
+ def send_error(error)
38
+ status error.status
42
39
 
43
- get '/geckodriver' do
44
- { path: Webdrivers::Geckodriver.update }.to_json
40
+ error.answer
45
41
  end
46
42
 
47
- post 'iedriver' do
48
- Webdrivers::IEdriver.required_version = params['version']
49
- { version: Webdrivers::IEdriver.required_version }.to_json
50
- end
43
+ # selenium calls
44
+ post '/wd/hub/session/*/element' do
45
+ content_type settings.headers['Content-Type']
46
+ results = settings.sessions[session_id].find(params, element_id)
47
+ return send_error(results.first) if results.first.class == Locatine::Error
51
48
 
52
- get '/iedriver' do
53
- { path: Webdrivers::IEdriver.update }.to_json
49
+ status 200
50
+ results.empty? ? raise_not_found : { value: results.first.answer }.to_json
54
51
  end
55
52
 
56
- post '/connect' do
57
- steal
58
- { result: true }.to_json
59
- end
53
+ post '/wd/hub/session/*/elements' do
54
+ content_type settings.headers['Content-Type']
55
+ results = settings.sessions[session_id].find(params, element_id)
56
+ return send_error(results.first) if results.first.class == Locatine::Error
60
57
 
61
- post '/lctr' do
62
- data = Hash[params.map { |k, v| [k.to_sym, v] }]
63
- data.each { |k, v| data[k] = false if v == 'false' }
64
- search.lctr(data).to_json
58
+ status 200
59
+ answer = results.empty? ? [] : results.map(&:answer)
60
+ { value: answer }.to_json
65
61
  end
66
62
 
67
- post '/set' do
68
- hash = params
69
- search.json = hash['json'] if hash['json']
70
- warn 'You cannot set browser like this. Use /connect' if hash['browser']
71
- params.each_pair do |key, value|
72
- unless (key == 'browser') || (key == 'json')
73
- value = false if value == 'false'
74
- search.instance_variable_set("@#{key}", value)
75
- end
76
- end
77
- { result: true }.to_json
63
+ post '/wd/hub/session' do
64
+ result = call_process('post')
65
+ the_session = JSON.parse(result)['value']['sessionId']
66
+ caps = params['desiredCapabilities']
67
+ locatine_caps = caps['locatine'] if caps
68
+ settings.sessions[the_session] = Locatine::Session
69
+ .new(selenium, the_session)
70
+ settings.sessions[the_session].configure(locatine_caps)
71
+ result
78
72
  end
79
73
 
80
- def search
81
- return settings.search unless settings.search.nil?
82
-
83
- settings.search = Locatine::Search.new
84
- settings.search.browser.quit
85
- settings.search
74
+ delete '/wd/hub/session/*' do
75
+ settings.sessions[session_id] = nil
76
+ call_process('delete')
86
77
  end
87
78
 
88
- def params
89
- request.body.rewind
90
- JSON.parse request.body.read
79
+ %w[get post put patch delete].each do |verb|
80
+ send(verb, '/wd/hub/*') do
81
+ call_process(verb)
82
+ end
91
83
  end
92
84
  end
93
85
  end
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'net/http'
4
+
5
+ module Locatine
6
+ module DaemonHelpers
7
+ #
8
+ # Methods that are used by daemon
9
+ module Methods
10
+ def api_request(type, path, query_string, body, new_headers)
11
+ uri = make_uri(path, query_string)
12
+ req = Net::HTTP.const_get(type).new(uri,
13
+ settings.headers.merge(new_headers))
14
+ req.body = body.read
15
+ Net::HTTP.new(uri.hostname, uri.port).start { |http| http.request(req) }
16
+ end
17
+
18
+ def make_uri(path, query_string)
19
+ parsed = URI.parse selenium
20
+ URI::HTTP.build(
21
+ host: parsed.host,
22
+ port: parsed.port,
23
+ path: path,
24
+ query: query_string
25
+ )
26
+ end
27
+
28
+ def all_headers(response)
29
+ header_list = {}
30
+ response.header.each_capitalized do |k, v|
31
+ header_list[k] = v unless k == 'Transfer-Encoding'
32
+ end
33
+ header_list
34
+ end
35
+
36
+ def incomming_headers(request)
37
+ request.env.map do |header, value|
38
+ if header.start_with?('HTTP_')
39
+ [header[5..-1].split('_').map(&:capitalize).join('-'), value]
40
+ end
41
+ end .compact.to_h
42
+ end
43
+
44
+ def send_answer(response)
45
+ content_type settings.headers['Content-Type']
46
+ status response.code
47
+ headers all_headers(response)
48
+ response.body
49
+ end
50
+
51
+ def call_process(verb)
52
+ start_request = Thread.new do
53
+ api_request(verb.capitalize, request.path_info, request.query_string,
54
+ request.body, incomming_headers(request))
55
+ end
56
+ send_answer(start_request.value)
57
+ end
58
+
59
+ def params
60
+ request.body.rewind
61
+ JSON.parse request.body.read
62
+ end
63
+
64
+ def selenium
65
+ settings.selenium
66
+ end
67
+
68
+ def raise_not_found
69
+ status 404
70
+ { value: { error: 'no such element',
71
+ message: 'no such element: Unable to locate element',
72
+ stacktrace: 'Locatine has no idea too' } }.to_json
73
+ end
74
+
75
+ def session_id
76
+ request.path_info.split('/')[4]
77
+ end
78
+
79
+ def element_id
80
+ path_array = request.path_info.split('/')
81
+ path_array.size >= 7 ? path_array[6] : nil
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Locatine
4
+ #
5
+ # Locatine single element
6
+ class Element
7
+ attr_accessor :answer
8
+
9
+ def initialize(session, element_code)
10
+ unless element_code
11
+ raise ArgumentError, 'Cannot init element with no element data'
12
+ end
13
+
14
+ @session = session
15
+ @answer = element_code
16
+ end
17
+
18
+ def parent
19
+ parent = File.read("#{HOME}/scripts/parent.js")
20
+ new_answer = @session.execute_script(parent, self)
21
+ new_answer.nil? ? nil : Locatine::Element.new(@session, new_answer)
22
+ end
23
+
24
+ def info
25
+ return @info if @info
26
+
27
+ info = File.read("#{HOME}/scripts/element.js")
28
+ @info = @session.execute_script(info, self)
29
+ @info
30
+ end
31
+ end
32
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Locatine
4
+ #
5
+ # Simple error
6
+ class Error
7
+ attr_accessor :answer, :status
8
+
9
+ def initialize(response)
10
+ @answer = response.body
11
+ @status = response.code
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'colorize'
4
+
5
+ module Locatine
6
+ #
7
+ # Methods for sending lines to STDOUT
8
+ module Logger
9
+ def warn(text)
10
+ puts "WARNING: #{Time.now}: ".yellow + text
11
+ end
12
+
13
+ def log(text)
14
+ puts "MESSAGE: #{Time.now}: ".magenta + text
15
+ end
16
+
17
+ def locatine_error(text)
18
+ puts "ERROR: #{Time.now}: ".red + text.red
19
+ end
20
+
21
+ def warn_locator
22
+ warn("Locator is broken. For #{name} by"\
23
+ " #{@locator['using']}>>>'#{locator['value']}'")
24
+ end
25
+
26
+ def warn_guess
27
+ warn("Locatine is trying to guess what is #{@name}")
28
+ end
29
+
30
+ def log_start
31
+ log "#{Time.now}: Locatine is working on #{@name}"
32
+ end
33
+
34
+ def warn_magic
35
+ warn "Locatine cannot find element #{@name} by usual methods and "\
36
+ 'starting to look for some element that is similar. Retrived '\
37
+ 'element may be not the one that is desired from this point'
38
+ end
39
+
40
+ def warn_lost
41
+ warn "Locatine found nothing for #{@name}"
42
+ end
43
+
44
+ def warn_unstable_page
45
+ warn 'Locatine detected stale element error. It means some elements'\
46
+ ' found by locatine are not attached to DOM anymore.'\
47
+ ' Page is unstable. Starting searching process again'
48
+ end
49
+
50
+ def log_found
51
+ log "Locatine found something as #{@name}"
52
+ log "XPATH == #{generate_xpath(raw_info)}"
53
+ end
54
+
55
+ def raise_script_error(script, args, answer)
56
+ locatine_error 'Locatine faced an error while trying to perform '\
57
+ "js script.\n ---Script was: #{script}\n\n ---Arguments was: #{args}"\
58
+ "\n\n ---Answer was: #{answer}"
59
+ raise answer['error']
60
+ end
61
+
62
+ def warn_error_detected(answer)
63
+ warn "selenium is returning an error with code #{answer.code} "\
64
+ 'It will be returned to your code. It can be locatine internal '\
65
+ 'bug, selenium problem (dead browser for example) or something '\
66
+ 'in your code (invalid locator for example)'
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,128 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Locatine
4
+ #
5
+ # Locatine results container
6
+ class Results < Array
7
+ include Locatine::ResultsHelpers::InfoGenerator
8
+ include Locatine::ResultsHelpers::XpathGenerator
9
+ include Locatine::ResultsHelpers::FindByMagic
10
+ include Locatine::ResultsHelpers::Common
11
+ include Locatine::Logger
12
+ include Locatine::ResultsHelpers::Guess
13
+
14
+ attr_accessor :name, :config, :locator
15
+
16
+ def configure(session, locator, parent)
17
+ @session = session
18
+ @locator = locator.clone
19
+ read_locator
20
+ @parent = parent
21
+ end
22
+
23
+ def simple_find
24
+ path = @parent ? "/element/#{@parent}/elements" : '/elements'
25
+ response = @session.api_request(path, 'Post', @locator.to_json)
26
+ found = JSON.parse(response.body)
27
+ error_present = (found['value'].class == Hash) && found['value']['error']
28
+ return error_routine(response) if error_present
29
+
30
+ found['value'].each do |item|
31
+ push Locatine::Element.new(@session, item)
32
+ end
33
+ self
34
+ # {"value"=>
35
+ # [{"element-6066-11e4-a52e-4f735466cecf"=>"c95a0580-4ac7-4c6d-..."},
36
+ # {"element-6066-11e4-a52e-4f735466cecf"=>"f419f6cf-1a04-4bc8-b246-..."},
37
+ # {"element-6066-11e4
38
+ end
39
+
40
+ def error_routine(answer)
41
+ @error = Locatine::Error.new(answer)
42
+ warn_error_detected(answer)
43
+ push @error
44
+ end
45
+
46
+ def classic_find
47
+ first_attempt
48
+ locating = (@locator['value'].empty? || tolerance.positive?) && !@error
49
+ return unless locating
50
+
51
+ second_attempt
52
+ third_attempt if known
53
+ forth_attempt if known
54
+ end
55
+
56
+ def first_attempt
57
+ log_start
58
+ simple_find unless @locator['value'].empty?
59
+ warn_locator if !@locator['value'].empty? && empty?
60
+ end
61
+
62
+ def third_attempt
63
+ base = {}
64
+ base['0'] = known['0']
65
+ find_by_data(base) if empty?
66
+ end
67
+
68
+ def second_attempt
69
+ find_by_data if known && empty?
70
+ end
71
+
72
+ def forth_attempt
73
+ base = {}
74
+ base['0'] = known['0'].select { |item| trusted.include?(item['name']) }
75
+ find_by_data(base) if empty? && !trusted.empty? && !base['0'].empty?
76
+ end
77
+
78
+ def find
79
+ timer
80
+ classic_find
81
+ guess if name_only?
82
+ return self unless empty?
83
+
84
+ find_by_magic if known && tolerance.positive?
85
+ similar? ? found : not_found
86
+ end
87
+
88
+ def found
89
+ log_found
90
+ uniq
91
+ end
92
+
93
+ def not_found
94
+ warn_lost
95
+ []
96
+ end
97
+
98
+ def read_locator
99
+ case @locator['using']
100
+ when 'css selector'
101
+ # "button/*{json}*/"
102
+ read_locator_routine(%r{/\*(.*)\*/$})
103
+ when 'xpath'
104
+ # "//button['{json}']"
105
+ read_locator_routine(/\[\'(.*)\'\]$/)
106
+ when 'locatine'
107
+ read_locator_routine(/(.*)/)
108
+ end
109
+ end
110
+
111
+ def read_locator_routine(regexp)
112
+ matched = @locator['value'].match(regexp)
113
+ @config = matched ? config_provided(matched[1]) : {}
114
+ @locator['value'] = @locator['value'].gsub(matched[0], '') if matched
115
+ @locator = @config['locator'] if @config['locator']
116
+ @name = @config['name'] || @locator['value']
117
+ end
118
+
119
+ def config_provided(config)
120
+ JSON.parse(config)
121
+ rescue StandardError
122
+ result = {}
123
+ result['tolerance'] = 0 if config.start_with?('exactly')
124
+ result['name'] = config.gsub('exactly ', '')
125
+ result
126
+ end
127
+ end
128
+ end