我正在尝试建立一种方法来向下或向上滑动移动网页/小部件以搜索目标元素。当其到达边界并确定视图未更改时,滑动方向会反转并继续搜索目标,或者尝试次数已用完。由于Android尚没有简便的方法来确定它是否已经达到边界,因此,我尝试通过在视图中找到活动元素来解决该解决方案,并在每次滑动后通过当前视图和以前使用switchTo()。activeElement()查看的元素。我通过Element.toString(),toJson(),hashCode()和getLocation()进行了比较。当然,使用switchTo()。activeElement()的当前问题是,如果它不能集中于View中的任何元素,则将其分配给网页/小部件的Body元素,使其与自身进行比较。
我想知道在测试过程中是否曾尝试过创建一个MobileElement并将其插入到网页/小部件中,或者是否创建了某种“粘滞点”来标记屏幕上的视图是否基于Point的位置发生了变化点。显然,不建议使用这种方法来创建和添加元素并将其附加到网页/小部件本身,但是目前,如果activeElement()是一个元素占据整个移动屏幕。
我不确定是否有人尝试过此方法,或者该学科是否使用了不同的术语,因为我的研究很少或没有,但是我很愿意提出这个案例,如果以前曾提出过。 / p>
我的搜索所用的当前时间:
void swipeUntilFound(By method, int attempts, MobileElement targetElement) {
int limitCount = 0;
double startScrollPoint = 0.8;
double endScrollPoint = 0.25;
MobileElement lastViewPoint = null;
MobileElement currentViewPoint;
int lastViewPointPosition = 0;
while (limitCount < attempts || driver.findElements(method).isEmpty()) {
if(lastViewPoint != null) {
lastViewPointPosition = lastViewPoint.getLocation().getY();
}
swipeVertical(startScrollPoint, endScrollPoint, 3000);
currentViewPoint = (MobileElement) driver.switchTo().activeElement();
try {
System.out.println("last Element in View: " + lastViewPoint.hashCode() + "Location: " + lastViewPoint.getLocation());
System.out.println("Current Element in View: " + currentViewPoint.hashCode() + "Location: " + currentViewPoint.getLocation());
}
catch (NullPointerException e) {
}
if(lastViewPoint == null || lastViewPoint.hashCode() != currentViewPoint.hashCode()) {
System.out.println("Last and Current don't match.");
lastViewPoint = currentViewPoint;
}
else if( lastViewPointPosition == lastViewPoint.getLocation().getY() ||
lastViewPoint.hashCode() == currentViewPoint.hashCode()) {
System.out.println("Last and Current match");
double temp = startScrollPoint;
startScrollPoint = endScrollPoint;
endScrollPoint = temp;
}
limitCount++;
}
if(limitCount == attempts) {
System.out.println("Unable to find Specified Element.");
}else {
targetElement = driver.findElement(method);
System.out.println("Element " + method.toString() + " was found.");
}
}