2016-09-28 80 views
1

在我的AUT中有兩個字段 - 'Product'下拉列表和一個'Amount'輸入字段。默認情況下,'Amount'字段顯示值'0.0'。當用戶從'Product'下拉菜單中選擇一個產品時,'Amount'字段會自動填充所選產品的價格(如果價格已經在DB中可用),否則'Amount'字段會顯示'0.0'。選擇產品後,需要一段時間將金額加載到'Amount'字段。在自動填充之前和之後,我無法觀察'Amount'字段的屬性值中的任何更改。在'Amount'字段的HTML是如何等待輸入字段在Selenium webdriver中刷新

<input id="id_expense_amt" class="form-control input" name="expense_amt" step="0.01" value="0" type="number"> 

的問題是,我怎麼能做出的webdriver等待選擇了產品之後刷新金額字段。我用Thread.sleep(),它工作正常。但有沒有其他方法可用。

回答

0

如果你想要你可以使用下面的一個,你可以根據你的需要更改timeunit。

driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS); 

如果您知道應該在量文本框中填入準確的數值您可以使用此之一,也是

WebDriverWait wait = new WebDriverWait(getWebDriver(), 10); 

WebElement element = wait.until(ExpectedConditions.visibilityOf(element)); 
+0

金額字段的可見性不會改變。它始終可見 – stackoverflow

+0

然後嘗試獲取下拉第一個索引的值。如果不可見,那麼您可以一次又一次地執行檢查,直到獲得該值。 –

0

,那麼你可以使用ExpectedConditions.textToBePresentInEl‌ement

WebDriverWait wait = new WebDriverWait(driver, 60); 
webDriverWait.until(ExpectedConditions.textToBePresentInEl‌ement(By.xpath("text‌​box xpath"), "text for which you are waiting")); 
1

基本上有兩種方式,如下使用WebDriverWait實現這樣的場景: -

WebDriverWait wait = new WebDriverWait(driver, 10); 
  • 如果你已經知道什麼量會從默認量後Amount文本字段的變化從Product下拉菜單中選擇一個選項,那麼您應該嘗試使用ExpectedConditions.textToBePresentInElementValue,它將等待,直到給定的數量出現在指定的元素值屬性中,如下所示: -

    webDriverWait.until(ExpectedConditions.textToBePresentInElementValue(By.id("id_expense_amt"), "Amount which would be change")); 
    
  • 如果你不知道什麼量會選擇從Product下拉選項後的變化,從默認金額Amount文本字段,但是你知道這是0默認量,那麼你需要創建自定義ExpectedConditions它會等到量爲默認0變化如下任何新的量: -

    wait.until(new ExpectedCondition<Boolean>() { 
          public Boolean apply(WebDriver d) { 
          WebElement el = d.findElement(By.id("id_expense_amt")); 
          String value = el.getAttribute('value'); 
          if(value.length() != 0 && !value.equals("0")) { 
           return true; 
          } 
          } 
    }); 
    
相關問題