Browser automation with Watir

Watir

  • test automation library
  • set of tool for automating browser interaction
  • powered by Selenium

Web

application

testing

in

Ruby

Focus and goals

  • improving test readability
  • make writing maintainable tests easier
  • extensibility

Stack

rspec
|- example
   |- page object?
      |- Watir
         |- Selenium
            |- browser driver
               |- browser

Problem

1. Otići u branch koji se zove manual_order_finish_fix
2. Napraviti file unutar config foldera koji se zove config_1.secret.exs 
   i u njega staviti ovaj sadržaj:
https://secret.infinum.co/secret/nzp8mjqv8ueksvb4a10i1z5ijrq3g8x
3. Za svaki order koji je pronađen updateati njegov purchased_at s
   vrijednošću iz fielda updated_at
4. Upaliti Elixir konzolu s iex -S mix
5. Pronaći order: order = ImageCloudApi.Repo.get(ImageCloudApi.Order, {order_id})
6. Napraviti order finish (slanje maila i završavanje ordera): 
   ImageCloudApi.Orders.FinishOrder.run(order)
7. Pronaći transakciju za taj order u tablici transactions i updateati card_name
   s jednom od kartica (AMEX, MASTERCARD, VISA, DISCOVER). Potražiti vrijednost u Excelu.
8. U Elixir konzoli izvršiti: ImageCloudApi.Dynamics.ExportOrder.perform(order.id)
./main.rb

Quick taste

require 'watir'

browser = Watir::Browser.new

browser.goto 'watir.com'
browser.link(text: 'Guides').click

browser.close

* stolen from watir.com

Page object - login

class Login < WatirPump::Page
  text_field_writer :uid, name: 'txtUID'
  text_field_writer :password, name: 'txtPwd'
  button :login, id: 'btnLogin'

  def fill(uid, password)
    fill_form(uid: uid, password: password)
  end

  def submit
    login.click
  end
end

page = Scraper::Merchant::Login.new(browser)
page.fill(Chamber.env.fac.uid, Chamber.env.fac.password)
page.submit

Page object - generate report

class GenerateReport < WatirPump::Page
  region :main, ->  { root.frameset(id: 'FSRIGHT').frame(name: 'main') } do
    tr :excel_report, id: 'ReportSelect_GridReports_ctl00__1'
    text_field_writer :start_at, id: 'TransactionFilters_dpTStart_dateInput'
    button :view_report, id: 'CommandViewReport'
  end

  def use_excel
    main.excel_report.click
  end

  def month_ago
    start_at = Date.today.to_time - 60 * 60 * 24 * 30
    main.start_at = start_at.strftime('%-m/%-d/%Y')
  end

  def submit
    main.view_report.click
  end
end

page = Scraper::Merchant::GenerateReport.new(browser)
page.use_excel
page.month_ago
page.submit

Page object - report results

class ReportResults < WatirPump::Page
  REPORT_FILENAME = 'rpTransaction - for Excel export.xlsx'

  region :main, ->  { root.frameset(id: 'FSRIGHT').frame(name: 'main') } do
    link :download, id: 'ReportViewer1_ctl05_ctl04_ctl00_ButtonLink'
    link :download_to_excel, title: 'Excel'
  end

  def wait_for_data
    sleep(5) # wait until table is populated
  end

  def download
    main.download.click
  end

  def download_to_excel
    main.download_to_excel.click
  end

  def wait_for_download(download_dir, opts = [])
    Watir::Wait.until(*opts) { File.exist?(File.expand_path(REPORT_FILENAME, download_dir)) }
    sleep(2)
  end
end

page = Scraper::Merchant::ReportResults.new(browser)
page.wait_for_data
page.download
page.download_to_excel
page.wait_for_download(downloads)

Putting it all together

#!/usr/bin/env ruby

require 'bundler/setup'
Bundler.require(:default)
require 'fileutils'

require_relative 'lib/scraper'

def scrape
  downloads = File.expand_path('tmp/scrape', __dir__)
  FileUtils.remove_dir downloads if Dir.exist?(downloads)
  FileUtils.mkdir_p downloads

  browser = Scraper.browser

  browser.goto Scraper::Merchant.administration_url

  page = Scraper::Merchant::Login.new(browser)
  # consume page

  page = Scraper::Merchant::GenerateReport.new(browser)
  # consume page

  page = Scraper::Merchant::ReportResults.new(browser)
  # consume page

  browser.close
end

scrape

I'am the last slide

Questions?

http://watir.com/

https://github.com/bwilczek/watir_pump

Ruby browser automation

By vrabac

Ruby browser automation

  • 509