我正在尝试为我们构建的SDK创建自动化的E2E测试。我们决定用一个测试应用程序来测试SDK,该应用程序将调用表中的命令,测试所有SDK功能。
我被困了一段时间,不明白为什么有些点击事件是有效的,而有些则是无效的。
我已经知道了这一点,这是因为那些正在工作的是可见的,而那些不工作的是不可见的(在创建单元时才真正存在?)。在任何情况下,这都是我所想或希望的,因为如果没有,我真的不明白发生了什么。
正在工作的代码示例:
@Test
public void step2_resetToken() throws InterruptedException {
int count = 0;
String s;
do {
MobileElement mElement = (MobileElement) iosDriver.findElement(By.name("Reset Token"));
mElement.click();
Thread.sleep(500);
count++;
assert count < TIMEOUTTIME;
s = element.getText();
System.out.println(s);
} while(!(element.getText().contains("reset token performed:")));
}不工作的代码示例:
@Test
public void step3_isAuthenticatedByPIN() throws InterruptedException {
WebElement tableView = (WebElement) iosDriver.findElementByClassName("XCUIElementTypeTable");
tableView.scrollTo("isAuthenticatedByPIN").click();
int count = 0;
String s;
do {
////XCUIElementTypeStaticText[@name="isAuthenticatedByPIN"]
MobileElement mElement = (MobileElement) iosDriver.findElementByClassName("XCUIElementTypeTable");
mElement.sendKeys("isAuthenticatedByPIN");
mElement.findElement(By.name("isAuthenticatedByPIN"));
mElement.click();
Thread.sleep(500);
count++;
assert count < TIMEOUTTIME;
s = element.getText();
System.out.println(s);
} while(!(element.getText().contains("isAuthenticatedByPIN: YES")));
}我以前也尝试过调用这个方法,所以会有一个滚动,但是它似乎找不到在应用程序启动时不可见的元素:
public static void scrolltoXPath(RemoteWebDriver driver, String xPath) {
RemoteWebElement parent = (RemoteWebElement)driver.findElement(By.className("XCUIElementTypeTable"));
String parentID = parent.getId();
HashMap<String, String> scrollObject = new HashMap<String, String>();
scrollObject.put("element", parentID);
scrollObject.put("name", "isConnected");
driver.executeScript("mobile:scroll", scrollObject);
}这是一个用scrollTo尝试的测试,但是Javascript不识别scrollTo (这是scrollTo或Javascript方法)。此外,我还尝试了在谷歌中找到的所有其他方法,但都搞不清楚这一点。。
谢谢。
发布于 2018-09-13 05:13:42
不能单击屏幕中未显示的元素。
您需要滚动到元素,然后才能单击该元素。你可以以不同的方式滚动。您可以使用协调滚动。您还可以使用文本、id、cont等来滚动。
用坐标滑动屏幕
import io.appium.java_client.TouchAction;
import io.appium.java_client.touch.WaitOptions;
import io.appium.java_client.touch.offset.PointOption;
import java.util.concurrent.TimeUnit;
import static java.time.Duration.ofSeconds;
TouchAction action = new TouchAction(driver);
action.press(PointOption.point(115, 650)).waitAction(WaitOptions.waitOptions(ofSeconds(1)))
.moveTo(PointOption.point(115, 350)).release().perform();使用cont-desc滚动屏幕
public static MobileElement scrollElementByContentDesc(String scrollableList, String uiClassSelector, String contentDesc) {
return driver.findElement(MobileBy.AndroidUIAutomator(
"new UiScrollable(new UiSelector().resourceId(\"" + scrollableList + "\"))" +
".getChildByDescription(new UiSelector().className(\"" + uiClassSelector + "\"), \"" + contentDesc + "\")"));
}有关appium滚动策略的更多信息,请遵循本教程
https://stackoverflow.com/questions/52291653
复制相似问题