Skip to content

Instantly share code, notes, and snippets.

@kylekyle
Created December 13, 2019 16:29
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 kylekyle/941a9baded0cc2ee49a25e20bff44a05 to your computer and use it in GitHub Desktop.
Save kylekyle/941a9baded0cc2ee49a25e20bff44a05 to your computer and use it in GitHub Desktop.
Canvas -> AMS with Watir
require 'csv'
require 'watir'
raise "Need a TSV with grades!" if ARGV.empty?
puts "Navigate to your Grade Book to continue ...";
browser = Watir::Browser.new :chrome, :switches => %w[--log-level=3 --test-type]
browser.goto 'https://apps.westpoint.edu/ams/main.cfm'
browser.wait_until(timeout: 3600) { browser.title[/Grade Book/] }
# string encoding are the worst ...
file = File.read(ARGV.first).encode('ASCII',
replace: '',
undef: :replace,
invlid: :replace
)
columns = CSV.parse(file, headers: true, col_sep: "\t")
# Okay, AMS uses two frames: One for the menu on the left and
# one for the content on the right. Every click reloads the
# the frames, which invalidates the handles
left = browser.frame(name: 'menu_win')
right = browser.frame(name: 'crse_main_win')
# figure out how many students there are
num_students_regex = /(?<records>\d+) Records/
records = right.p(visible_text: num_students_regex).wait_until(&:present?)
num_students = records.text.match(num_students_regex)[:records].to_i
unless columns.count == num_students
raise "The number of rows (#{columns.count}) don't match the number of students (#{num_students})!"
end
columns.by_col.each do |event,grades|
break if event == "STOP"
puts "Entering grades for #{event} ... "
left.link(text: event).click
right.link(text: /Edit/i).click
right = browser.frame(name: 'crse_main_win')
browser.wait_until(timeout: 3600) do
right.text_fields.count == num_students
end
fields = right.text_fields
grades.each_with_index {|grade,i| fields[i].set grade}
right.inputs(type:'submit').last.click
end
puts "Done"; sleep
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment