Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package com.microsoft.hydralab.t2c.runner;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.microsoft.hydralab.t2c.runner.controller.AndroidDriverController;
import com.microsoft.hydralab.t2c.runner.controller.BaseDriverController;
import com.microsoft.hydralab.t2c.runner.controller.EdgeDriverController;
Expand All @@ -12,6 +11,8 @@
import com.microsoft.hydralab.t2c.runner.elements.BaseElementInfo;
import com.microsoft.hydralab.t2c.runner.elements.EdgeElementInfo;
import com.microsoft.hydralab.t2c.runner.elements.WindowsElementInfo;
import com.microsoft.hydralab.t2c.runner.finder.ElementFinder;
import com.microsoft.hydralab.t2c.runner.finder.ElementFinderFactory;
import io.appium.java_client.android.nativekey.AndroidKey;
import org.jetbrains.annotations.NotNull;
import org.openqa.selenium.WebElement;
Expand All @@ -25,28 +26,14 @@ public class T2CAppiumUtils {
private static boolean isSelfTesting = false;

public static WebElement findElement(BaseDriverController driver, BaseElementInfo element, Logger logger) {
WebElement elementFinded = null;
WebElement elementFound = null;
Comment thread
TedaLIEz marked this conversation as resolved.
Resolved
if (element == null) return null;
Map<String, String> keyToVal = element.getBasisSearchedBy();
if (keyToVal.get("accessibilityId") != null && keyToVal.get("accessibilityId").length() != 0) {
elementFinded = driver.findElementByAccessibilityId(keyToVal.get("accessibilityId"));
if (elementFinded != null) {
return elementFinded;
}
}
if (keyToVal.get("text") != null && keyToVal.get("text").length() != 0) {
elementFinded = driver.findElementByName(keyToVal.get("text"));
if (elementFinded != null) {
return elementFinded;
}
}
if (keyToVal.get("xpath") != null && keyToVal.get("xpath").length() != 0) {
elementFinded = driver.findElementByXPath(keyToVal.get("xpath"));
if (elementFinded != null) {
return elementFinded;
}
ElementFinder<BaseElementInfo> finder = ElementFinderFactory.createElementFinder(driver);
elementFound = finder.findElement(element);
if (elementFound != null) {
return elementFound;
}
logger.warn("Page source: " + driver.webDriver.getPageSource());
logger.warn("Page source: " + driver.getPageSource());
throw new IllegalArgumentException("Element can not be found in current UI. Element info is " + element.getElementInfo());
}

Expand All @@ -61,7 +48,7 @@ public static void doAction(@NotNull BaseDriverController driver, @NotNull Actio
logger.error("doAction at " + index + ", description: " + description + ", with exception: " + e.getMessage());
if (!isOption) {
throw new IllegalStateException("Failed at " + index + ", description: " + description + ", " + e.getMessage()
+ ", page source: \n" + driver.webDriver.getPageSource(), e);
+ ", page source: \n" + driver.getPageSource(), e);
}
}
}
Expand Down Expand Up @@ -186,7 +173,7 @@ public static void chooseActionType(BaseDriverController driver, ActionInfo acti
} else if (toElementStr != null) {
BaseElementInfo toElementInfo;
if (driver instanceof AndroidDriverController) {
toElementInfo = AndroidElementInfo.getAndroidElementFromJson(JSONObject.parseObject(toElementStr));
toElementInfo = JSON.parseObject(toElementStr, AndroidElementInfo.class);
} else if (driver instanceof WindowsDriverController) {
toElementInfo = JSON.parseObject(toElementStr, WindowsElementInfo.class);
} else if (driver instanceof EdgeDriverController) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ private ArrayList<ActionInfo> getActionList(JSONArray caseJsonArray) {

if (elementInfo != null && !elementInfo.isEmpty()) {
if (driveIdToTypeMap.get(driverId).equals("android")) {
androidElement = AndroidElementInfo.getAndroidElementFromJson(elementInfo);
androidElement = JSON.parseObject(caseJsonObject.getString("elementInfo"), AndroidElementInfo.class);
actionInfo = new ActionInfo(i, androidElement, actionType, arguments, driverId, description, isOptional);
}
if (driveIdToTypeMap.get(driverId).equals("windows")) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -134,20 +134,13 @@ public void dragAndDropWithPosition(int fromX, int fromY, int toX, int toY) {
}

@Override
public WebElement findElementByName(String name) {
WebElement elementFound = null;
try {
elementFound = new WebDriverWait(webDriver, Duration.ofSeconds(10))
.until(driver -> driver.findElement(AppiumBy.xpath("//*[@text='" + name + "']")));
} catch (Exception e) {
logger.info("Can not find element by text: " + name);
}
return elementFound;
public void setClipboard(String text) {
androidDriver.setClipboardText(text);
}

@Override
public void setClipboard(String text) {
androidDriver.setClipboardText(text);
public String getPageSource() {
return androidDriver.getPageSource();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
// Licensed under the MIT License.
package com.microsoft.hydralab.t2c.runner.controller;

import com.microsoft.hydralab.t2c.runner.elements.AndroidElementInfo;
import com.microsoft.hydralab.t2c.runner.elements.WindowsElementInfo;
import io.appium.java_client.AppiumBy;
import io.appium.java_client.android.nativekey.AndroidKey;
import org.jetbrains.annotations.Nullable;
import org.openqa.selenium.Dimension;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
Expand All @@ -15,8 +18,8 @@

import java.time.Duration;

public class BaseDriverController {
public WebDriver webDriver;
public abstract class BaseDriverController {
protected WebDriver webDriver;
protected Logger logger;

public BaseDriverController(WebDriver webDriver, Logger logger) {
Expand Down Expand Up @@ -140,6 +143,7 @@ public void paste(WebElement webElement) {
public void setClipboard(String text) {
}

@Nullable
public WebElement findElementByAccessibilityId(String accessibilityId) {
WebElement elementFound = null;
try {
Expand All @@ -157,6 +161,7 @@ public WebElement apply(WebDriver input) {
return elementFound;
}

@Nullable
public WebElement findElementByXPath(String xpath) {
WebElement elementFound = null;
try {
Expand All @@ -168,14 +173,38 @@ public WebElement findElementByXPath(String xpath) {
return elementFound;
}

public WebElement findElementByName(String name) {
@Nullable
public WebElement findElementByText(String text) {
WebElement elementFound = null;
try {
elementFound = new WebDriverWait(webDriver, Duration.ofSeconds(10))
.until(driver -> driver.findElement(AppiumBy.name(name)));
.until(driver -> driver.findElement(AppiumBy.xpath("//*[@text='" + text + "']")));
Comment thread
TedaLIEz marked this conversation as resolved.
Resolved
} catch (Exception e) {
logger.info("Can not find element by name: " + name);
logger.info("Can not find element by text: " + text);
}
return elementFound;
}


/**
* In windows, id refers to {@link WindowsElementInfo#getName()}
* In android, id refers to {@link AndroidElementInfo#getResourceId()}
* @param id
* @return
*/
@Nullable
public WebElement findElementById(String id) {
WebElement elementFound = null;
try {
elementFound = new WebDriverWait(webDriver, Duration.ofSeconds(10))
.until(driver -> driver.findElement(AppiumBy.name(id)));
} catch (Exception e) {
logger.info("Can not find element by id: " + id);
}
return elementFound;
}


public abstract String getPageSource();

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import org.openqa.selenium.interactions.Sequence;
import org.slf4j.Logger;

import java.awt.*;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
Expand Down Expand Up @@ -49,6 +49,12 @@ public void setClipboard(String text) {
clipboard.setContents(selection, null);
}

@Override
public String getPageSource() {
return "Windows page: \n" + windowsDriver.getPageSource() + "\n Edge source: " + edgeDriver.getPageSource();
}


@Override
public void paste(WebElement webElement) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import org.openqa.selenium.interactions.Sequence;
import org.slf4j.Logger;

import java.awt.*;
import java.awt.Toolkit;
import java.awt.datatransfer.Clipboard;
import java.awt.datatransfer.DataFlavor;
import java.awt.datatransfer.StringSelection;
Expand Down Expand Up @@ -58,6 +58,12 @@ public void setClipboard(String text) {
clipboard.setContents(selection, null);
}

@Override
public String getPageSource() {
return windowsDriver.getPageSource();
}


@Override
public void paste(WebElement webElement) {
Clipboard clipboard = Toolkit.getDefaultToolkit().getSystemClipboard();
Expand Down
Loading