-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdeclare_antigen.rb
101 lines (83 loc) · 2.99 KB
/
declare_antigen.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# frozen_string_literal: true
require 'dotenv'
Dotenv.load!
require 'nokogiri'
require 'webdrivers/chromedriver'
require 'watir'
# Declare antigen test health declaration for schools.
#
# @example
#
# DeclareAntigen.new.call
class DeclareAntigen
attr_reader :url, :parent_tz, :parent_name, :childrens_tz, :childrens_names
def self.call
new.call
end
def initialize(url: ENV['URL'], parent_tz: ENV['PARENT_TZ'], parent_name: ENV['PARENT_NAME'], childrens_tz: ENV['CHILDRENS_TZ'], childrens_names: ENV['CHILDRENS_NAMES'])
@url = url
@parent_tz = parent_tz
@parent_name = parent_name
@childrens_tz = childrens_tz.include?(',') ? childrens_tz.split(',') : childrens_tz
@childrens_names = childrens_names.include?(',') ? childrens_names.split(',') : childrens_names
validate!
end
def validate!
raise ArgumentError, 'Missing URL environment variable' unless ENV['URL']
raise ArgumentError, 'Missing PARENT_NAME environment variable' unless ENV['PARENT_NAME']
raise ArgumentError, 'Missing PARENT_TZ environment variable' unless ENV['PARENT_TZ']
raise ArgumentError, 'Missing CHILDRENS_TZ environment variable' unless ENV['CHILDRENS_TZ']
raise ArgumentError, 'Missing CHILDRENS_NAMES environment variable' unless ENV['CHILDRENS_NAMES']
end
def call
go_to_form
end
def go_to_form
browser = Watir::Browser.new :chrome, headless: true
browser.goto(@url)
fill_out_form(browser)
end
def fill_out_form(page)
item_count = 0
if @childrens_tz.is_a?(Array)
@childrens_tz.count.times do
complete_individual_form(item_count, page)
submit_form(page)
check_for_errors(page)
item_count += 1
page.refresh
end
else
complete_individual_form(page)
submit_form(page)
check_for_errors
item_count += 1
end
validate_success(item_count)
end
def complete_individual_form(count = nil, page) # rubocop:disable Metrics/AbcSize
page.text_field(id: /txtMisparZehutHore/).wait_until(&:present?).set(@parent_tz)
page.text_field(id: /txtShemPratiHore/).wait_until(&:present?).set(@parent_name)
if @childrens_tz.is_a?(Array)
page.text_field(class: /mispar-zehut-yeled form-control/).set(@childrens_tz[count])
page.text_field(class: /shem-prati-yeled form-control/).set(@childrens_names[count])
page.radio(name: /rdoResult_0/).click
else
page.text_field(class: /mispar-zehut-yeled form-control/).set(@childrens_tz)
page.text_field(class: /shem-prati-yeled form-control/).set(@childrens_names)
page.radio(name: /rdoResult_0/).click
end
end
def submit_form(page)
until page.div(id: /bot/).exists? do sleep 3 end
page.iframe.click
page.button(id: /cmdSend/).click
end
def validate_success(count)
message = "Form submitted #{count} times successfully for all children!"
puts message
end
def check_for_errors(page)
raise 'Something went wrong' if page.div(class: /error-line/).present?
end
end