How to use Listner in Appium to get pass fail result in excel

Hi any one help me out how to get pass and fail result automatic by using Listner and how to derive under Testng.xml

i have written code for that it is working fro web application same i am using for mobile application

please Help me out ASAP

i have attached the code for that please have a look and give suggestion .

Appium code.txt (5.4 KB)

did you add @Listeners(TestListener.class) on your test class ?

hi @Bilal_Jarwan i have added , i have shared my code above .

3.Testng .xml file

<?xml version="1.0" encoding="UTF-8"?>
<!--  <listeners> <listener class-name="POC_Concept.TestListner" /> </listeners> -->



<parameter name="" value="" />

<test thread-count="5" name="Mobile_Automation">




	<classes>

		<class name="AppiumTestNg.AppiumTestTest" />

	</classes>
	
	
	

</test> <!-- Test -->

hi @Bilal_Jarwan

1.code for appium loader main class

package AppiumLaunch;

import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;

import org.automationtesting.excelreport.Xl;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterSuite;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeTest;

public class AppiumTest implements AutoConstant {

public static WebDriver appium;
{

}



@BeforeTest

public void Test() throws InterruptedException, MalformedURLException 
{

	// Set the Desired Capabilities

	DesiredCapabilities caps = new DesiredCapabilities();

// caps.setCapability(“deviceName”, “My Phone”);
// caps.setCapability(“udid”, “192.168.238.101:5555”); // Give Device ID of your mobile phone
// caps.setCapability(“platformName”, “Android”);
// caps.setCapability(“platformVersion”, “7.1.0”);
// caps.setCapability(“appPackage”, “ebroker.com.ebroker”);
// caps.setCapability(“appActivity”, “ebroker.com.ebroker.ui.login.LoginActivity”);
// caps.setCapability(“automationName”,“UiAutomator2”);
// caps.setCapability(“noReset”, “true”);

	caps.setCapability	("appActivity", "ebroker.com.ebroker.ui.login.LoginActivity");
	caps.setCapability  ("appPackage", "ebroker.com.ebroker");
	caps.setCapability  ("automationName","UiAutomator2");
	caps.setCapability  ("deviceName", "My Phone");
	caps.setCapability  ("noReset", "true");
	caps.setCapability  ("platformName", "Android");
	caps.setCapability  ("platformVersion", "7.1.0");
	caps.setCapability  ("udid", "192.168.155.101:5555");

	

	// Instantiate Appium Driver

	appium = new RemoteWebDriver(new URL("http://127.0.0.1:4723/wd/hub"), caps);
	appium.manage().timeouts().implicitlyWait(10,TimeUnit.SECONDS);
}



@AfterTest

//quite 

public static void closeAndroid() 

{
    if (appium != null) 
    
    {
        appium.quit();
    }
}
  1. code for TestListner

package POC_Concept;

import java.io.IOException;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.poi.EncryptedDocumentException;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.testng.ITestResult;
import org.testng.TestListenerAdapter;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;

import AppiumLaunch.AppiumTest;
import AppiumLaunch.AutoConstant;
import AppiumLaunch.Lib;

public class TestListner extends TestListenerAdapter implements AutoConstant

{

String values = "";
static int count = 1;


@Override
public void onTestStart(ITestResult result)

{

	System.out.println("Test has started running:" + result.getMethod().getMethodName() + " at:" + result.getStartMillis());
}


@Override
public void onTestFailure(ITestResult result)

{
	System.out.println("Log Message:: @AfterTest: " + result.getMethod().getMethodName() + " has Failed");

	values = "FAIL";

	captureScreenshot(AppiumTest.appium, result.getMethod().getMethodName());

	try {

		Lib.setColumnValue(TEST_EXCEL_PATH1, "EBROKER", count, 6, values);
	} catch (IOException | InvalidFormatException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	count++;
}



@Override
public void onTestSuccess(ITestResult result) {
	System.out.println("Log Message:: @AfterTest: " + result.getMethod().getMethodName() + " has Passed");
	values = " PASS ";
	try {
		// System.out.println(Lib.getCellValue(EXCEL_PATH, "Valid_Login", 1,
		// 1));

		Lib.setColumnValue(TEST_EXCEL_PATH1, "EBROKER", count,6, values);
		// System.out.println(Lib.getCellValue(EXCEL_PATH, "Valid_Login", 1,
		// 1));
	} catch (IOException | EncryptedDocumentException | InvalidFormatException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	count++;
}



@Override
public void onTestSkipped(ITestResult result) 
{

	System.out.println("Log Message:: @AfterTest: " + result.getMethod().getMethodName() + " has Skipped");

}

public static String captureScreenshot(WebDriver driver, String screenshotName) {

	try 
	   {
		TakesScreenshot ts = (TakesScreenshot) driver;

		File source = ts.getScreenshotAs(OutputType.FILE);

		FileUtils.copyFile(source, new File ("./screenshots/"+ screenshotName + ".jpg"));
		
		return new SimpleDateFormat("yyyy-MM-dd HH-mm-ss").format(new Date());
	} 
	catch (Exception e) 
	{

		System.out.println("Exception while taking screenshot " + e.getMessage());
	}
	return screenshotName;
}

}

@rohit1
try to add @Listeners(TestListener.class) above your test class instead of xml file and let me know the result please :slight_smile:

sure @Bilal_Jarwan will get back to you

hi @Bilal_Jarwan u want like that i need to drive ryt plz have a look on screeshot

not here into Test class not base class

oho @Bilal_Jarwan that mean there

still it it not working it is making excel sheet corrupted plz help me