Testing Web Sites with Ruby and Selenium

I used Selenium about 6 years ago when I was working on my MSc, and was testing mobile web applications for W3C best practice implementation. Recently, I had cause to install Selenium again to do some web application testing at work that I wanted to automate – but this time, instead of using the Java binding, I used Ruby. I captured the notes below on how to get all this set up.

Firstly, you can get the Selenium webdriver gem by running:

gem install selenium-webdriver

The documentation is available at: http://seleniumhq.github.io/selenium/docs/api/rb/.

Anyways, I decided to use the Firefox driver, as I like working with the Firefox Dev tools, and the setup is a bit quicker than using Chrome. Just make sure Firefox is installed on your dev machine and you will be good to go with this.

To get up and running, I wrote a ruby script to connect to the web application, log in and click some screen elements to navigate around the UI.

require "selenium-webdriver"

#set up webdriver
driver = Selenium::WebDriver.for :firefox

driver.manage.timeouts.implicit_wait=5

driver.navigate.to "http://localhost/jams/login"

#get creds to login
username = driver.find_element(:id, "mat-input-0").send_keys "username"
password = driver.find_element(:id, "mat-input-1").send_keys "password"

#click on login
form = driver.find_element(:tag_name, "button").click

#just print the title to make sure it works.
puts driver.title

# find the Definitions folder and open it

definitions = driver.find_elements(:class, "nav-item-label")

definitions[1].click

#expand the menu to see all the subfolders
expand = driver.find_element(:class, "toggle-children").click

#close the webdriver
#driver.quit
Advertisements

Solving ‘Countdown’ with Ruby

I used to love watching Countdown (https://en.wikipedia.org/wiki/Countdown_(game_show)) on TV after school each day, and I used to be super jealous at how clever the contestants were to be able to quickly unscramble the anagrams and form words from the randomly selected consonants and vowels (yes, my path to nerdom started at a young age).

So, just for fun I decided to write a little Ruby script that would allow me to solve the Countdown word games in double-quick time. There is really nothing to this little script, but let me talk through the logic. It creates a ‘dictionary’ of words by loading in a text file, containing a list of English words on each line, and then re-orders the letters of each word in alphabetical order. The ordered and un-ordered letter sequences are added to a Hash as keys and values, which serves as a look up against user-entered letter sequences. Any letter sequences which match, the original word is added to the result set.

Take a look at the code below, and leave a comment if there is a better way to do this!

# Method declarations

def dictionary (wordlist)

        ordered_word_list = Hash.new

        wordlist.each do |w|

                            ordered_word_list[w.chomp.chars.sort.join.to_s] = w.chomp.to_s      

                            end

        return ordered_word_list

end

def match_words (user_input, d)

    #order the input string for comparison against ordered_word_list keys

    ordered_input = user_input.chars.sort.join.to_s

    #matching loop, where there is a hit - add the value for the key to the result set

    result_set = []

    d.each do |k, v|

        if (k.length > 3) && (ordered_input.include? k)

            result_set << v

        end

    end

    puts "Matches identified:"

    puts result_set

end

def display_message

    puts "This appication takes a string of letters from the standard input, and returns a list of words which can be formed from those letters..."

    puts "Like Countdown..."

    puts "Please enter the string to match:"

    STDOUT.flush

end

# entry point for main program

# create the ordered_word_list from a word list file

puts "Generating dictionary..."

d = dictionary (File.open('words.txt'))

puts "Dictionary successfully created!"

#get user input to match against

display_message

user_input = gets.chomp.downcase

# pass this input to match_words to generate the hits

match_words user_input, d

Using Ruby & WMI to gather Software Metering Data

I have been working on SCCM Software Metering recently and diving quite a bit into the metering data retained in the SCCM database. I am not very experienced with the SCCM data model, and wasn’t 100% sure if the records in the Recently_Used_Apps view corresponds to everything retained in WMI on the clients themselves. So, I wrote a short ruby script to connect to the root\\ccm\SoftwareMeteringAgent namespace and check the local CCM_RecentlyUsedApps data. I want to cross-reference this against what I am seeing for some machines in the corresponding SCCM view.

Anyways, the script is pretty straightforward – just uses ‘win32ole’ to provide the WMI connection capabilities and ‘date’ to do some date conversion. The script pumps out the recently used applications to a csv file, as that seems like a sensible place to sort the LastUsedDates and Company Names, etc.

require 'win32ole'
require 'date'

puts "Enter hostname to scan"
host = gets.chomp

objWMI = WIN32OLE.new("WbemScripting.SWbemLocator")

connection = objWMI.ConnectServer("#{host}", "root\\ccm\\SoftwareMeteringAgent")

recently_used = connection.ExecQuery("select * from CCM_RecentlyUsedApps")

csv_output = ""
csv_output << "CompanyName,ExplorerFileName,FileDescription,LastUsedTime,LastUserName,LaunchCount\n"

for app in recently_used do
 csv_output << "#{app.CompanyName.gsub(",", "-")},#{app.ExplorerFileName.gsub(",", "-")},#{app.FileDescription.gsub(",", "-")},#{Date.parse(app.LastUsedTime.slice(0,8))},#{app.LastUserName.gsub(",", "-")},#{app.LaunchCount}\n"
 end
filename = "#{host}_#{Time.now.to_s.slice(0,10)}.csv"
out_file = File.new(filename,"w")
out_file << csv_output
out_file.close
puts "Query executed successfully. Check the current directory for #{filename}."