2016-08-26 39 views
1

我的應用程序在記錄行上使用無限滾動,這意味着任何特定記錄的定位器,比如最後一個記錄,總是相同,但每次記錄的下方都會不同我做一個卷軸。量角器 - 比較具有相同定位器的兩個元素

我想比較之前滾動

後,被滾動到對應於最後一個元素相同的元素對應的最後一個元素的元素

量角器有一個函數「等於」,但我覺得它比較基礎上,這就是爲什麼當我比較具有相同定位符但不同記錄的最後一個元素時,它總是作爲真正的結果。

是否有任何其他方式直接比較兩個元素?

回答

1

它非常簡單。在執行滾動之前,將文本存儲在某個變量中。再次執行滾動後,使用相同的定位器獲取最新記錄的文本。看下面的示例代碼。

var textOfLastRecordBeforescrolling = element.all(by.css("someLocator")).last().getText(); 
//Perform Scrolling 
var textOfLastRecordAfterscrolling = element.all(by.css("someLocator")).last().getText(); 
expect(textOfLastRecordBeforescrolling).toEqual(textOfLastRecordAfterscrolling) //do whatever comparison you want do 
1

你可能會需要從第一個元素保存信息,然後滾動,得到第二的信息,然後進行比較。

it('Has different elements after infinite scrolling', function(){ 
 
    var elementOfInterest = $$('.infiniteScrollElement').last(); 
 

 
    // Get the text of the first element and pass it down 
 
    elementOfInterest.getText().then(function(firstElementsText){ 
 
     // Scroll however far you need to 
 
     browser.executeScript('window.scrollTo(0,500);').then(function(){ 
 
     // Compare the current elements text with the past elements text 
 
     expect(elementOfInterest.getText()).not.toEqual(firstElementsText, 'Error: The text should be different between the two elements, but was not'); 
 
     }) 
 
    }) 
 
    });

+0

是沒可能比較元素對象,而不是直接閱讀文本? – Manya

+0

@Manya你想用它做什麼?量角器Web元素只包含一些靜態事物,如其選擇器和可執行的函數列表,但這些都會解析爲在調用時完成的承諾。其功能的結果通常是有用的,通常不是對象本身。 – user2020347

1

比較網絡元素的實例,可以測試通過element(...).getId()返回的值。

返回的id是爲頁面中每次遇到的對象HTMLElement生成的引用,並且與id屬性/屬性無關。

這是要更換,以滾動的內容和等待最後一個元素的例子:

browser.get("https://twitter.com/BBC/"); 

// define the locator for the last element 
var lastElement = $$('#stream-items-id > li').last(); 

// store the reference for the last element 
var storedRef = lastElement.getId(); 

// scroll to the last element 
browser.actions().mouseMove(lastElement).perform(); 

// wait for the last element to be another reference 
browser.wait(function(){ 
    return storedRef.then(function(a){ 
     return lastElement.getId().then(b => a !== b); 
    }, 3000); 
}); 

// compare the stored reference with the last element 
expect(lastElement.getId()).not.toEqual(storedRef); 
+0

該解決方案看起來不錯。唯一的缺點是我無法使用===運算符來比較'ids'。我必須使用localeCompare作爲 – Manya

+0

@Manya,執行是異步的,所以無法直接將兩個元素與等號運算符進行比較。它必須經歷一個承諾的解決。 –

相關問題