Thursday, October 29, 2009

Cucumber and Ruby on Rails (Part 7) My learning materials

Now lets see a scenario to go through the links and how the cucumber deals with that scenario.
Before write the scenario lets look at the scree shots and what could be the scenario.

1 Step:- I am on the “List of products” page and I can see
Titled = Ruby on Rails
Description = Model View Architecture
Image url = ror.jpg
Price = 22


2 Step:- Now I click on “New Product” link.


3 Step:- Now click on “Back” link again. Then I am again on “List of Products”.


4 Step:- Now click on “Show” link and then I can see



5 Step: like wise we can navigate through the links

Now lets see how this scenario can be written. Before that look at these step definitions. You can find them in webrat_steps.rb file.

When /^I press "([^\"]*)"$/ do |button|
click_button(button)
end

This can be used for when we click on button

When /^I follow "([^\"]*)"$/ do |link|
click_link(link)
end

When /^I follow "([^\"]*)" within "([^\"]*)"$/ do |link, parent|

click_link_within(parent, link)

end

These two can be used when we navigate through the links.
Now lets see the scenario.

Scenario: Go Through Links
Given I have products titled Ruby on Rails, description Model View Architecture, Image url ror.jpg, Price 22
When I go to the list of Products
Then I should see "Ruby on Rails"
And I should see "Model View Architecture"
And I should see "ror.jpg"
And I should see "22"
When I follow "New Product"
And I follow "Back"
And I am on the list of Products
Then I should see "Ruby on Rails"
And I should see "Model View Architecture"
And I should see "ror.jpg"
And I should see "22"
When I follow "Show"
Then I should see "Ruby on Rails"
And I should see "Model View Architecture"
And I should see "ror.jpg"
And I should see "22"
When I follow "Back"
And I am on the list of Products
Then I should see "Ruby on Rails"
And I should see "Model View Architecture"
And I should see "ror.jpg"
And I should see "22"
When I follow "Edit"
And I follow "Back"
And I am on the list of Products
Then I should see "Java"
And I should see "Platform Independent"
And I should see "java.jpg"
And I should see "20"

save this scenario as go_through_links.feature and run the cucumber features/go_through_links.feature

Then you can see the following screen shot.


Why this scenario failed. Look at these steps.

When I follow "Edit"
And I follow "Back"
And I am on the list of Products
Then I should see "Java"
And I should see "Platform Independent"
And I should see "java.jpg"
And I should see "20"

These steps are wrong. They should be as follows.

When I follow "Edit"
And I follow "Back"
And I am on the list of Products
Then I should see "Ruby on Rails"
And I should see "Model View Architecture"
And I should see "ror.jpg"
And I should see "22"

Edit the feature file and run again cucumber features/go_through_links.feature. Now you can get the all the steps are passed massage.


This is how the cucumber work with the navigation scenario.

Friday, October 23, 2009

Software Test Tools

What Are Software Test Tools?
Software test tools help development teams investigate software bugs, verify functionality, and ensure both the reliability and security of the software they develop. Tools are available for all stages of a software development project. Some software testing tool vendors offer an integrated suite that will support testing and development throughout the life of a project, from gathering requirements to supporting the live system. Other vendors concentrate on a single part of the application development life cycle.

Software Test Tools Business Drivers and Benefits
1) Increase the quality of software applications.
2) Software performance measurement.
3) Improving process management lifecycle of the product.
4) Perform a risk analysis and benchmarking.
5) Gain consistency in testing procedures with automated testing tools (as opposed to manual testing, which may be incompatible).
6) Improving the time to put on the market following the effective detection of functional performance, and security issues.
7) Save money on software development and maintenance costs

Software Test Tools Risks
1. Automated testing can produce quantities of raw data cumbersome. Test for choosing one form tool you can easily manipulate the data that is capable of change and should work together.
2. It undermines the efficiency of fault injection is not important. Fault injection tool specific code paths above weaknesses, particularly, the path that are victims of security attacks can not show through the check.
3. There are two drawbacks you enough for choosing the wrong software tools have not only wasted time and resources, but also an obstacle that you can get your teams made solutions difficult.

Monday, October 19, 2009

Test Automation

Test automation is the use of software for monitoring the performance tests, comparing actual results with expected results, the functions of setting up test conditions and control tests and other testing Reference [1]. Generally, test automation involves automating a manual process already in place that uses a formalized testing process.

Following two pictures are describing the test automation steps.



Cucumber and Ruby on Rails (Part 6) My learning materials

OK now I think you all have good knowledge about my pasts posts. I f you have any question you can ask me any time by mailing me. Now lets move to our todays lesson.

What can we do further with our project. Hmmmmm... We can validate user inputs . Lets see how can the validations can be done using cucumber.

What can we validate. We can validate “user inputs are OK such as is price is a numerical value or not and all the fields are not empty etc..”

Before doing the cucumber part lets see the what is happening in GUI part.

Go to the product/app/models/ folder and open product.rb file. Afetr that copy and paste following codes to it as follows.

class Product < ActiveRecord::Base
validates_presence_of :title, :description, :image_url
validates_numericality_of :price
end

Now lets see the GUI.
1)First go to the product folder in the terminal and then run the server “ruby script/server”
2) Then gotothe browser and type “http://localhost:3000/products” . Then you can see following GUI



3)click on New product link and you can see this page.



4)Now check what is happening when click on create button without filling any field. You can see following error massages.



5)Now you can see what is happening when there is one field is filling and click create button. Check them with your self.

6)Now lets see how the cucumber can use for this.
7)Go to the feature folder and create a new file called validate_product_details.feature and copy and paste following scenario.
8)
Scenario: Validate Product Details
Given I have no products
And I am on the list of Products
When I follow "New Product"
And I fill in "Title" with ""
And I fill in "Description" with ""
And I fill in "Image Url" with ""
And I fill in "Price" with ""
And I press "Create"
Then I should see "Title can't be blank"
And I should see "Description can't be blank"
And I should see "Image url can't be blank"
And I should see "Price is not a number"

9)Now run the cucumber features/validate_product_details.feature then you can see following.



10)It says all steps are passed. In this scenario we can check alll the validation by writing steps like follows
Scenario: Validate Product Details
Given I have no products
And I am on the list of Products
When I follow "New Product"
And I fill in "Title" with ""
And I fill in "Description" with ""
And I fill in "Image Url" with ""
And I fill in "Price" with ""
And I press "Create"
Then I should see "Title can't be blank"
And I should see "Description can't be blank"
And I should see "Image url can't be blank"
And I should see "Price is not a number"

When I fill in "Title" with "C#"
And I fill in "Description" with ""
And I fill in "Image Url" with ""
And I fill in "Price" with ""
And I press "Create"
Then I should see "Description can't be blank"
And I should see "Image url can't be blank"
And I should see "Price is not a number"

When I fill in "Title" with "C#"
And I fill in "Description" with ""
And I fill in "Image Url" with ""
And I fill in "Price" with "23"
And I press "Create"
Then I should see "Description can't be blank"
And I should see "Image url can't be blank"

When I fill in "Title" with "C#" .......


like wise we can write so many steps.



11)But you can see that is not much effective because write all the validation like this is not good. What can we do for it. Think a little bit....
12)If you can remember the last post why we can't use that. Then the above scenario can be written as follows.
Scenario: Validate Product Details
Given I have no products
And I am on the list of Products
When I follow "New Product"
When I fill in the following:
| Title | |
| Description | |
| Image Url | |
| Price | |
And I press "Create"
Then I should see "Title can't be blank"
And I should see "Description can't be blank"
And I should see "Image url can't be blank"
And I should see "Price is not a number"

13)Now run the cucumber features/validate_product_details.feature

href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhXWx6nkJ05AYrAPNXrASffsQ9wDpH0VJmyP8y-RwWxbHzKRk2Ln6JhqEaLQk1vZoQfE5ndbRfNsddELqHFsCHZs7Vh4Inj8b_Dw74KtPhWA2mNNkdiSKcZeUVnbZVF7EBCsEAY5vcGn10/s1600-h/5.png">

14)What a simple way to write validation file. Now lets expand this as follows and try to understand to what is happening there.

Scenario: Validate Product Details
Given I have no products
And I am on the list of Products
When I follow "New Product"
When I fill in the following:
| Title | |
| Description | |
| Image Url | |
| Price | |
And I press "Create"
Then I should see "Title can't be blank"
And I should see "Description can't be blank"
And I should see "Image url can't be blank"
And I should see "Price is not a number"

When I fill in the following:
| Title | |
| Description | |
| Image Url | |
| Price | 20 |
And I press "Create"
Then I should see "Title can't be blank"
And I should see "Description can't be blank"
And I should see "Image url can't be blank"

When I fill in the following:
| Title | |
| Description | |
| Image Url | a.jpg |
| Price | |
And I press "Create"
Then I should see "Title can't be blank"
And I should see "Description can't be blank"
And I should see "Price is not a number"

When I fill in the following:
| Title | |
| Description | My name is JTK |
| Image Url | |
| Price | |
And I press "Create"
Then I should see "Title can't be blank"
And I should see "Image url can't be blank"
And I should see "Price is not a number"

When I fill in the following:
| Title | JTK |
| Description | |
| Image Url | |
| Price | |
And I press "Create"
Then I should see "Description can't be blank"
And I should see "Image url can't be blank"
And I should see "Price is not a number"


15)Now run the cucumber features/validate_product_details.feature now try to write your own validation files for your projects.

Tuesday, October 13, 2009

Cucumber and Ruby on Rails (Part 5) My learning materials

OK now I think you have a good idea of my past posts. Pardon me because my new post is late. As I said earlier there is a simple way of the last scenario. First we look at that scenario.

Scenario: Create Valid Product
Given I have no products
And I am on the list of Products
When I follow "New Product"
And I fill in "Title" with "Ruby On Rails"
And I fill in "Description" with "Model View Control Architecture"
And I fill in "Image Url" with "ror.jpg"
And I fill in "Price" with "22"
And I press "Create"
Then I should see "Product was successfully created."
And I should see "Ruby On Rails"
And I should see "Model View Control Architecture"
And I should see "ror.jpg"
And I should see "22"
And I should have 1 product

In this scenario we used “I fill in “(field name)” with “(value)” ” lot of time. But we can give this in a simple way. Lets look at that scenario.

Scenario: Create Valid Product
Given I have no products
And I am on the list of Products
When I follow "New Product"
When I fill in the following:
| Title | C# |
| Description | ABC |
| Image Url | c.jpg |
| Price | 5 |
And I press "Create"
Then I should see "Product was successfully created."
And I should see "C#"
And I should see "ABC"
And I should see "C#"
And I should see "5"
And I should have 1 product

When we write this what is happening. Go to your webrat_step.rb file and you can see the following step definition.

# Use this to fill in an entire form with data from a table. Example:
#
# When I fill in the following:
# | Account Number | 5002 |
# | Expiry date | 2009-11-01|
# | Note | Nice guy |
# | Wants Email? | |
#
# TODO: Add support for checkbox, select og option
# based on naming conventions.
#

When /^I fill in the following:$/ do |fields|
fields.rows_hash.each do |name, value|
When %{I fill in "#{name}" with "#{value}"}
end
end

There you can see the the cucumber give a easy way to do our job without typing same thing several times like in the 1st scenario.

What is happening from this step definition is it created a step for us by getting the values in table. How it does lets look at step by step.

When /^I fill in the following:$/ do |fields|
From this it takes the one field of the raw for a example | Title | C# |

fields.rows_hash.each do |name, value|
From this it takes the values of the field as name and value then
name= Title
value=c#

When %{I fill in "#{name}" with "#{value}"}
From this step its create a step using that value then the step comes as
When I fill in Title with C#
and end like wise all the fields are translated by the cucumber.

Then what happen. Now it check this new step with the webrat file or with our step definition file and go through it.

The correct step definition is

When /^I fill in "([^\"]*)" with "([^\"]*)"$/ do |field, value|
fill_in(field, :with => value)
end

When we run this scenario using cucumber features/create_product.feature we got as follows.



it says all the steps are pass. that means code is OK. see how the cucumber is easy to work. according to that you can type these steps as table in that scenario

Then I should see "Product was successfully created."
And I should see "C#"
And I should see "ABC"
And I should see "C#"
And I should see "5"

try it your self.

Best Regards,
Jeewantha.

Sunday, October 4, 2009

Cucumber and Ruby on Rails (Part 4) My learning materials

I think you get a good knowledge from my past posts. OK now lets see to test adding new products. Before that lets see what is happening when new product is adding.

1)He should be in the List of Product page as follows.



2)Now what he do . He clicks the New Products Link. Now he can see the following page.



3)Now he fill the fields as
Title = Ruby On Rails
Description = Model View Control Architecture
Image url= ror.jpg
Price=22

4)Now he clicks Create button and he can see following page.



5)He can see “Product was successfully created” and he can see “Ruby On Rails”, “Model View Control Architecture”, “ror.jpg” and “22”.

6)Now lets see how we can test it using Cucumber Testing.

7)Create a new file called “create_product.feature” in product/features folder and paste this scenario.

Scenario: Create Valid Product

Given I have no products

And I am on the list of Products

When I follow "New Product"

And I fill in "Title" with "Ruby On Rails"

And I fill in "Description" with "Model View Control Architecture"

And I fill in "Image Url" with "ror.jpg"

And I fill in "Price" with "22"

And I press "Create"

Then I should see "Product was successfully created."

And I should see "Ruby On Rails"

And I should see "Model View Control Architecture"

And I should see "ror.jpg"

And I should see "22"

And I should have 1 product



8)This scenario check all the steps which I shown above. Check them your self. Now go to the "Product_steps.rb"page in features/step_definition folder and paste following codes.

Given /^I have no products$/ do
Product.delete_all
end

Then /^I should have ([0-9]+) product$/ do |count|
Product.count.should == count.to_i
end


9)Now we have 2 feature files as list_of_product.feature and create_product.feature
10)when we run cucumber features all the features files are run. If we want to run only one file we can run cucumber features/(file name)
eg:- cucumber features/ create_product.feature

11)Run the cucumber features/ create_product.feature and you can see following.



12)It says all the steps are passed. Now you can test to verify actually this scenario works with the code for that we can change some codes in scenario and check running cucumber features.

13)I give it to you as a home work. Check what happening when the scenario change and run cucumber features.

14)Lets see in the next post how this scenario can be written in simple way.

Thursday, October 1, 2009

Cucumber and Ruby on Rails (Part 3) My learning materials

Now lets start to create scenarios and do testing using cucumber

1)Lets create our first scenario as follows and save it in the product/features folder as named
list_of_product.feature

Scenario: Products List
Given I have products titled Java, description Platform Independent, Image url java.jpg, Price 20
When I go to the list of Products
Then I should see "Java"
And I should see "Platform Independent"
And I should see "java.jpg"
And I should see "20"

2)Now run the cucumber features and see the out put as




3)You get 1 scenario (1 undefined)
6 steps (5 skipped, 1 undefined)
that means your 1st step is not defined. That means the cucumber do not know how to deal with the step

GivenI have products titled Java, description Platform Independent, Image url java.jpg, Price 20

To deal with this that should be in the webrat_step.rb file in the features/step_definitions folder. If not we have to create them for the use of cucumber and you can see the cucumber suggest you some definitions as

Given /^I have products titled Java, description Platform Independent, Image url java\.jpg, Price 20$/ do
pending
end



4)Now lets create the definition file. Create a file named product_step.rb file in the features/step_definitions folder and paste the following code there and save it.
Given /^I have products titled (.+), description (.+), Image url (.+), Price (.+)$/ do |title,description,image_url,price|

Product.create!(:title => title, :description => description, :image_url => image_url, :price => price)

end


In this step it says to store the data in the table called Product.

5)Now lets run again cucumber features and see the result. You can see following error.



This says there is no such a constant like Product. This means still we do not create our Product Table lets create our product table. Type on the terminal as follows.

ruby script/generate scaffold product title:string description:text image_url:string price:decimal

rake db:migrate


6)Now lets run again cucumber features



That says that product_test.products table. That because these scenarios working with the tables in the test database. So lets fix it. Run following code.

rake db:test:clone

Now lets run again cucumber features.

7)Its hows the following error and add it to the file path.rb in features/support

this should come after case statement

when /the list of Products/

products_path



the reason for this is (

We have our first passing step! The second step is now failing, though. This is because Cucumber doesn’t know how to translate “the list of articles” into a path in our application.
One of the files installed by Cucumber is called paths.rb, which lives in the /features/support directory. In this file we can define custom paths that map the English definitions in Cucumber files to paths in our Rails application. To add a mapping we just add a new when condition to the case statement in the path_to method. (There’s a comment in the file to show us where to add it.)
)
8)Now run again cucumber features



9)You get all the scenarios are passed.
10)Now our first scenario is passed lets try another scenario in the next post. Try this scenario and check you can understand it. Copy and paste to the same feature file

Scenario: Products List
Given I have products titled Java, description Platform Independent, Image url java.jpg, Price 20
And I have products titled Ruby on Rails, description Rich internet Application, Image url ror.gif, Price 25
When I go to the list of Products
Then I should see "Java"
And I should see "Platform Independent"
And I should see "java.jpg"
And I should see "20"
Then I should see "Ruby on Rails"
And I should see "Rich internet Application"
And I should see "ror.gif"
And I should see "25"

Wednesday, September 30, 2009

Ruby cucumber Part3

Hi all,
I am very sorry for the delay of my 3rd post because these days are i am very busy. i hope to do it as soon as possible. Most probably today afternoon i can do it.
Cheers,
jeewantha.

Sunday, September 27, 2009

Cucumber and Ruby on Rails (Part 2) My learning materials

Create a project and do testing using cucumber.

Project name “product”

Requirement

  1. The user can see the available products.

When the user come to the page he should be able to see all the available products with description like

Title: Ruby on Rails

Description: Model, view, controller architecture

Image Url: ror.jpg

Price: 21

  1. He should be able to add new products.

  2. He should be able to show the products.

  3. He should be able to edit products.

  4. He should be able to validate products details when adding new products.

Error massages should be display when there is a wrong with adding data like

Price should be number

Title cant be blank etc.

  1. When Edit product the validation also should be done.

  2. He should be able to navigate any places by clicking links.

  3. He should be able to delete products.

Product table contents

Title = String

Description = String

Image Url = String

Price = Decimal



Lets start developing the project.

  1. Install cucumber

sudo gem1.8 install cucumber

  1. The plugins’ dependencies must be installed separately:

gem install term-ansicolor treetop diff-lcs nokogiri

  1. Lets start developing our project

Open a terminal then type

rails -d mysql product

cd product

rake db:create:all

rake db:migrate

  1. Now lets try to create cucumber

ruby script/generate cucumber

  1. Then run

Cucumber features

  1. Then u can see as follows







  1. Now you are ready to work with cucumber

  2. lets see that in next post.





Saturday, September 26, 2009

Cucumber and Ruby on Rails (Part 1)



I write this post to exchange my experienced with you about Cucumber and ruby and rails.

What is cucumber?
Actually when i heard this word i get a funny and i get a picture as follows.

After that i searched web and i try to find tutorials for learning this myself. when i was finding about cucumber i got idea about as follows.


After that i started finding and do various type of tutorials and try to get basics of cucumber. But it was very difficult to me learn this because there are not much tutorials for that i found 2 tutorials and by refering them i got basic idea and i think to share my knowledge with others so that is why this kind of blog post is come out. I think this will for the begginers to get an idea.

Now lets see what is the cucumber in ruby on rais and in other post how to start to test a project using cucumber.

Cucumber (Behavioral Driven Development ) is used for testing the behavior of a code. the programmers says that cucumber is used for accepting testing. Any way this is used for behaviral testing.

Reads plain texts….
Texts are written as scenarios
Scenarios interact with the code
Check the Codes are correct

Cucumber lets software development teams describe how software should behave in plain text. The text is written in a business-readable domain-specific language and serves as documentation, automated tests and development-aid - all rolled into one format.

Lets see the simple senario and discuss about it.

Scenario: Listing & showing products
Given I have products titled Java, description Platform Independent, Image url java.jpg, Price 20
When I go to the list of Products
Then I should see "Java"
And I should see "Platform Independent"
And I should see "java.jpg"
And I should see "20"

As u can see the the senario is describe in palain text. here the Given, When, Then and And are the key words which is used by the cucumber. how the cucumber work with this type of plain text for that it uses the regular expressions. See the folowing regular expression (Don't wory that much about that we'll see them leter in detail)

Given /^I have products titled (.+), description (.+), Image url (.+), Price (.+)$/ do
|title,description,image_url,price|
Product.create!(:title => title, :description => description, :image_url => image_url, :price => price)
end

This describes the first step of the above senario. we do not much trouble with regular expressions. Becuse when we generate cucumber we get predefine regular expressions in webrat_steps.rb file.

Now I think u have basic idea about cucumber. In next posts lets see step by step to create a project and testing project using cucumber.

Thursday, September 24, 2009

Working with Ruby on Rails Cucumber From the Beginning(Part I)


Create a project and do testing using cucumber.

Project name “product”


Requirement

  1. The user can see the available products.

When the user come to the page he should be able to see all the available products with description like

Title: Ruby on Rails

Description: Model, view, controller architecture

Image Url: ror.jpg

Price: 21

  1. He should be able to add new products.

  2. He should be able to show the products.

  3. He should be able to edit products.

  4. He should be able to validate products details when adding new products.

Error massages should be display when there is a wrong with adding data like

Price should be number

Title cant be blank etc.


  1. When Edit product the validation also should be done.

  2. He should be able to navigate any places by clicking links.

  3. He should be able to delete products.

Product table contents

Title = String

Description = String

Image Url = String

Price = Decimal


Lets start developing the project.


  1. Install cucumber

sudo gem1.8 install cucumber


  1. The plugins’ dependencies must be installed separately:

gem install term-ansicolor treetop diff-lcs nokogiri



  1. Lets start developing our project

Open a terminal then type

rails -d mysql product

cd product

rake db:create:all

rake db:migrate




  1. Now lets try to create cucumber

ruby script/generate cucumber


  1. Then run

Rake Features Or Cucumber features


  1. Then u can see as follows



  1. Now you are ready to work with cucumber

  2. lets see that in next post.






Thursday, August 27, 2009

Test Automation Tool

There are a lot of test automation tool in software industry. the iMacro and selenium IDE re two of them. Both can be installed as a firefox add-on. lets see how can it be done. in the next post.

Wednesday, August 26, 2009

Test Automation

Test automation is the use of software to control the execution of tests, the comparison of actual outcomes to predicted outcomes, the setting up of test preconditions, and other test control and test reporting functions. Commonly, test automation involves automating a manual process already in place that uses a formalized testing process.

Although manual tests may find many defects in a software application, it is a laborious and time consuming process. In addition it may not be effective in finding certain classes of defects. Test automation is a process of writing a computer program to do testing that would otherwise need to be done manually. Once tests has been automated, they can be run quickly. This is often the most cost effective method for software products that have a long maintenance life, because even minor patches over the lifetime of the application can cause features to break which were working at an earlier point in time.

There are two general approaches to test automation:

  • Code-driven testing. The public interfaceto classes, modules, or libraries are tested with a variety of input arguments to validate that the results that are returned are correct.
  • Graphical User Interface testing. A testing framework generates user interface events such as keystrokes and mouse clicks, and observes the changes that result in the user interface, to validate that the observable behavior of the program is correct.

Test automation tools can be expensive, and it is usually employed in combination with manual testing. It can be made cost-effective in the longer term, especially when used repeatedly in regression testing.

What to automate, when to automate, or even whether one really needs automation are crucial decisions which the testing (or development) team has to take. Selecting the correct features of the product for automation largely decides the success of the automation. Automating unstable features or features that are undergoing changes should be avoided.

reference:- http://en.wikipedia.org/wiki/Test_automation