Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- require 'bundler/setup'
- require 'io/console'
- require 'utils'
- DRIVER_PATH = File.absolute_path case Utils::os
- when :osx
- './driver/chromedriver_mac64'
- when :windows
- './driver/chromedriver_win64.exe'
- when :linux
- './driver/chromedriver_linux64'
- end
- class MyBrowser
- def initialize(debug: false)
- @username = ''
- @password = ''
- @debug = debug
- @driver = nil
- self.create_browser
- end
- def create_browser()
- options = Selenium::WebDriver::Chrome::Options.new()
- options.add_argument('--headless') unless @debug
- @driver = Selenium::WebDriver.for :chrome, driver_path: DRIVER_PATH, options: options
- end
- def goto(url)
- @driver.navigate.to(url)
- if @driver.find_element(id: 'login_form')
- self.login()
- end
- end
- def login()
- ask_username_password
- @driver.find_element(name: "username").send_keys(@username)
- @driver.find_element(name: "password").send_keys(@password)
- @driver.find_element(id: "login").click()
- end
- def ask_username_password
- if @debug
- @username = ENV['username'] if ENV['username']
- @password = ENV['password'] if ENV['password']
- end
- if @username.empty?
- print "USERNAME: "
- @username = gets.chomp!
- end
- if @password.empty?
- print "PASSWORD: "
- @password = STDIN.noecho(&:gets)
- end
- end
- end
Add Comment
Please, Sign In to add comment