Monthly Archives: April 2014

SoapUI – Converting your functional tests into load tests

In the SoapUI – Getting Started we have seen how to create a functional test.

Now we are going to see how to do load tests for the API’s by reusing your functional tests that you have created.

Once you have created your functional tests, under TestCase tree, you will be having three child nodes.
one is test step where your functional test steps resides. Second is the Load Test node.

Right Cick on the Load Test Node and select “New Load Test”. This will automatically create the Load Tests Steps for you.

Load1

If you open the window, You can see some new terms or facts in the window. Lets see these facts one by one.

1. In the LoadTest tool bar buttons, you can see a Limit option by default sets to 60 seconds.
This is nothing but Setting the limit for the LoadTest Execution time.
2. Threads – Sets the number of threads/Virtual users to be used.
3. Strategy – Choose a LoadTest Strategy. Lets see this in detail. By default I will use Simple which means, TestCases will be executed within a configurable Test delay.
4. Test Delay – The time in milliseconds between each batch of requests.
5. Random – The random factor of the Test Load. This means by default the Test delay is 1000 ms and the random factor is 0.5.
So the threads will be distributed randomly between 500ms – 1000ms
6. min-The shortest response time the step has taken (in milliseconds).
7. max-The longest response time the step has taken (in milliseconds).
8. avg-The average response time for the test step (in milliseconds).
9. last-The last response time for the test step (in milliseconds).
10. cnt-The number of times the test step has been executed.
11. tps-The number of transactions per second for the test step.
12. bytes-The number of bytes processed by the test step.
13. bps-The bytes per second processed by the test step.
14. err-The number of assertion errors for the test step.
15. rat-Percentage of requests that failed

When clicking on the Run button, you can see the data/statistics for all the facts mentioned above.

Now we have also done performance tests for our API’s by re-using our existing functional tests.

Advertisement

Script Assertions in SoapUI

SoapUI uses XPath to manipulate XML. But we cannot use XPath expressions on JSON.
But soapUI have capability to convert JSON to XML.
Only difference is that instead of using Response property you have to use ResponseAsXml property.
ResponseAsXml in soapUI will convert JSON to XML.
The best and easy way to assert on a JSON response is to use a Groovy Script Assertion
Now lets see how to use Script Assertion on a JSON reponse.

"Token":    {
      "UserId": "test@test.com",
      "TimeStamp": "31/12/2014",
      "SignedInAs": "test"
   },

The following is the script we can use to assert the content of the above Json response

import groovy.json.JsonSlurper
def response = messageExchange.response.responseContent
def slurper = new JsonSlurper()
def json = slurper.parseText response
assert json.Token.UserId = "test@test.com"
assert json.Token.TimeStamp == "31/12/2014"
assert json.Token.SignedInAs == "test"

Basic Assertions in SoapUI

1. Contains:
Search for the existence of a String in the Response Value. Applicable for JSON/XML reponses.

Assertion1

For Example if your response JSON is

"Success": true,
   "AUTH":    {
      "UserId": "linkeshkannavelu",
      "TimeStampUtc": "2014-04-22T07:25:05.0077957Z",
      "SignedAs": "4Rfc0/test/Vcpk="
   }

and you are looking for the string linkeshkannavelu in the response, then you can enter the string in the Assertion Window.

Assertion2

2. XPath Match:
Traverse the XML response and return the value presents in the specific XML Node and then compares it with the expected value given.
Applicable only to property in XML Format.

Assertion3

For Example, in the last post we have verified the title of the site http://linkeshkananvelu.com.
If the returned response is

<html>
	<head>
		<body>
			<title>Keep Software Testing Weird</title>
		</body>
	</head>
</html>

You can check the title of the page by using the following Xpath.

Assertion4

3. Not Contains
Search for the non existence of a String in the Response Value. Applicable for JSON/XML reponses.

Assertion5

For Example if your response JSON is

"Success": true,
   "AUTH":    {
      "UserId": "linkeshkannavelu",
      "TimeStampUtc": "2014-04-22T07:25:05.0077957Z",
      "SignedAs": "4Rfc0/test/Vcpk="
   }

and you are looking for the string TestUsername to be not present in the response, then you can enter the string in the Assertion Window.

Assertion6

If the String exists in the response, then the test will fail.

4. Valid HTTP Status Codes:
In the reponse, usually we will get a HTTP Status Code. We can assert these response codes also.

Assertion7

For example, if you are checking that your response message got the code HTTP Status 200, then you can assert the code by

Assertion8

5. Invalid HTTP Status Codes:
This is similar to Not Contains. If the HTTP Status Code that we are receiving Should not contain the list of Codes, then we can check those assertions using this.

Assertion9

For example, if you are checking that your response message should not have the code HTTP Status 401 and 404, then you can assert the response code by

Assertion10

These are the frequently used assertions in SoapUI.

SoapUI Getting Started

SoapUI is a free and open source cross-platform Functional Testing solution. With an easy-to-use graphical interface, and enterprise-class features,
SoapUI allows you to easily and rapidly create and execute automated functional, regression, compliance, and load tests.
Download SoapUI From http://sourceforge.net/projects/soapui/files/latest/download
Install this by double clicking on the downloaded file and simply following the steps. Now Open SoapUI.

My First Project in SoapUI:
Today We are going to create first project in SoapUI by sending simple HTTP Requests and verifying the Response.

1. After Opening SoapUI, Goto File and Click “New Rest Project”

SoapUI1

2. Enter the URL for the endpoint/Service that you are going to test and Click Ok. I have entered “http://linkeshkannavelu.com&#8221;
SoapUI2

3. This will create the “Rest Project 1” with the resource http://linkeshkannavelu.com
Right Click and delete all the Child Resources under http://linkeshkannavelu.com
4. Right Click the “Rest Project 1” and Select “New TestSuite”. Enter the name for the Test Suite as “MyTestSuite”

SoapUI3

5. Right Click on the “MyTestSuite” and Select “New TestCase”. Enter the name for the Test Case as “TestCase1”

SoapUI4

6. Under TestCase1, right click on “Test Steps(0)” and Select “Add Step” and Select “HTTP Test Request”.
7. Enter the Test Step Name and End Point and Give OK.

SoapUI5

8. Look at the following figure. The Method is “GET” and requested URL is http://linkeshkannavelu.com

SoapUI8

9. Now we are going to hit the Request and Verify the expected result with the actual.
To do this. we are going to insert “Assertions” here.
10. At the bottom of the Test Request screen, Click on the “Assertions” and Click on the + SYmbol to add an assertion for this test request.
11. Select the type of assertion as “Property Content” in the LHS and Click “Xpath Match”.

SoapUI9

12. Now we are going to Verify the title of the http response. Title of my site should be “Keep Software Testing Weird”

SoapUI6

13. Hit the Submit Request button at the top of the Test Request screen and see the response in response window.
As the Xpath matches with the actual response, My Test Step should Go Green and the Xpath Match Assertion should also Go Green.
It means, the Expected Result “Keep Software Testing Weird” matched with the actuals.

SoapUI7

Thats it. We Sent a request to a end point and got the required response and verified that.

Automate/Schedule the Database Back Up

Today we are going to see how to automate the backing up a database.
If we are frequently making changes in a database on a daily basis, then it is a good practice to backup the database
before making Changes. Instead of doing this manually, we can also automate the database backup task in MS-SQL Server.
It is very easy and very simple.

1. Open the SQL Server Management Studio. Connect to the database server. I have connected to localhost database server.
2. Expand the “Management” Option. Under that You can able to see an option called “Maintenance Plans”
3. Right Click on the Maintenance Plan and Select “New Maintenance Plan”

Image

4. Enter the Name for the Maintenance Plan. I have given name as “AutoBackup-AdventureWorksDB” and Give Ok.
This will navigate you to the design page of that plan.

Image

5. Enter Description as “To Automate the Database Backup on a daily basis”
6. For Scheduling this job, Click on the Calendar Like icon on the SubPlan Schedule Column.

Image

7. This will open the “Job Schedule Properties” window. Select the “Frequency Occurs” as “Daily”. Leave remaining things as it is.

Image

8. If you are backing up a database in your local machine, give the time as “12:00:00 PM”. When the job runs during that time, the machine/server should not be shut down/switched off.
9. Give Ok. Now Click on the Toolbox tab in the “Design” Page and Double Click “Backup Database Task”

Image

10. This will create the “Back Up Database Task” in the Design page.

Image

11. Double Click on the “Back Up Database Task” in the Design Page and this will open the “Back up Database Task” window.

Image

12. If you are backing up the database in local, then leave the connection as “Local Server Connection”
Or Click on the New button and select the Server. Leave Backup type as “FULL”
Select the Database that you want to backup. I have selected “Adventureworks2008R2” database.

Image

 

13. Enter the Directory for the Database backup. I have given as “D:\SQL DB\BACKUP”. Leave remaining settings as it is and give Ok.

Image

14. Click on “Save” or press “Ctrl+S”.

Thats it. Now you have successfully automated the backup of “AdventureWorks” database.
Now this job will run daily on “12:00:00 PM” and it will create a backup [.bak] file in “D:\SQL DB\BACKUP” directory.

 

Cursors in SQL Server and its Usage:

In SQL Server, we can use While loop to iterate using a Variable. But if we want to iterate through the rows of a result set, we can go for Cursors.
So Cursors is a mechanism to explicitly enumerate through the rows of a result set, rather than retrieving it as it is.
Syntax of a Cursor is,

DECLARE [YOUR CURSOR NAME] CURSOR
 FOR
  [USE YOUR SELECT STATEMENT HERE]
 OPEN [YOUR CURSOR NAME]
 FETCH NEXT FROM [CURSOR NAME]
 WHILE (@@Fetch_Status = 0)
  BEGIN
   [Do Manipulation for every Row Here]
  END
CLOSE [YOUR CURSOR NAME]
DEALLOCATE [YOUR CURSOR NAME]

Now we are going to see an example for CURSOR with better usage:
Consider this scenario. I have a 17 tables in my database with similar name %TenantSummary% like
1. AppTenantSummary
2. ClientTenantSummary
3. RowTenantSummary
.
.
.
.
.
17. DataTenantSummary

I want to insert similar set of values in all these 17 tables. For this, We can create individual Insert statements.
But I am going to simplify this by Creating a Cursor that will insert similar set of values in all these 17 tables.

Following is the code that will insert similar set of values in all the TenantSummary tables.

DECLARE @TableName varchar(200)
DECLARE @Query varchar(Max)

DECLARE TenantSummary CURSOR
FOR
 SELECT '[' + schemas.name + '].[' + tables.name + ']' FROM sys.tables
 INNER JOIN sys.schemas ON schemas.schema_id = tables.schema_id WHERE tables.Name LIKE '%TenantSummary'
 OPEN TenantSummary
 FETCH NEXT FROM TenantSummary into @TableName
 WHILE (@@Fetch_Status = 0)
  BEGIN
   SET @Query = 'INSERT INTO ' + @TableName + ' SELECT ID, Name, 1 FROM Master WHERE ID LIKE ''%DTS%'' GROUP BY ID '
   PRINT @Query
   EXEC(@Query)
   FETCH NEXT FROM TenantSummary into @TableName
  END
CLOSE TenantSummary
DEALLOCATE TenantSummary

But always there is a saying that Cursors will cause severe Performance issues in SQL. So limit the usage
of Cursors. But for QA, if we are not bothered about some extra seconds of execution for SQL Queries, We can do magics using Cursors in SQL.

Restore/Create Database using a database backup in SQL Query

Restore/Create SQL Server Database from a DB BackUp using SQL Query:
1. Connect to the database server in sql server management studio. For example localhost. Right Click on the “Databases” and Choose “Restore Database” option. This will open the “Restore Database” window
2. To extract the query required to restore/Create the database, Click the Script button on the top and select “Script Action to New Query Window”

Image

 

3. This will give you the query to restore a database using an existing backup.

Below mentioned is the Query to create new database “TEST” from the database backup in the “D:\SQL DB\BACKUP\AdventureWorks2008R2.bak” path in device.

RESTORE DATABASE 
[TEST] 
FROM DISK = N'D:\SQL DB\BACKUP\AdventureWorks2008R2.bak' 
WITH FILE = 1, 
MOVE N'AdventureWorks2008R2_Data' 
TO N'D:\SQL DB\DATA\TEST.mdf', 
MOVE N'AdventureWorks2008R2_Log' 
TO N'D:\SQL DB\DATA\TEST_1.LDF', 
NOUNLOAD, 
STATS = 10
GO

You may also change the path of the .mdf and .ldf files if you want. By changing the .mdf and .ldf files, will create the database in a different location that you have specified.

Restore SQL Server DataBase from a Backup

Now we are going to see how to restore a database backup in MS-Sql Server using management studio.

1. Connect to the database server in sql server management studio. For example localhost. Right Click on the “Databases” and Choose “Restore Database” option

Image

2. This will Open the restore database window. Enter the name of the database that you want to create in the “To Database” Field. Here I have entered my database name as “TEST”

Image

3. Choose the option “From Device” and click on the browse button.Now locate the backup file “.bak” file and give Ok.

Image

4. This will navigate you to “Specify Backup” directory. where your backup file will be specified. You can remove that file here bu remove button and add the new database backup file using “add” button.

Image

5. Give “OK” in the “Specify Backup” window. This will navigate you to the “Restore Database” screen and select the “Restore” check box in that window.

 Image

 

6. Click on the Options page and there you may specify the path for your database to be created. 

Image

7. Again Goto “General” Page and Click the Ok button.

Image

8. You can see the progress the the Left side bottom in % and once the restore is complete you should get a prompt as 

Image

 

Thats it. Now the new database “TEST” has been created and you can play with it.