use of com.seleniumtests.driver.CustomEventFiringWebDriver in project seleniumRobot by bhecquet.
the class ElementInfo method updateInfo.
/**
* Update information by calling the real element.
* @param htmlElement
* @param driver
*/
public void updateInfo(HtmlElement htmlElement) {
if (htmlElement.getRealElement() == null) {
throw new CustomSeleniumTestsException(String.format("Updating element information [%s] is not possible if real element has not yet been searched", name));
}
String newText = htmlElement.getRealElement().getText();
// depending on drivers, rect may raise an error
Rectangle newRectangle;
try {
newRectangle = htmlElement.getRealElement().getRect();
} catch (WebDriverException e) {
Point location = htmlElement.getRealElement().getLocation();
Dimension size = htmlElement.getRealElement().getSize();
newRectangle = new Rectangle(location, size);
}
String newB64Image = "";
String newTagName = "";
Map<String, Object> newAttributes = new HashMap<>();
// only capture picture in FULL mode
if (SeleniumTestsContextManager.getThreadContext().getAdvancedElementSearch() == Mode.FULL) {
try {
BufferedImage fullImg = getScreenshot();
// Get the location of htmlElement on the page
Point point = newRectangle.getPoint();
// Get width and height of the element
int eleWidth = newRectangle.getWidth();
int eleHeight = newRectangle.getHeight();
Point scrollPosition = ((CustomEventFiringWebDriver) htmlElement.getDriver()).getScrollPosition();
// Crop the entire page screenshot to get only element screenshot. Keep 20 px around the picture
BufferedImage eleScreenshot = ImageProcessor.cropImage(fullImg, Math.max(0, point.getX() - scrollPosition.getX() - 20), Math.max(0, point.getY() - scrollPosition.getY() - 20), Math.min(eleWidth + 40, fullImg.getWidth()), Math.min(eleHeight + 40, fullImg.getHeight()));
// for debug purpose
/*File tmp = File.createTempFile("screenshot", ".png");
tmp.deleteOnExit();
ImageIO.write(eleScreenshot, "png", tmp);*/
newB64Image = ImageProcessor.toBase64(eleScreenshot);
} catch (Exception e) {
logger.error("Error taking element screenshot", e);
}
}
if (SeleniumTestsContextManager.isWebTest()) {
newTagName = htmlElement.getRealElement().getTagName();
newAttributes = (Map<String, Object>) ((JavascriptExecutor) htmlElement.getDriver()).executeScript(JAVASCRIPT_GET_ATTRIBUTES, htmlElement.getRealElement());
}
// record stability information (is the information stable over time or not)
totalSearch += 1;
textStability = newText.equals(text) ? textStability + 1 : 0;
tagStability = newTagName.equals(tagName) ? tagStability + 1 : 0;
rectangleStability = newRectangle.equals(new Rectangle(coordX, coordY, height, width)) ? rectangleStability + 1 : 0;
for (Entry<String, Object> entryAttr : newAttributes.entrySet()) {
// attribute was unknown
if (!attributes.containsKey(entryAttr.getKey()) || !attributesStability.containsKey(entryAttr.getKey())) {
attributesStability.put(entryAttr.getKey(), 0);
} else // attribute is known but changed
if (attributes.containsKey(entryAttr.getKey())) {
if (!attributes.get(entryAttr.getKey()).equals(newAttributes.get(entryAttr.getKey()))) {
attributesStability.put(entryAttr.getKey(), 0);
} else {
attributesStability.put(entryAttr.getKey(), attributesStability.get(entryAttr.getKey()) + 1);
}
}
}
// reset indicators for attributes that are not found anymore
for (Entry<String, Object> entryAttr : attributes.entrySet()) {
if (!newAttributes.containsKey(entryAttr.getKey())) {
attributesStability.put(entryAttr.getKey(), 0);
}
}
// TODO: image
b64ImageStability = 0;
text = newText;
coordX = newRectangle.x;
coordY = newRectangle.y;
width = newRectangle.width;
height = newRectangle.height;
b64Image = newB64Image;
tagName = newTagName;
attributes = newAttributes;
lastUpdate = LocalDateTime.now();
}
use of com.seleniumtests.driver.CustomEventFiringWebDriver in project seleniumRobot by bhecquet.
the class HtmlElement method clickAt.
/**
* Click element in native way by Actions.
*
* <p/>
*
* <pre class="code">
* clickAt(1, 1);
* </pre>
*
* @param value
*/
@ReplayOnError(waitAfterAction = true)
public void clickAt(int xOffset, int yOffset) {
findElement();
((CustomEventFiringWebDriver) getDriver()).scrollToElement(getRealElementNoSearch(), yOffset);
outlineElement(getRealElementNoSearch());
try {
new Actions(getDriver()).moveToElement(getRealElementNoSearch(), xOffset, yOffset).click().perform();
} catch (InvalidElementStateException e) {
logger.error(e);
getRealElementNoSearch().click();
}
}
use of com.seleniumtests.driver.CustomEventFiringWebDriver in project seleniumRobot by bhecquet.
the class HtmlElement method clickMouse.
@ReplayOnError(waitAfterAction = true)
public void clickMouse() {
Rectangle viewportPosition = detectViewPortPosition();
// always scroll to element so that we can click on it with mouse
setScrollToElementBeforeAction(true);
findElement(true);
outlineElement(getRealElementNoSearch());
Rectangle elementRect = getRect();
Point scrollPosition = ((CustomEventFiringWebDriver) getDriver()).getScrollPosition();
CustomEventFiringWebDriver.leftClicOnDesktopAt(true, elementRect.x + elementRect.width / 2 + viewportPosition.x - scrollPosition.x, elementRect.y + elementRect.height / 2 + viewportPosition.y - scrollPosition.y, SeleniumTestsContextManager.getThreadContext().getRunMode(), SeleniumTestsContextManager.getThreadContext().getSeleniumGridConnector());
}
use of com.seleniumtests.driver.CustomEventFiringWebDriver in project seleniumRobot by bhecquet.
the class HtmlElement method findElement.
/**
* Finds the element using By type. Implicit Waits is built in createWebDriver()
* in WebUIDriver to handle dynamic element problem. This method is invoked
* before all the basic operations like click, sendKeys, getText, etc. Use
* waitForPresent to use Explicit Waits to deal with special element which needs
* long time to present.
*
* @param waitForVisibility wait for element to be visible
* @param makeVisible whether we try to make the element visible. Should
* be true except when trying to know if element is
* displayed
*/
public void findElement(boolean waitForVisibility, boolean makeVisible) {
// TODO:
// https://discuss.appium.io/t/how-can-i-scroll-to-an-element-in-appium-im-using-android-native-app/10618/14
// String DESTINATION_ELEMENT_TEXT= "KUBO";
// ((AndroidDriver) driver).findElementByAndroidUIAutomator("new
// UiScrollable(new UiSelector())
// .scrollIntoView(new UiSelector().text(DESTINATION_ELEMENT_TEXT))");
ElementInfo elementInfo = null;
// search element information. Do not stop if something goes wrong here
if (SeleniumTestsContextManager.getThreadContext().getAdvancedElementSearch() != ElementInfo.Mode.FALSE) {
try {
elementInfo = ElementInfo.getInstance(this);
} catch (Exception e) {
logger.info("Error getting element info");
}
}
// if a parent is defined, search for it before getting the sub element
setDriver(updateDriver());
if (parent != null) {
parent.findElement(false, false);
// inside a parent
if (by instanceof ByXPath) {
try {
Field xpathExpressionField = ByXPath.class.getDeclaredField("xpathExpression");
xpathExpressionField.setAccessible(true);
String xpath = (String) xpathExpressionField.get(by);
if (xpath.startsWith("//")) {
by = By.xpath("." + xpath);
}
} catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
throw new CustomSeleniumTestsException(e);
}
}
setElement(findSeleniumElement(parent.getRealElementNoSearch(), elementInfo));
} else {
setElement(findSeleniumElement(getDriver(), elementInfo));
}
if (makeVisible) {
makeWebElementVisible(getRealElementNoSearch());
if (scrollToElementBeforeAction) {
((CustomEventFiringWebDriver) getDriver()).scrollToElement(getRealElementNoSearch(), OPTIMAL_SCROLLING);
}
}
// element
if (waitForVisibility && makeVisible) {
try {
new WebDriverWait(getDriver(), 1).until(ExpectedConditions.visibilityOf(getRealElementNoSearch()));
} catch (TimeoutException e) {
logger.error(String.format("Element %s has never been made visible", toString()));
}
}
// If we are here, element has been found, update elementInformation
if (elementInfo != null) {
try {
elementInfo.updateInfo(this);
elementInfo.exportToJsonFile(false, this);
} catch (Exception e) {
logger.warn("Error storing element information: " + e.getMessage());
}
}
}
use of com.seleniumtests.driver.CustomEventFiringWebDriver in project seleniumRobot by bhecquet.
the class HtmlElement method simulateClick.
/**
* Click with javascript
*/
@ReplayOnError(waitAfterAction = true)
public void simulateClick() {
if (SeleniumTestsContextManager.isWebTest()) {
((CustomEventFiringWebDriver) updateDriver()).updateWindowsHandles();
}
findElement(true);
outlineElement(getRealElementNoSearch());
DriverConfig driverConfig = WebUIDriver.getWebUIDriver(false).getConfig();
String mouseOverScript;
if ((driverConfig.getBrowserType() == BrowserType.FIREFOX && FirefoxDriverFactory.isMarionetteMode()) || driverConfig.getBrowserType() == BrowserType.EDGE || (driverConfig.getBrowserType() == BrowserType.CHROME && driverConfig.getMajorBrowserVersion() >= 75)) {
mouseOverScript = "var event = new MouseEvent('mouseover', {view: window, bubbles: true, cancelable: true}) ; arguments[0].dispatchEvent(event);";
} else {
mouseOverScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('mouseover', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onmouseover');}";
}
executeScript(mouseOverScript, getRealElementNoSearch());
WaitHelper.waitForSeconds(2);
String clickScript = "";
if ((driverConfig.getBrowserType() == BrowserType.FIREFOX && FirefoxDriverFactory.isMarionetteMode()) || driverConfig.getBrowserType() == BrowserType.EDGE || (driverConfig.getBrowserType() == BrowserType.CHROME && driverConfig.getMajorBrowserVersion() >= 75)) {
clickScript = "var event = new MouseEvent('click', {view: window, bubbles: true, cancelable: true}) ;" + "arguments[0].dispatchEvent(event);";
} else {
clickScript = "if(document.createEvent){var evObj = document.createEvent('MouseEvents');evObj.initEvent('click', true, false); arguments[0].dispatchEvent(evObj);} else if(document.createEventObject) { arguments[0].fireEvent('onclick');}";
}
executeScript(clickScript, getRealElementNoSearch());
WaitHelper.waitForSeconds(2);
}
Aggregations