lib/generic_test/setup.rb in generic_test-0.1.10 vs lib/generic_test/setup.rb in generic_test-0.1.11

- old
+ new

@@ -1,4 +1,45 @@ -# One file to require gem and set things up +# frozen_string_literal: true + +# One file to require gem and set things up, arriving at page to check require 'generic_test' -GenericTest.setup +# @return [String] Generic Test URL to login before arriving at PAGE_URL to test. +# Needs GT_USERNAME and GT_PASSWORD to be used +GT_LOGIN_URL = 'GT_LOGIN_URL' +GT_USERNAME = 'GT_USERNAME' +GT_PASSWORD = 'GT_PASSWORD' +GT_PAGE_SETTING_FILE = 'generic_test.page.yml' + +ENV['HEADLESS'] ||= 'true' +ENV['PAGE_NUM'] ||= '1' +browser_args = %w[--disable-popup-blocking --no-sandbox --disable-dev-shm-usage] +browser_args << '--headless' if ENV['HEADLESS'] == 'true' + +GenericTest.browser = Watir::Browser.new :chrome, args: browser_args + +raise 'PAGE_URL environment variable not set' unless ENV['PAGE_URL'] + +require 'yamler' +if File.exist?(GT_PAGE_SETTING_FILE) && !File.read(GT_PAGE_SETTING_FILE).empty? + gt_page_settings = Yamler.load 'generic_test.page.yml' + if gt_page_settings['login'] + ENV[GT_USERNAME] = gt_page_settings['login']['username'] + ENV[GT_PASSWORD] = gt_page_settings['login']['password'] + ENV[GT_LOGIN_URL] = gt_page_settings['login']['url'] + end +end + +browser = GenericTest.browser +if ENV[GT_LOGIN_URL] && ENV[GT_USERNAME] && ENV[GT_PASSWORD] + browser.goto ENV[GT_LOGIN_URL] + browser.text_field(xpath: "//input[@id=(//label[text()='Username']/@for)]|//input[@*='username']").set ENV[GT_USERNAME] + browser.text_field(xpath: "//input[@id=(//label[text()='Password']/@for)]|//input[@*='password']").set ENV[GT_PASSWORD] + login_button = browser.button(xpath: "//*[@type='submit']|//*[@*='Login']") + login_button.click + login_button.wait_while(&:present?) + puts "Login through #{ENV[GT_LOGIN_URL]} successful!" +end + +browser.goto ENV['PAGE_URL'] +sleep 2.5 # Give a bit of time for page to load +GenericTest.pages << GenericTest::Page.new(browser) \ No newline at end of file