Download Appium - Capture Screenshot On Failure Or Pass In Android Test Automation - Software Testing Tutorials graphic type that can be scaled to use with the Silhouette Cameo or Cricut. An SVG's size can be increased or decreased without a loss of quality. All of our downloads include an image, Silhouette file, and SVG file. It should be everything you need for your next project. Our SVG files can be used on adhesive vinyl, heat transfer and t-shirt vinyl, or any other cutting surface
- onFinish(ITestContext context) - Invoked after all the software tests have run.
- onStart(ITestContext context) - Invoked after the test class is instantiated.
- onTestFailure(ITestResult result) - Invoked each time a test fails.
- onTestSkipped(ITestResult result) - Invoked each time a test is skipped.
- onTestStart(ITestResult result) - Invoked each time before a test will be invoked.
- onTestSuccess(ITestResult result) - Invoked each time a test succeeds.
ScreenshotUtility.java
package Android;
import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.testng.ITestContext;
import org.testng.ITestListener;
import org.testng.ITestResult;
public class ScreenshotUtility implements ITestListener {
// This method will execute before starting of Test suite.
public void onStart(ITestContext tr) {
}
// This method will execute, Once the Test suite is finished.
public void onFinish(ITestContext tr) {
}
// This method will execute only when the test is pass.
public void onTestSuccess(ITestResult tr) {
captureScreenShot(tr, "pass");
}
// This method will execute only on the event of fail test.
public void onTestFailure(ITestResult tr) {
captureScreenShot(tr, "fail");
}
// This method will execute before the main test start (@Test)
public void onTestStart(ITestResult tr) {
}
// This method will execute only if any of the main test(@Test) get skipped
public void onTestSkipped(ITestResult tr) {
}
public void onTestFailedButWithinSuccessPercentage(ITestResult tr) {
}
// Function to capture screenshot.
public void captureScreenShot(ITestResult result, String status) {
// AndroidDriver driver=ScreenshotOnPassFail.getDriver();
String destDir = "";
String passfailMethod = result.getMethod().getRealClass().getSimpleName() + "." + result.getMethod().getMethodName();
// To capture screenshot.
File scrFile = ((TakesScreenshot) ScreenshotOnPassFail.driver).getScreenshotAs(OutputType.FILE);
DateFormat dateFormat = new SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");
// If status = fail then set folder name "screenshots/Failures"
if (status.equalsIgnoreCase("fail")) {
destDir = "screenshots/Failures";
}
// If status = pass then set folder name "screenshots/Success"
else if (status.equalsIgnoreCase("pass")) {
destDir = "screenshots/Success";
}
// To create folder to store screenshots
new File(destDir).mkdirs();
// Set file name with combination of test class name + date time.
String destFile = passfailMethod + " - " + dateFormat.format(new Date()) + ".png";
try {
// Store file at destination folder location
FileUtils.copyFile(scrFile, new File(destDir + "/" + destFile));
} catch (IOException e) {
e.printStackTrace();
}
}
}
ScreenshotOnPassFail.java
package Android;
import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.TimeUnit;
import io.appium.java_client.android.AndroidDriver;
import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
@Listeners({ ScreenshotUtility.class })
public class ScreenshotOnPassFail {
static AndroidDriver driver;
@BeforeClass
public void initialize() throws MalformedURLException {
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities.setCapability("deviceName", "ZX1B32FFXF");
capabilities.setCapability("browserName", "Android");
capabilities.setCapability("platformVersion", "4.4.2");
capabilities.setCapability("platformName", "Android");
capabilities.setCapability("appPackage", "com.android.calculator2");
capabilities.setCapability("appActivity", "com.android.calculator2.Calculator");
driver = new AndroidDriver(new URL("http://127.0.0.1:4723/wd/hub"), capabilities);
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
}
// Method Is written In such a way to fail It Intentionally.
// This method will fail as actual result 6 will not match with expected 7.
@Test
public void TestForFailure() throws IOException {
// Click on DELETE/CLR button to clear result text box before running test.
((WebElement) driver.findElements(By.xpath("//android.widget.Button")).get(0)).click();
// Click on 2, +, 4 and = buttons.
driver.findElement(By.name("2")).click();
driver.findElement(By.name("+")).click();
driver.findElement(By.name("4")).click();
driver.findElement(By.name("=")).click();
// Get result from calc result textbox.
String result = driver.findElement(By.className("android.widget.EditText")).getText();
// Compare actual and expected result using testng assertion and mark test pass or fail based on result.
// Assertion will fail. So It will call onTestFailure method from ScreenshotUtility.
assert result.equals("7") : "Expected value : 7 did not match with Actual value: "+ result;
}
@Test
public void TestForPass() throws IOException {
// Click on DELETE/CLR button to clear result text box before running test.
((WebElement) driver.findElements(By.xpath("//android.widget.Button")).get(0)).click();
// Click on 3, +, 4 and = buttons.
driver.findElement(By.name("3")).click();
driver.findElement(By.name("+")).click();
driver.findElement(By.name("4")).click();
driver.findElement(By.name("=")).click();
// Get result from calc result textbox.
String result = driver.findElement(By.className("android.widget.EditText")).getText();
// Compare actual and expected result using testng assertion and mark test pass or fail based on result.
// Assertion will pass. So It will call onTestSuccess method from ScreenshotUtility.
assert result.equals("7") : "Expected value : 7 did not match with Actual value: " + result;
}
@AfterClass
public void End() {
driver.quit();
}
}
- TestForFailure() method : Here assertion will fail so onTestFailure method will be called from ScreenshotUtility.java. It will capture screenshot and store It in Failures folder under screenshots folder.
- TestForPass() method : Here assertion will pass so onTestSuccess method will be called from ScreenshotUtility.java. It will capture screenshot and store It in Success folder under screenshots folder.
Download Appium - Capture Screenshot On Failure Or Pass In Android Test Automation - Software Testing Tutorials All SVG file downloads also come bundled with DXF, PNG, and EPS file formats. All designs come with a small business commercial license. These SVG cut files are great for use with Silhouette Cameo or Cricut and other Machine Tools.