Skip to content

Instantly share code, notes, and snippets.

View henrrich's full-sized avatar

He Huang henrrich

View GitHub Profile
@henrrich
henrrich / RetryUtil.java
Created February 19, 2019 13:07
a java retry util implementation
import java.util.Optional;
import java.util.function.Function;
import java.util.function.Supplier;
public class RetryUtil {
private static final int TRY_MULTIPLE_TIMES_DEFAULT_SLEEP = 100;
public static class Backoff {
@henrrich
henrrich / convertCoordinates.java
Created October 18, 2017 12:05
appium code snippets for converting webelement coordinates inside webview to coordinates in native context
public Point convertWebviewLocationToTouchCoordinates(WebElement webViewElement) {
//Find the center point of element on webview
Point webviewLocation = webViewElement.getLocation();
Dimension size = webViewElement.getSize();
int elementWebviewCenterX = webviewLocation.getX() + Math.round(size.getWidth() / 2);
int elementWebviewCenterY = webviewLocation.getY() + Math.round(size.getHeight() / 2);
System.out.println("Element WebView Location: ( " + elementWebviewCenterX + ", " + elementWebviewCenterY + ")");
@henrrich
henrrich / ChangeAnnotationAtRuntime.java
Last active August 24, 2022 09:05
Modify annotation at runtime, java 8 support
public final class RuntimeAnnotations {
private static final Constructor<?> AnnotationInvocationHandler_constructor;
private static final Constructor<?> AnnotationData_constructor;
private static final Method Class_annotationData;
private static final Field Class_classRedefinedCount;
private static final Field AnnotationData_annotations;
private static final Field AnnotationData_declaredAnotations;
private static final Method Atomic_casAnnotationData;
private static final Class<?> Atomic_class;
@henrrich
henrrich / WaitUtil.java
Created August 11, 2016 06:44
Running task in seperated thread using executor framework and handles timeout
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.concurrent.*;
/**
* Created by hehuan on 10/08/2016.
*
* This class uses a single thread to execute the task assigned to it and wait for the result.
@henrrich
henrrich / SafariMoveToElement.java
Created August 8, 2016 13:07
move to element implementation using javascript for java webdriver for Safari
private static void moveToElementJs(WebDriver driver, WebElement element) {
String javaScript = "var evObj = document.createEvent('MouseEvents');" +
"evObj.initMouseEvent(\"mouseover\",true, false, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);" +
"arguments[0].dispatchEvent(evObj);";
((JavascriptExecutor)driver).executeScript(javaScript, element);
}
@henrrich
henrrich / InPortView.java
Created July 13, 2016 12:20
Implementation of webdriver inViewPort method in javascript.
/**
* This method is a replacement to the inViewPort method from selenium webdriver API.
* It uses javascript to scroll.
* For parameter details, check the link below:
* @see <a href="https://developer.mozilla.org/en-US/docs/Web/API/Element/scrollIntoView">Element.scrollIntoView()</a>
*/
public void inViewPort(boolean alignToTop, WebElement element) {
((JavascriptExecutor) driver).executeScript("arguments[0].scrollIntoView(" + alignToTop + ")", element);
}
@henrrich
henrrich / BrowserStackRemoteWebDriverCapabilityBuilder.java
Last active July 12, 2016 13:38
Webdriver capability builder classes for different browsers
// Running towards BrowserStack using remote webdriver
// decorator class on top of basic capability builder classes from different browsers
public class BrowserStackRemoteWebDriverCapabilityBuilder extends CapabilityConfigurator.CapabilityBuilderDecorator {
public BrowserStackRemoteWebDriverCapabilityBuilder(CapabilityConfigurator.CapabilityBuilder builder) {
super(builder);
}
@Override
public void buildDesiredCapabilities() {
@henrrich
henrrich / HighlightWebElement.java
Last active July 12, 2016 08:53
method to highlight webelement in yellow color
public void highlight(WebElement element, long highlightInterval) throws InterruptedException {
int flexibleWait = 5;
long pollingInterval = 500;
WebDriverWait wait = new WebDriverWait(ngDriver, flexibleWait);
wait.pollingEvery(pollingInterval, TimeUnit.MILLISECONDS);
wait.until(ExpectedConditions.visibilityOf(element));
executeScript("arguments[0].style.border='3px solid yellow'", element);
Thread.sleep(highlightInterval);
//executeScript("arguments[0].style.border=''", element);
}
@henrrich
henrrich / WaitForAngular.java
Last active July 12, 2016 13:44
webdriver, expected condition method to wait for angular requests to finish
public static ExpectedCondition<Boolean> angularHasFinishedProcessing() {
return new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
String returnValue = ((JavascriptExecutor) driver).executeScript(
"return (window.angular !== undefined) && (angular.element(document).injector() !== undefined) && " +
"(angular.element(document).injector().get('$http').pendingRequests.length === 0)").toString();
return Boolean.valueOf(returnValue);
}