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);
}
}
}
}
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");
}
Aggregations