|
| 1 | +package selenium.listeners; |
| 2 | + |
| 3 | +import driver.Driver; |
| 4 | +import driver.DriverManager; |
| 5 | +import io.qameta.allure.Attachment; |
| 6 | +import org.apache.commons.io.FileUtils; |
| 7 | +import org.apache.commons.io.IOUtils; |
| 8 | +import org.openqa.selenium.OutputType; |
| 9 | +import org.openqa.selenium.TakesScreenshot; |
| 10 | +import org.openqa.selenium.WebDriver; |
| 11 | +import org.testng.IInvokedMethod; |
| 12 | +import org.testng.IInvokedMethodListener; |
| 13 | +import org.testng.ITestResult; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.io.FileInputStream; |
| 17 | +import java.io.IOException; |
| 18 | +import java.io.InputStream; |
| 19 | +import java.net.MalformedURLException; |
| 20 | +import java.text.SimpleDateFormat; |
| 21 | +import java.util.Date; |
| 22 | + |
| 23 | +public class WebDriverListener implements IInvokedMethodListener { |
| 24 | + |
| 25 | + @Override |
| 26 | + public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { |
| 27 | + |
| 28 | + if(method.isTestMethod()) { |
| 29 | + |
| 30 | + String browserName = ((Base)method.getTestMethod().getInstance()).getBrowser(); |
| 31 | + try { |
| 32 | + WebDriver driver = DriverManager.createInstance(browserName, ((Base)method.getTestMethod().getInstance()).getAppUrl()); |
| 33 | + System.out.println("Initializing webdriver session --> Thread ID: " + Thread.currentThread().getId()); |
| 34 | + System.out.println("Running test --> " + method.getTestMethod().getMethodName()); |
| 35 | + Driver.setWebDriver(driver); |
| 36 | + } catch (MalformedURLException e) { |
| 37 | + e.printStackTrace(); |
| 38 | + } |
| 39 | + } |
| 40 | + } |
| 41 | + |
| 42 | + @Override |
| 43 | + public void afterInvocation(IInvokedMethod method, ITestResult testResult) { |
| 44 | + |
| 45 | + if(method.isTestMethod()) { |
| 46 | + |
| 47 | + WebDriver driver = Driver.getDriver(); |
| 48 | + if(driver != null) { |
| 49 | + |
| 50 | + try { |
| 51 | + takeScreenshotOnFailure(testResult); |
| 52 | + } catch (IOException e) { |
| 53 | + e.printStackTrace(); |
| 54 | + } |
| 55 | + System.out.println("Closing webdriver session: " + Thread.currentThread().getId()); |
| 56 | + driver.quit(); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + public void takeScreenshotOnFailure(ITestResult testResult) throws IOException { |
| 62 | + |
| 63 | + if(testResult.getStatus() == ITestResult.FAILURE) { |
| 64 | + |
| 65 | + File screenShot = ((TakesScreenshot) Driver.getDriver()).getScreenshotAs(OutputType.FILE); |
| 66 | + File destination = new File("target/failure-screenshots/" + testResult.getName() + "-" |
| 67 | + + new SimpleDateFormat("dd-MM-yyyy HH-mm-ss").format(new Date()) + ".png"); |
| 68 | + FileUtils.copyFile(screenShot, destination); |
| 69 | + |
| 70 | + InputStream screenShotStream = new FileInputStream(destination); |
| 71 | + byte[] screen = IOUtils.toByteArray(screenShotStream); |
| 72 | + |
| 73 | + saveScreenshot(screen); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + @Attachment(value = "Screenshot of the failure", type = "image/png") |
| 78 | + public byte[] saveScreenshot(byte[] screenShot) { |
| 79 | + |
| 80 | + return screenShot; |
| 81 | + } |
| 82 | +} |
0 commit comments