Skip to content

Commit

Permalink
Merge pull request #341 from telekom/feature/uielement-highlighter
Browse files Browse the repository at this point in the history
Feature/uielement highlighter
  • Loading branch information
martingrossmann authored Aug 15, 2023
2 parents 99f0b16 + d2ed8bd commit b1e073b
Show file tree
Hide file tree
Showing 7 changed files with 197 additions and 160 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,19 @@
*/
package eu.tsystems.mms.tic.testframework.webdrivermanager;

import eu.tsystems.mms.tic.testframework.common.Testerra;
import eu.tsystems.mms.tic.testframework.constants.JSMouseAction;
import eu.tsystems.mms.tic.testframework.internal.StopWatch;
import eu.tsystems.mms.tic.testframework.logging.Loggable;
import eu.tsystems.mms.tic.testframework.pageobjects.UiElement;
import eu.tsystems.mms.tic.testframework.pageobjects.UiElementHighlighter;
import eu.tsystems.mms.tic.testframework.utils.JSUtils;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;

import java.awt.Color;

public final class DesktopWebDriverUtils implements Loggable {

Expand All @@ -35,26 +45,92 @@ public DesktopWebDriverUtils() {

public void clickAbsolute(UiElement guiElement) {
log().debug("Absolute navigation and click on: " + guiElement.toString());
guiElement.findWebElement(webElement -> utils.clickAbsolute(guiElement.getWebDriver(), webElement));
WebDriver driver = guiElement.getWebDriver();
guiElement.findWebElement(webElement -> {
// utils.clickAbsolute(guiElement.getWebDriver(), webElement);
// Start the StopWatch for measuring the loading time of a Page
StopWatch.startPageLoad(driver);

Point point = webElement.getLocation();

Actions action = new Actions(driver);

// goto 0,0
action.moveToElement(webElement, 1 + -point.getX(), 1 + -point.getY());

// move y, then x
action.moveByOffset(0, point.getY()).moveByOffset(point.getX(), 0);

// move to webElement
action.moveToElement(webElement);
action.moveByOffset(1, 1);
action.click().perform();
});
}

public void mouseOverAbsolute2Axis(UiElement guiElement) {
guiElement.findWebElement(webElement -> utils.mouseOverAbsolute2Axis(guiElement.getWebDriver(), webElement));
WebDriver driver = guiElement.getWebDriver();
guiElement.findWebElement(webElement -> {
// utils.mouseOverAbsolute2Axis(guiElement.getWebDriver(), webElement);
demoMouseOver(driver, webElement);
Actions action = new Actions(driver);

Point point = webElement.getLocation();

// goto 0,0
action.moveToElement(webElement, 1 + -point.getX(), 1 + -point.getY()).perform();

// move y, then x
action.moveByOffset(0, point.getY()).moveByOffset(point.getX(), 0).perform();

// move to webElement
action.moveToElement(webElement).perform();
});
}

public void mouseOverJS(UiElement guiElement) {
guiElement.findWebElement(webElement -> utils.mouseOver(guiElement.getWebDriver(), webElement));
WebDriver driver = guiElement.getWebDriver();
guiElement.findWebElement(webElement -> {
// utils.mouseOver(guiElement.getWebDriver(), webElement);
demoMouseOver(driver, webElement);

final String script = "var fireOnThis = arguments[0];"
+ "var evObj = document.createEvent('MouseEvents');"
+ "evObj.initEvent( 'mouseover', true, true );"
+ "fireOnThis.dispatchEvent(evObj);";
JSUtils.executeScriptWOCatch(driver, script, webElement);
});
}

public void clickJS(UiElement guiElement) {
guiElement.findWebElement(webElement -> utils.click(guiElement.getWebDriver(), webElement));
WebDriver driver = guiElement.getWebDriver();
guiElement.findWebElement(webElement -> {
JSUtils.executeScriptWOCatch(driver, "arguments[0].click();", webElement);
});
}

public void rightClickJS(UiElement guiElement) {
guiElement.findWebElement(webElement -> utils.rightClick(guiElement.getWebDriver(), webElement));
WebDriver driver = guiElement.getWebDriver();
guiElement.findWebElement(webElement -> {
final String script = "var element = arguments[0];" +
"var e = element.ownerDocument.createEvent('MouseEvents');" +
"e.initMouseEvent('contextmenu', true, true, element.ownerDocument.defaultView, 1, 0, 0, 0, 0, false,false, false, false,2, null);" +
"return !element.dispatchEvent(e);";
JSUtils.executeScriptWOCatch(driver, script, webElement);
});
}

public void doubleClickJS(UiElement guiElement) {
guiElement.findWebElement(webElement -> utils.doubleClick(guiElement.getWebDriver(), webElement));
guiElement.findWebElement(webElement -> {
Point location = webElement.getLocation();
JSUtils.executeJavaScriptMouseAction(guiElement.getWebDriver(), webElement, JSMouseAction.DOUBLE_CLICK, location.getX(), location.getY());
});
}

private void demoMouseOver(final WebDriver webDriver, final WebElement webElement) {
if (Testerra.Properties.DEMO_MODE.asBool()) {
UiElementHighlighter elementHighlighter = Testerra.getInjector().getInstance(UiElementHighlighter.class);
elementHighlighter.highlight(webDriver, webElement, new Color(255, 255, 0));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
import eu.tsystems.mms.tic.testframework.listeners.ShutdownSessionsListener;
import eu.tsystems.mms.tic.testframework.listeners.WatchdogStartupListener;
import eu.tsystems.mms.tic.testframework.pageobjects.DefaultUiElementFactory;
import eu.tsystems.mms.tic.testframework.pageobjects.DefaultUiElementHighlighter;
import eu.tsystems.mms.tic.testframework.pageobjects.UiElementHighlighter;
import eu.tsystems.mms.tic.testframework.pageobjects.internal.AriaElementLocator;
import eu.tsystems.mms.tic.testframework.pageobjects.internal.DefaultPageFactory;
import eu.tsystems.mms.tic.testframework.pageobjects.internal.DefaultUiElementFinderFactory;
Expand Down Expand Up @@ -75,6 +77,7 @@ protected void configure() {

// Instances
bind(BrowserInformation.class).to(UapBrowserInformation.class);
bind(UiElementHighlighter.class).to(DefaultUiElementHighlighter.class);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* Testerra
*
* (C) 2023, Martin Großmann, Deutsche Telekom MMS GmbH, Deutsche Telekom AG
*
* Deutsche Telekom AG and all other contributors /
* copyright owners license this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package eu.tsystems.mms.tic.testframework.pageobjects;

/**
* Default implementation for UiElement highlighting
* <p>
* highlight.js added an outlined border to the WebElement
*
* @author mgn
*/
public class DefaultUiElementHighlighter implements UiElementHighlighter {
@Override
public String getJSSnippet() {
return "snippets/highlight.js";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
/*
* Testerra
*
* (C) 2023, Martin Großmann, Deutsche Telekom MMS GmbH, Deutsche Telekom AG
*
* Deutsche Telekom AG and all other contributors /
* copyright owners license this file to you under the Apache
* License, Version 2.0 (the "License"); you may not use this
* file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package eu.tsystems.mms.tic.testframework.pageobjects;

import eu.tsystems.mms.tic.testframework.common.Testerra;
import eu.tsystems.mms.tic.testframework.logging.Loggable;
import eu.tsystems.mms.tic.testframework.utils.JSUtils;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

import java.awt.Color;
import java.util.stream.Stream;

/**
* Created on 2023-07-07
*
* @author mgn
*/
public interface UiElementHighlighter extends Loggable {

String getJSSnippet();

default void highlight(WebDriver driver, WebElement webElement, Color color) {
long timeout = Testerra.Properties.DEMO_MODE_TIMEOUT.asLong();
try {
JSUtils.executeScriptWOCatch(
driver,
String.format(
"%s\n" +
"ttHighlight(arguments[0], '%s', %d)",
JSUtils.readScriptResources(Stream.of(getJSSnippet())),
toHex(color),
timeout
),
webElement
);
} catch (Exception e) {
log().error("Unable to highlight WebElement: {}", e.getMessage());
}
}

default String toHex(Color color) {
return String.format("#%02x%02x%02x%02x", color.getRed(), color.getGreen(), color.getBlue(), color.getAlpha());
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import eu.tsystems.mms.tic.testframework.internal.Timings;
import eu.tsystems.mms.tic.testframework.logging.Loggable;
import eu.tsystems.mms.tic.testframework.pageobjects.Locator;
import eu.tsystems.mms.tic.testframework.pageobjects.UiElementHighlighter;
import eu.tsystems.mms.tic.testframework.pageobjects.internal.DefaultLocator;
import eu.tsystems.mms.tic.testframework.pageobjects.internal.WebElementRetainer;
import eu.tsystems.mms.tic.testframework.utils.JSUtils;
Expand Down Expand Up @@ -536,8 +537,8 @@ public void hover() {
}

protected void highlightWebElement(WebElement webElement, Color color) {
JSUtils utils = new JSUtils();
utils.highlight(guiElementData.getWebDriver(), webElement, color);
UiElementHighlighter instance = Testerra.getInjector().getInstance(UiElementHighlighter.class);
instance.highlight(guiElementData.getWebDriver(), webElement, color);
}

/**
Expand Down
Loading

0 comments on commit b1e073b

Please sign in to comment.