Created
October 25, 2012 02:16
-
-
Save waterlou/3950082 to your computer and use it in GitHub Desktop.
phantomjs script to automate form submission process for Hong Kong iPhone 5 reservation lucky draw
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### | |
Usage: phantomjs iphonereserve.coffee "First Name" "Last Name" "email address" "ID Card number" "product code" "store code" | |
product code: | |
"MD297ZP/A" : 16G Black | |
"MD298ZP/A" : 16G White | |
"MD299ZP/A" : 32G Black | |
"MD300ZP/A" : 32G White | |
"MD662ZP/A" : 64G Black | |
"MD663ZP/A" : 64G White | |
store code: | |
"R428" : IFC | |
"R485" : Festival Walk | |
### | |
page = require('webpage').create() | |
system = require('system') | |
phantom.settings = | |
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.55.3 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10" | |
page.state = 0 | |
url = "https://reserve.apple.com/HK/en_HK/reserve/iPhone" | |
page.onAlert = (msg) -> | |
console.log "alert> #{msg}" | |
page.onConfirm = (msg) -> | |
console.log "confirm> #{msg}" | |
return true | |
page.onPrompt = (msg) -> | |
console.log "prompt> #{msg}" | |
page.onConsoleMessage = (msg) -> | |
console.log "console> #{msg}" | |
page.onError = (msg, trace) -> | |
console.log(msg) | |
for item in trace | |
console.log "#{item.file} : #{item.line}" | |
page.onResourceRequested = (res) -> | |
console.log "requesting> #{JSON.stringify(res)}" if false | |
page.onResourceReceived = (res) -> | |
console.log "requested> #{JSON.stringify(res)}" if false | |
page.onUrlChanged = (res) -> | |
console.log "urlChanged> #{JSON.stringify(res)}" if true | |
++page.state | |
firstname = system.args[1] | |
lastname = system.args[2] | |
email = system.args[3] | |
govid = system.args[4] | |
model = system.args[5] | |
storecode = system.args[6] | |
retValue = 0 # by default return 0 | |
console.log "data: #{firstname}, #{lastname}, #{email}, #{govid}, #{model}, #{storecode}" | |
page.onLoadFinished = (status) -> | |
console.log "status: #{status}" | |
if status != 'success' | |
console.log "Connection failed." | |
phantom.exit() | |
return | |
# no need to reload jquery for apple site | |
#page.injectJs 'jquery-1.8.2.min.js' | |
if page.state==1 | |
# select store and product | |
page.evaluate (model, storecode) -> | |
jQuery("#store").val(storecode).change() | |
console.log "store selected" | |
filterstring = '[value="' + model + '"]' | |
jQuery('input[name = product]').filter(filterstring).attr('checked', true).click() | |
console.log "product selected" | |
, model, storecode | |
# wait until contract type dialog popup | |
page.evaluate -> | |
planbox = jQuery('.planoverlayBox') | |
console.log "loop until popup shown" | |
while planbox.css('display')=='none' | |
; | |
# select SIM-Free contract and click confirm | |
page.evaluate -> | |
jQuery('input[name = planRadioButton]').filter('[value=UNLOCKED]').attr('checked', true).click() | |
jQuery('#continueLabelC').click() | |
# enter all other info and wait for url change | |
page.evaluate (firstname, lastname, email, govid)-> | |
jQuery("#quantity").val("2").change() | |
jQuery('#firstname').val(firstname) | |
jQuery('#lastname').val(lastname) | |
jQuery('#email').val(email) | |
jQuery('#govid').val(govid) | |
jQuery('#submitButton a').click() | |
, firstname, lastname, email, govid | |
if page.state==2 | |
page.render('apple.png') # save last screen | |
console.log "done" | |
phantom.exit(retValue) | |
page.open url |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment