webaccount 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 7be023b85deb2330712bc3202c2215d20c8024ff074ddcf677f08f7720b8994b
4
+ data.tar.gz: 543880a40c28d83efdd8839927be8c556e752cd5a4a268466af8b184a8f58a48
5
+ SHA512:
6
+ metadata.gz: cc5e55208933dd0aadf508cc198047d0d174313faf8b49d87e7119bec56d2a6f8bbea7c543a53ffa5a487b2825cec7be955fe92e64b826ca1e4157be767c93fc
7
+ data.tar.gz: 7e8eb9a63de804731d720729b6d4cb3a5a2b87b9a59e92b02103358f042d4ae3419265a5f069f07fe6a1810ff1f44cc05d21689c86dffe0cdd3be16cbe1b03c9
@@ -0,0 +1,53 @@
1
+ # Thoran/Selenium/WebDriver/Attempt/attempt.rb
2
+ # Thoran::Selenium::WebDriver::Attempt.attempt
3
+
4
+ # 20200418
5
+ # 0.0.0
6
+
7
+ # Examples:
8
+ # 1. driver.attempt do |driver|
9
+ # driver.get('https://example.com/users/sign_in')
10
+ # enter_username
11
+ # enter_password
12
+ # sign_in
13
+ # end
14
+
15
+ # Changes:
16
+ # 1. + Thoran namespace.
17
+
18
+ module Thoran
19
+ module Selenium
20
+ module WebDriver
21
+ module Driver
22
+ module Attempt
23
+
24
+ def attempt(max_attempts = 3, &block)
25
+ attempts = 0
26
+ loop do
27
+ begin
28
+ yield
29
+ break
30
+ rescue Timeout::Error, Selenium::WebDriver::Error::UnknownError, Selenium::WebDriver::Error::NoSuchElementError => e
31
+ attempts += 1
32
+ if attempts >= max_attempts
33
+ @driver.quit
34
+ puts "Giving up after #{max_attempts} attempts to #{__method__} because #{e}."
35
+ exit
36
+ end
37
+ end
38
+ end
39
+ end
40
+
41
+ end
42
+ end
43
+ end
44
+ end
45
+ end
46
+
47
+ module Selenium
48
+ module WebDriver
49
+ class Driver
50
+ include Thoran::Selenium::WebDriver::Driver::Attempt
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,51 @@
1
+ # Thoran/Selenium/WebDriver/Remote/W3C/BridgeMonkeyPatch/ConvertLocators.convert_locators.rb
2
+ # Thoran::Selenium::WebDriver::Remote::W3C::BridgeMonkeyPatch::ConvertLocators.convert_locators
3
+
4
+ # 20200418
5
+ # 0.0.0
6
+
7
+ # Changes:
8
+ # 1. + Thoran namespace.
9
+
10
+ module Thoran
11
+ module Selenium
12
+ module WebDriver
13
+ module Remote
14
+ module W3C
15
+ module Bridge
16
+ module ConvertLocators
17
+
18
+ def convert_locators(how, what)
19
+ case how.to_s
20
+ when 'class name'
21
+ how = 'css selector'
22
+ what = ".#{escape_css(what)}"
23
+ when 'id'
24
+ how = 'css selector'
25
+ what = "##{escape_css(what)}"
26
+ when 'name'
27
+ how = 'css selector'
28
+ what = "*[name='#{escape_css(what)}']"
29
+ when 'tag name'
30
+ how = 'css selector'
31
+ end
32
+ [how, what]
33
+ end
34
+
35
+ end
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
41
+ end
42
+
43
+ module Selenium
44
+ module WebDriver
45
+ module Remote
46
+ module W3C
47
+ Bridge.prepend(Thoran::Selenium::WebDriver::Remote::W3C::Bridge::ConvertLocators)
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,39 @@
1
+ # Thoran/Selenium/WebDriver/SearchContext/ElementPresentQ/element_presentQ.rb
2
+ # Thoran::Selenium::WebDriver::SearchContext::ElementPresentQ.element_present?
3
+
4
+ # 20200418
5
+ # 0.0.0
6
+
7
+ # Examples:
8
+ # 1. driver.element_present?(:xpath, '//a[@id="submit"]')
9
+ # 2. driver.element_present?(:id, @logout_button_id)
10
+
11
+ # Changes:
12
+ # 1. + Thoran namespace.
13
+
14
+ module Thoran
15
+ module Selenium
16
+ module WebDriver
17
+ module SearchContext
18
+ module ElementPresentQ
19
+
20
+ def element_present?(selector_type, selector)
21
+ bridge.find_element_by(selector_type, selector)
22
+ true
23
+ rescue ::Selenium::WebDriver::Error::NoSuchElementError
24
+ false
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ module Selenium
34
+ module WebDriver
35
+ class Driver
36
+ include Thoran::Selenium::WebDriver::SearchContext::ElementPresentQ
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,39 @@
1
+ # Thoran/Selenium/WebDriver/SearchContext/ElementsPresentQ/elements_presentQ.rb
2
+ # Thoran::Selenium::WebDriver::SearchContext::ElementsPresentQ.elements_present?
3
+
4
+ # 20200418
5
+ # 0.0.0
6
+
7
+ # Examples:
8
+ # 1. driver.elements_present?(:xpath, '//li[@class="item"]')
9
+ # 2. driver.elements_present?(:css, 'li.item')
10
+
11
+ # Changes:
12
+ # 1. + Thoran namespace.
13
+
14
+ module Thoran
15
+ module Selenium
16
+ module WebDriver
17
+ module SearchContext
18
+ module ElementsPresentQ
19
+
20
+ def elements_present?(selector_type, selector)
21
+ bridge.find_elements_by(selector_type, selector)
22
+ true
23
+ rescue ::Selenium::WebDriver::Error::NoSuchElementError
24
+ false
25
+ end
26
+
27
+ end
28
+ end
29
+ end
30
+ end
31
+ end
32
+
33
+ module Selenium
34
+ module WebDriver
35
+ class Driver
36
+ include Thoran::Selenium::WebDriver::SearchContext::ElementsPresentQ
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,68 @@
1
+ # Thoran/Selenium/WebDriver/Setup/setup.rb
2
+ # Thoran::Selenium::WebDriver.setup
3
+
4
+ # 20200418
5
+ # 0.0.0
6
+
7
+ # Usage:
8
+ # 1. Binary is in the path:
9
+ # Selenium::WebDriver.setup('chrome') # String - lowercase
10
+ # Selenium::WebDriver.setup('Chrome') # String - capitalised
11
+ # Selenium::WebDriver.setup(:chrome) # Symbol - lowercase
12
+ # Selenium::WebDriver.setup(:Chrome) # Symbol - capitalised
13
+ # 2. Binary is not in the path:
14
+ # Selenium::WebDriver.setup('~/Applications/Firefox.app/Contents/MacOS/firefox-bin')
15
+
16
+ # Changes:
17
+ # 1. + Thoran namespace.
18
+
19
+ require 'selenium-webdriver'
20
+ require 'Thoran/String/ToConst/to_const'
21
+
22
+ module Thoran
23
+ module Selenium
24
+ module WebDriver
25
+ module Setup
26
+
27
+ module_function
28
+
29
+ def setup(path_or_browser)
30
+ if File.exist?(File.expand_path(path_or_browser.to_s))
31
+ path = path_or_browser
32
+ Selenium::WebDriver.at(path)
33
+ Selenium::WebDriver.for(selenium_browser(path))
34
+ else
35
+ browser = path_or_browser.to_sym.downcase
36
+ Selenium::WebDriver.for(browser)
37
+ end
38
+ end
39
+
40
+ private
41
+
42
+ def at(path)
43
+ selenium_binary_constant(path).path = File.expand_path(path)
44
+ end
45
+
46
+ def selenium_browser(path)
47
+ case path
48
+ when /chrome/i; :chrome
49
+ when /firefox/i; :firefox
50
+ when /safari/i; :safari
51
+ end
52
+ end
53
+
54
+ # This method seem to be necessary only to Firefox, since there are no equivalent constants for Safari or Chrome.
55
+ def selenium_binary_constant(path)
56
+ "Selenium::WebDriver::#{selenium_browser(path).to_s.capitalize}::Binary".to_const
57
+ end
58
+
59
+ end
60
+ end
61
+ end
62
+ end
63
+
64
+ module Selenium
65
+ module WebDriver
66
+ include Thoran::Selenium::WebDriver::Setup
67
+ end
68
+ end
@@ -0,0 +1,11 @@
1
+ # Thoran/Selenium.rb
2
+ # Thoran::Selenium
3
+
4
+ # 20200418
5
+ # 0.0.0
6
+
7
+ require_relative './Selenium/WebDriver/Driver/Attempt/attempt'
8
+ require_relative './Selenium/WebDriver/Remote/W3C/Bridge/ConvertLocators/convert_locators'
9
+ require_relative './Selenium/WebDriver/SearchContext/ElementPresentQ/element_presentQ'
10
+ require_relative './Selenium/WebDriver/SearchContext/ElementsPresentQ/elements_presentQ'
11
+ require_relative './Selenium/WebDriver/Setup/setup'
@@ -0,0 +1,5 @@
1
+ class WebAccount
2
+
3
+ VERSION = '0.2.3'
4
+
5
+ end
data/lib/WebAccount.rb ADDED
@@ -0,0 +1,118 @@
1
+ # WebAccount.rb
2
+ # WebAccount
3
+
4
+ require 'selenium-webdriver'
5
+ require 'Thoran/Selenium'
6
+
7
+ class WebAccount
8
+
9
+ attr_accessor :username
10
+ attr_accessor :password
11
+ attr_writer :user_agent_alias
12
+
13
+ def initialize(
14
+ username: nil, password: nil,
15
+ login_page_url: nil,
16
+ login_page_username_field_id: nil, login_page_password_field_id: nil,
17
+ login_page_submit_button_id: nil,
18
+ logout_button_id: nil,
19
+ logged_out_xpath: nil
20
+ )
21
+ @username, @password = username, password
22
+ @login_page_url = login_page_url
23
+ @login_page_username_field_id, @login_page_password_field_id = login_page_username_field_id, login_page_password_field_id
24
+ @login_page_submit_button_id = login_page_submit_button_id
25
+ @logout_button_id = logout_button_id
26
+ @logged_out_xpath = logged_out_xpath
27
+ end
28
+
29
+ def login(username: nil, password: nil)
30
+ @logged_in = false
31
+ username ||= self.username
32
+ password ||= self.password
33
+ attempts = 0
34
+ driver.attempt do
35
+ get_login_page
36
+ enter_username(username)
37
+ enter_password(password)
38
+ login_page_submit_button.click
39
+ end
40
+ @logged_in = driver_wait.until do
41
+ driver.element_present?(:id, @logout_button_id)
42
+ end
43
+ end
44
+ alias_method :logon, :login
45
+
46
+ def logout
47
+ driver.find_element(:id, @logout_button_id).click
48
+ if logged_out?
49
+ @logged_in = false
50
+ end
51
+ end
52
+ alias_method :logoff, :logout
53
+
54
+ def shutdown
55
+ logout if logged_in?
56
+ driver.quit
57
+ end
58
+
59
+ # predicate methods
60
+
61
+ def logged_in?
62
+ @logged_in
63
+ end
64
+
65
+ def logged_out?
66
+ driver_wait.until do
67
+ driver.element_present?(:xpath, @logged_out_xpath)
68
+ end
69
+ end
70
+
71
+ private
72
+
73
+ def user_agent_alias
74
+ @user_agent_alias ||= @user_agent_alias || :chrome
75
+ end
76
+
77
+ def driver
78
+ @driver ||= (
79
+ driver = Selenium::WebDriver.setup(user_agent_alias)
80
+ driver.manage.timeouts.implicit_wait = 5
81
+ driver
82
+ )
83
+ end
84
+
85
+ def driver_wait
86
+ @driver_wait ||= Selenium::WebDriver::Wait.new(timeout: 180)
87
+ end
88
+
89
+ # logging in
90
+
91
+ def login_page
92
+ driver.get(@login_page_url)
93
+ end
94
+ alias_method :get_login_page, :login_page
95
+
96
+ def enter_username(username = nil)
97
+ username ||= self.username
98
+ username_field.send_keys(username)
99
+ end
100
+
101
+ def username_field
102
+ driver.find_element(:id, @login_page_username_field_id)
103
+ end
104
+
105
+ def enter_password(password = nil)
106
+ password ||= self.password
107
+ password_field.send_keys(password)
108
+ end
109
+
110
+ def password_field
111
+ driver.find_element(:id, @login_page_password_field_id)
112
+ end
113
+
114
+ def login_page_submit_button
115
+ driver.find_element(:id, @login_page_submit_button_id)
116
+ end
117
+
118
+ end
@@ -0,0 +1,6 @@
1
+ # web_account.rb
2
+
3
+ # Notes:
4
+ # 1. This is to enable an alternate means of loading: require 'web_account'.
5
+
6
+ require_relative 'WebAccount'
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: webaccount
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.2.3
5
+ platform: ruby
6
+ authors:
7
+ - thoran
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-04-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: selenium-webdriver
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ description: Easily log into web accounts but just supplying a username and password,
28
+ the URL of the login page, and a few page ids to a subclass of WebAccount.
29
+ email: code@thoran.com
30
+ executables: []
31
+ extensions: []
32
+ extra_rdoc_files: []
33
+ files:
34
+ - lib/Thoran/Selenium.rb
35
+ - lib/Thoran/Selenium/WebDriver/Driver/Attempt/attempt.rb
36
+ - lib/Thoran/Selenium/WebDriver/Remote/W3C/Bridge/ConvertLocators/convert_locators.rb
37
+ - lib/Thoran/Selenium/WebDriver/SearchContext/ElementPresentQ/element_presentQ.rb
38
+ - lib/Thoran/Selenium/WebDriver/SearchContext/ElementsPresentQ/elements_presentQ.rb
39
+ - lib/Thoran/Selenium/WebDriver/Setup/setup.rb
40
+ - lib/WebAccount.rb
41
+ - lib/WebAccount/VERSION.rb
42
+ - lib/web_account.rb
43
+ homepage: http://github.com/thoran/WebAccount
44
+ licenses:
45
+ - Ruby
46
+ metadata: {}
47
+ post_install_message:
48
+ rdoc_options: []
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: '2.5'
56
+ required_rubygems_version: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - ">="
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubygems_version: 3.1.2
63
+ signing_key:
64
+ specification_version: 4
65
+ summary: "# Description: An abstract superclass for navigating a web-based user account
66
+ via Selenium WebDriver."
67
+ test_files: []