Search in sources :

Example 1 with DriverUsage

use of com.seleniumtests.core.StatisticsStorage.DriverUsage in project seleniumRobot by bhecquet.

the class CustomEventFiringWebDriver method quit.

/**
 * After quitting driver, if it fails, some pids may remain. Kill them
 */
@Override
public void quit() {
    // get list of pids we could have to kill. Sometimes, Chrome does not close all its processes
    // so we have to know which processes to kill when driver is still active
    List<Long> pidsToKill = new ArrayList<>();
    if (browserInfo != null && driverMode == DriverMode.LOCAL) {
        pidsToKill.addAll(browserInfo.getAllBrowserSubprocessPids(driverPids));
        // issue #401: also get the browser process in case of attached browser (chrome only)
        if (attachExistingDriverPort != null && attachExistingDriverPort > 1000) {
            Integer browserProcess = OSUtilityFactory.getInstance().getProcessIdByListeningPort(attachExistingDriverPort);
            if (browserProcess != null) {
                pidsToKill.add(browserProcess.longValue());
            }
        }
    }
    Capabilities caps;
    try {
        caps = getCapabilities();
    } catch (WebDriverException e) {
        caps = new MutableCapabilities();
    }
    // close windows before quitting (this is the only way to close chrome attached browser when it's not started by selenium)
    try {
        List<String> handles = new ArrayList<>(driver.getWindowHandles());
        Collections.reverse(handles);
        for (String handle : handles) {
            driver.switchTo().window(handle);
            driver.close();
        }
    } catch (Exception e) {
    // nothing to do
    }
    Long duration = 0L;
    try {
        duration = new Date().getTime() - (Long) internalCapabilities.getCapability(DriverUsage.START_TIME);
    } catch (Exception e) {
    // catch error
    }
    String gridHub = caps.getCapability(DriverUsage.GRID_HUB) != null ? caps.getCapability(DriverUsage.GRID_HUB).toString() : null;
    String sessionId = caps.getCapability(DriverUsage.SESSION_ID) != null ? caps.getCapability(DriverUsage.SESSION_ID).toString() : null;
    // store driver stats
    DriverUsage usage = new DriverUsage(gridHub, (String) caps.getCapability(DriverUsage.GRID_NODE), (Long) internalCapabilities.getCapability(DriverUsage.START_TIME), duration, sessionId, caps.getBrowserName(), (Long) internalCapabilities.getCapability(DriverUsage.STARTUP_DURATION), (String) internalCapabilities.getCapability(DriverUsage.TEST_NAME));
    StatisticsStorage.addDriverUsage(usage);
    try {
        driver.quit();
    } catch (WebDriverException e) {
        caps = new MutableCapabilities();
        logger.error("Error while quitting driver: " + e.getMessage());
    } finally {
        // wait for browser processes to stop
        WaitHelper.waitForSeconds(2);
        // only kill processes in local mode
        if (!pidsToKill.isEmpty()) {
            for (Long pid : pidsToKill) {
                OSUtilityFactory.getInstance().killProcess(pid.toString(), true);
            }
        }
    }
}
Also used : MutableCapabilities(org.openqa.selenium.MutableCapabilities) Capabilities(org.openqa.selenium.Capabilities) HasCapabilities(org.openqa.selenium.HasCapabilities) MutableCapabilities(org.openqa.selenium.MutableCapabilities) ArrayList(java.util.ArrayList) DriverUsage(com.seleniumtests.core.StatisticsStorage.DriverUsage) HeadlessException(java.awt.HeadlessException) ScenarioException(com.seleniumtests.customexception.ScenarioException) JavascriptException(org.openqa.selenium.JavascriptException) NoSuchSessionException(org.openqa.selenium.NoSuchSessionException) AWTException(java.awt.AWTException) WebDriverException(org.openqa.selenium.WebDriverException) UnhandledAlertException(org.openqa.selenium.UnhandledAlertException) IOException(java.io.IOException) WebSessionEndedException(com.seleniumtests.customexception.WebSessionEndedException) NoSuchWindowException(org.openqa.selenium.NoSuchWindowException) Date(java.util.Date) WebDriverException(org.openqa.selenium.WebDriverException)

Example 2 with DriverUsage

use of com.seleniumtests.core.StatisticsStorage.DriverUsage in project seleniumRobot by bhecquet.

the class TestWebUIDriver method testCapsOnDriverQuit.

/**
 * Check that usage duration is added on driver quit
 */
@Test(groups = { "ut" })
public void testCapsOnDriverQuit() {
    SeleniumTestsContextManager.getThreadContext().setBrowser("htmlunit");
    CustomEventFiringWebDriver driver = (CustomEventFiringWebDriver) WebUIDriver.getWebDriver(true);
    driver.quit();
    List<DriverUsage> driverUsages = StatisticsStorage.getDriverUsage();
    Assert.assertEquals(driverUsages.size(), 1);
    Assert.assertEquals(driverUsages.get(0).getBrowserName(), "htmlunit");
    Assert.assertTrue(driverUsages.get(0).getDuration() > 0.0);
    Assert.assertNull(driverUsages.get(0).getGridHub());
    Assert.assertNull(driverUsages.get(0).getGridNode());
    Assert.assertTrue(driverUsages.get(0).getStartTime() > 0);
    Assert.assertEquals(driverUsages.get(0).getTestName(), "testCapsOnDriverQuit");
}
Also used : CustomEventFiringWebDriver(com.seleniumtests.driver.CustomEventFiringWebDriver) DriverUsage(com.seleniumtests.core.StatisticsStorage.DriverUsage) Test(org.testng.annotations.Test) GenericTest(com.seleniumtests.GenericTest) MockitoTest(com.seleniumtests.MockitoTest) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest)

Aggregations

DriverUsage (com.seleniumtests.core.StatisticsStorage.DriverUsage)2 GenericTest (com.seleniumtests.GenericTest)1 MockitoTest (com.seleniumtests.MockitoTest)1 ScenarioException (com.seleniumtests.customexception.ScenarioException)1 WebSessionEndedException (com.seleniumtests.customexception.WebSessionEndedException)1 CustomEventFiringWebDriver (com.seleniumtests.driver.CustomEventFiringWebDriver)1 AWTException (java.awt.AWTException)1 HeadlessException (java.awt.HeadlessException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 Capabilities (org.openqa.selenium.Capabilities)1 HasCapabilities (org.openqa.selenium.HasCapabilities)1 JavascriptException (org.openqa.selenium.JavascriptException)1 MutableCapabilities (org.openqa.selenium.MutableCapabilities)1 NoSuchSessionException (org.openqa.selenium.NoSuchSessionException)1 NoSuchWindowException (org.openqa.selenium.NoSuchWindowException)1 UnhandledAlertException (org.openqa.selenium.UnhandledAlertException)1 WebDriverException (org.openqa.selenium.WebDriverException)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1