dreadmachine

lucky_nums_spec.rb.old

Dec 6th, 2021
1,166
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. require 'rails-helper'
  2. include RSpec::Expectations
  3.  
  4. CHARS = ('0'..'9').to_a
  5.  
  6. describe 'Main page' do
  7.   def random_lucky(len)
  8.     half = [0] * (3 - len) + CHARS.sample(len)
  9.     (half + half.shuffle).join
  10.   end
  11.  
  12.   def element_present?(how, what)
  13.     @driver.find_element(how, what)
  14.     true
  15.   rescue Selenium::WebDriver::Error::NoSuchElementError
  16.     false
  17.   end
  18.  
  19.   before(:each) do
  20.     @driver = Selenium::WebDriver.for :chrome
  21.     @base_url = 'http://localhost:3000/'
  22.     @accept_next_alert = true
  23.     @driver.manage.timeouts.implicit_wait = 1
  24.     @verification_errors = []
  25.   end
  26.  
  27.   after(:each) do
  28.     @driver.quit
  29.     expect(@verification_errors).to be_empty
  30.   end
  31.  
  32.   it 'should get input form' do
  33.     @driver.get @base_url
  34.     expect(element_present?(:xpath, '//form')).to eq true
  35.     expect(element_present?(:xpath, '//form/input[@id=\'from\']')).to eq true
  36.     expect(element_present?(:xpath, '//form/input[@id=\'to\']')).to eq true
  37.     expect(element_present?(:xpath, '//form/div/input[@id=\'submit-btn\']')).to eq true
  38.     expect(element_present?(:xpath, '//form/div/input[@id=\'clear-btn\']')).to eq true
  39.   end
  40.  
  41.   it 'should get proper output from 0 to 9999 via AJAX' do
  42.     @driver.get @base_url
  43.     @driver.find_element(:id, 'from').click
  44.     @driver.find_element(:id, 'from').clear
  45.     @driver.find_element(:id, 'from').send_keys '0'
  46.     @driver.find_element(:id, 'to').click
  47.     @driver.find_element(:id, 'to').clear
  48.     @driver.find_element(:id, 'to').send_keys '9999'
  49.     @driver.find_element(:id, 'form').click
  50.     @driver.find_element(:id, 'submit-btn').click
  51.     nums = @driver.find_element(:id, 'result-table')
  52.                   # .find_elements(:xpath, '//tbody[@id=\'result-table\']//td[2]')
  53.                   .find_elements(:css, 'tr>:nth-child(2)')
  54.                   .map(&:text)
  55.     expect(nums.size).to eq(220)
  56.     100.times do
  57.       expect(nums).to include(random_lucky(1))
  58.     end
  59.   end
  60.  
  61.   def verify
  62.     yield
  63.   rescue ExpectationNotMetError => e
  64.     @verification_errors << e
  65.   end
  66. end
  67.  
RAW Paste Data