Created
October 9, 2018 15:11
-
-
Save diegommarino/c131263d0e0883f09da39605edda4414 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "selenium-webdriver" | |
DEFAULT_TIMEOUT = 20 | |
def setup | |
driver | |
end | |
def select_by_label(id, label, options = {}) | |
reverse_merge(options, how: :id) | |
wait_for_visible(id, options) | |
Selenium::WebDriver::Support::Select.new(driver.find_element(options[:how], id)).select_by(:text, label) | |
end | |
def click(id, options = {}) | |
reverse_merge(options, {wait_for_visible: true, how: :id, scroll_to_element: false}) | |
wait_for_visible(id, options) if options[:wait_for_visible] | |
begin | |
element = driver.find_element(options[:how], id) | |
driver.execute_script("arguments[0].scrollIntoView(true);", element) if options[:scroll_to_element] | |
element.click | |
rescue Selenium::WebDriver::Error::StaleElementReferenceError | |
#try again | |
sleep(3) #hackity hack | |
element = driver.find_element(options[:how], id) | |
element.click | |
end | |
end | |
def type(id, text, options = {}) | |
reverse_merge(options, wait_for_visible: true, how: :id) | |
wait_for_visible(id, options) | |
clear(options[:how], id, options) | |
send_keys(id, text, options) | |
end | |
def clear(how, id, options = {}) | |
find_elements(how, id, options).clear | |
end | |
def send_keys(id, text, options = {}) | |
reverse_merge(options, how: :id) | |
wait_for_visible(id, options) | |
find_elements(options[:how], id, options).send_keys text | |
end | |
def enable_field(id, options={}) | |
driver.execute_script("$('#{jquery_selector_for(id, options[:how])}').removeAttr('readonly')") | |
end | |
def find_elements(how, id, options = {}) | |
if options[:index] | |
driver.find_elements(how, id)[options[:index]] | |
else | |
driver.find_element(how, id) | |
end | |
end | |
def wait_for_visible(selector, options = {}) | |
return if options[:wait_for_visible] == false | |
reverse_merge(options, {how: :id}) | |
Selenium::WebDriver::Wait.new(timeout: DEFAULT_TIMEOUT).until {driver.find_element(options[:how], selector).displayed? rescue false} | |
end | |
def reverse_merge(options, defaults) | |
options.replace(defaults.merge(options)) | |
end | |
def jquery_selector_for(selector, kind) | |
kind == :id ? "##{selector}" : selector | |
end | |
def driver | |
@driver ||= Selenium::WebDriver.for :chrome | |
end | |
setup | |
driver.navigate.to "https://burghquayregistrationoffice.inis.gov.ie/Website/AMSREG/AMSRegWeb.nsf/AppSelect?OpenForm" | |
select_by_label("Category", "Other") # Category | |
select_by_label("SubCategory", "Join family member") # Type | |
select_by_label("ConfirmGNIB", "No") # Have GNIB number? If yes add a new command bellow to fill the GNIB number. | |
click("UsrDeclaration") | |
type("GivenName", "name") | |
type("SurName", "Last names") | |
# Need to fix this date of birth | |
# enable_field("DOB") | |
# sleep 2 | |
# type("DOB", "dd/mm/yyyy") | |
type("Email", "email") | |
type("EmailConfirm", "email") | |
select_by_label("Nationality", "Brazil") | |
select_by_label("FamAppYN", "Yes")# Family appplication? | |
select_by_label("FamAppNo", "2") # Family qty | |
select_by_label("PPNoYN", "Yes") | |
type("PPNo", "passport_number") | |
# click(".recaptcha-checkbox-checkmark", how: :class) | |
sleep 100000 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment