3 回答

TA貢獻1871條經驗 獲得超8個贊
這里最好的做法是將它們分成自己的小函數并返回布爾值。喜歡
Boolean isElementDisplayed(WebElement element){
if (element.isDisplayed())
return true;
System.out.println(element + " is not displayed!");
return false;
}
Boolean isElementEnabled(WebElement element){
if (element.isEnabled())
return true;
System.out.println(element + " is not enabled!");
return false;
}
但我還建議在執行 moveToElement 之后調用 isElementDisplayed,因為某些瀏覽器對“顯示”的含義有不同的考慮。
您還可以使用 try catch 來記錄每個函數的異常。

TA貢獻1831條經驗 獲得超9個贊
Boolean isActionSuccess = false;
CommonFunctions.silentWait(1);
actionToE.moveToElement(currentObject).perform();
if (CommonFunctions.isElementDisplayed(currentObject)) {
if (CommonFunctions.isElementEnabled(currentObject)) {
if (!actionPar.isEmpty()) {
// do something
}
} else {
currentObject.sendKeys(Keys.ARROW_LEFT);
isActionSuccess = true;
}
}
}

TA貢獻1786條經驗 獲得超13個贊
在使用Selenium執行自動化測試時,您不需要在實際操作之前對 Web 元素進行任何額外的標準檢查。根據記錄,每增加一行代碼都會導致額外的指令和指令周期。相反,您需要優化您的代碼/程序。
如果你的用例是調用click()
或者sendKeys()
不需要調用isDisplayed()
或者isEnabled()
單獨去檢查。相反,您需要結合使用WebDriverWait和ExpectedConditions來等待預定義的時間段(根據測試規范)。
例子:
presenceOfElementLocated()
是檢查頁面 DOM 上是否存在元素的期望。這并不一定意味著該元素是可見的。new?WebDriverWait(driver,?20).until(ExpectedConditions.presenceOfElementLocated(By.cssSelector("button.nsg-button")));
visibilityOfElementLocated()
是檢查元素是否存在于頁面 DOM 上并且可見的期望??梢娦允侵冈夭粌H能顯示,而且高度和寬度都大于0。new?WebDriverWait(driver,?20).until(ExpectedConditions.visibilityOfElementLocated(By.cssSelector("button.nsg-button")));
elementToBeClickable()
是檢查元素是否可見并啟用以便您可以單擊它的期望。new?WebDriverWait(driver,?20).until(ExpectedConditions.elementToBeClickable(By.xpath("http://button[@class='nsg-button']"))).click();
添加回答
舉報