Skip to content

Instantly share code, notes, and snippets.

@fizz
Created December 12, 2017 00:06
Show Gist options
  • Save fizz/24023ac53f79f90f6febbc7636f28755 to your computer and use it in GitHub Desktop.
Save fizz/24023ac53f79f90f6febbc7636f28755 to your computer and use it in GitHub Desktop.
VickyBot.coffee
# Update product notification emails
class NotificationsBot extends require('./vickybot').VickyBot
NotificationsBot::urls.products = ->
'/products/index.php'
NotificationsBot::selectors.pagination = '.limeListSelectBox'
NotificationsBot::selectors.loading = '#loadingproductList'
NotificationsBot::selectors.search_field = '#list_search_id_productList'
NotificationsBot::selectors.product =
name: '#product_name'
id: '#product_id'
sku: '#product_sku'
price: '#product_price'
max_quantity: '#product_max_quantity'
notifications: 'table.summary.notification-list tr:not(:first)'
save_button: '#btn_product_save'
NotificationsBot::get = (slug, id...) ->
if slug is 'products'
@then ->
@echo 'waiting for SearchLimeList'
@waitFor ->
@evaluate ->
typeof SearchLimeList is 'function'
@then ->
@echo 'SearchLimeList'
@thenEvaluate (sel) ->
$s = jQuery sel
$s.val('ship-n-save')
$s.keyup()
, {sel: @selectors.search_field}
@then ->
@echo 'waiting for ListSearchAheadInProgress'
@waitFor ->
@evaluate ->
ListSearchAheadInProgress is false
@then ->
@echo 'waiting for ListJumpMin'
@waitFor ->
@evaluate ->
typeof ListJumpMin is 'function'
@thenEvaluate (sel) ->
$s = jQuery sel
$s.attr('selected', 'selected')
$s.parent().change()
, {sel: "#{@selectors.pagination} option:last"}
@then ->
@waitWhileVisible @selectors.loading, ->
@emit 'products.index', @evaluate ->
$ = jQuery
rows = $('tr:contains(RG),tr:contains(Golf)')
.not('tr:contains(0.00)').get()
$.map rows, (el,i) ->
$(el).children('td:eq(0)').text()
else super slug, id
NotificationsBot::snap = (arg)->
@captureSelector "#{Date.now()}#{arg}.png", 'html'
notificationsbot = new NotificationsBot
stepTimeout: 120000
waitTimeout: 90000
verbose: true
logLevel: 'debug'
notificationsbot.setFilter 'open.location', (location) ->
location = "#{@base_url}#{location}"
notificationsbot.start()
notificationsbot.viewport 1024, 768
notificationsbot.then -> @load_url 'products'
# notificationsbot.then -> @load_url 'login'
notificationsbot.run()
notificationsbot.on 'url.loaded', (model, id...) -> @get model, id
notificationsbot.on 'url.redirected', (redirect_to) ->
if redirect_to is 'login_ok'
@load_url 'products'
notificationsbot.on 'products.index', (product_ids) ->
@snap 'index'
@log "#{product_ids.length} #{product_ids}"
@options.timeout = 1000 * 90 * product_ids.length
@test.info "Total timeout set to #{@options.timeout}"
@each product_ids, (self, id) ->
@load_url 'product', id
notificationsbot.on 'model.hydrated', (model, propbag) ->
@snap 'hydrated'
@evaluate ->
$ = jQuery
$.map $('[id^=button_delete]'), (el, i)->
$(el).click()
@echo 'waiting for SaveProduct'
@waitFor ->
@evaluate ->
typeof SaveProduct is 'function'
@then ->
@click @selectors.product.save_button
@waitWhileSelector @selectors.product.save_button, ->
@test.pass "Removed notifications for #{propbag.sku}-#{propbag.name}"
notificationsbot.on 'waitFor.timeout', -> @snap 'waitfor'
notificationsbot.on 'step.timeout', -> @snap 'stepTimeout'
notificationsbot.on 'timeout', -> @snap 'timeout'
notificationsbot.on 'load.failed', -> @snap 'load.failed'
notificationsbot.on 'page.error', (msg, trace)-> @echo msg
notificationsbot.on 'exit', ->
@test.renderResults()
# Fix misattributed revenue pixels server-side
class OPBot extends require('./vickybot').VickyBot
OPBot::urls.campaigns = ->
'/campaign/index.php'
OPBot::selectors.pagination = '.limeListSelectBox'
OPBot::selectors.loading = '#loadinglist_campaign'
OPBot::selectors.campaign =
id: '#campaign_id'
name: 'input[name=input_campaign_name]'
integration_type: 'input[name=radio_integration]:checked'
campaign_type: 'input[name=radio_campaign_type]:checked'
save_button: '#btn_save'
save_ok: '#custom_save_pop'
OPBot::get = (slug, id...) ->
if slug is 'campaigns'
@evaluate (sel) ->
$s = jQuery sel
$s.attr('selected', 'selected')
, {sel: "#{@selectors.pagination} option:last"}
@evaluate (sel) ->
$s = jQuery sel
$s.change()
, {sel: @selectors.pagination}
# @waitUntilVisible @selectors.loading, ->
@waitWhileVisible @selectors.loading, ->
@emit 'campaigns.index', @evaluate ->
$ = jQuery
rows = [$('tr:contains(RG)').get(),
$('tr:contains(Golf)').get(),
]
merged = []
$.each rows, (i, row) ->
$.merge merged, row
uniq = $.unique merged
$.map uniq, (el,i) ->
$(el).children('td:eq(0)').text()
else super slug, id
opbot = new OPBot
stepTimeout: 180000
waitTimeout: 60000
verbose: true
# logLevel: 'debug'
opbot.setFilter 'open.location', (location) ->
location = "#{@base_url}#{location}"
opbot.start()
opbot.viewport 1024, 768
opbot.then -> @load_url 'login'
opbot.run()
opbot.on 'url.loaded', (model, id...) -> @get model, id
opbot.on 'url.redirected', (redirect_to) ->
if redirect_to is 'login_ok'
@load_url 'campaigns'
opbot.on 'campaigns.index', (campaign_ids) ->
@log "#{campaign_ids.length} #{campaign_ids}"
opbot.options.timeout = 1000 * 90 * campaign_ids.length
@test.info "Total timeout set to #{opbot.options.timeout}"
@each campaign_ids, (self, id) ->
@load_url 'campaign', id
opbot.on 'model.hydrated', (model, propbag) ->
id = propbag.id
name = propbag.name
campaign_type = propbag.campaign_type
integration_type = propbag.integration_type
@waitUntilVisible @selectors.campaign.save_button
switch integration_type
when '1'
switch campaign_type
when '1'
sel = '#input_webform_url_page_one_id'
when '2'
sel = '#input_webform_url_page_two_id'
ocp = @evaluate (sel) ->
$s = jQuery sel
v = $s.val()
unless /ltpx/.test(v)
product = jQuery('#list_products a').text()
lp = switch
when /video/i.test(product) then 5
when /cookies/i.test(product) then 4
when /shots|drink/i.test(product) then 3
when /urgent/i.test(product) then 2
when /vitamin/i.test(product) then 1
if lp
if v.indexOf('?') is -1
$s.val "#{v}?ltpx=#{lp}"
else
$s.val "#{v}&ltpx=#{lp}"
$s.val()
, {sel: sel}
if ocp isnt null
@waitFor ->
@evaluate ->
typeof SaveCampaign is 'function'
, ->
@click @selectors.campaign.save_button
@waitForSelector @selectors.campaign.save_ok, ->
@test.pass "#{id} ocp changed to #{ocp}"
else
@test.pass "#{id} is ok"
@wait 5000
opbot.on 'waitFor.timeout', ->
@captureSelector "#{Date.now()}.png", 'html'
opbot.on 'exit', ->
@test.renderResults()
# update tax profiles on all campaigns (we now only have nexus in Colorado
class TaxBot extends require('./vickybot').VickyBot
TaxBot::urls.campaigns = ->
'/campaign/index.php'
TaxBot::selectors.pagination = '.limeListSelectBox'
TaxBot::selectors.loading = '#loadinglist_campaign'
TaxBot::selectors.campaign =
id: '#campaign_id'
name: 'input[name=input_campaign_name]'
taxable: 'input[name=bool_tax]'
tax_profiles: '#taxProfileId'
save_button: '#btn_save'
save_ok: '#custom_save_pop'
TaxBot::get = (slug, id...) ->
if slug is 'campaigns'
@evaluate (sel) ->
$s = jQuery sel
$s.attr('selected', 'selected')
, {sel: "#{@selectors.pagination} option:last"}
@evaluate (sel) ->
$s = jQuery sel
$s.change()
, {sel: @selectors.pagination}
@waitWhileVisible @selectors.loading, ->
@emit 'campaigns.index', @evaluate ->
$ = jQuery
rows = [$('tr:contains(RG)').get(),
$('tr:contains(Golf)').get(),
]
merged = []
$.each rows, (i, row) ->
$.merge merged, row
uniq = $.unique merged
$.map uniq, (el,i) ->
$(el).children('td:eq(0)').text()
else super slug, id
taxbot = new TaxBot
stepTimeout: 180000
waitTimeout: 60000
# verbose: true
# logLevel: 'debug'
taxbot.setFilter 'open.location', (location) ->
location = "#{@base_url}#{location}"
taxbot.start()
taxbot.viewport 1024, 768
taxbot.then -> @load_url 'login'
taxbot.run()
taxbot.on 'url.loaded', (model, id...) -> @get model, id
taxbot.on 'url.redirected', (redirect_to) ->
if redirect_to is 'login_ok'
@load_url 'campaigns'
taxbot.on 'campaigns.index', (campaign_ids) ->
@log "#{campaign_ids.length} #{campaign_ids}"
taxbot.options.timeout = 1000 * 90 * campaign_ids.length
@test.info "Total timeout set to #{taxbot.options.timeout}"
@each campaign_ids, (self, id) ->
@load_url 'campaign', id
taxbot.on 'model.hydrated', (model, propbag) ->
id = propbag.id
@test.assertEquals propbag.tax_profiles, ['1'],
"Colorado is only state we charge tax in for ##{id} \
(found: profile ##{propbag.tax_profiles})"
taxable = @evaluate (sel) ->
$s = jQuery sel
$s.attr 'checked'
, {sel: @selectors.campaign.taxable}
@test.assertEquals taxable, 'checked',
"Taxable is enabled on campaign ##{propbag.id}"
unless taxable is 'checked'
@evaluate (sel)->
$s = jQuery sel
$s.click()
, {sel: @selectors.campaign.taxable}
unless propbag.tax_profiles?.toString() is '1'
@evaluate (sel)->
$s = jQuery sel
$s.val 1
, {sel: @selectors.campaign.tax_profiles}
if taxable isnt 'checked' or propbag.tax_profiles?.toString() isnt '1'
@test.info "Fixing sales tax for campaign ##{id}"
@click @selectors.campaign.save_button
@waitForSelector @selectors.campaign.save_ok, ->
@then ->
@test.pass "Fixed sales tax for campaign ##{id}"
# @test.pass "Fixed sales tax for campaign ##{id}"
taxbot.on 'waitFor.timeout', ->
@captureSelector "#{Date.now()}.png", 'html'
taxbot.on 'exit', ->
@test.renderResults()
class VickyBot extends require('casper').Casper
utils: require('utils')
jq_sel_invoke: (sel, func) ->
@evaluate (sel, func) ->
$s = jQuery(sel)
if typeof $s[func] is 'function'
if $s.length is 1
$s[func]()
else
$s.map (i, el) ->
jQuery(el)[func]()
.get()
else
$s[func]
, {sel, func}
re:
(s) ->
new RegExp s
.replace(/\//g,'\\/')
.replace(/\./g, '\\.')
.replace(/\?/g, '\\?')
base_url: "https://admin.example.net/admin"
urls:
login: -> '/login.php'
login_ok: -> '/admin/dashboard.php'
campaign: (id) -> "/campaign/profile.php?id=#{id}"
product: (id) -> "/products/products.php?product_id=#{id}"
verify_url: (slug, id...) ->
@test.assertUrlMatch @re(@base_url),
"I am on the correct base URL, #{@base_url}"
@test.assertUrlMatch @re(@urls[slug](id)),
"I am on the #{slug} page, #{@urls[slug](id)}"
load_url: (slug, id...) ->
url = @urls[slug](id)
@thenOpen url, ->
@verify_url slug, id
@emit 'url.loaded', slug, id
verify_fields: (model) ->
@each Object.keys(@selectors[model]), (self, sel) ->
@test.assertExists @selectors[model][sel],
"Checking that #{@selectors[model][sel]} (#{model}##{sel}) exists"
selectors:
login:
form: 'form[name=login]'
campaign:
name: 'input[name=input_campaign_name]'
id: '#campaign_id'
description: 'textarea#campaign_description'
product_ids: 'input[name^=list_products]'
upsell_ids: 'input[name^=list_upsell_products]'
upsell_collection: 'input[name^=hidden_upsell_product_]'
product:
name: '#product_name'
id: '#product_id'
sku: '#product_sku'
price: '#product_price'
max_quantity: '#product_max_quantity'
config:
credentials:
admin_name: 'fizz'
admin_pass: 'PASSWORD'
must_provide: (configs..., msg) ->
provided = true
@each configs, (self, i) ->
unless @utils.getPropertyPath(@, "config.#{i}").length > 1
provided = false
@test.assert provided is true, msg
get: (slug, id...) ->
switch slug
when 'campaign', 'product'
@hydrate slug, id
when 'login'
@must_provide 'credentials.admin_name', 'credentials.admin_pass',
"I have credentials to log in with."
creds = @config.credentials
@waitForSelector @selectors.login.form, ->
@fill @selectors.login.form, creds, true
@then ->
@test.assertUrlMatch @re(@urls.login_ok()),
"I was redirected to #{@urls.login_ok()}"
@emit 'url.redirected', 'login_ok'
hydrate: (model, id...) ->
@echo 'waiting for jQuery'
@waitFor ->
@evaluate ->
typeof jQuery is 'function'
@verify_fields model
@then ->
propbag = {}
model_fields = @selectors[model]
@each Object.keys(model_fields), (self, sel) ->
val = @jq_sel_invoke model_fields[sel], 'val'
if val then propbag[sel] = val
@emit "model.hydrated", model, propbag
exports.VickyBot = VickyBot
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment