Creating multiple JIRA users with Ruby and Watir

I recently wrote a quick Watir script that creates multiple JIRA users that are specified in a csv file. It was very easy and quick to write because the JIRA interface is nice to automate.

I would have preferred to use the JIRA API because it would have run quicker, and I generally prefer to automate an API if it is available. I quickly discovered that the XML-RPC API doesn’t allow you to create JIRA users (bummer), and I couldn’t quickly get the SOAP API working using ruby.

Users are specified in a comma separated file as username, full name, email.


require "watir"

JIRA_URL = "http://jira.com/"
JIRA_USERNAME = "username"
JIRA_PASSWORD = "password"
USERS_FILE_PATH = "users.csv"

browser = Watir::IE.new
browser.speed = :fast
browser.goto(JIRA_URL)

if browser.text.include? 'login' then
   browser.text_field(:name,"os_username").set JIRA_USERNAME
   browser.text_field(:name,"os_password").set JIRA_PASSWORD
   browser.button(:id,"login").click
end

browser.link(:id, "admin_link").click
browser.link(:id, "user_browser").click

File.open(USERS_FILE_PATH) do |file|
   while line = file.gets
      browser.link(:id, "add_user").click
      browser.text_field(:name, "username").set(line.split(',')[0])
      browser.text_field(:name, "password").set(line.split(',')[0]) #same as username
      browser.text_field(:name, "confirm").set(line.split(',')[0])
      browser.text_field(:name, "fullname").set(line.split(',')[1])
      browser.text_field(:name, "email").set(line.split(',')[2])
      browser.checkbox(:name, "sendEmail").clear
      browser.button(:name,"Create").click

      if browser.text.include? 'Create New User' then
         # We have an error
         error = ''
         browser.spans.each do |span|
            if span.attribute_value( 'className' ) == "errMsg" then
               error << span.text
            end
         end
         puts "ERROR: username: #{line.split(',')[0]} error message: #{error}"
         browser.button(:id, "cancelButton").click
      else
         puts "SUCCESS: username: #{line.split(',')[0]}"
         browser.link(:id, "user_browser").click
      end
   end
end

browser.link(:text, "Log Out").click
browser.close

Leave a Reply