我正在尝试验证文本是否出现在下拉菜单中。我的断言显示所有项目,但有错误...期望[]但找到[]。请在下面找到我的脚本和测试失败消息。在此先感谢您的帮助!
我的剧本:
driver.findElement(By.cssSelector("#s2id_autogen4 > a.select2-choice > span")).click();;
ArrayList<String> expectedDropDownItems = new ArrayList<String>();
expectedDropDownItems.add("keysearch");
expectedDropDownItems.add("short");
expectedDropDownItems.add("standard");
expectedDropDownItems.add("to-date");
Assert.assertEquals(expectedDropDownItems, driver.findElement(By.xpath(".//*[@id='select2-drop']/ul")).getText());
错误消息:
java.lang.AssertionError:expected [keysearch
短
标准
至今]但找到[[keysearch,short,standard,to-date]]
HTML:
<div style="top: 1973px; left: 261px; width: 500px; display: block;" class="select2-drop select2-with-searchbox select2-drop-active" id="select2-drop">
<div class="select2-search">
<input type="text" class="select2-input" autocomplete="off" tabindex="0">
</div>
<ul class="select2-results" style="">
<li class="select2-results-dept-0 select2-result select2-result-selectable">
<div class="select2-result-label">
<span class="select2-match"></span>keysearch</div></li>
<li class="select2-results-dept-0 select2-result select2-result-selectable">
<div class="select2-result-label">
<span class="select2-match"></span>short</div></li>
<li class="select2-results-dept-0 select2-result select2-result-selectable">
<div class="select2-result-label">
<span class="select2-match"></span>standard</div></li>
<li class="select2-results-dept-0 select2-result select2-result-selectable" style="">
<div class="select2-result-label">
<span class="select2-match"></span>to-date</div></li></ul>
</div>
答案 0 :(得分:0)
根据断言,我可以设想并建议下一步尝试修复你的问题(p.s.我不擅长java,但根据我在C#中的exp): 1.尝试打印从driver.findElement返回的数据(By.xpath(&#34; .//* [@ id =&#39; select2-drop&#39;] / ul&#34;))。getText()代码并确保此数据确实等于来自arrayList的数据。 2.如果数据看起来相等,请尝试另一种断言方法(例如Assert.That(object,matcher)(例如在C#中可能是Assert.That(new [] {1},Is.EquivalentTo(new [] {1} ));),Assert.arrayEquals等)
如果两个数组相等并且断言没有帮助,请提供html示例,我们将尝试以其他方式解决该问题。
答案 1 :(得分:0)
你能做的是,
1-首先获取下拉菜单的所有元素
2-将其放入列表中
3-遍历列表,获取每个元素的文本,然后将其放入一个新的字符串ArrayList中
4-使用此ArrayList和项目的原始ArrayList断言。
以下代码可以帮助您:
//Putting actual items in the Arraylist of expectedDropDownItems
ArrayList<String> expectedDropDownItems = new ArrayList<String>();
expectedDropDownItems.add("keysearch");
expectedDropDownItems.add("short");
expectedDropDownItems.add("standard");
expectedDropDownItems.add("to-date");
//Iterating over expected items and printing
System.out.println("\n--- Expected Items");
for(int i=0;i<expectedDropDownItems.size();i++)
System.out.println(expectedDropDownItems.get(i));
System.out.println("\n");
ArrayList<String> actualDropDownItems = new ArrayList<String>();
//Getting the list of all items in dropdown
List<WebElement> elements = driver.findElements(By.xpath("//div[@class='select2-result-label']"));
//Iterating over the list of dropdown, getting text of each element and adding the text to the Arraylist of actualDropDownItems
for(WebElement element : elements){
System.out.println("Adding element: "+ element.getText());
actualDropDownItems.add(element.getText());
}
//Iterating over actual items and printing
System.out.println("\n--- Actual Items");
for(int i=0;i<actualDropDownItems.size();i++)
System.out.println(actualDropDownItems.get(i));
//Asserting the ArrayLists if they are equal or not
Assert.assertEquals(expectedDropDownItems, actualDropDownItems);
System.out.println("Asserted text match");