Guest User

Untitled

a guest
Jan 5th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. require 'bundler/setup'
  2. require 'io/console'
  3. require 'utils'
  4.  
  5. DRIVER_PATH = File.absolute_path case Utils::os
  6. when :osx
  7. './driver/chromedriver_mac64'
  8. when :windows
  9. './driver/chromedriver_win64.exe'
  10. when :linux
  11. './driver/chromedriver_linux64'
  12. end
  13.  
  14. class MyBrowser
  15. def initialize(debug: false)
  16. @username = ''
  17. @password = ''
  18. @debug = debug
  19. @driver = nil
  20. self.create_browser
  21. end
  22.  
  23. def create_browser()
  24. options = Selenium::WebDriver::Chrome::Options.new()
  25. options.add_argument('--headless') unless @debug
  26. @driver = Selenium::WebDriver.for :chrome, driver_path: DRIVER_PATH, options: options
  27. end
  28.  
  29. def goto(url)
  30. @driver.navigate.to(url)
  31. if @driver.find_element(id: 'login_form')
  32. self.login()
  33. end
  34. end
  35.  
  36. def login()
  37. ask_username_password
  38. @driver.find_element(name: "username").send_keys(@username)
  39. @driver.find_element(name: "password").send_keys(@password)
  40. @driver.find_element(id: "login").click()
  41. end
  42.  
  43. def ask_username_password
  44. if @debug
  45. @username = ENV['username'] if ENV['username']
  46. @password = ENV['password'] if ENV['password']
  47. end
  48. if @username.empty?
  49. print "USERNAME: "
  50. @username = gets.chomp!
  51. end
  52. if @password.empty?
  53. print "PASSWORD: "
  54. @password = STDIN.noecho(&:gets)
  55. end
  56. end
  57. end
Add Comment
Please, Sign In to add comment