Save and restore watir session

I want to test interactive authentication using watir. I need to open the browser once on the selenium server, enter without user interaction with its username / password (stored on the server, the user does not enter them, he just needs to enter the verification code), a new text box appears with the label β€œenter verification code”, then the system sends a confirmation code to the user and closes the browser (here I need to somehow save the session). The user receives a confirmation code and sends it to the server, the server opens a new browser (restores somehow saved session) and enters the confirmation code received from the user.

I could leave the browser open and simply enter the verification code in the text box that appears, but if the user does not send me the verification code that he received, the browser will remain open, and not a good solution.

So, I tried something like this:

params = { login: 'userexample.com', password: '123456' } adapter = Adapters::Test.new(params) adapter.sign_in #opens browser, fills credentials fields, clicks 'get verification code', 'enter verification code' field appears cookies = adapter.browser.cookies.to_a #save browser state url = adapter.browser.url adapter.browser.close adapter = Adapters::Test.new(params) adapter.browser.goto url adapter.browser.cookies.clear cookies.each do |saved_cookie| adapter.browser.cookies.add(saved_cookie[:name], saved_cookie[:value]) end adapter.browser.refresh #I should be on the same page with appeared 'enter verification code' field, but nothing happens after refresh, I am still on the main page with only login/password fields. 

How to save the state of the browser, close it, and then open it again with the same session?

+4
source share
1 answer
  def prepare_for_sign_in browser.goto login_url browser.text_field(:name => "login").set("admin") browser.text_field(:name => "password").set("admin") browser.a(:class => "enter").click until browser.div(:text => /Welcome/).present? do if browser.div(:class => 'error').text.include?("incorrect username or password") raise InvalidCredentials.new("Invalid login or password.") end end # here you need to sleep few secs or use similar conditional wait Watir::Wait.until { browser.cookies.to_a.first[:name] == "JSESSIONID" } params["interactive_data"] = { "cookies" => JSON.parse(browser.cookies.to_a.to_json), "proceed_url" => browser.url } end def sign_in restore_cookies(params["interactive_data"]["proceed_url"]) browser.text_field(:name => "sms").set('123456') browser.a(:text => "Proceed").click end def restore_cookies(url) browser.goto url browser.cookies.clear params["interactive_data"]["cookies"].each do |cookie| browser.cookies.add( cookie["name"], cookie["value"], { domain: cookie["domain"], path: cookie["path"], expires: cookie["expires"], secure: cookie["secure"] } ) end browser.goto url browser.refresh end 

There are a few more source codes, but I cannot separate them, but the logic is similar to

+3
source

Source: https://habr.com/ru/post/1497568/


All Articles