例如,10个测试将通过但是一个将失败(下面列出的示例图像)
我的代码:
public void selectTitleFromDropdownMenu(WebElement dropdown, String textToSearchFor) throws Exception {
Wait<WebDriver> tempWait = new WebDriverWait(this.driver, 30);
try {
tempWait.until(ExpectedConditions.elementToBeClickable(dropdown));
List<WebElement> options = dropdown.findElements(By.tagName("option"));
Select selectDropdown = new Select(dropdown);
for (int i = 0; i < options.size(); i++) {
if (options.get(i).getText().equals(textToSearchFor))
this.wait.until(ExpectedConditions.visibilityOf(options.get(i)));
selectDropdown.selectByVisibleText(textToSearchFor);
}
System.out.println("Successfully selected the following text: " + textToSearchFor + ", using the following webelement: " + "<" + dropdown.toString() + ">");
} catch (Exception e) {
System.out.println("Unable to select the following text: " + textToSearchFor + ", using the following WebElement: " + "<" + dropdown.toString() + ">");
System.out.println("Exception: " + e.getMessage());
}
}
即使以下代码失败:
public void selectTitleFromDropdownMenu(WebElement dropdown) throws Exception {
Wait<WebDriver> tempWait = new WebDriverWait(this.driver, 30);
try {
tempWait.until(ExpectedConditions.visibilityOf(dropdown));
WebElement mySelectElm = dropdown;
Select mySelect= new Select(mySelectElm);
mySelect.selectByVisibleText("Mr.");
元素:
<select class="form-control title ng-touched ng-dirty ng-valid-parse ng-valid ng-valid-required" name="Salutation" ng-model="AddingDelivery.EditingDeliveryAddress.Title" ng-options="salut.id as salut.id for salut in Salutations" required="">
<option class="ng-binding" value="">Please select</option>
<option value="0" label="Mr.">Mr.</option>
<option value="1" label="Miss">Miss</option>
<option value="2" label="Mrs.">Mrs.</option>
<option value="3" label="Ms.">Ms.</option>
<option value="4" label="Dr.">Dr.</option>
</select
异常:我看到的唯一例外是超时异常,例如15秒后无法定位元素...
感谢您的帮助
答案 0 :(得分:0)
如果我是你,我会在你的测试框架中没有内置的东西时添加一些简单的重试逻辑 - 例如重试失败的测试几次。
简单重试代码的C#示例,在Java中我猜是相同的:
public void DoWithRetry(Action action, TimeSpan sleepPeriod, int tryCount = 3)
{
if (tryCount <= 0)
throw new ArgumentOutOfRangeException(nameof(tryCount));
while (true) {
try {
action();
break; // success!
} catch {
if (--tryCount == 0)
throw;
Thread.Sleep(sleepPeriod);
}
}
}
答案 1 :(得分:0)
我建议尝试以下方法并删除显式等待:
// when you instantiate your driver
// this is global across all elements, and you no longer need to wait for one element at a time, i.e explicit waits
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
try {
Select selectDropdown = new Select(dropdown);
selectDropdown.selectByVisibleText(textToSearchFor);
System.out.println("Successfully selected the following text: " + textToSearchFor + ", using the following webelement: " + "<" + dropdown.toString() + ">");
} catch (Exception e) {
System.out.println("Unable to select the following text: " + textToSearchFor + ", using the following WebElement: " + "<" + dropdown.toString() + ">");
System.out.println("Exception: " + e.getMessage());
}
}