Skip to content

Instantly share code, notes, and snippets.

@helioalb
Last active May 31, 2016 18:57
Show Gist options
  • Select an option

  • Save helioalb/e9f759bdd2d26dab2bd8bef4579b3984 to your computer and use it in GitHub Desktop.

Select an option

Save helioalb/e9f759bdd2d26dab2bd8bef4579b3984 to your computer and use it in GitHub Desktop.
(function($) {
'use strict';
function FormFiller() {
this.path = document.location.pathname;
this.container = $('form[accept-charset]', '#main-wrapper');
this.groupName = this.name + this.path;
this.group();
this.log('initialized');
}
var fn = FormFiller.prototype;
fn.name = '[FormFiller] ';
fn.vehicleRegexp = /seguros\/seguro-auto\/veiculo\//;
fn.driversRegexp = /seguros\/seguro-auto\/condutores\/\d+/;
fn.cartRegexp = /seguros\/seguro-auto\/compra\/\d+\/carrinho/;
fn.paymentRegexp = /seguros\/seguro-auto\/compra\/\d+\/pagamento/;
fn.init = function init() {
this.loadDependencies().done(
_.bind(this.findPage, this)
);
};
fn.log = function log() {
var args = Array.prototype.slice.call(arguments);
args.unshift(this.name);
console.log.apply(console, args);
};
fn.loadDependencies = function loadDependencies(callback) {
this.log('loading dependencies: Faker.js');
return $.getScript('https://cdn.rawgit.com/Marak/Faker.js/0.7.2/MinFaker.js');
};
fn.findPage = function findPage() {
this.log('Matching the page url with recognized forms');
if (this.isHome()) {
this.log('filling `home` vehicle selection');
this.fillHomePage();
} else if (this.vehicleRegexp.test(this.path)) {
this.log('filling quote_request `vehicle` information');
this.fillVehiclePage();
} else if (this.driversRegexp.test(this.path)) {
this.log('filling quote_request `drivers` information');
this.fillDriversPage();
} else if (this.cartRegexp.test(this.path)) {
this.log('filling `cart` information');
this.fillCartPage();
} else if (this.paymentRegexp.test(this.path)) {
this.log('filling `payment` information');
this.fillPaymentPage();
} else {
this.log('unknow page, bye!');
this.groupEnd();
}
};
fn.isHome = function isHome() {
var urls = [ '/', '/seguros', '/seguros/seguro-auto' ];
return ~urls.indexOf(this.path);
};
fn.group = function() {
console.group(this.groupName);
};
fn.groupEnd = function() {
console.groupEnd(this.groupName);
};
fn.choose = function choose(element, value) {
var option, random, el = $(element);
if (value === undefined) {
option = Faker.random.array_element(el.find('option:gt(0)'));
$(option).attr('selected', true);
} else {
el.val(value);
}
this.log('choose', (element.id || element), '=', (value || option.value));
return el
.trigger('chosen:updated')
.trigger('change');
};
fn.fill = function fill(selector, value) {
this.log('fill', selector, '=', value);
return this.container.find(selector).val(value);
};
fn.click = function click(selector) {
this.log('click', selector);
return this.container.find(selector).trigger('click');
};
fn.waitForOptions = function(element, value) {
var self = this, eventName = 'chosen:updated';
$(element).on(eventName, function hasOptions(e) {
var $element = $(this);
if ($element.find('option').length > 1) {
$element.off(eventName, hasOptions);
self.choose(this, value);
}
});
};
fn.submit = function submit() {
if(confirm('Submeter formulário?')) {
this.container.trigger('submit');
this.groupEnd();
}
};
fn.chooseState = function chooseState(selector) {
this.choose(selector, Faker.random.br_state_abbr());
};
fn.chooseVehicle = function chooseVehicle() {
if ( this.isVehicleSelected() ) {
return mediator.trigger('vehicle-version-search:load-end');
}
this.choose('[data-vehicle-brand]', 'FIAT');
this.waitForOptions('[data-vehicle-model]', 'PALIO');
this.waitForOptions('[data-vehicle-manufacturing-year]', '2012');
this.waitForOptions('[data-vehicle-model-year]', '2012');
};
fn.isVehicleSelected = function isVehicleSelected() {
var values, selects = this.container.find('#select-vehicle select');
values = selects.map(function mapValues() {
return $(this).val();
});
return _.compact(values).length == 4;
};
fn.fillHomePage = function fillHomePage() {
mediator.on('vehicle-selection:complete', _.bind(this.submit, this));
this.chooseVehicle();
};
fn.fillVehiclePage = function fillVehiclePage() {
mediator.on('vehicle-version-search:load-end', _.bind(function() {
this.click('[data-vehicle-version-search-results] li:first');
this.click('#vehicle_quote_request_vehicle_attributes_already_owns_true');
this.click('#vehicle_quote_request_vehicle_attributes_utilization_is_using');
this.fill('#vehicle_quote_request_vehicle_attributes_overnight_cep', '04565-001');
this.choose('#vehicle-overnight-local', 'STREET');
this.click('#vehicle_quote_request_vehicle_attributes_vehicle_functions_leisure');
this.fill('#vehicle_quote_request_vehicle_attributes_daily_distance',
Faker.Helpers.replaceSymbolWithNumber('###'));
this.submit();
}, this));
this.chooseVehicle();
};
fn.fillDriversPage = function fillDriversPage() {
var today = new Date(),
name = 'TESTE ' + Faker.Name.firstName() + ' ' + Faker.Name.lastName();
this.fill('#vehicle_quote_request_user_name', name);
this.fill('#vehicle_quote_request_user_phone_number', '(11) 1234-5678').keydown().keyup();
this.fill('#vehicle_quote_request_user_mobile_phone_number', '(11) 98765-4321').keydown().keyup();
this.fill('#vehicle_quote_request_user_email', 'testedeemail+' + Faker.random.number(10000) + '@bidu.com.br');
mediator.on('vehicle-insurance-roles', _.after(3, _.bind(function(e) {
// main driver information
this.choose('#vehicle-main-driver-gender');
this.fill('#vehicle_quote_request_driver_attributes_birthdate',
today.getDate() + '/' + (today.getMonth() + 1) + '/1985');
this.fill('#vehicle_quote_request_driver_attributes_cpf', '417.566.744-00');
this.choose('#vehicle_quote_request_driver_attributes_marital_status');
this.choose('#vehicle_quote_request_driver_attributes_has_dependents', '0');
this.fill('#vehicle_quote_request_driver_attributes_years_licensed', '7');
this.choose('#vehicle_quote_request_driver_attributes_residential_local', 'HOME');
this.choose('#vehicle_quote_request_driver_attributes_schooling');
this.choose('#vehicle_quote_request_driver_attributes_number_of_use_days_per_week', '1');
this.click('#vehicle_quote_request_driver_attributes_has_been_stolen_in_the_last_24_months_false');
// insured information
this.click('#vehicle_quote_request_insured_attributes_is_studying_false');
this.click('#vehicle_quote_request_insured_attributes_is_working_true');
this.click('#vehicle_quote_request_insured_attributes_renewal_false');
this.submit();
}, this)));
// chose all the same role
this.click('#insurance-roles input[value="true"]');
};
fn.fillCartPage = function fillCartPage() {
var today = new Date();
// information of Vehicle
this.fill('#vehicle_free_willy_proposal_vehicle_attributes_registration_plate', 'FIT-2907');
this.chooseState('#vehicle_free_willy_proposal_vehicle_attributes_uf');
this.fill('#vehicle_free_willy_proposal_chassis_number', '93HGD18604Z130294');
this.click('#vehicle_free_willy_proposal_vehicle_attributes_changed_chassis_number_false');
this.fill('#vehicle_free_willy_proposal_vehicle_attributes_renavam', '830935410');
this.choose('#vehicle_free_willy_proposal_vehicle_attributes_color');
this.click('#vehicle_free_willy_proposal_vehicle_attributes_financed_false');
// insured documents
this.fill('#vehicle_free_willy_proposal_insured_attributes_document_number',
Faker.Helpers.replaceSymbolWithNumber('#########'));
this.choose('#vehicle_free_willy_proposal_insured_attributes_document_uf');
this.fill('#vehicle_free_willy_proposal_insured_attributes_document_issuance_date',
today.getDate() + '/' + (today.getMonth() + 1) + '/2000');
this.click('#vehicle_free_willy_proposal_insured_attributes_politically_exposed_false');
this.choose('#vehicle_free_willy_proposal_insured_attributes_schooling');
this.choose('#vehicle_free_willy_proposal_insured_attributes_monthly_income');
this.choose('#vehicle_free_willy_proposal_insured_attributes_profession');
// main driver documents
this.fill('#vehicle_free_willy_proposal_driver_attributes_cnh',
Faker.Helpers.replaceSymbolWithNumber('###########'));
// insured address
this.fill('#vehicle_free_willy_proposal_shipping_address_attributes_street',
Faker.Address.streetName());
this.fill('#vehicle_free_willy_proposal_shipping_address_attributes_number',
Faker.Helpers.replaceSymbolWithNumber('###'));
this.fill('#vehicle_free_willy_proposal_shipping_address_attributes_complement',
Faker.Address.secondaryAddress());
this.fill('#vehicle_free_willy_proposal_shipping_address_attributes_district',
Faker.Address.ukCounty());
this.fill('#vehicle_free_willy_proposal_shipping_address_attributes_city',
Faker.Address.city());
this.chooseState('#vehicle_free_willy_proposal_shipping_address_attributes_state');
this.submit();
};
fn.fillPaymentPage = function fillPaymentPage() {
this.click('[data-tab="debit"]');
this.choose('#vehicle_free_willy_proposal_payment_attributes_installment_plan_id');
this.choose('#vehicle_free_willy_proposal_payment_attributes_bank');
this.fill('#vehicle_free_willy_proposal_payment_attributes_bank_agency_number',
Faker.Helpers.replaceSymbolWithNumber('####'));
this.fill('#vehicle_free_willy_proposal_payment_attributes_bank_account_number',
Faker.Helpers.replaceSymbolWithNumber('######'));
this.click('#vehicle_free_willy_proposal_payment_attributes_is_insured_account_holder_true');
this.click('#vehicle_free_willy_proposal_terms');
this.submit();
};
// initialize
$(function() {
var filler = new FormFiller();
filler.init();
});
}(jQuery));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment