Skip to content

Instantly share code, notes, and snippets.

@rubypirate
Created November 25, 2016 10:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rubypirate/21afea25e3006a7e2258a8333f88b7ee to your computer and use it in GitHub Desktop.
Save rubypirate/21afea25e3006a7e2258a8333f88b7ee to your computer and use it in GitHub Desktop.
class Refund
def submit
#raise self.journey.end_time.strftime("%h").inspect
session = if ARGV[0] != 'phantomjs'
Capybara::Session.new(:selenium)
else
require 'capybara/poltergeist'
Capybara::Session.new(:poltergeist)
end
#window = session.driver.browser.manage.window
#window.resize_to(800, 600)
session.visit('https://account.tfl.gov.uk/oyster/login')
session.fill_in 'UserName', with: self.user.tfl_username
session.fill_in 'Password', with: self.user.tfl_password
session.click_button 'Sign in'
session.click_link 'My refunds'
session.click_link 'Service delay refund'
session.choose 'Oyster card'
session.select self.journey.card.card_number, from: 'Please select an Oyster card number:'
sleep 1
session.click_button 'Continue'
### fill journey details
session.select 'London Underground', from: 'tflNetworkLine'
### fill journey delay
session.select self.journey.from_station.name, from: 'startStationNlc'
session.select self.journey.to_station.name, from: 'endStationNlc'
### fill and select end time
session.execute_script("$('#journeyEndDate').val('#{self.journey.end_time.strftime("%d/%m/%Y")}'); $('#journeyStartDate').val('#{self.journey.start_time.strftime("%d/%m/%Y")}');")
### fill and select start time
session.select self.journey.start_time.strftime("%H"), from: 'journeyStartDate_hh'
session.select self.journey.start_time.strftime("%M"), from: 'journeyStartDate_mins'
### fill and select end time
session.select self.journey.end_time.strftime("%H"), from: 'journeyEndDate_hh'
session.select self.journey.end_time.strftime("%M"), from: 'journeyEndDate_mins'
### select how long delay was
session.select '00', from: 'lengthOfDelay_hh'
session.select '20', from: 'lengthOfDelay_mins'
sleep 6
session.click_button 'Continue'
session.choose 'Payment to your bank account'
session.fill_in 'Sort Code', with: '123456'
session.fill_in 'Account Number', with: '2345678'
end
agent = Mechanize.new
after_login_page ||= Core.login_to_oyster(agent, User.first)
refund_page = agent.click(after_login_page.link_with(:text => /My refunds/))
if refund_page.title == "Oyster online - Transport for London - Refund history"
if refund_page.search('div#contentbox').at("div.row").text.strip == "We do not have any refund history for you."
end
new_refund_page = agent.click(refund_page.link_with(:text => /Service delay refund/))
if new_refund_page.title == "Oyster online - Transport for London - Service delay refund details"
form = new_refund_page.form_with(action: '/oyster/sdr.do')
form.radiobutton_with(:value => /OYSTER/).check
form.field_with(:name => "oysterCardId").option_with(:value => "051553034992").click
after_submit_page = form.submit
if after_submit_page.search('div.formrow').at("h3").text.strip == "Your journey details"
refund_form = after_submit_page.form_with(action: '/oyster/sdr.do')
refund_form.field_with(:name => "tflNetworkLine").option_with(:value => "UNDERGROUND").click
refund_form.field_with(:name => "startStationNlc").option_with(:value => "750").click
refund_form.field_with(:name => "endStationNlc").option_with(:value => "1404").click
refund_form.journeyStartDate = '25/07/2014'
refund_form.journeyStartDate_hh = '23'
refund_form.journeyStartDate_mins = '00'
refund_form.journeyEndDate = '25/07/2014'
refund_form.journeyEndDate_hh = '23'
refund_form.journeyEndDate_mins = '59'
refund_form.lengthOfDelay_hh = '03'
refund_form.lengthOfDelay_mins = '59'
# make sure delayed journey took place within the last 14 days and that
# end date is after start date
captcha_page = refund_form.submit
#captcha_page.open_in_browser
# captcha
iframe_url = captcha_page.iframes.first.src
params = iframe_url.split("?").last
captcha_iframe = agent.click(captcha_page.iframes.first)
captcha_form = captcha_iframe.forms.first
captcha_image = captcha_iframe.parser.css("img").first["src"]
# open browser with captcha image
system("open", "http://api.recaptcha.net/#{captcha_image}")
# enter captcha response in terminal
captcha_says = ask("Enter Captcha from Browser Image: ") { |q| q.echo = true }
captcha_form["recaptcha_response_field"] = captcha_says
# submit captcha
captcha_form.action = "http://www.google.com/recaptcha/api/noscript?#{params}"
captcha_response = captcha_form.submit
# grab secret
captcha_response = captcha_response.at("textarea").text
# submit form
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment