【发布时间】:2019-07-10 00:53:26
【问题描述】:
在我们的移动应用程序中,我们有几个屏幕,我们需要在屏幕上滑动以查看元素或滑动加载新元素。在我们的代码中,每当我们去检查一个元素是否存在时,我们都会调用一个方法来检查屏幕上当前的一组元素,然后滑动并在滑动后再次读取屏幕上的所有元素。现在,比较两个列表中的最后一个元素以检查是否加载了新元素。如果没有加载新元素,那么我们得出结论,我们正在寻找的元素没有加载到屏幕上。下面是我为此目的编写的代码。此代码应识别滑动是否正在加载新元素。
问题在于我读取所有元素并加载到列表的步骤。这一步变得非常繁重,有时会导致代码执行超过 5 分钟。
有人可以建议我是否可以在这里做得更好。
public synchronized boolean isScrollingLoadsNewElement (AppiumDriver<MobileElement> driver)
{
boolean isNewElementLoaded = false;
System.out.println("inside the new method to scroll and check new element");
//declare a list to accept all the elements on current screen
//go to the end of the list to read the last element. Store in a variable
this.driver = driver;
List<MobileElement> lAllElements = this.driver.findElements(By.xpath(".//*"));
System.out.println("list of element before swiping has been read");
MobileElement lastElement = lAllElements.get(lAllElements.size()-1);
//scroll and then again read the list of all elements.
//read the last element on the list and then compare the elements on above 2 steps.
//if the elements are different than return true else false.
swipeScreen(driver);
List<MobileElement> lAllElementsAfterSwipe = this.driver.findElements(By.xpath(".//*"));
System.out.println("list of element after swiping has been read");
MobileElement lastElementAfterSwipe = lAllElementsAfterSwipe.get(lAllElementsAfterSwipe.size()-1);
if (lastElementAfterSwipe.equals(lastElement))
isNewElementLoaded = false;
else
isNewElementLoaded = true;
return isNewElementLoaded;
}
【问题讨论】:
-
已经回答了,如果有帮助请告诉我:)
-
@Sameer Arora 你的方法肯定会奏效。我们之前有相同的实现,但由于在少数测试用例上硬编码计数器而失败。
标签: java selenium appium appium-ios appium-android