Java 我的Webdriver方法是';不可靠';单击';项目';是否存储在下拉菜单中?
单击下拉菜单中存储的“项目”时,我的Webdriver方法不可靠 例如,10个测试将通过,但有一个测试将失败(下面列出的示例图像) 我的代码:Java 我的Webdriver方法是';不可靠';单击';项目';是否存储在下拉菜单中?,java,selenium,selenium-webdriver,webdriver,Java,Selenium,Selenium Webdriver,Webdriver,单击下拉菜单中存储的“项目”时,我的Webdriver方法不可靠 例如,10个测试将通过,但有一个测试将失败(下面列出的示例图像) 我的代码: public void selectTitleFromDropdownMenu(WebElement dropdown, String textToSearchFor) throws Exception { Wait<WebDriver> tempWait = new WebDriverWait(this.driver, 30
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下拉菜单,String textToSearchFor)引发异常{
Wait tempWait=newwebdriverwait(this.driver,30);
试一试{
tempWait.until(ExpectedConditions.ElementTobelickable(下拉菜单));
列表选项=下拉.findElements(按.tagName(“选项”));
选择下拉菜单=新建选择(下拉菜单);
对于(int i=0;i
即使以下代码也会失败:
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.");
public void selectTitleFromDropdownMenu(WebElement下拉菜单)引发异常{
Wait tempWait=newwebdriverwait(this.driver,30);
试一试{
tempWait.Toll(预期条件.可视性(下拉菜单));
WebElement mySelectElm=下拉列表;
选择mySelect=newselect(mySelectElm);
mySelect.selectByVisibleText(“先生”);
要素:
<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
请选择
先生
错过
夫人
太太
博士
如果我是你,我会添加一些简单的重试逻辑,如果你的测试框架中没有内置的东西-例如,重试失败的测试几次
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);
}
}
}
public void DoWithRetry(操作操作,TimeSpan sleepPeriod,int tryCount=3)
{
如果(tryCount如果我是你,我会添加一些简单的重试逻辑,如果你的测试框架中没有内置的东西-例如,重试失败的测试几次
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);
}
}
}
public void DoWithRetry(操作操作,TimeSpan sleepPeriod,int tryCount=3)
{
如果(tryCount,我建议尝试以下方法并删除显式等待:
// 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());
}
}
//当您实例化驱动程序时
//这在所有元素中都是全局的,您不再需要一次等待一个元素,即显式等待
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
试一试{
选择下拉菜单=新建选择(下拉菜单);
选择Dropdown。选择ByVisibleText(textToSearchFor);
System.out.println(“使用以下webelement:+”,成功选择了以下文本:“+textToSearchFor+”;
}捕获(例外e){
System.out.println(“无法选择以下文本:“+textToSearchFor+”,使用以下Web元素:“+”);
System.out.println(“异常:+e.getMessage());
}
}
我建议尝试以下方法并删除显式等待:
// 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());
}
}
//当您实例化驱动程序时
//这在所有元素中都是全局的,您不再需要一次等待一个元素,即显式等待
driver.manage().timeouts().implicitlyWait(30,TimeUnit.SECONDS);
试一试{
选择下拉菜单=新建选择(下拉菜单);
选择Dropdown。选择ByVisibleText(textToSearchFor);
System.out.println(“使用以下webelement:+”,成功选择了以下文本:“+textToSearchFor+”;
}捕获(例外e){
System.out.println(“无法选择以下文本:“+textToSearchFor+”,使用以下Web元素:“+”);
System.out.println(“异常:+e.getMessage());
}
}
它不可靠,因为它看起来像是使用JavaScript生成的下拉列表。您应该使用隐式等待,并有一个合理的超时时间,以使其更可靠。@AntonB谢谢您的建议,如果我已经为该方法设置了显式等待,这会有很大区别吗?另外,我认为您不需要这一行:this、 wait.until(ExpectedConditions.visibilityOf(options.get(i)));
,这基本上是说等待选项值的可见性…您已经等待了select下拉列表中的元素可选择的,这通常已经足够了。另外,请注意选项值的可见性将失败,因为html中的选项值的高度和宽度为0。这是不可靠的,因为se看起来下拉列表是使用JavaScript生成的。您应该使用隐式等待,并有一个合理的超时时间,以使其更可靠。@AntonB谢谢您的建议,如果我已经为该方法设置了显式等待,这会有很大的不同吗?另外,我认为您不需要这一行:this.wait.until(ExpectedConditions.visibilityOf(options.get(i)));
,这基本上是说等待选项值的可见性…您已经等待了select下拉列表中的元素可选择的,这通常已经足够了。另外,请注意visibilityOf
选项值将失败,因为html中的选项值的高度和宽度为0。thread.sleep
是一个反模式
,它会消耗线程,不应该用于这种情况,请参阅:我知道。因此,我说“简单”。您可以编写许多更优化的代码,但我不确定是否所有情况下都需要它。如果您反编译WebDriver DLL,您会发现更多这样的情况…)线程。睡眠
是