<file_sep>When(/^I select "([^"]*)" option$/) do |option|
  case option.downcase
  when 'lease'
    @trim_page.select_lease
  when 'loan'
    @trim_page.select_loan
  else
    raise 'Not supported option'
  end
end

When(/^I select "([^"]*)" car$/) do |car|
  @trim_page.select_car(car)
end

Then(/^I can see the lease cost$/) do
  lease_cost = @trim_page.lease_cost
  expect(lease_cost).to_not be nil
  puts lease_cost
end

Then(/^I can see the lease term$/) do
  lease_term = @trim_page.lease_term
  expect(lease_term).to_not be nil
  puts lease_term
end

When(/^I click on "SELECT LEASE" button$/) do
  @trim_page.click_select_lease
  @dealer_page = DealerPage.new
end
<file_sep>Given(/^I navigate to the Vehicle page$/) do
  @vehicle_page = VehiclePage.new
  @vehicle_page.visit_page
end

When(/^I select "([^"]*)" make in ([^"]*)$/) do |make, category|
  case category.downcase
  when 'featured'
    @vehicle_page.select_make_in_featured(make)
  when 'all'
    @vehicle_page.select_make_in_all(make)
  else
    raise 'Not supported category'
  end
  @model_page = ModelPage.new
end
<file_sep>class ApplicationPage < BasePage
  include AG::Form

  def fill_in_first_name(value)
    form.fill_in('firstNameTextField', with: value)
  end

  def fill_in_last_name(value)
    form.fill_in('lastNameTextField', with: value)
  end

  def fill_in_dob(value)
    form.fill_in('dobTextField', with: value)
  end

  def fill_in_mobile_phone(value)
    form.fill_in('mobilePhoneTextField', with: value)
  end

  def fill_in_home_phone(value)
    form.fill_in('homePhoneTextField', with: value)
  end

  def click_log_in
    form.find('a', text: 'Log In', visible: false).click
  end

  def fill_in_email(value)
    form.fill_in('emailTextField', with: value)
  end

  def fill_in_password(value)
    form.fill_in('passwordTextField', with: value)
  end

  def click_log_in_and_continue
    form.find('button:not([disabled=""])',
              text: 'Log In and Continue',
              visible: false).click
  end

end
<file_sep>class ResidencePage < BasePage
  include AG::Form

  def select_residence_status(option)
    all_dropdowns.first.click
    select_span(option)
  end

  def fill_in_address(value)
    form.fill_in('aginputaddress', with: value)
  end

  def fill_in_address2(value)
    form.fill_in('aginputaddress2', with: value)
  end

  def fill_in_city(value)
    form.fill_in('aginputcity', with: value)
  end

  def select_state(option)
    all_dropdowns.last.click
    select_span(option)
  end

  def fill_in_zip_code(value)
    form.fill_in('aginputzip', with: value)
  end

  def fill_in_move_in_date(value)
    form.fill_in('aginputmoveindate', with: value)
  end

  def fill_in_payment(value)
    form.fill_in('aginputpayment', with: value)
  end

  def click_next_button
    form.find('button:not([disabled=""])',
              text: 'Next',
              visible: false).click
  end

end
<file_sep>source 'https://rubygems.org'

gem 'capybara'
gem 'cucumber'
gem 'poltergeist'
gem 'rspec', require: 'spec'
gem 'selenium-webdriver'
<file_sep>When(/^I fill in my personal information:$/) do |table|
  info = table.rows_hash
  @application_page.fill_in_first_name(info['First Name']) if info.key?('First Name')
  @application_page.fill_in_last_name(info['Last Name']) if info.key?('Last Name')
  @application_page.fill_in_dob(info['Date of Birth']) if info.key?('Date of Birth')
  @application_page.fill_in_mobile_phone(info['Mobile Phone']) if info.key?('Mobile Phone')
  @application_page.fill_in_home_phone(info['Home Phone']) if info.key?('Home Phone')
end

When(/^I click the "Log In" link$/) do
  @application_page.click_log_in
end

When(/^I fill in my user credentials$/) do
  @application_page.fill_in_email(USER_EMAIL)
  @application_page.fill_in_password(<PASSWORD>)
end

When(/^I click "LOG IN AND CONTINUE" button$/) do
  @application_page.click_log_in_and_continue
  @residence_page = ResidencePage.new
end
<file_sep>class IdentificationPage < BasePage
  include AG::Form

  def fill_in_ssn(value)
    form.find('input[id*="SOCIALSECURITYNUMBER"]').set(value)
  end

end
<file_sep>When(/^I fill in my employment information:$/) do |table|
  info = table.rows_hash
  if info.key?('Employment Status')
    @employment_page.select_employment_status(info['Employment Status'])
  end
  if info.key?('Employer Name')
    @employment_page.fill_in_employer(info['Employer Name'])
  end
  if info.key?('Title')
    @employment_page.fill_in_title(info['Title'])
  end
  if info.key?('Start Date')
    @employment_page.fill_in_start_date(info['Start Date'])
  end
  if info.key?('Employer Phone Number')
    @employment_page.fill_in_employer_phone(info['Employer Phone Number'])
  end
  if info.key?('Gross Monthly Income')
    @employment_page.fill_in_income(info['Gross Monthly Income'])
  end
end

When(/^I click "NEXT" button on the Employment page$/) do
  @employment_page.click_next_button
  @identification_page = IdentificationPage.new
end
<file_sep>class DealerPage < BasePage

  def map_search(address)
    map.find('[type="text"]').send_keys(address).native.send_keys(:return)
  end

  def select_dealership(num)
    list_of_dealers.find(".list-group-item:nth-child(#{num})").click
  end

  def click_select_dealer
    base_content.click_button('Select this Dealer')
  end

  private

  def map
    base_content.find('h4+[class^="map_"]')
  end

  def list_of_dealers
    base_content.find('.list-group')
  end
end
<file_sep>Then(/^I should see "([^"]*)" as a header of the page$/) do |header|
  expect(@identification_page.header).to match(header)
end

Then(/^I should see text "([^"]*)" on the page$/) do |text|
  expect(@identification_page.all_text).to include(text)
end
<file_sep>class TrimPage < BasePage

  def select_lease
    base_content.click_button('Lease')
  end

  def select_loan
    base_content.click_button('Loan')
  end

  def select_car(car)
    list_of_cars.find('div', text: car, match: :first).click
  end

  def lease_cost
    deals.find('div.col-xs-6', text: 'LEASE').find('div:nth-child(2)').text
  end

  def lease_term
    deals.find('div.col-xs-6', text: 'LEASE').find('div:nth-child(3)').text
  end

  def click_select_lease
    base_content.click_button('SELECT LEASE')
  end

  private

  def list_of_cars
    base_content.find('.clearfix+.container-fluid')
  end

  def deals
    base_content.find('.row>.clearfix:nth-child(2)')
  end

end
<file_sep>class EmploymentPage < BasePage
  include AG::Form

  def select_employment_status(option)
    all_dropdowns.first.click
    select_span(option)
  end

  def fill_in_employer(value)
    form.fill_in('employer-name', with: value)
  end

  def fill_in_title(value)
    form.fill_in('employee-title', with: value)
  end

  def fill_in_employer_phone(value)
    form.fill_in('employer-phone-number', with: value)
  end

  def fill_in_start_date(value)
    form.fill_in('employee-start-date', with: value)
  end

  def fill_in_income(value)
    form.fill_in('gross-monthly-income', with: value)
  end

  def click_next_button
    form.find('button:not([disabled=""])',
              text: 'Next',
              visible: false).click
  end
end
<file_sep>When(/^I fill in my residence information:$/) do |table|
  info = table.rows_hash
  if info.key?('Residence Status')
    @residence_page.select_residence_status(info['Residence Status'])
  end
  if info.key?('Address')
    @residence_page.fill_in_address(info['Address'])
  end
  if info.key?('Address (Line 2)')
    @residence_page.fill_in_address2(info('Address (Line 2)'))
  end
  if info.key?('City')
    @residence_page.fill_in_city(info['City'])
  end
  if info.key?('State')
    @residence_page.select_state(info['State'])
  end
  if info.key?('Zip Code')
    @residence_page.fill_in_zip_code(info['Zip Code'])
  end
  if info.key?('Move in Date')
    @residence_page.fill_in_move_in_date(info['Move in Date'])
  end
  if info.key?('Mortgage/Rent Payment')
    @residence_page.fill_in_payment(info['Mortgage/Rent Payment'])
  end
end

When(/^I click "NEXT" button on the Residence page$/) do
  @residence_page.click_next_button
  @employment_page = EmploymentPage.new
end
<file_sep>When(/^I select "([^"]*)" model$/) do |model|
  @model_page.select_model(model)
  @trim_page = TrimPage.new
end
<file_sep>When(/^I click "START FINANCING" button$/) do
  @credit_page.click_start_financing
  @application_page = ApplicationPage.new
end
<file_sep># ag-cucumber-test

## Preconditions ##
* Clone repo
```
gem install bundler
bundle install
```
* You may need to install PhantomJS
```
brew install phantomjs
```

## Run Test Suit ##
To run Cucumber in production use this command:
```
cucumber TEST_ENV=prd
```

### Using different drivers ###
Framework runs headless using Poltergeist by default. To run a different driver use DRIVER environment variable:
```
cucumber TEST_ENV=prd DRIVER=selenium
```
Supported drivers:
- poltergeist
- selenium

Make sure you have chromedriver installed if you want to use Selenium:
```
npm install -g chromedriver
```
<file_sep>Before do
  case Capybara.current_driver
  when :selenium
    # page.driver.browser.manage.window.maximize
    # page.driver.browser.manage.window.resize_to(RESOLUTION.first, RESOLUTION.last)
  when :poltergeist
    page.driver.headers = { 'User-Agent' => USER_AGENT }
  end
end

After do |scenario|
  if scenario.failed?
    page.save_screenshot
    # page.save_page
  end
end
<file_sep>class BasePage
  include Capybara::DSL

  def initialize
    @header = '#content header'
  end

  def base_header
    find(@header)
  end

  def base_content
    find(@header + '+div>.container')
  end

  def base_status_bar
    find(@header + '+div>div:nth-child(1)')
  end

  def all_text
    base_content.text
  end
end
<file_sep>class VehiclePage < BasePage

  def visit_page
    visit "#{BASE_URL}/vehicle"
  end

  def select_make_in_featured(make)
    featured_makes.find('img+h5', text: make, visible: false).click
  end

  def select_make_in_all(make)
    all_makes.find('img+h5', text: make, visible: false).click
  end

  private

  def makes
    base_content.find('.container-fluid:nth-child(2)')
  end

  def featured_makes
    makes.find('div:nth-child(2).row')
  end

  def all_makes
    makes.find('div:nth-child(4).row')
  end
end
<file_sep>class CreditPage < BasePage

  def click_start_financing
    base_content.click_button('Start Financing')
  end

end
<file_sep>class ModelPage < BasePage

  def select_model(model)
    base_content.find('.row h5', text: model).click
  end
end
<file_sep>When(/^I fill in the dealership address with: "(.*?)"$/) do |address|
  @dealer_page.map_search(address)
end

When(/^I select the (\d+) dealership$/) do |num|
  @dealer_page.select_dealership(num)
end

When(/^I click "SELECT THIS DEALER" button$/) do
  @dealer_page.click_select_dealer
  @credit_page = CreditPage.new
end
<file_sep>module AG
  module Form

    def page_name
      find('.application>div>span').text
    end

    def header
      form.find('h4').text
    end

    private

    def aside
      find('aside')
    end

    def form
      find('form')
    end

    def all_dropdowns
      dropdowns = 'button[style*="border-box"]'
      form.has_css?(dropdowns)
      form.find_all(dropdowns, visible: false)
    end

    def select_span(option)
      find('span', text: option, visible: false).click
    end
  end
end
<file_sep>require 'capybara/cucumber'
require 'capybara/poltergeist'
require 'rspec'
require 'selenium-webdriver'
require 'yaml'
require_relative '../modules/base_page.rb'
require_relative '../modules/form_module.rb'

config = YAML.load_file(File.dirname(__FILE__) + '/config.yml')

# 'prd' environment supported only. Use: cucumber TEST_ENV=prd
T_ENV = config[ENV['TEST_ENV']]
raise 'Need to specify the environment' unless T_ENV

DRIVER = ENV['DRIVER'].to_sym || :poltergeist

BASE_URL = T_ENV['base_url']
RESOLUTION = config['resolution'].split(':')
USER_AGENT = config['user_agent']
DEFAULT_TIMEOUT = config['default_max_timeout'].to_i
USER_EMAIL = T_ENV['email']
USER_PASSWORD = <PASSWORD>['<PASSWORD>']

Capybara.default_driver = DRIVER
Capybara.default_max_wait_time = DEFAULT_TIMEOUT
Capybara.register_driver DRIVER do |app|
  case DRIVER
  when :selenium
    capybara = Capybara::Selenium
    options = { browser: :chrome }
  when :poltergeist
    capybara = Capybara::Poltergeist
    options = {
      js_errors: false,
      window_size: [RESOLUTION.first, RESOLUTION.last],
      phantomjs_options:
        ['--load-images=yes',
         '--disk-cache=true',
         '--ignore-ssl-errors=no',
         '--ssl-protocol=any']
    }
  else
    raise "Not supported driver #{DRIVER}"
  end
  capybara::Driver.new(app, options)
end
