[英]What i'm doing wrong?
I'm practicing in selenium, not familiar jet neither with Java or Selenium but i'm trying so, i have written the below code: 我在硒中练习,对Java或Selenium都不熟悉,但我正在尝试,我编写了以下代码:
System.setProperty("webdriver.chrome.driver","G:\\Programming\\Selenium\\tools\\drivers\\chromedriver_win32\\chromedriver.exe");
WebDriver driver = new ChromeDriver();
driver.get("https://www.ultimateqa.com/simple-html-elements-for-automation/");
Select dropDown = new Select(driver.findElement(By.xpath("//div[@class='et_pb_blurb_description']//select")));
int counter = dropDown.getOptions().size();
for (int i=0; i<counter; i++){
String expectedNames [] = {"Volvo","Saab","Opel","Audi"};
WebElement items = dropDown.getOptions().get(i);
String actualNames = items.getText();
boolean isDisplay = dropDown.getOptions().get(i).isDisplayed();
if (isDisplay){
Assert.assertEquals(actualNames, expectedNames[0]);
System.out.println("Item1 Asserted Succesfuly");
}
if (isDisplay){
Assert.assertEquals(actualNames,expectedNames[1]);
System.out.println("Item2 Asserted Succesfuly");
}
if (isDisplay){
Assert.assertEquals(actualNames,expectedNames[2]);
System.out.println("Item3 Asserted Succesfuly");
}
if (isDisplay){
Assert.assertEquals(actualNames,expectedNames[3]);
System.out.println("Item4 Asserted Succesfuly");
}
}
I'm expecting: 我期待着:
Item1 Asserted Succesfuly 第1项主张成功
Item2 Asserted Succesfuly 第2项主张成功
Item3 Asserted Succesfuly Item3认定成功
Item4 Asserted Succesfuly Item4认定成功
But i get an assertion error expected [Saab] but found [Volvo], it does not go to read the elements of the expected names in the list for some reason 但是我得到了一个预期的断言错误[Saab],但是却发现了[Volvo],由于某种原因,它无法读取列表中预期名称的元素
First off, it seems like your Selenium code is looking good. 首先,您的Selenium代码看起来不错。
The issue actually lies in the logic of your conditional statements: 问题实际上在于条件语句的逻辑:
if (isDisplay){
Assert.assertEquals(actualNames, expectedNames[0]);
System.out.println("Item1 Asserted Succesfuly");
}
if (isDisplay){
Assert.assertEquals(actualNames,expectedNames[1]);
System.out.println("Item2 Asserted Succesfuly");
}
if (isDisplay){
Assert.assertEquals(actualNames,expectedNames[2]);
System.out.println("Item3 Asserted Succesfuly");
}
if (isDisplay){
Assert.assertEquals(actualNames,expectedNames[3]);
System.out.println("Item4 Asserted Succesfuly");
}
In each iteration of the loop, your code goes into all 4 conditional statements (because isDisplay=true
. The error occurs because you are then asserting if actualNames
equals to expectedNames[0], expectedNames[1], expectedNames[2], expectedNames[3]
. 在循环的每次迭代中,您的代码都会进入所有4条条件语句(因为isDisplay=true
。发生错误,是因为您随后断言actualNames
等于actualNames
expectedNames[0], expectedNames[1], expectedNames[2], expectedNames[3]
。
To fix, you just need 1 if
statement which utilizes the i
counter to iterate through both the list and array, so that the corresponding item in both collections are the ones being matched against. 要解决此问题,您只需要1 if
语句即可,该语句利用i
计数器遍历列表和数组,以便两个集合中的对应项都与之匹配。
if (isDisplay){
Assert.assertEquals(actualNames, expectedNames[i]);
System.out.println("Item"+ i + " Asserted Succesfuly");
}
A Slight Off-Track 偏离轨道
With that out of the way, I'm going to go a bit off-topic and say that for a drop-down list, there is usually a value
attribute for each element in the list. 顺便说一句,我将离开主题,对一个下拉列表来说,列表中的每个元素通常都有一个value
属性。 For example: 例如:
<option value=volvo">Volvo</option>
<option value=saab">Saab</option>
A good practice would be to instead use this value
attribute instead of the visible text, which you have done. 一个好的做法是改为使用此value
属性而不是您已完成的可见文本。 The isDisplayed()
method MIGHT return false
, depending on the implementation of the dropdown element, especially if it isn't the default value of the dropdown list. isDisplayed()
方法可能会返回false
,具体取决于dropdown元素的实现,尤其是如果它不是下拉列表的默认值时。 The value=volvo
attribute will always be available for you to use to select the element, as long as it appears in the DOM. 只要该元素出现在DOM中, value=volvo
属性将始终可供您用来选择该元素。
To do that, it's simple: 为此,这很简单:
String expectedNames [] = {"volvo","saab","opel","audi"};
for (int=0; i<count; i++){
WebElement item = dropDown.getOptions().get(i);
String attrValue = item.getAttribute("value");
Assert.assertEquals(attrValue, expectedNames[i]);
System.out.println("Item " + i " asserted successfully.");
}
This will come in handy for you when you actually intend to click to select another option. 当您实际打算单击以选择另一个选项时,这将对您很方便。 Selenium may throw a NoSuchElementException
error otherwise. 硒可能会引发NoSuchElementException
错误。
@Proko Notice that after your for loop first iteration it asserts first if condition but not subsequent because at next iteration Saas(2nd element in array) will come and same will fetched from getOptions()
method so you don't need 4 if conditions, 1 is suffice to assert all values, remove all if condition except first and add i as index element for array. @Proko注意,在for循环的第一次迭代之后,它将首先声明if条件,但不会断言随后的条件,因为在下一次迭代中,Saas(数组中的第二个元素)将到来,并且将从getOptions()
方法中获取相同内容,因此您不需要4 if条件, 1声明所有值就足够了,首先除去所有if条件,然后将i添加为数组的索引元素。 code for your reference. 代码供您参考。
if (isDisplay){
Assert.assertEquals(actualNames, expectedNames[i]);
System.out.println("Item1 Asserted Succesfuly");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.