use of com.seleniumtests.driver.CustomEventFiringWebDriver in project seleniumRobot by bhecquet.
the class ScreenshotUtil method capturePage.
/**
* Capture current page (either web or app page)
* This is a wrapper around the selenium screenshot capability
* @return
*/
public BufferedImage capturePage(int cropTop, int cropBottom) {
if (driver == null) {
return null;
}
try {
// Don't capture snapshot for htmlunit
if (uiDriver != null && uiDriver.getConfig().getBrowserType() == BrowserType.HTMLUNIT) {
return null;
}
TakesScreenshot screenShot = (TakesScreenshot) driver;
// android does not support screenshot from webview context, switch temporarly to native_app context to take screenshot
if (uiDriver != null && uiDriver.getConfig().getBrowserType() == BrowserType.BROWSER) {
((AndroidDriver<WebElement>) ((CustomEventFiringWebDriver) driver).getWebDriver()).context("NATIVE_APP");
}
String screenshotB64 = screenShot.getScreenshotAs(OutputType.BASE64);
if (uiDriver != null && uiDriver.getConfig().getBrowserType() == BrowserType.BROWSER) {
((AndroidDriver<WebElement>) ((CustomEventFiringWebDriver) driver).getWebDriver()).context("WEBVIEW");
}
BufferedImage capturedImage = ImageProcessor.loadFromB64String(screenshotB64);
// crop capture by removing headers
if (cropTop >= 0 && cropBottom >= 0) {
// in case driver already capture the whole content, do not crop anything as cropping is used to remove static headers when scrolling
Dimension contentDimension = ((CustomEventFiringWebDriver) driver).getContentDimension();
if (capturedImage.getWidth() == contentDimension.width && capturedImage.getHeight() == contentDimension.height) {
return capturedImage;
}
Dimension dimensions = ((CustomEventFiringWebDriver) driver).getViewPortDimensionWithoutScrollbar();
capturedImage = ImageProcessor.cropImage(capturedImage, 0, cropTop, dimensions.getWidth(), dimensions.getHeight() - cropTop - cropBottom);
}
return capturedImage;
} catch (Exception ex) {
// Ignore all exceptions
logger.error("capturePageScreenshotToString: ", ex);
}
return null;
}
use of com.seleniumtests.driver.CustomEventFiringWebDriver in project seleniumRobot by bhecquet.
the class ScreenshotUtil method capture.
/**
* Capture a picture
* @param target which picture to take, screen or page.
* @param exportClass The type of export to perform (File, ScreenShot, String, BufferedImage)
* @param allWindows if true, will take a screenshot for all windows (only available for browser capture)
* @param force force capture even if set to false in SeleniumTestContext. This allows PictureElement and ScreenZone to work
* @param scrollDelay time in ms between the scrolling (when it's needed) and effective capture. A higher value means we have chance all picture have been loaded (with progressive loading)
* but capture take more time
* @return The image in the requested format
*/
public <T extends Object> List<T> capture(SnapshotTarget target, Class<T> exportClass, boolean allWindows, boolean force, int scrollDelay) {
if (!force && (SeleniumTestsContextManager.getThreadContext() == null || outputDirectory == null || !SeleniumTestsContextManager.getThreadContext().getCaptureSnapshot())) {
return new ArrayList<>();
}
LocalDateTime start = LocalDateTime.now();
List<NamedBufferedImage> capturedImages = captureAllImages(target, allWindows, scrollDelay);
// back to page top
try {
if (target.isPageTarget()) {
((CustomEventFiringWebDriver) driver).scrollTop();
}
} catch (WebDriverException e) {
// ignore errors here.
// com.seleniumtests.it.reporter.TestTestLogging.testManualSteps() with HTMLUnit driver
// org.openqa.selenium.WebDriverException: Can't execute JavaScript before a page has been loaded!
}
return exportBufferedImages(exportClass, start, capturedImages);
}
use of com.seleniumtests.driver.CustomEventFiringWebDriver in project seleniumRobot by bhecquet.
the class CompositeActions method updateHandles.
/**
* Update window handles when a click is requested in a composite Action (to get the same behavior between native clicks
* and clicks in CompositeAction
* Capture is done on all Action sub-classes, else it would never be done
*
* TO KEEP until ClickAction and other equivalents are there in selenium code
*
* @param joinPoint
* @throws SecurityException
* @throws NoSuchFieldException
* @throws IllegalAccessException
* @throws IllegalArgumentException
*/
@Before("call(public void org.openqa.selenium.interactions.Action+.perform ())")
public void updateHandles(JoinPoint joinPoint) throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
if (!(joinPoint.getTarget() instanceof CompositeAction)) {
return;
}
CompositeAction compositeAction = (CompositeAction) joinPoint.getTarget();
Field actionListField = CompositeAction.class.getDeclaredField("actionsList");
actionListField.setAccessible(true);
@SuppressWarnings("unchecked") List<Action> actionsList = (List<Action>) actionListField.get(compositeAction);
boolean clickRequested = false;
for (Action action : actionsList) {
if (action instanceof ClickAction) {
clickRequested = true;
}
}
if (clickRequested) {
((CustomEventFiringWebDriver) WebUIDriver.getWebDriver(false)).updateWindowsHandles();
}
}
use of com.seleniumtests.driver.CustomEventFiringWebDriver in project seleniumRobot by bhecquet.
the class CompositeActions method updateWindowHandles.
/**
* Analyse action list and determine if a clic action occured. If it's the case, update window handles
* @param actionsList
* @throws NoSuchFieldException
* @throws IllegalAccessException
*/
private void updateWindowHandles(LinkedList<Interaction> actionsList) throws NoSuchFieldException, IllegalAccessException {
Boolean clic = null;
boolean clickRequested = false;
for (Interaction action : actionsList) {
if (action.getClass().getName().contains("PointerInput$PointerPress")) {
Field buttonField = action.getClass().getDeclaredField("button");
buttonField.setAccessible(true);
int button = buttonField.getInt(action);
Field directionField = action.getClass().getDeclaredField("direction");
directionField.setAccessible(true);
String direction = directionField.get(action).toString();
// only left button
if (button != 0) {
clic = null;
continue;
}
// check we have a DOWN -> UP sequence
if ("DOWN".equals(direction) && clic == null) {
clic = true;
} else if (Boolean.TRUE.equals(clic) && "UP".equals(direction)) {
clic = null;
clickRequested = true;
} else {
clic = null;
}
} else {
clic = null;
}
}
if (clickRequested) {
((CustomEventFiringWebDriver) WebUIDriver.getWebDriver(false)).updateWindowsHandles();
}
}
use of com.seleniumtests.driver.CustomEventFiringWebDriver in project seleniumRobot by bhecquet.
the class Element method checkForMobile.
/**
* Check if the current platform is a mobile platform
* if it's the case, search for the element, else, raise a ScenarioException
*/
protected PerformsTouchActions checkForMobile() {
CustomEventFiringWebDriver driver = (CustomEventFiringWebDriver) WebUIDriver.getWebDriver(false);
if (driver == null) {
throw new ScenarioException("Driver has not already been created");
}
if (!SeleniumTestsContextManager.isMobileTest()) {
throw new ScenarioException("action is available only for mobile platforms");
}
if (!(driver.getWebDriver() instanceof AppiumDriver<?>)) {
throw new ScenarioException("action is available only for mobile platforms");
}
findElement(true);
return (PerformsTouchActions) driver.getWebDriver();
}
Aggregations