Showing posts with label ruby. Show all posts
Showing posts with label ruby. Show all posts

30 January 2013

Getting Safari Webdriver To Work

This is going to take a good part of your day... so be prepared if you want to do this.

Where I work, I set up a Cucumber automation framework that basically runs: IE9, FF, Chrome and I was just given a Mac Mini to support Safari testing.

The ultimate goal is to have: IE8, IE9, FF, Chrome and Safari - running in parallel.

FF, Chrome and IE are pretty easy to set up.  HOWEVER, Safari, is a big pain.

First, you need to do everything here:
http://watirmelon.com/2012/04/17/using-watir-webdriver-with-safari-at-last/

That will pretty much take half your day.  If you have a fresh Mac, you'll need xCode and you'll need to get the command line package for xCode so you can use svn.  What Alister Scott on that link will tell you to do can be summed up as:
  • Make a Apple Safari Dev account (free)
  • Get a certificate
  • Download the certificate
  • Open Safari and install the certificate
  • One thing not mentioned in the instructions, is that you can't install the cerficiate till Safari knows you're a developer, to do that you have to open the key chain on the mac and click system and drag/drop your certificate and they you must right-click the certificate, and set all actions as "TRUST" and finally you have to accept these changes.
  • Then you build the extension (downloading via svn the code, and building the extension.
That's where Alister's tutorial ends... but you'll no doubt get a problem.  If you are like most, you'll get Safari hitting 127.0.0.1/wd for each test... not hitting your target site, and you'll get an error referencing "EOFError: end of file reached" One of the comments on Alister's post has the solution...

You now need to do a gem install selenium-webdriver (i couldn't do the -pre that he suggested, so i just did gem install selenium-webdriver) and I added this to my env.rb file as gem 'selenium webdriver'

After that, just rebuild: bundle update and you should be good.

27 September 2012

Rails application - Palindrome Validation

I wrote a ruby/rails application to validate a palindrome.  Here's the published palindrome app on Heroku:
http://glacial-river-2425.herokuapp.com/

I came up with the code in my head on the way home from work.  I came across the method .reverse in rails.  This makes checking for a palindrome pretty easy in rails.

After I came home from work I sat down, and ran IRB then input this code:
x = "racecar"
if x == x.reverse
puts "this is a palindrome"
else "this is not a palindrome"
end

It worked.  So now I just needed to put this into a published Rails application. 

I created a Rails project
Generated a Controller and a some views

in the Index view I put this code on the page:
<%=form_tag(:action =>'results') do %>

<%=text_field_tag(:word)  %>
<%= submit_tag("Submit") %>
<% end %>

Then in the controller I put this code:
  def results
    @word = params[:word]
    if @word == @word.reverse
      @palindrome = "This is a palindrome"
    else
      @palindrome = "This is NOT a palindrome"
    end
  end

Finally on the results page:
<%= @palindrome %>

So basically that simple line of code, is used to verify that a word input into the form is a palindrome or not and the results are put to a results page.


14 September 2012

Testing API's with Cucumber

I put together several API tests.  Here's the project on Github.com:
https://github.com/wbwarnerb/movielookup

The project has three Cucumber features (under the features directory) that talk with a public API.

For the purpose of this test I'm using Rotten Tomatoes public API.  They have a nice JSON response to parse and utilize for these examples.

Example 1: /features/movie_json_smoke.feature

The first feature is a smoke test it basically validates the json itself.  The idea here is: We know what we expect to return in the JSON, we verify that the categories and sub categories are intact and no changes have occurred. This is useful, if a change to the service was made, but the overall structure of the JSON should remain the same.  Just kick off this test, and it validates the JSON structure.

Here's how I did it in cucumber:
https://github.com/wbwarnerb/movielookup/blob/master/features/movie_json_smoke.feature
from here we have the Cucumber test itself.  I build the test out as a Scenario Outline, this allows me to utilize the data tables.  When I get to the "Then", I pass in a parameter from the table, like this:
Then the response should have  the <category> expected
 
The category parameter matches the data table, which has a column header "category" - every element under that column in the table is passed into the Then statement.

The actual code is in the step definition:
it's here that I grab the parameters coming form the table / test and I pass them into the relevant spots.  
Here's an example:
Then /^the response should have the (.*) expected$/ do |category|
  @mquery["#{category}"].should be_true
end
First, notice the (.*), that is the placeholder for the variable in the test <category>. Second, the argument I'm grabbing |category| is coming from the test. It takes the Category value (from the data table in the test) and passes it through here.

The next line of code reads @mquery (that's the instance variable) ["#{category}"].should be_true
So this is the way ruby picks up the JSON objects ['category']['subcategory'] But since we're passing in a dynamic parameter here, we need double quotes. so ["#{Category}"] (the #{category} will be replaced with the value in the data table.

The .should be_true tripped me up. I kept trying == !nil and .should == !nil, nothing worked until I found it should be done like this .should be_true. This validates that the JSON element the method is attached to is present.
 At this point, I just iterate through each parameter in their JSON returned.  I go through each category and subcategory and make sure that it's all there.  

I figure that's a good smoke test.  If I worked at RT, and some dev committed some code to their service/api, I could just run this and quickly see that json categories are still present.  

Example 2: movielookup_data_validation.feature


In this test, I'm treating a data table in the test as the source of truth.  This could be a database, but I'm using a data table with Cucumber's Scenario Outline.  This time, I've added several columns.  I'm verifying the mpaa_rating and the profile image stored at Rotten Tomatoes.   

The code for verifying this is similar in concept to what I did previously:

I go through several movies... passed through, like this:

Given /^the API is queried for (.*)$/ do |title|
  @mquery = JSON.parse(open("http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=jc7eaxjfpemb2uz7qsrudnsq&q=#{title}&page_limit=1").read)
end

The argument is passed through and is passed through to the API URI and grabs that movie's details which are then verified

Notice that the main difference then before is this code:
 @mquery['movies'][0]['mpaa_rating'].should == "#{mpaa_rating}"

The main difference, is that I'm passing the argument through to use as a validation check.  

Example 3: /features/movielookup_json_validation.feature


These tests I separated out of the other two.  These tests are made to just test the JSON functionality itself.


Given /^A request for an unknown movie is sent to the API$/ do
  @bquery = JSON.parse(open("http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=jc7eaxjfpemb2uz7qsrudnsq&q=bbxa&page_limit=1").read)
end
Then /^the response should be zero movies returned$/ do
  @bquery['total'].should == 0
end
Given /^A API call is made for more then one movie$/ do
  @mquery = JSON.parse(open("http://api.rottentomatoes.com/api/public/v1.0/movies.json?apikey=jc7eaxjfpemb2uz7qsrudnsq&q=Toy+Story&page_limit=1").read)
end
Then /^the results wil have a total greater than 1$/ do
  @mquery['total'].should > 1
end
Basically in these two simple tests I'm checking that if a query is sent for a movie not in the RT movie database, that we get back a proper response. 
The second test verifies that if a query is sent for a movie with more then one hit (like Toy Story) it will display a total value greater the 1.

01 August 2012

Rails Basic's Notes

So what is rails?
it's a web app framework for ruby's

That means it's a set of code libraries that can provide general functionality that can be used, and reused. It also has default values built in and flow controls.  Libraries usually don't set default values.




Rails uses MVC Architecture 
MVC architecture stands for: Model View Controler

The Model is the objects

The View is the presentation layer (what the user sees and interacts with)

The Control processes and responses to events - such as user actions

So...
Decision code goes in the controller

Data code goes in the model

Presentation code goes in the view

Rails refers to the view as the action view

Rails refers to the model as the active record

Rails refers to the model and view together as the "action pack"

31 July 2012

Watir does support all element tags

In my last post, I commented on how brittle the Watir example was in the Watir documentation.  But I since found that Watir does support all the normal ID's and element Class'... here's a list of some of it's capabilties:
http://wiki.openqa.org/display/WTR/Ways+Available+To+Identify+HTML+Element

Watir: simple automation sample that's far too brittle

Once Watir is installed, along with Ruby and the Ruby DevKit (instructions for which are at www.watir.com), I was able to write out the following after reading their basic manual:

require "watir-webdriver"
browser = Watir::Browser.new :ff
browser.goto "google.com"
browser.link(:text => "Images").click
browser.text_field(:name => "q").set "plane"
browser.button(:value => "Search by image").click


I saved it to a .rb file and ran it: ruby google_search.rb 

Is there any problem with this example?

Yes, there is.  It works, for now.  That's the problem.  It's entirely too brittle.  In fact the example that comes with Watir doesn't work.  Google has changed their value for the Search button since the documentation was written. 

The Watir example used a value of "Search Images" - which has since changed to "Search by image" - if you use this type of automation text will no doubt change often.  It's best to use CSS ID's or Classes as often as you can. 

I'm not sure yet if Watir supports ID's, i'm sure it does.  But I'll need to dig into it further

Watir and Ruby

I started QA / Web automation in a language called Groovy.  I found it very difficult to pick up.  The people who suggested it as a useful language felt it was easy for newcomers to pick up. 

The problem with Groovy is:
  • The training resources are written for Java developers.  If you're not a Java developer you almost need to pick up Java first then learn Groovy - which is entirely backwards to the goal
  • Groovy has little support out there, compared to Ruby
  • Pain in the Ass to set up an automation framework in it.  Very fragile.  I've been using GEB - but you end up having all sorts of conflicts with the Groovy compiler verison, Webdriver verison, Spock version, etc.   It's constantly needing supervision and maintenance. 
The upside of Groovy is:
  • Native use of Java (you can just type Java code and the Groovy compiler will run it)
  • Native support for JODBC drivers.
But tonight, having never used Ruby, I decided to try and install Watir to see how daunting a task it would be. 

In less then about 20min (10min to install Ruby and Watir) I was able to write a test in the Watir framework. 

Groovy took me months to get the GEB framework going. 

I love IRB - interactive ruby is so great to actually see how to drive the browser before comitting to code... I can just type:
irb
>require "watir"
>browser=Watir::Browser.new

At this point a default browser spawns!

>browser.goto "watir.com"

It pulls up www.watir.com

Pretty nifty and easy.  IRB is great to just see what you want to get done. 

I'm not sure yet if Watir will have the great JQuery that Groovy has built in, but I'll give it a spin and see how it works out.