简体   繁体   English

我做错了什么?

[英]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.

相关问题 硒日历:月和日期选择:我在做什么错 - Selenium Calendar: Months and Dates Selections: What i'm doing wrong Selenium中的xPath-我在做什么错? - xPath in Selenium - what am I doing wrong? 使用 selenium 的函数我做错了什么? - What am I doing wrong using functions with selenium? Webdriver.io +摩卡-我在做什么错? - Webdriver.io + Mocha - What am I doing wrong?? 多次抓取:代码中的问题。 我究竟做错了什么? - Multiple scraping: problem in the code. What am I doing wrong? 在SkipException,testNG上遇到问题,我在做什么错? - Having trouble with SkipException, testNG, what am I doing wrong? 我究竟做错了什么? 我正在尝试将 selenium 依赖项添加到 Intellij,但我不断收到错误 - What am I doing wrong? I am trying to add selenium dependencies to Intellij and I keep getting errors 量角器以离线模式安装-webdriver-manager启动错误-我在做什么错 - Protractor installed in offline mode - error for webdriver-manager start - what am I doing wrong 无法使用 Python Selenium 选择项目我做错了什么? - Unable to select item using Python Selenium What am I doing wrong? 收到错误“消息:过时的元素参考:元素未附加到页面文档”。我做错了什么 - Getting error "Message: stale element reference: element is not attached to the page document".what i am doing wrong
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM