BasePage

PHOTO EMBED

Sat Jun 17 2023 09:52:42 GMT+0000 (Coordinated Universal Time)

Saved by @mehran

package tests;

import org.testng.ITestResult;
import com.adak.ir.LoggingUtils;

@Listeners(BasePage.TestListener.class)
public class BasePage {
    public static final Logger LOGGER = LoggerFactory.getLogger(BasePage.class);
    public static AppiumDriver driver;

    @BeforeClass
    public void Android_setUp() throws MalformedURLException {
        LOGGER.info("آماده سازی دستگاه");

        DesiredCapabilities capabilities = new DesiredCapabilities();
        capabilities.setCapability(MobileCapabilityType.AUTOMATION_NAME, "UIAutomator2");
        capabilities.setCapability(MobileCapabilityType.PLATFORM_VERSION, "11");
        capabilities.setCapability(MobileCapabilityType.DEVICE_NAME, "R9WRC0DMSJJ");
        capabilities.setCapability(MobileCapabilityType.PLATFORM_NAME, "Android");
        capabilities.setCapability(MobileCapabilityType.APP, "/Users/adak/Documents/mymci.apk");
        capabilities.setCapability("appPackage", "ir.mci.ecareapp");
        capabilities.setCapability("appActivity", "ir.mci.ecareapp.ui.activity.LauncherActivity");
        capabilities.setCapability(MobileCapabilityType.NO_RESET, true);

        driver = new AndroidDriver(new URL("http://localhost:4723/wd/hub"), capabilities);
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
    }

    @AfterClass
    public void tearDown() {
        if (driver != null) {
            driver.quit();
        }
    }

    public static class TestListener implements ITestListener {
        @Override
        public void onTestStart(ITestResult result) {
            LOGGER.info("Test Started: " + result.getName());
        }

        @Override
        public void onTestSuccess(ITestResult result) {
            LOGGER.info("Test Passed: " + result.getName());
        }

        @Override
        public void onTestFailure(ITestResult result) {
            LOGGER.info("Test Failed: " + result.getName());
            captureScreenshot(result.getMethod().getMethodName());
        }

        private void captureScreenshot(String methodName) {
            File screenshotFile = ((TakesScreenshot) driver).getScreenshotAs(OutputType.FILE);
            String destDir = "path/to/sup/";
            String destFile = methodName + "_" + getCurrentTimestamp() + ".png";
             
            try {
                  String screenshotBase64 = ((TakesScreenshot) driver).getScreenshotAs(OutputType.BASE64);
        LoggingUtils.logBase64(screenshotBase64, methodName);
                FileHandler.copy(screenshotFile, new File(destDir + destFile));
                LOGGER.info("Screenshot saved successfully: " + destDir + destFile);
            } catch (IOException e) {
                LOGGER.error("Failed to capture screenshot: " + e.getMessage());
            }
        }

        private String getCurrentTimestamp() {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd_HH-mm-ss");
            return dateFormat.format(new Date());
        }

        // Other methods from ITestListener
        // ...
    }
}
content_copyCOPY