ADS
Showing posts with label test. Show all posts
Showing posts with label test. Show all posts

Wednesday, July 9, 2014

Logger in Protractor E2E testing

Log files are very important while creating automation test suites. It helps you by providing each and exact information about your steps.
In coding language like JAVA you can use Log4j, similar to that, you can use Log4js to generate log files in Javascript and in your Protractor tests.

First you need to install Log4js which can be done using the following command
npm install -g log4js

Now you just need to add following command to to use Log4js API:
var log4js = require('log4js');
var logger = log4js.getLogger();

You need to configure you Log4js, which can be done like following:
{
  "appenders": [
    {
      "type": "file",
      "filename": "relative/path/to/log_file.log",
      "maxLogSize": 20480,
      "backups": 3,
      "category": "relative-logger"
    }
  ]
}

You can use the following to make entries in the log files:
logger.trace('Entering cheese testing');
logger.debug('Got cheese.');
logger.info('Cheese is Gouda.');
logger.warn('Cheese is quite smelly.');
logger.error('Cheese is too ripe!');
logger.fatal('Cheese was breeding ground for listeria.');

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.

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.