All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.selgp.moladek.robots.BaseRobotsTest Maven / Gradle / Ivy

The newest version!
package com.selgp.moladek.robots;

import com.selgp.moladek.core.TestRunner;
import com.selgp.moladek.core.loggers.BaseLoggingTest;
import com.selgp.moladek.core.loggers.screenrecorder.RecorderH264;
import com.selgp.moladek.core.loggers.screenshots.MoladekScreenshooter;
import org.apache.log4j.Logger;
import org.testng.ITestResult;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;

import java.awt.*;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.lang.reflect.Method;
import java.util.UUID;

/**
 * Base class for all Java AWT Robot tests.
 *
 * @author [email protected]
 */

public abstract class BaseRobotsTest extends BaseLoggingTest {

    private static final Logger LOGGER = Logger.getLogger(BaseRobotsTest.class);

    private InheritableThreadLocal globalRobotDriver = new InheritableThreadLocal();

    private InheritableThreadLocal globalVideoRecorder = new InheritableThreadLocal();

    public String testName;
    private Process process;

    public synchronized Process getProcess() {
        return process;
    }

    public synchronized void setProcess(Process process) {
        this.process = process;
    }

    public synchronized String getTestName() {
        return testName;
    }

    public synchronized void setTestName(String testName) {
        this.testName = testName;
    }

    @Override
    @BeforeMethod(alwaysRun = true)
    protected void setup(Method method, Object[] testArguments) {
        super.setup(method, testArguments);
        setTestName(method.getName() + "-" + UUID.randomUUID());
        try {
            globalRobotDriver.set(new Robot());
        } catch (AWTException e) {
            e.printStackTrace();
        }
        LOGGER.info("Starting Robot session for " + getTestName());
        if (Desktop.isDesktopSupported()) {
            Runtime runtime = Runtime.getRuntime();
            try {
                setProcess(runtime.exec(TestRunner.getInstance().config.getProperty("desktopApp", "unset")));
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
                LOGGER.info("Starting Local Recording here ... " + TestRunner.CURRENT_EXECUTION + method.getName() + File.separator + getTestName() + ".mp4");
                globalVideoRecorder.set(new RecorderH264(TestRunner.CURRENT_EXECUTION + method.getName() + File.separator + getTestName() + ".mp4"));
                globalVideoRecorder.get().start();
            } catch (Exception e) {
                e.printStackTrace();
            }
            globalRobotDriver.get().delay(3000);
            maximizeApplication();
        }
    }

    @Override
    @AfterMethod(alwaysRun = true)
    protected void teardown(ITestResult tr, Method method) {
        if ((logger() != null) && (tr.getStatus() == ITestResult.FAILURE)) {
            logUnexpectedException(tr.getThrowable());
        }
        getProcess().destroy();
        globalRobotDriver.get().delay(3000);
        LOGGER.info("Stop Recording! Stored Local Recording here ... " + TestRunner.CURRENT_EXECUTION + method.getName() + File.separator + getTestName() + ".mp4");
        try {
            globalVideoRecorder.get().shutdown();
            globalVideoRecorder.get().join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        super.teardown(tr, method);
    }

    protected Robot robot() {
        return globalRobotDriver.get();
    }

    @Override
    protected void logScreenshot(String screenshotName) {
        logResource(new MoladekScreenshooter(robot(), screenshotName)
                .getScreenshot());
    }

    public void maximizeApplication() {
        robot().keyPress(KeyEvent.VK_ALT);
        robot().keyPress(KeyEvent.VK_SPACE);
        robot().keyRelease(KeyEvent.VK_ALT);
        robot().keyRelease(KeyEvent.VK_SPACE);
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //Press down arrow key to move down the menu

        robot().keyPress(KeyEvent.VK_DOWN);
        robot().keyRelease(KeyEvent.VK_DOWN);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        robot().keyPress(KeyEvent.VK_DOWN);
        robot().keyRelease(KeyEvent.VK_DOWN);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        robot().keyPress(KeyEvent.VK_DOWN);
        robot().keyRelease(KeyEvent.VK_DOWN);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        robot().keyPress(KeyEvent.VK_DOWN);
        robot().keyRelease(KeyEvent.VK_DOWN);
        try {
            Thread.sleep(100);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        //Press enter to invoke the Maximize menu option

        robot().keyPress(KeyEvent.VK_ENTER);
        robot().keyRelease(KeyEvent.VK_ENTER);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy