How to display Test Cases details in Jenkins Console Output? - java

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));
}

Related

How to add my comment in Test Result in the TestRail using testrail-api-java-client

I am migrating to maven and Now it has dependency
<!-- https://mvnrepository.com/artifact/com.codepine.api/testrail-api-java-client -->
<dependency>
<groupId>com.codepine.api</groupId>
<artifactId>testrail-api-java-client</artifactId>
<version>2.0.1</version>
</dependency>
I have written the below code to update the result..
TestRail testrail= TestRail.builder("https://mytestrail.domain.com", "username", "password").applicationName("appname").build();
Tests tests = testrail.tests();
java.util.List<com.codepine.api.testrail.model.Test> lst = tests.list(43662).execute();
System.out.println(lst.get(0));
System.out.println(lst.size());
List<ResultField> customResultFields = testrail.resultFields().list().execute();
//HashMap data = new HashMap();
//data.put("comment", "Test Purpose for all");
//customResultFields= (java.util.List<ResultField>) data;
int status=5;
testrail.results().addForCase(43662, 30056283, new Result().setStatusId(status), customResultFields).execute();
I have a list of step details which is extracted from ExtentReport. So basically how to update my own custom message instead of just "This test was marked as 'Failed' or 'Passed'"
By seeing this.. https://www.gurock.com/testrail/docs/user-guide/howto/fields
May be we need to create something on the Field class. But anybody has idea on that would be good to guide that complete that. As I am using automation results.. i dont want each step result.. Just adding as comment entire log and make it pass/fail.
Result has few more options to add like setComment along with Status..
testrail.results().addForCase(43662, 30056283, new Result().setComment("Test Purpose Print as Pass").setStatusId(status), customResultFields).execute();
It will set the comment whatever you are giving and change the status..
You can define a Mapper named Data and put the necessary information in it. You can then add this data as a parameter to the http request.
public static void sendScenarioResults(boolean failed, int runId, int caseId, String errorMessage) {
try {
Map data = new HashMap();
data.put("status_id", failed ? 5 : 1);
data.put("comment", errorMessage);
client.sendPost("add_result_for_case/" + runID + "/" + caseId,
data);
} catch (IOException e) {
e.printStackTrace();
} catch (APIException e) {
e.printStackTrace();
}
}
}
In addition, you can add a lot of information to the data, you can use Testrail Api docs for this.
Testrail Api docs
Based on your testing framework (JUnit of TestNG), try to use one of these libs:
TestRail-JUnit
TestRail-TestNG
Both of them have Medium articles on how to integrate it just in a few steps (see README.md there)

Transforming if-else into switch case throws error [Java]

I tried to convert my if-else statements into a switch case but I had the following problem.
Old code:
if (properties.get("database").toString().equalsIgnoreCase("SQLSERVER")) {
manager = new CManagingSQLServer();
} else if (properties.get("database").toString().equalsIgnoreCase("ORACLE")){
manager = new CManagingOracle();
} else if (properties.get("database").toString().equalsIgnoreCase("MYSQL")){
manager = new CManagingMySQL();
} else {
System.out.println("Not supported DB: " + properties.get("database").toString() + "\n");
System.out.println("Supported DB:");
System.out.println("- ORACLE");
System.out.println("- SQLSERVER");
System.out.println("- MYSQL");
System.exit(0);
}
New code:
String database = properties.get("database").toString();
switch (database) {
case database.equalsIgnoreCase("SQLSERVER"):
manager = new CManagingSQLServer();
break;
case database.equalsIgnoreCase("ORACLE"):
manager = new CManagingOracle();
break;
case database.equalsIgnoreCase("MYSQL"):
manager = new CManagingMySQL();
break;
default:
System.out.println(database + "is not a supported database.");
System.exit(0);
break;
}
First, the String database threw an error that I have to change setting/property (actually don't know) into version 1.7?! After doing so, my cases are throwing now errors. They say: Type mismatch cannot convert from boolean to String.
I read other SO-thread and they said I have to try (String)something or something.ToString(). But both cases didn't work and I don't understand what changed with the above mentioned change to version 1.7.
And how can I make my cases work again?
Change database variable to
String database = properties.get("database").toString().toUpperCase();
And switch case to
case "SQLSERVER":
Currently, you are getting error because database.equalsIgnoreCase("SQLSERVER") returns boolean but you are switching on database which is a String.
Also, you need to use minimum of Java 7 because Java versions before that don't support switch case on String.
The problem you are facing is that in switch you pass a String typed database.
In case of section you want to work with boolean expression database.equalsIgnoreCase(...).
The easiest way to deal with that is to change the line:
String database = properties.get("database").toString();
to:
String database = properties.get("database").toString().toUpperCase();
and in case section use simple approach (as you have already upper cased database variable):
case "SQLSERVER"
instead of
case database.equalsIgnoreCase("SQLSERVER")
INFORMATION:
Switch expressions that work with strings are available from JDK 7.
you are missing the whole concept of switch case , you don't have to put equal condtion in your switch case.
just put like this it will work fine
String database = properties.get("database").toString().toUpperCase();
switch (database) {
case "SQLSERVER":
manager = new CManagingSQLServer();
break;
case "ORACLE":
manager = new CManagingOracle();
break;
case "MYSQL":
manager = new CManagingMySQL();
break;
default:
System.out.println(database + "is not a supported database.");
System.exit(0);
break;
}
Use the string value in case statements.
Case "SQLSERVER":

How do I Check search keyword in the URL

I am a beginner in Selenium. I wanted to know how I can verify the URL against the keyword I entered in the search bar.
The search page url is https://catalog-mytest.com/search?
When I entered redcar in the search bar and hit enter, the url becomes https://catalog-mytest.com/search?keywords=redcar
Could you guide me on how to write a piece of code that would verify the URL with the keywords? thank you.
There can be two approaches-
Approach-1:
String url = Driver.getCurrentUrl();
boolean passed = url.contains("your keyword here");
if (passed) {
System.out.println("Test Passed");
} else {
System.out.println("Test Failed");
Assert.fail("This message will be printed in stacktrace if the assertion fails.");
}
This is the simplest way of doing this.
Approach-2:
String keyword = Driver.getCurrentUrl().split("?")[1].split("=")[1];
boolean passed = keyword.equals("your keyword here");
if (passed) {
System.out.println("Test Passed");
} else {
System.out.println("Test Failed");
Assert.fail("This message will be printed in stacktrace if the assertion fails.");
}
This approach can be error prone, if the URL does not always contain
?, = in it then the test can fail with
ArrayIndexOutOfBoundsException

SoapUI Loop through TestCases and log test case custom property

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")

Unit testing is not working with XSLT tansformer

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.

Categories

Resources