locatine 0.02637 → 0.03050

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