2016-10-04 33 views
1

我注意到我正在測試的應用程序,當我點擊一個提交按鈕一個網格刷新。我有一個行數我有一個跨度訪問,並希望做一些事情,如:看着ExpectedConditions類,試圖找到onChange

(new WebDriverWait(driver, upperTimeoutLimit)) 
    .until(ExpectedConditions.elementChanged(By.cssSelector(mySelector))); 

我的目標是看一個節點,並做了WebDriverWait,直到它改變某種方式,理想的的getText

編輯我是說@FlorentB,他正沿着以下提的東西:

WebDriverWait wait = new WebDriverWait(driver, upperTimeoutLimit); 
WebElement grid = ui.getExternalCommandGrid(); 
submit.click(); 
//staleness 
System.out.println("about to check for staleness"); 
wait.until(ExpectedConditions.stalenessOf(grid)); 
System.out.println("About to check presence of"); 
wait.until(ExpectedConditions.presenceOfElementLocated(
    By.cssSelector(ui.getExternalCommandGridSizeSelector()))); 

它似乎沒有獲得通過的stalenessOf(網格)的一部分。我的想法可能是,我在點擊之前選擇網格,運行點擊,然後希望在獲得下一個拼圖之前,它會等到它識別出一個變化(也許我需要檢查行)。

+0

API本身有很多條件可以使用 - https://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/support/ui/ ExpectedConditions.html ... 。textMatches,textToBe,\t textToBePresentInElement,textToBePresentInElementLocated,\t textToBePresentInElementValue等 – Grasshopper

+0

我可以用文本匹配來檢查「textNotMatches」,不過是的,我一直在尋找的是API,並試圖想我可以利用我的情況 – Fallenreaper

+0

結合起來與not()ExpectedCondition將會給你邏輯相反。 ExpectedConditions.not(ExpectedCondition.textMatches())應該訣竅 – Grasshopper

回答

0

我最終得到這個工作將是如下的方式:

//submit is a button. 
//driver is a WebDriver. 
//uppderTimeoutLimit is an int, 10000 
WebDriverWait wait = new WebDriverWait(driver, upperTimeoutLimit); 
WebElement sizeEle = driver.findElement(By.cssSelector(selector)); 
String beforeCount = sizeEle.getText(); 
submit.click(); 
System.out.println("about to wait until change"); 
wait.until(ExpectedConditions.not(ExpectedConditions.textMatches(
     By.cssSelector(selector), 
     Pattern.compile(beforeCount)))); 

它會超時,如果它沒有改變,如果它,它會繼續。現在我唯一能想到的就是用一種嘗試來包裝它,因爲如果它超時,我想要一些人類可讀的內容來理解爲什麼。

0

據我所知,selenium中沒有像elementChanged這樣的選項。你可以用你自己的方式做到這一點。首先,在點擊提交按鈕之前獲取網格的行數。然後等待元素的數量大於先前的計數。

//count the grid's row before clicking submit button 
int previousRowCount = driver.findElements(By.cssSelector("your selector for row")).size(); 

//click submit button & wait for grid's row count to be greater than previousRowCount 
WebDriverWait wait = new WebDriverWait(driver, 20); 
wait.until(ExpectedConditions.numberOfElementsToBeMoreThan(By.cssSelector("your table rows locator"), numberCountBeforeTableLoad)); 

增加:

所有這些方法等待指定的參數的最大時間量。你無法等待你的頁面/元素加載無限的時間。在我的一個項目中,有很多數據加載到表中。在排序數據時,表格消失。所以,我做了一個竅門。首先,我等待表格/行的隱形,然後等待最多5分鐘的同一事物的可視性(是的,實際上是5個小時!!)。而且,它爲我工作。

+0

這就是我現在做的,有一個更長的等待,問題是,網絡請求需要不同的時間量,這是我我試圖繞過。我試圖通過一些更有活力的東西來避免等待很長時間。元素不止,不能工作,因爲它是分頁,所以每頁最多有100行,如果是270行,如果頁面重新加載,它不會顯示行數改變。 – Fallenreaper

+0

如果行數減少或不變,該怎麼辦?元素可能已更新,但計數可能不會按照您的預期更改。你真的應該以不同的方式來做到安全。 – JeffC

+0

單擊「提交」按鈕後,表格/網格會消失嗎?然後,分別等待表/表格行的隱藏和可見性。 –

1

一種簡單的方式來等待網格被刷新會等待網格內的定位元素的陳舊程度:

WebDriverWait wait = new WebDriverWait(driver, 10); 

// element in grid 
WebElement element = driver.findElement(By.cssSelector(mySelector)); 

// trigger the reload 
button.click(); 

// waits for the element to become stale 
wait.until(ExpectedConditions.stalenessOf(element)); 

// waits for a new element 
element = wait.until(ExpectedConditions.presenceOfElementLocated(By.cssSelector(mySelector))); 
+0

什麼是「陳舊」?我不確定它是如何定義的。我看到了它,但並不確定是否需要以某種方式切換。 – Fallenreaper

+0

'findElement'返回的結果是對頁面中'HTMLElement'的引用。它存儲在'WebElement.id'中。當文檔重新加載或者元素從頁面中刪除或替換時,此引用變爲陳舊(不再可用) 。 –

+0

哦,所以如果文字改變等等,它會變得陳舊?我要測試這個,然後如果它有效,這是我想要的答案。 – Fallenreaper