Search in sources :

Example 1 with VideoRecorder

use of com.seleniumtests.util.video.VideoRecorder in project seleniumRobot by bhecquet.

the class CustomEventFiringWebDriver method startVideoCapture.

/**
 * Start video capture using VideoRecorder class
 * @param driverMode
 * @param gridConnector
 * @param videoName		name of the video to record so that it's unique. Only used locally. In remote, grid sessionId is used
 */
public static VideoRecorder startVideoCapture(DriverMode driverMode, SeleniumGridConnector gridConnector, File videoFolder, String videoName) {
    if (driverMode == DriverMode.LOCAL) {
        try {
            VideoRecorder recorder = new VideoRecorder(videoFolder, videoName);
            recorder.start();
            return recorder;
        } catch (HeadlessException e) {
            throw new ScenarioException("could not initialize video capture with headless robot: " + e.getMessage());
        }
    } else if (driverMode == DriverMode.GRID && gridConnector != null) {
        gridConnector.startVideoCapture();
        return new VideoRecorder(videoFolder, videoName, false);
    } else {
        throw new ScenarioException("driver supports startVideoCapture only in local and grid mode");
    }
}
Also used : HeadlessException(java.awt.HeadlessException) VideoRecorder(com.seleniumtests.util.video.VideoRecorder) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Example 2 with VideoRecorder

use of com.seleniumtests.util.video.VideoRecorder in project seleniumRobot by bhecquet.

the class WebUIDriver method createRemoteWebDriver.

/**
 * prepare driver:
 * - create it
 * - add listeners
 * - create and start video capture
 * - create and start network capture proxy
 * - record driver and browser pid so that they can be deleted at the end of test session
 * @return
 */
public WebDriver createRemoteWebDriver() {
    webDriverBuilder = getWebDriverBuilderFactory();
    logger.info("driver mode: " + config.getMode());
    synchronized (createDriverLock) {
        // get browser info used to start this driver. It will be used then for managing pids
        BrowserInfo browserInfo = webDriverBuilder.getSelectedBrowserInfo();
        List<Long> existingPids = new ArrayList<>();
        // get pid pre-existing the creation of this driver. This helps filtering drivers launched by other tests or users
        if (browserInfo != null) {
            existingPids.addAll(browserInfo.getDriverAndBrowserPid(new ArrayList<>()));
        }
        TestStep cuurrentTestStep = TestStepManager.getCurrentRootTestStep();
        long start = new Date().getTime();
        long duration;
        try {
            driver = webDriverBuilder.createWebDriver();
        } finally {
            duration = new Date().getTime() - start;
            if (cuurrentTestStep != null) {
                cuurrentTestStep.setDurationToExclude(duration);
            }
            scenarioLogger.info(String.format("driver creation took: %.1f secs", duration / 1000.0));
        }
        WaitHelper.waitForSeconds(2);
        List<Long> driverPids = new ArrayList<>();
        // get the created PIDs
        if (browserInfo != null) {
            driverPids = browserInfo.getDriverAndBrowserPid(existingPids);
        }
        // issue #280: we use 'webDriverBuilder.getSelectedBrowserInfo()' as 'browserInfo' variable is null for grid, whereas, 'webDriverBuilder.getSelectedBrowserInfo()'
        // gets an updated version once the driver has been created on grid
        driver = handleListeners(driver, webDriverBuilder.getSelectedBrowserInfo(), driverPids);
        if (driver != null) {
            MutableCapabilities caps = ((CustomEventFiringWebDriver) driver).getInternalCapabilities();
            caps.setCapability(DriverUsage.STARTUP_DURATION, duration);
            caps.setCapability(DriverUsage.START_TIME, start);
            // capability from IDestkopCapabilitiesFactory is not available when we request capabilities from driver.
            if (config.getTestContext() != null && config.getTestContext().getTestNGResult() != null) {
                String testName = TestNGResultUtils.getTestName(config.getTestContext().getTestNGResult());
                caps.setCapability(DriverUsage.TEST_NAME, testName);
            }
        }
        if (config.getBrowserMobProxy() != null) {
            config.getBrowserMobProxy().newHar(SeleniumTestsContextManager.getThreadContext().getRelativeOutputDir());
        }
        if (config.getVideoCapture() != VideoCaptureMode.FALSE && videoRecorder.get() == null) {
            try {
                VideoRecorder recorder = CustomEventFiringWebDriver.startVideoCapture(SeleniumTestsContextManager.getThreadContext().getRunMode(), SeleniumTestsContextManager.getThreadContext().getSeleniumGridConnector(), new File(SeleniumTestsContextManager.getThreadContext().getOutputDirectory()), "videoCapture.avi");
                videoRecorder.set(recorder);
                TestStepManager.setVideoStartDate();
            } catch (ScenarioException e) {
                logger.warn("Video capture won't start: " + e.getMessage());
            }
        }
    }
    return driver;
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) BrowserInfo(com.seleniumtests.browserfactory.BrowserInfo) ArrayList(java.util.ArrayList) VideoRecorder(com.seleniumtests.util.video.VideoRecorder) Date(java.util.Date) MutableCapabilities(org.openqa.selenium.MutableCapabilities) File(java.io.File) ScenarioException(com.seleniumtests.customexception.ScenarioException)

Example 3 with VideoRecorder

use of com.seleniumtests.util.video.VideoRecorder in project seleniumRobot by bhecquet.

the class TestWebUIDriver method testCleanUpWithVideoCapture.

/**
 * video is closed when driver is cleaned
 * @throws Exception
 */
@Test(groups = { "ut" })
public void testCleanUpWithVideoCapture() throws Exception {
    PowerMockito.mockStatic(CustomEventFiringWebDriver.class);
    PowerMockito.doReturn(videoRecorder).when(CustomEventFiringWebDriver.class, "startVideoCapture", eq(DriverMode.LOCAL), eq(null), any(File.class), eq("videoCapture.avi"));
    SeleniumTestsContextManager.getThreadContext().setVideoCapture("true");
    SeleniumTestsContextManager.getThreadContext().setBrowser("htmlunit");
    WebUIDriver uiDriver = WebUIDriverFactory.getInstance("foo");
    uiDriver.createRemoteWebDriver();
    VideoRecorder vRecorder = WebUIDriver.getVideoRecorder().get();
    Assert.assertNotNull(WebUIDriver.getVideoRecorder().get());
    WebUIDriver.cleanUp();
    PowerMockito.verifyStatic(CustomEventFiringWebDriver.class, times(1));
    CustomEventFiringWebDriver.stopVideoCapture(eq(DriverMode.LOCAL), eq(null), eq(vRecorder));
    Assert.assertNull(WebUIDriver.getVideoRecorder().get());
}
Also used : WebUIDriver(com.seleniumtests.driver.WebUIDriver) VideoRecorder(com.seleniumtests.util.video.VideoRecorder) File(java.io.File) Test(org.testng.annotations.Test) GenericTest(com.seleniumtests.GenericTest) MockitoTest(com.seleniumtests.MockitoTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 4 with VideoRecorder

use of com.seleniumtests.util.video.VideoRecorder in project seleniumRobot by bhecquet.

the class TestWebUIDriver method testCleanUpWithVideoCaptureWithError.

/**
 * No error is raised when error happens closing video
 * videoRecorder is also removed
 * @throws Exception
 */
@Test(groups = { "ut" })
public void testCleanUpWithVideoCaptureWithError() throws Exception {
    PowerMockito.mockStatic(CustomEventFiringWebDriver.class);
    PowerMockito.doReturn(videoRecorder).when(CustomEventFiringWebDriver.class, "startVideoCapture", eq(DriverMode.LOCAL), eq(null), any(File.class), eq("videoCapture.avi"));
    PowerMockito.doThrow(new NullPointerException("error")).when(CustomEventFiringWebDriver.class, "stopVideoCapture", eq(DriverMode.LOCAL), eq(null), any(VideoRecorder.class));
    SeleniumTestsContextManager.getThreadContext().setVideoCapture("true");
    SeleniumTestsContextManager.getThreadContext().setBrowser("htmlunit");
    WebUIDriver uiDriver = WebUIDriverFactory.getInstance("foo");
    uiDriver.createRemoteWebDriver();
    VideoRecorder vRecorder = WebUIDriver.getVideoRecorder().get();
    Assert.assertNotNull(WebUIDriver.getVideoRecorder().get());
    WebUIDriver.cleanUp();
    PowerMockito.verifyStatic(CustomEventFiringWebDriver.class, times(1));
    CustomEventFiringWebDriver.stopVideoCapture(eq(DriverMode.LOCAL), eq(null), eq(vRecorder));
    Assert.assertNull(WebUIDriver.getVideoRecorder().get());
}
Also used : WebUIDriver(com.seleniumtests.driver.WebUIDriver) VideoRecorder(com.seleniumtests.util.video.VideoRecorder) File(java.io.File) Test(org.testng.annotations.Test) GenericTest(com.seleniumtests.GenericTest) MockitoTest(com.seleniumtests.MockitoTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Example 5 with VideoRecorder

use of com.seleniumtests.util.video.VideoRecorder in project seleniumRobot by bhecquet.

the class LogAction method commonLogTestStep.

/**
 * Log a TestStep, inside a parent TestStep or not
 * Common method used for all test step logging
 * @return
 * @throws Throwable
 */
private Object commonLogTestStep(ProceedingJoinPoint joinPoint, String stepNamePrefix, boolean configStep) throws Throwable {
    Object reply = null;
    boolean rootStep = false;
    TestStep previousParent = null;
    // step name will contain method arguments only if it's not a configuration method (as they are generic)
    TestStep currentStep = buildRootStep(joinPoint, stepNamePrefix, !configStep);
    if ("openPage".equals(joinPoint.getSignature().getName()) && joinPoint.getTarget() instanceof PageObject) {
        PageObject page = (PageObject) joinPoint.getTarget();
        currentStep.addAction(new TestAction(String.format("Opening page %s", page.getClass().getSimpleName()), false, new ArrayList<>()));
    }
    BrowserMobProxy mobProxy = WebUIDriver.getBrowserMobProxy();
    NLWebDriver neoloadDriver = WebUIDriver.getNeoloadDriver();
    VideoRecorder videoRecorder = WebUIDriver.getThreadVideoRecorder();
    // if rootStep is null, parent step is also null
    if (TestStepManager.getCurrentRootTestStep() == null) {
        // will also set parent step
        TestStepManager.setCurrentRootTestStep(currentStep);
        rootStep = true;
        if (mobProxy != null) {
            mobProxy.newPage(currentStep.getName());
        }
        if (videoRecorder != null) {
            CustomEventFiringWebDriver.displayStepOnScreen(currentStep.getName(), SeleniumTestsContextManager.getThreadContext().getRunMode(), SeleniumTestsContextManager.getThreadContext().getSeleniumGridConnector(), videoRecorder);
        }
        if (neoloadDriver != null) {
            neoloadDriver.startTransaction(currentStep.getName());
        }
    } else {
        TestStepManager.getParentTestStep().addStep(currentStep);
        previousParent = TestStepManager.getParentTestStep();
        TestStepManager.setParentTestStep(currentStep);
    }
    // set the start date once step is initialized so that when we get the video frame associated to step, step name displayed on screen is the same as the running step name
    currentStep.setStartDate();
    try {
        reply = joinPoint.proceed(joinPoint.getArgs());
    } catch (Throwable e) {
        currentStep.setFailed(true);
        currentStep.setActionException(e);
        // issue #287 (https://github.com/cbeust/testng/issues/2148): is method an @AfterMethod. Then do not rethrow exception
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        if (methodSignature.getMethod().getAnnotation(AfterMethod.class) != null) {
            scenarioLogger.error(String.format("Error in @AfterMethod %s: %s", methodSignature, e.getMessage()));
        } else {
            throw e;
        }
    } finally {
        if (rootStep) {
            TestStepManager.getCurrentRootTestStep().updateDuration();
            TestStepManager.logTestStep(TestStepManager.getCurrentRootTestStep());
            if (neoloadDriver != null) {
                neoloadDriver.stopTransaction();
            }
        } else {
            TestStepManager.setParentTestStep(previousParent);
        }
    }
    return reply;
}
Also used : TestStep(com.seleniumtests.reporter.logger.TestStep) MethodSignature(org.aspectj.lang.reflect.MethodSignature) NLWebDriver(com.neotys.selenium.proxies.NLWebDriver) ArrayList(java.util.ArrayList) BrowserMobProxy(net.lightbody.bmp.BrowserMobProxy) PageObject(com.seleniumtests.uipage.PageObject) VideoRecorder(com.seleniumtests.util.video.VideoRecorder) PageObject(com.seleniumtests.uipage.PageObject) TestAction(com.seleniumtests.reporter.logger.TestAction)

Aggregations

VideoRecorder (com.seleniumtests.util.video.VideoRecorder)7 File (java.io.File)4 TestStep (com.seleniumtests.reporter.logger.TestStep)3 Test (org.testng.annotations.Test)3 GenericTest (com.seleniumtests.GenericTest)2 MockitoTest (com.seleniumtests.MockitoTest)2 ScenarioException (com.seleniumtests.customexception.ScenarioException)2 WebUIDriver (com.seleniumtests.driver.WebUIDriver)2 ArrayList (java.util.ArrayList)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 NLWebDriver (com.neotys.selenium.proxies.NLWebDriver)1 BrowserInfo (com.seleniumtests.browserfactory.BrowserInfo)1 ConfigurationException (com.seleniumtests.customexception.ConfigurationException)1 ReporterTest (com.seleniumtests.it.reporter.ReporterTest)1 MultipleInfo (com.seleniumtests.reporter.info.MultipleInfo)1 TestAction (com.seleniumtests.reporter.logger.TestAction)1 PageObject (com.seleniumtests.uipage.PageObject)1 HeadlessException (java.awt.HeadlessException)1 Date (java.util.Date)1 BrowserMobProxy (net.lightbody.bmp.BrowserMobProxy)1