I have a groovy script that loops through each test step, in each test case, in each test suite in the project. Each test case in the project has two custom properties assigned it is, Test_Case_Response_Time and Test_Case_Response_Size. I am trying to get it so that when it loops through each test case it log.info those two custom property for each test case.
Groovy Script:
//Loop thru the suites, followed by cases in each suite
suites.each
{ suite ->
//For each test SUITE perform the following action
//------------------------------------------------
def tSuite = project.getTestSuiteByName(suite)
tSuite.testCaseList.each
{ kase ->
//For each test CASE perform the following action
//-----------------------------------------------
kase.testStepList.each
{
//For each test step perform the following action
//-----------------------------------------------
if(it.metaClass.hasProperty(it,'assertionStatus')){
def assertions = it.getAssertionList()
assertions.each
{ assertion ->
if(it.assertionStatus == AssertionStatus.VALID)
{
PassedTestCase += 1
}
else if(it.assertionStatus == AssertionStatus.FAILED)
{
FailedTestCase += 1
}
}
}
//-----------------------------------------------
}
log.info testRunner.testCase["Test_Case_00: Setup"].getPropertyValue("Test_Case_Response_Time")
log.info testRunner.testCase.testSuite.getTestCaseByName("Test_Case_00: Setup").getPropertyValue("Test_Case_Response_Time")
//-----------------------------------------------
}
//-----------------------------------------------
}
I have tried the following with no success:
log.info testRunner.testCase[kase.name].getPropertyValue("Test_Case_Response_Time")
log.info testRunner.testCase.testSuite.getTestCaseByName(kase.name).getPropertyValue("Test_Case_Response_Time")
The first line give me the following
groovy.lang.MissingPropertyException: No such property: Test_Case_00:
Setup for class: com.eviware.soapui.impl.wsdl.testcase.WsdlTestCase
error at line: 37
and the second line gives me the following error:
java.lang.NullPointerException: Cannot invoke method
getPropertyValue() on null object error at line:37
The below statement is not correct as testCase[kase.name] gives you property of testcase and not testcase itself
System is trying to search a property with the name"Test_Case_00: Setup" and hence giving error "No such property: Test_Case_00: Setup"
log.info testRunner.testCase[kase.name].getPropertyValue("Test_Case_Response_Time")
I was able to run the below code
log.info testRunner.testCase.testSuite.getTestCaseByName(kase.name).getPropertyValue("Test_Case_Response_Time")
In your actual code you have used the below line instead of kase.name
getTestCaseByName("**Test_Case_00: Setup**")
Looks like, testcase name is wrong, please copy the exact name and paste and it will work.
Below code worked for me. Its your code only.
def tSuite = testRunner.testCase.testSuite.project.getTestSuiteByName("TestSuite") // modified to run
tSuite.testCaseList.each
{ kase ->
//For each test CASE perform the following action
//-----------------------------------------------
kase.testStepList.each
{
//For each test step perform the following action
//-----------------------------------------------
}
//-----------------------------------------------
// log.info kase.name
// log.info testRunner.testCase[kase.name].getPropertyValue("Test_Case_Response_Time") <-- wrong syntax
log.info testRunner.testCase.testSuite.getTestCaseByName(kase.name).getPropertyValue("Test_Case_Response_Time")
}
I believe I was looking in the wrong Test Suite. Using the following i was able to get it to find the correct properties:
testRunner.testCase.testSuite.project.getTestSuiteByName(suite).getTestCaseByName(kase.name).getPropertyValue("Test_Case_Response_Time")
Related
I am trying to test the deadLetterChanel in apache camel,
here's the production code snippet (part of my RouterBuilder):
from(dateQueue())
.routeId(ROUTE_DATE)
.to(log(myConsumer))
.transform().body(String.class, it -> myConsumer.consume(LocalDate.parse(it)))
.split(body())
.to(log(myConsumer))
.marshal().json()
.to(processorExchange());
errorHandler(deadLetterChannel(dlqDirect()));
// #formatter:off
from(dlqDirect())
.to("log:dlq")
.choice()
.when(it -> ROUTE_DATE.equals(it.getFromRouteId())).to(dateDLExchange())
// other when expressions here
.end();
// #formatter:on
And here's the test code:
#Test
void testErrorCaseOfDateRoute_ShouldGoToDateDlExchange() throws Exception {
// given:
MockEndpoint dateDLExchangeMock = camelContext.getEndpoint("mock:test", MockEndpoint.class);
AdviceWith.adviceWith(camelContext, ROUTE_DATE,
in -> in.interceptSendToEndpoint(dateDLExchange()).to(dateDLExchangeMock)
);
dateDLExchangeMock.expectedMessageCount(1);
dateDLExchangeMock.expectedBodiesReceived("invalid date");
// when:
producerTemplate.sendBody(dateExchange(), "invalid date");
// then:
dateDLExchangeMock.assertIsSatisfied();
}
The assertion fails!
However, if I changed the mock to intercept dlqDirect() (direct endpoint) instead of dateDLExchange() (rabbitmq endpoint), it works fine, but I want to include the choice() function in my tests as well.
I am doing mini-integration tests using RabbitMQ in TestContainer, so I really think to listen to the queue in rabbitmq instance and assert the message there instead of doing the test using Camel! (Can camel help here?)
It worth noting that when I test this case manullay, I got my invalid message into the rabbitmq date_dl_queue which is bound to date_dl_exchange. ( I mean, it works)
I solved it by using a route-scoped errorHandler, instead of using a generic error handler for all routes and then using when-expressions.
So, the code changed to look like this:
from(dateQueue())
.routeId(ROUTE_DATE)
.errorHandler(buildErrorHandler(dateDLExchange())) // <<< Notice this
.to(log(myConsumer))
.transform().body(String.class, it -> myConsumer.consume(LocalDate.parse(it)))
.split(body())
.to(log(myConsumer))
.marshal().json()
.to(processorExchange());
private DefaultErrorHandlerBuilder buildErrorHandler(String deadLetterUri) {
return deadLetterChannel(deadLetterUri)
.logExhausted(true)
.logHandled(true)
.....
.;
}
I am using #NonCPS in my Jenkinsfile function Because I need to get attributes on XML use XmlSlurper and i'm still getting java.io.NotSerializableException error even with the #NonCPS annotation.
Follow is my code
#Field prBranchCoverage
#NonCPS
def xmlCovergeParse(CoverageXml) {
println "start coverage"
def prParser = new XmlSlurper()
prParser.setFeature("http://apache.org/xml/features/disallow-doctype-decl", false)
prParser.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
def prCovergeDoc = prParser.parseText(CoverageXml)
def branchCoverage = prCovergeDoc['#line-rate']
return branchCoverages
}
stage('Pr coverage ') {
sh "mvn -V -B -U cobertura:cobertura -PcodehausCoverage -Dmaven.test.failure.ignore=true -DskipWeaving=true test"
publishTestResults cobertura: [archive: true,pattern: "**/target/site/cobertura/coverage.xml"]
def CoverageXml = readFile file: './target/site/cobertura/coverage.xml'
def prBranchCoverage = xmlCovergeParse(CoverageXml)
echo "start coverage"
println prBranchCoverage
}
I get error is:
Error: java.io.NotSerializableException: groovy.util.slurpersupport.Attributes
an exception which occurred:
in field com.cloudbees.groovy.cps.impl.BlockScopeEnv.locals
in object com.cloudbees.groovy.cps.impl.BlockScopeEnv#72e69aed
in field com.cloudbees.groovy.cps.impl.CallEnv.caller
in object com.cloudbees.groovy.cps.impl.FunctionCallEnv#1a22d024
in field com.cloudbees.groovy.cps.Continuable.e
Could anyone help this?
your method xmlCovergeParse(CoverageXml) returns non serializable object
jenkins requires all variables in the pipeline to be Serializable because the next step could be executed on other node then the previous one. so to transfer variables between the nodes they will be serialized.
just add return branchCoverages.toString() in your method xmlCovergeParse
I want to display Excel(XLSX) Test cases id,description and status while running selenium testng execution in the Jenkins Console output,below is the format.
TC_ID|Test Cases Description|Status
TC01|Verify Login Home|PASS
TC02|Verify Login Screen|FAIL
In case any one did this type of model please help me to achieve this in Jenkins Output Console.
If you are using TestNG in your project, anything you log using Reporter class will be also printed in Jenkins console output :
Reporter.log("This will be displayed in the TestNG report AND in Jenkins console");
Note : You need to add the following import :
import org.testng.Reporter;
Refer this link for steps to create a TestNG project in Jenkins. Alternatively, you can also as well create a Maven project where your pom file will execute your TestNG tests.
Now, if you want to get the data from an excel file, you can use Apache POI. Refer this link for an example to read data from excel.
If you want PASS, FAIL status to be printed beside your test case name, you can use ITestResult. Suppose you are printing the data in your #AfterMethod method, you would do something like -
#AfterMethod
public void afterSuite(ITestResult result)
{
if(result.getStatus() == ITestResult.SUCCESS)
{
//pass
}
else if(result.getStatus() == ITestResult.FAILURE)
{
//fail
}
else if(result.getStatus() == ITestResult.SKIP )
{
//skip
}
}
Now, you can store the each respective test method result along with the name you read from the excel file, store it in a Map and then print it one by one in your #AfterSuite method.
Maybe will be better to implement listener but you can use code below:
#AfterMethod(alwaysRun = true)
public void beforeMethod(Method test, ITestResult testResult) {
//get name of test. if testName annotation is empty get method name.
String name = firstNonEmpty(
test.getDeclaredAnnotation(org.testng.annotations.Test.class).testName(),
test.getName()).get();
String description = test.getDeclaredAnnotation(org.testng.annotations.Test.class).description();
//get test result
String result = "UNKNOWN";
switch (testResult.getStatus()) {
case 1:
result = "SUCCESS";
break;
case 2:
result = "FAILURE";
break;
case 3:
result = "SKIP";
break;
case 4:
result = "SUCCESS_PERCENTAGE_FAILURE";
break;
case 16:
result = "STARTED";
break;
}
//report as you wish..
System.out.println(String.format("%s|%s|%s", name, description, result));
}
if(!reqAdSize.equalsIgnoreCase(hm.get("ad_size")))
{
Failure = true;
FailureMessage = "Ad Sizes Doesn't Match";
}
if(!reqCur.equalsIgnoreCase(resCur))
{
Failure = true;
FailureMessage = "Request & Responce Currency are NOT same";
}
if(!reqID.equalsIgnoreCase(resID))
{
Failure = true;
FailureMessage = "Request & Responce Id is NOT same";
}
In my jeMter here is my BeanShell Assertion Code all if conditions are satisfying.
In result it will showing the last FailureMessage. I need to stop the execution of code if first condition is true and it will not execute further.
I tried to use the System.exit(0);andexit();. But jMeter GUI is automatically closing.
What is the method to stop the execution at current line in BeanShell.
Add return keyword wherever you want to stop the execution of BeanShell code.
You have SampleResult pre-defined variable in the Beanshell Assertion, it is a shorthand for org.apache.jmeter.samplers.SampleResult class. Take a look into following methods:
SampleResult.setStopTest()
SampleResult.setStopTestNow()
Something like:
if (!reqAdSize.equalsIgnoreCase(hm.get("ad_size"))) {
Failure = true;
FailureMessage = "Ad Sizes Doesn't Match";
SampleResult.setStopTest(true);
}
Guide on Beanshell scripting in JMeter context: How to Use BeanShell: JMeter's Favorite Built-in Component
I have written unit test cases to test the message processors individually in my mule flow.
But the unit test fails with error
org.mule.api.transformer.TransformerMessagingException: Property "xsl-file or xsl-text" not set.
One or more of them must be set (org.mule.api.lifecycle.InitialisationException).
Message payload is of type: String
(org.mule.api.transformer.TransformerMessagingException). Message payload is of type: String
One of the transformers is an XSLT as shown below.
<mule-xml:xslt-transformer maxIdleTransformers="2" maxActiveTransformers="5" xsl-file="C:\EWS\myproj\src\main\resources\xslt\DataAdder.xsl"
name="AdderXSLT" >
</mule-xml:xslt-transformer>
The unit test method looks as below.
MessageProcessor subFlow = muleContext.getRegistry().lookupObject("AdderXSLT");
MuleEvent result = subFlow.process(getTestEvent(getFileAsString("SamplePayloads/input.xml")));
System.out.println("The output from Event is " + result.getMessageAsString());
System.out.println("The converted XML is " + result.getMessage().getPayloadAsString());
assertNotNull(result);
assertNull(result.getMessage().getExceptionPayload());
assertFalse(result.getMessage().getPayload() instanceof NullPayload);
Please help me understand what's going wroong here.
I came across something similar before where you need to initialise the transformer explicitly when you're not executing it within the context of a flow. To test xslt transformers I have used similar to the following is the past:
XsltTransformer xslt = FunctionalTestCase.muleContext.getRegistry()
.lookupObject("SuccessResponseTransformer");
xslt.setReturnDataType(DataType.STRING_DATA_TYPE);
xslt.initialise();
String result = (String) xslt.transform(srcXML);
You could try something like this or try casting to an XsltTransformer to initialise.
I believe this is because when you execute the MP as part of a flow it is part of a MessageProcessorChain that will initialise each MP where appropriate. If you take a look at the following code from AbstractMessageProcessorChain - http://grepcode.com/file/repo1.maven.org/maven2/org.mule/mule-core/3.3.1/org/mule/processor/chain/AbstractMessageProcessorChain.java#AbstractMessageProcessorChain.initialise%28%29 :
public void initialise() throws InitialisationException
{
for (MessageProcessor processor : processors)
{
// MULE-5002 TODO review MP Lifecycle
if (processor instanceof Initialisable /* && !(processor instanceof Transformer) */)
{
((Initialisable) processor).initialise();
}
}
}
Note that (!instanceof Transformer) is commented out. So it will initialise the XsltTransformer for you.
Where as directly referencing the MessageProcessor will not.