亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

檢查在移動屏幕上滑動是否加載了新元素

檢查在移動屏幕上滑動是否加載了新元素

慕的地8271018 2022-06-23 16:22:29
在我們的移動應用程序上,我們有幾個屏幕,我們需要在屏幕上滑動以查看元素或滑動加載新元素。在我們的代碼中,每當我們去檢查一個元素是否存在時,我們都會調用一個方法來檢查屏幕上當前的一組元素,然后滑動并在滑動后再次讀取屏幕上的所有元素?,F在,比較兩個列表中的最后一個元素以檢查是否加載了新元素。如果沒有加載新元素,那么我們得出結論,我們正在尋找的元素沒有加載到屏幕上。下面是我為此目的編寫的代碼。此代碼應識別滑動是否正在加載新元素。問題在于我正在讀取所有元素并加載到列表的步驟。這一步變得非常繁重,有時會導致代碼執行超過 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;}
查看完整描述

2 回答

?
素胚勾勒不出你

TA貢獻1827條經驗 獲得超9個贊

與其匹配滑動前的最后一個元素和滑動后的第一個元素,不如通過檢查其列表大小來檢查所需元素是否顯示在頁面上。


假設您的頁面上有 4 個元素,加載后,顯示第 5 個元素,按照您的方法,您將檢查第 5 個元素與第 4 個元素不同,并且您將通過測試用例,但這不會讓您確定顯示的元素是您正在尋找的元素,因為第 5 個元素可以是任何其他不打算顯示在頁面上但按照您的邏輯的元素,測試用例將通過。


因此,您應該獲取要查找的元素的 xpath,然后在每次滑動后檢查元素列表大小,因為元素列表大小在頁面上顯示時會大于 0,您應該限制滑動到一個限制,以便在該次數的滑動之后,您應該將布爾值返回為 false,否則循環將在無限狀態下繼續以檢查元素是否存在。


你的代碼邏輯應該是這樣的:


List<WebElement> element = driver.findElements(By.xpath("mention the xpath of the element that needs to be found"));

boolean elementLoaded = false;

int swipeCount = 0;

// Taking the swipeCount limit as 5 here

while (!elementLoaded && swipeCount < 5) {

    if (element.size() == 0) {

        // Swipe the screen

        swipe(driver);

        swipeCount++;

    } else {

        elementLoaded = true;

    }

}

return elementLoaded;


查看完整回答
反對 回復 2022-06-23
?
烙印99

TA貢獻1829條經驗 獲得超13個贊

而是存儲所有元素并與下一個狀態進行比較,只需在下一次滾動后獲取一個元素,確保該元素不應出現在屏幕中

注意:雖然我對您的應用沒有太多背景信息


查看完整回答
反對 回復 2022-06-23
  • 2 回答
  • 0 關注
  • 149 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號