ADS

Wednesday, December 25, 2013

Page object model for automation using Protractor

Creating automating test suites using Page Object Design pattern is very successful in languages like Java and C#, but Automation Engineers faces many problems while creating page object when creating automation test suites using JavaScript. Astrolabe is a famous extension for Protractor, but I did not find it of much usefulness to implement that, instead you can create your own page objects without the need of any extension.

You can use the following procedure to implement the page object:

Page.js
var pageObject = {
// Locator of a link
link : function(){
   return ptor.findElement(protractor.By.id(<locator>));
},

// function to click on element
clickOnLink : function(){
   pageObject.locators().click();
}
};
exports.pageObj = pageObject;

Test.js
var pageObject = require(<path to Page1.js>);

describe('Click on an element', function(){
  it('Verify page should open', function(){

     // Click on Link by calling the function defined in the Page.js
     pageObj.clickOnLink();
     // Verify actual value to be expected value
     expect(actual value).toBe(expected value);
  });
});

In this architecture, we have created Page.js in which we have found a locator of a Link and a function to click on that Link. If you can recall, it is the same way we used to do in Java, C# or in any other similar language.
Now we want to click on the link and then expect something over there, so in the Test.js, we have called the function to click on the link and then we verify our expected value. I am writing my automation scripts using this architecture and till now I am very successful in this approach.

Sunday, December 22, 2013

Handling automation testing of Repeaters using Protractor

AngularJS is becoming popular day by day and thus are the demand of creating automation test suites for AngularJS applications. Protractor is a popular framework for testing AngularJS applications which runs on WebDriver and uses most of its classes.

Protractor uses Jasmine structure to create test suites.
That being the overview, In this post, I will cover the handling the automation test scenarios of the Repeaters of AngularJS.
Repeaters are just like tables in any normal applications.
In a repeater, there are following things which we might need to verify:
  • Fetching the text from a particular row
  • Click on an element in a row
  • Checking the check-box in a particular row
Now for any of the above cases, there will be a particular row in which you will need to perform some of the tasks and lets say you need to find the particular row using the text present in it, then you can handle mostly everything using the following code:

Repeater : 
  1. <ul class="phones">
  2. <li ng-repeat="phone in phones | filter:query">
  3. {{phone.name}}
  4. <p> Text </p>
  5. </li>
  6. </ul>

TableLocator : This will be the locator of the table which holds all the element in the repeater. So this will return an Array.
ElementLocator : This will be the locator of the element on which you want to perform the some action.
TextLocator : This will be the locator which will hold the text on basis of which you need to perform the task.
Text : This is the text which we need to find to perform some operations.

So based on that lets see the code
Not just repeaters, all the table like structure can be handled in this way.

TableLocator()
.then(function(ElementList){
ElementList.map(function(element, index){
element.findElement(TextLocator).getText().then(function(retrievedText){
if(text == retrievedText)
{
/* Perform actions on the element like clicking on element, checking the checkbox, etc. */
element.findElement(ElementLocator).click();
}
})
})
})

Friday, August 2, 2013

Test Step results from Groovy in SOAPUI

After having lot of Groovy written for just retrieving the data from one place and copying it to another place, I thought to investigate some new commands. Although these are not new in the Javadoc, it was just new for me only ;).

So I just went ahead on how to extract the status of the test steps that has been executed.
One point to note over here is that, add the Groovy script to get the status of test steps at least in a test case or you can add the script to get the status of test step execution in the teardown script of the test case or test suite.

So here is the simple Groovy that will fetch you the status of your test steps

for (r in testRunner.results)

{
       log.info "Test Step " + <test_step_name> + "has been executed with status as " + r.status
}

If test step has been passed then result will be OK, on the other hand if test step has been failed, then status will be Failed
In the end it turns out to be very simple command which will be really very helpful for everyone.

Sunday, May 12, 2013

Assertion in SOAPUI using Groovy

If we need to add some basic assertions at runtime using Groovy, then following can be useful for you
def project = testRunner.testCase.testSuite.project
def testst = project.getTestSuiteAt(0).getTestCaseAt(0).getTestStepAt(0)
Adding CONTAINS assertion
def assertioncontains = testst.addAssertion("Contains")
assertioncontains.name = "contains assert"
assertioncontains.setToken("54")
assertioncontains.setIgnoreCase(true)
assertioncontains.setUseRegEx(true)
Adding NOT Contains assertion
def assertionnotcontains = testst.addAssertion("Not Contains")
assertionnotcontains.setToken("50")
assertionnotcontains.setIgnoreCase(true)
Adding XPATH Match assertion
def assertionxpath = testst.addAssertion("XPath Match")
assertionxpath.name = "xpath assert"
assertionxpath.setPath("path")
assertionxpath.setExpectedContent("expected content")
assertionxpath.setAllowWildcards(true)
Adding XQuery Match assertion
def assertionxquery = testst.addAssertion("XQuery Match")
assertionxquery.setPath("path")
assertionxquery.setExpectedContent("expected content")
assertionxquery.setAllowWildcards(true)
Similarly you can add more assertion based on your requirements. Now for the different properties that you need to set, you can refer the API doc provided by the SOAPUI http://www.soapui.org/apidocs/index.html

Sunday, April 28, 2013

Just a simple Groovy test flow - Data Driven

My previous post Just a simple Groovy test flow - Update, Execute and Assert states a simple flow of a simple test case, but in that scenario everything was hard-coded in the code, means we have provided value in code only but what if we have to execute the same test for different set of values, then we can easily use data driven technique i.e., we can use excel file - store our test data over there and then execute the groovy code.

Yes, I know this might have already made you jumped out of your seat, so without delaying further let me also jump straight to the point.

For accessing data driven using Excel file we will be using JExcelApi. You need to download the jxl.jar file and place it in you /SOAPUI-Installation folder/lib folder and then make sure you also need to restart your SOAPUI.

Modifying the code we have created for the previous post, our new code will look like this.
import jxl.*

// Step 1: Update the Request in the SOAP webservice

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def request = groovyUtils.getXmlHolder("<TestStepName>#Request")
request.namespaces["ns1"] = "http://www.webserviceX.NET/"

// Opening the Excel file

Workbook testData = Workbook.getWorkbook(new File("D:\\soapui practise\\test.xls"))
// Opening the first sheet
Sheet sheet1 = testData.getSheet(0)
// Retrieving the data

Cell a1 = sheet1.getCell(0,0) // Retrieve the fromCurrency
Cell a2 = sheet1.getCell(1,0) // Retrieve the toCurrency

def fromCurrency = a1.getContents()
def toCurrency = a2.getContents()

request.setNodeValue("//ns1:ConversionRate/ns1:FromCurrency",fromCurrency)
request.updateProperty()
request.setNodeValue("//ns1:ConversionRate/ns1:ToCurrency",toCurrency)
request.updateProperty()

// Step 2: Execute the SOAP webservice.
testRunner.runTestStepByName("<TestStepName>")

// Retrieve Response
def response = groovyUtils.getXmlHolder("<TestStepName>#Response")
response.namespaces["ns2"] = "http://www.w3.org/2001/XMLSchema"

// Step 3: Add xPath assertion

def testst = testRunner.testCase.getTestStepByName("<TestStepName>")

// Following pseudo code will just get if any assertion of name xPath exists or not, if no assertion of xPath exists then it will procede to create a new xPath assertion else it will remove pre-existing before and then will create a new one.

def asserting = testst.getAssertionByName("XPath Match")
if (asserting instanceof com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XPathContainsAssertion)
{
testst.removeAssertion(asserting)
}

def assertion = testst.addAssertion("XPath Match")
assertion.path = "//ConversionRateResponse/ConversionRateResult"
assertion.expectedContent = <type in here whatever the content you want to match with>

There is one point which you need to take care of creating test data in Excel file, which is, JExcelAPI only works till Excel 2003 and not there after, so you need to create Excel file in Excel 2003 or below.

Saturday, April 27, 2013

Just a simple Groovy test flow - Update, Execute and Assert

So after long study of SOAPUI, I thought of giving Groovy script a try with a real webservice test flow.

In this script we will do the following with Groovy:

  • Update the Request in the SOAP webservice 
  • Execute the SOAP webservice
  • Add xPath assertion
So let's get started and have a look at the sample webservice we will be using the following:

Request:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://www.webserviceX.NET/">
<soap:Header/>
<soap:Body>
<web:ConversionRate>
<web:FromCurrency>SEK</web:FromCurrency>
<web:ToCurrency>DKK</web:ToCurrency>
</web:ConversionRate>
</soap:Body>
</soap:Envelope>

Response:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ConversionRateResponse xmlns="http://www.webserviceX.NET/">
<ConversionRateResult>5.7187</ConversionRateResult>
</ConversionRateResponse>
</soap:Body>
</soap:Envelope>

These two are our Request and Response. Now lets get started with our Groovy script what we want to achieve
// Step 1: Update the Request in the SOAP webservice

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )

def request = groovyUtils.getXmlHolder("<TestStepName>#Request")
request.namespaces["ns1"] = "http://www.webserviceX.NET/"

def fromCurrency = "SEK"
def toCurrency = "DKK"

request.setNodeValue("//ns1:ConversionRate/ns1:FromCurrency",fromCurrency)
request.updateProperty()
request.setNodeValue("//ns1:ConversionRate/ns1:ToCurrency",toCurrency)
request.updateProperty()

// Step 2: Execute the SOAP webservice.
testRunner.runTestStepByName("<TestStepName>")

// Retrieve Response
def response = groovyUtils.getXmlHolder("<TestStepName>#Response")
response.namespaces["ns2"] = "http://www.w3.org/2001/XMLSchema"

// Step 3: Add xPath assertion

def testst = testRunner.testCase.getTestStepByName("<TestStepName>")

// Following pseudo code will just get if any assertion of name xPath exists or not, if no assertion of xPath exists then it will procede to create a new xPath assertion else it will remove pre-existing before and then will create a nw one.
// Also I found very useful line in the SOAPUI forum
// Alls types of assertions:
// CrossSiteScriptAssertion, GroovyScriptAssertion, HttpDownloadAllResourcesAssertion, InvalidHttpStatusCodesAssertion, JdbcStatusAssertion, JdbcTimeoutAssertion, JMSStatusAssertion, JMSTimeoutAssertion, NotSoapFaultAssertion, ResponseSLAAssertion, SchemaComplianceAssertion, SensitiveInfoExposureAssertion, SimpleContainsAssertion, SimpleNotContainsAssertion, SoapFaultAssertion, SoapRequestAssertion, SoapResponseAssertion, ValidHttpStatusCodesAssertion, WSARequestAssertion, WSAResponseAssertion, WsdlMessageAssertion, WSSStatusAssertion, XPathContainsAssertion, XQueryContainsAssertion

def asserting = testst.getAssertionByName("XPath Match")
if (asserting instanceof com.eviware.soapui.impl.wsdl.teststeps.assertions.basic.XPathContainsAssertion)
{
testst.removeAssertion(asserting)
}

def assertion = testst.addAssertion("XPath Match")
assertion.path = "declare namespace ns1='http://www.webserviceX.NET/';\n //ns1:ConversionRateResponse/ns1:ConversionRateResult"
assertion.expectedContent = <type in here whatever the content you want to match with>

Hope it will be useful information for newbie.

Tuesday, April 2, 2013

Handling SOAP Request and Response from Groovy script

In this post, we will be learning how we can access SOAP Requests and Response using Groovy Scripts.

Let us take the following example
Request:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:web="http://www.webserviceX.NET/">
<soap:Header/>
<soap:Body>
<web:ConversionRate>
<web:FromCurrency>SEK</web:FromCurrency>
<web:ToCurrency>DKK</web:ToCurrency>
</web:ConversionRate>
</soap:Body>
</soap:Envelope>

Response:
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ConversionRateResponse xmlns="http://www.webserviceX.NET/">
<ConversionRateResult>5.7187</ConversionRateResult>
</ConversionRateResponse>
</soap:Body>
</soap:Envelope>

Now we want to access the content of above SOAP Request and Response, then we can use the following code:
import com.eviware.soapui.support.XmlHolder

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context )
def step = context.testCase.getTestStepAt(0)
if( step instanceof com.eviware.soapui.model.testsuite.SamplerTestStep && step.testRequest.response != null ) // this step indicate that retrieved TestStep is a SOAP request and response is present for it
{
def response = groovyUtils.getXmlHolder( step.name +"#Response")
def request = groovyUtils.getXmlHolder(step.name + "#Request")
response.namespaces["ns"] = "http://www.w3.org/2001/XMLSchema"
log.info (request.getNodeValue("//ConversionRateResponse/ConversionRateResult")) 
log.info response.isEmpty()
log.info response.getNamespaces()

For more functions which you can do with SOAP Request and Response can be found at http://www.soapui.org/apidocs/index.html and go to package com.eviware.soapui.support and XmlHolder class in it.

Monday, April 1, 2013

Basic Groovy Scripts commands for SOAPUI

Using Groovy scripts in SOAPUI can help you do lots of stuff but beginning with this feature can be at times difficult for new users. Following are the commands that you can find really useful for starting with Groovy scripts in SOAPUI.

Go to the project level using the command
def project = testRunner.testCase.testSuite.project

Access the Test Suite using the following commands
def testsuite = project.getTestSuiteAt(index)
def testsuite = project.getTestSuiteByName("TestSuite Name")

Access the Test Case using the following commands
def testcase = testsuite.getTestCaseAt(index)
def testcase = testsuite.getTestCaseByName("TestCase Name")

Access the Test Steps using the following commands
def teststep = testcase.getTestStepAt(index)
def teststep = testcase.getTestStepByName("TestStep Name")

Run a Test Step using the following command
teststep.run(testRunner,context)

set property value at different level of hierarchy

set property at Project level
project.setPropertyValue("property_name","value") 


set property at TestSuite level
testsuite.setPropertyValue("property_name","value") 

set property at TestCase level
testcase.setPropertyValue("property_name","value") 

get property value from different level of hierarchy

get property from Project level
project.getPropertyValue("property_name")


get property from TestSuite level
testsuite.getPropertyValue("property_name")

get property from TestCase level
testcase.getPropertyValue("property_name") 


get the count of number of TestSuites, TestCases and TestSteps
project.testSuiteCount // get TestSuite count in the project
testsuite.testCaseCount // get TestCase count in the TestSuite
testcase.testStepCount // get TestSteps count in the TestCase
Any other basic commands which you feel should be added in the list please provide in the comment section.