Appium - Capture Screenshot On Failure Or Pass In Android Test Automation

Earlier In previous post, We learnt how to capture screenshot of android mobile software app hide on whatever phase In android appium software automation test. Now supposing I wants to capture screenshot on essay failure or I wants to capture screenshot on software essay pass. In this case,  ITestListener Interface of TestNG tin give the axe aid us to capture screenshot on for sure occurrence similar essay failure or software essay pass.

ITestListener Interface
ITestListener Is an Interface of TestNG which tin give the axe Invoke it's specific method on essay failure or essay croak or essay skips, ect.. There are bellow given useful methods available In ITestListener Interface.
  • onFinish(ITestContext context) - Invoked afterwards all the software tests stimulate got run.
  • onStart(ITestContext context) -  Invoked afterwards the essay degree is instantiated.
  • onTestFailure(ITestResult result) - Invoked each fourth dimension a essay fails.
  • onTestSkipped(ITestResult result) - Invoked each fourth dimension a essay is skipped.
  • onTestStart(ITestResult result) -  Invoked each fourth dimension earlier a essay volition move invoked.
  • onTestSuccess(ITestResult result) - Invoked each fourth dimension a essay succeeds.
You tin give the axe sentiment to a greater extent than especial on ITestListener Interface on THIS PAGE for to a greater extent than detail.

App To Use Capture Screenshot Test
We volition piece of occupation android mobile's estimator app In this software automation essay example.

Aim to Achieve
We wants to capture screenshot on essay croak or failure occurrence too shop file inward related folder. If essay Is neglect too then capture screenshot too shop It In Failures folder too If essay Is croak too then capture screenshot too shop It In Pass folder nether screenshots folder of your projection every bit shown inward bellow Image.


How To Do
We volition exercise split degree file to write logic of capture screenshot on essay neglect or pass. Inside It, We volition exercise split method captureScreenShot to write logic to capture screenshot. We volition piece of occupation testng assertion to verify our software essay is neglect or croak too piece of occupation ITestListener Interface to Invoke onTestFailure method on assertion failure too onTestSuccess on assertion croak too and then telephone telephone captureScreenShot method to accept screenshot.

In essay degree file, We volition write ii essay methods TestForFailure() and TestForPass(). Test method TestForFailure() Is written In such a agency to neglect It Intentionally.

Screenshot of TestForFailure() method volition move stored Inside Failure folder too screenshot of TestForPass() method volition move stored Inside Success folder.

Create And Run Test
I stimulate got created real uncomplicated illustration to demonstrate you lot how to capture screenshot on essay failure or pass. You postulate to exercise ii degree files nether Android bundle of your projection every bit bellow.

ScreenshotUtility.java file contains logic to capture screenshot on essay failure or pass.

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;  populace degree ScreenshotUtility implements ITestListener {  // This method volition execute earlier starting of Test suite.  populace void onStart(ITestContext tr) {   }   // This method volition execute, Once the Test suite is finished.  populace void onFinish(ITestContext tr) {   }   // This method volition execute entirely when the essay is pass.  populace void onTestSuccess(ITestResult tr) {   captureScreenShot(tr, "pass");  }   // This method volition execute entirely on the trial of neglect test.  populace void onTestFailure(ITestResult tr) {   captureScreenShot(tr, "fail");  }   // This method volition execute earlier the principal essay commencement (@Test)  populace void onTestStart(ITestResult tr) {   }   // This method volition execute entirely if whatever of the principal test(@Test) become skipped  populace void onTestSkipped(ITestResult tr) {  }   populace void onTestFailedButWithinSuccessPercentage(ITestResult tr) {  }   // Function to capture screenshot.  populace 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 = novel SimpleDateFormat("dd-MMM-yyyy__hh_mm_ssaa");   // If condition = neglect too then fix folder refer "screenshots/Failures"   if (status.equalsIgnoreCase("fail")) {    destDir = "screenshots/Failures";   }   // If condition = croak too then fix folder refer "screenshots/Success"   else if (status.equalsIgnoreCase("pass")) {    destDir = "screenshots/Success";   }    // To exercise folder to shop screenshots   novel File(destDir).mkdirs();   // Set file refer amongst combination of essay degree refer + engagement time.   String destFile = passfailMethod + " - " + dateFormat.format(new Date()) + ".png";    endeavor {    // Store file at destination folder location    FileUtils.copyFile(scrFile, novel File(destDir + "/" + destFile));   } grab (IOException e) {    e.printStackTrace();   }  } }


ScreenshotOnPassFail.java file comprise android cal app test. If you lot come across In bellow given test, We stimulate got used @Listeners annotation. It mansion that testng listeners are Implemented In ScreenshotUtility degree file to listen.

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 }) populace degree ScreenshotOnPassFail {  static AndroidDriver driver;   @BeforeClass  populace void initialize() throws MalformedURLException {   DesiredCapabilities capabilities = novel 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 = novel 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 agency to neglect It Intentionally.  // This method volition neglect every bit actual effect six volition non check amongst expected 7.  @Test  populace void TestForFailure() throws IOException {   // Click on DELETE/CLR push to clear effect text box earlier running test.   ((WebElement) driver.findElements(By.xpath("//android.widget.Button")).get(0)).click();   // Click on 2, +, four too = buttons.   driver.findElement(By.name("2")).click();   driver.findElement(By.name("+")).click();   driver.findElement(By.name("4")).click();   driver.findElement(By.name("=")).click();   // Get effect from calc effect textbox.   String effect = driver.findElement(By.className("android.widget.EditText")).getText();   // Compare actual too expected effect using testng assertion too score essay croak or neglect based on result.   // Assertion volition fail. So It volition telephone telephone onTestFailure method from ScreenshotUtility.   assert result.equals("7") : "Expected value : vii did non check amongst Actual value: "+ result;  }   @Test  populace void TestForPass() throws IOException {   // Click on DELETE/CLR push to clear effect text box earlier running test.   ((WebElement) driver.findElements(By.xpath("//android.widget.Button")).get(0)).click();   // Click on 3, +, four too = buttons.   driver.findElement(By.name("3")).click();   driver.findElement(By.name("+")).click();   driver.findElement(By.name("4")).click();   driver.findElement(By.name("=")).click();   // Get effect from calc effect textbox.   String effect = driver.findElement(By.className("android.widget.EditText")).getText();   // Compare actual too expected effect using testng assertion too score essay croak or neglect based on result.   // Assertion volition pass. So It volition telephone telephone onTestSuccess method from ScreenshotUtility.   assert result.equals("7") : "Expected value : vii did non check amongst Actual value: " + result;  }   @AfterClass  populace void End() {   driver.quit();  } }

Connect your device amongst PC too run to a higher house ScreenshotOnPassFail essay using testng too appium.
  • TestForFailure() method : Here assertion volition neglect so onTestFailure method volition move called from ScreenshotUtility.java. It volition capture screenshot too shop It in Failures folder nether screenshots folder.
  • TestForPass() method : Here assertion volition croak so onTestSuccess method volition move called from ScreenshotUtility.java. It volition capture screenshot too shop It in Success folder nether screenshots folder.
This Is the agency to capture screenshot on failure or croak of whatever android appium software automation test.

More interesting articles here :Generation Enggelmundus Internet Marketing Tool here :Zeageat IM http://www.software-testing-tutorials-automation.com/
Post a Comment (0)
Previous Post Next Post