HI Everyone,
I am getting an issue with creating a screenshots.
Code everthing is correct executing successfull i don’t know why i am unable to genrate screensshots for failed testcases.
pls get me out of this
BasePage:-
package YahaalPkg.Base;
import YahaalPkg.utils.TestUtils;
import com.aventstack.extentreports.testng.listener.ExtentITestListenerAdapter;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.AppiumDriver;
import io.appium.java_client.PerformsTouchActions;
import io.appium.java_client.TouchAction;
import io.appium.java_client.android.AndroidDriver;
import io.appium.java_client.android.AndroidTouchAction;
import io.appium.java_client.android.StartsActivity;
import io.appium.java_client.pagefactory.AppiumFieldDecorator;
import io.appium.java_client.touch.LongPressOptions;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
import java.io.File;
import java.io.InputStream;
import java.net.URL;
import java.time.Duration;
import java.util.HashMap;
import java.util.Properties;
@Listeners(ExtentITestListenerAdapter.class)
public class BasePage {
protected static AppiumDriver driver;
protected static Properties props;
protected static String dateTime;
protected static HashMap<String, String> strings = new HashMap<String, String>();
InputStream inputStream;
InputStream stringsis;
TestUtils utils;
static Logger log = LogManager.getLogger(BasePage.class.getName());
public BasePage() {
PageFactory.initElements(new AppiumFieldDecorator(BasePage.driver),this);
}
@Parameters({"platformName","platformVersion","deviceName"})
@BeforeTest
public void BeforeTest(String platformName, String platformVersion, String deviceName) throws Exception {
utils = new TestUtils();
dateTime = utils.dateTime();
log.info("This is a info message");
log.error("This is a error message");
log.debug("This is a debug message");
log.warn("This is a warn message");
try {
props = new Properties();
String propFileName = "config.properties";
String xmlFileName = "strings/strings.xml";
inputStream = getClass().getClassLoader().getResourceAsStream(propFileName);
props.load(inputStream);
stringsis = getClass().getClassLoader().getResourceAsStream(xmlFileName);
strings = (HashMap<String, String>) utils.parseStringXML(stringsis);
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability("platformName", platformName);
desiredCapabilities.setCapability("appium:platformVersion", platformVersion);
desiredCapabilities.setCapability("appium:deviceName", deviceName);
desiredCapabilities.setCapability("appium:automationName", props.getProperty("androidAutomationName"));
desiredCapabilities.setCapability("appium:appPackage", props.getProperty("androidAppPackage"));
desiredCapabilities.setCapability("appium:appActivity", props.getProperty("androidAppActivity"));
// desiredCapabilities.setCapability("appium:udid","8f8dd32a");
// desiredCapabilities.setCapability("appium:ignoreHiddenApiPolicyError","true");
URL appUrl = getClass().getClassLoader().getResource(props.getProperty("androidAppLocation"));
String appUrls = System.getProperty("user.dir")+ File.separator+"src"+File.separator+"test"+File.separator+"resources"+File.separator+"app"+File.separator+"YahaalNewDev.apk";
desiredCapabilities.setCapability("appium:app", appUrls);
URL url = new URL(props.getProperty("appiumURL"));
driver = new AndroidDriver(url, desiredCapabilities);
String sessionID = driver.getSessionId().toString();
System.out.println(sessionID);
} catch (Exception e) {
e.printStackTrace();
throw e;
}
finally {
if (inputStream !=null) {
inputStream.close();
}
if (stringsis !=null) {
stringsis.close();
}
}
}
public AppiumDriver getDriver() {
return driver;
}
public String getDateTime() {
return dateTime;
}
public void waitForVisibility(WebElement element) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(TestUtils.WAIT));
wait.until(ExpectedConditions.visibilityOf(element));
wait.until(ExpectedConditions.elementToBeClickable(element));
}
public void click(WebElement element) {
waitForVisibility(element);
element.click();
}
public void sendKeys(WebElement element, String txt) {
waitForVisibility(element);
element.sendKeys(txt);
}
public void getAttribute(WebElement element, String attribute) {
waitForVisibility(element);
element.getAttribute(attribute);
}
public void clear(WebElement element) {
waitForVisibility(element);
element.clear();
}
@Deprecated
public void performSwipeUp() {
Dimension size = driver.manage().window().getSize();
int starty = (int) (size.height * 0.80);
int endy = (int) (size.height * 0.20);
int startx = size.width / 2;
TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
touchAction.longPress(LongPressOptions.longPressOptions().withPosition(PointOption.point(startx, starty)))
.moveTo(PointOption.point(startx, endy)).release().perform();
}
@Deprecated
public void performSwipeDown() {
Dimension size = driver.manage().window().getSize();
int starty = (int) (size.height * 0.80);
int endy = (int) (size.height * 0.20);
int startx = size.width / 2;
TouchAction touchAction = new TouchAction((PerformsTouchActions) driver);
touchAction.longPress(LongPressOptions.longPressOptions().withPosition(PointOption.point(startx, starty)))
.moveTo(PointOption.point(startx, endy)).release().perform();
}
@AfterTest
public void afterTest() {
// driver.quit();
}
}
TestListenerclass:-
package YahaalPkg.listeners;
import YahaalPkg.Base.BasePage;
import YahaalPkg.utils.TestUtils;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.ITestListener;
import org.testng.ITestResult;
import org.testng.Reporter;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
public class TestListener implements ITestListener {
public void onTestFailure(ITestResult result) {
if (result.getThrowable() !=null) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
result.getThrowable().printStackTrace(pw);
System.out.println(sw.toString());
}
BasePage basepage = new BasePage();
File file = basepage.getDriver().getScreenshotAs(OutputType.FILE);
Map <String, String> params = new HashMap<String, String>();
params = result.getTestContext().getCurrentXmlTest().getAllParameters();
String imagePath = "Screenshots" + File.separator + params.get("platformName")
+ "_" + params.get("deviceName") + File.separator + basepage.getDateTime() + File.separator
+ result.getTestClass().getRealClass().getSimpleName() + File.separator + result.getName() + ".png";
String completeImagePath = System.getProperty("user.dir") + File.separator + imagePath;
try {
FileUtils.copyFile(file, new File(imagePath));
Reporter.log("This is the sample screenshot");
Reporter.log("<a href='"+ completeImagePath + "'> <img src='"+ completeImagePath + "' height='400' width='400'/> </a>");
} catch (IOException e) {
e.printStackTrace();
}
}
}
pom.xml:-
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.example</groupId>
<artifactId>YahaalMobileAppAuto</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<!-- </properties>-->
<!-- <properties>-->
<jvm.options>--add-opens java.base/java.lang=ALL-UNNAMED</jvm.options>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M5</version>
<configuration>
<argLine>${jvm.options}</argLine>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<!-- https://mvnrepository.com/artifact/io.appium/java-client -->
<dependency>
<groupId>io.appium</groupId>
<artifactId>java-client</artifactId>
<version>8.3.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.testng/testng -->
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>7.7.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.webjars.npm</groupId>
<artifactId>swipebox-10quality</artifactId>
<version>2.0.0</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.20.0</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.20.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.aventstack/extentreports-testng-adapter -->
<dependency>
<groupId>com.aventstack</groupId>
<artifactId>extentreports-testng-adapter</artifactId>
<version>1.0.7</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20230227</version>
</dependency>
</dependencies>
</project>