如何在java中使用selenium验证具有下拉菜单的按钮

时间:2016-02-05 07:11:03

标签: java html selenium selenium-webdriver

有一个按钮。单击此按钮时,会出现一个包含两个选项的下拉菜单。如何在java中使用selenium验证此场景。

<div class="hpDropDownButton">
<button class="button ng-binding">Holidays Operation</button>
<ul>
    <li>
        <a class="ng-binding" ng-click="uploadHolidays()">Upload Holidays</a>
    </li>
    <li>
        <a class="ng-binding" ng-click="deleteHolidays()">Delete Holidays</a>
    </li>
</ul>

4 个答案:

答案 0 :(得分:0)

点击按钮

现在: -

        Boolean dropdownPresent = driver.findElement("YOUR LOCATOR OF DROPDOWN").isDisplayed();

        if(dropdownPresent==true)
        {
            System.out.println("Dropdown is appearing");
        }
        else{
            System.out.println("Dropdown is not appearing");
        }

希望它会对你有所帮助:)。

答案 1 :(得分:0)

您要求验证整个方案。您需要先了解Selenium-WebDriver是什么。有关详细说明,请参阅this教程。

但是,您可以按照以下代码

     WebDriver driver = new FirefoxDriver();
     String appUrl = "your url";
     driver.get(appUrl);
     // maximize the browser window
     driver.manage().window().maximize();

     // upto code from your button
     WebElement button_element = driver.findElement(button_locator);
     button_element.click();

     // to verify a drop down menu having two option appears
     boolean flag = isPresent(dropdown_locator);
     if (flag) {
        // code bases on dropdown displayed
    }
     else {
        // code bases on dropdown not displayed
    }

验证元素是否存在使用此方法

public boolean isPresent(String locator) {
    return findElements(locator).size() > 0 ? true : false;
}

答案 2 :(得分:0)

  1. 首先收集列表中的所有下拉值,列表值=上传假期#删除假期
  2. 然后使用DropdownFieldName = driver.findElement(by.xpath(//button[@class='button ng-binding'])).click();
  3. 点击Dropdown WebElement
  4. 通过drptotalCount = driver.findElements(by.xpath(//a[@class='button ng-binding']));
  5. 获取下拉值的数字
  6. 现在您已经预期DropDown值和下拉值计数。
  7. 您可以从以下代码中获取参考:

    checkDropDownValues(String DropdownFieldName, String values){
             driver.findElement(By.xath(DropdownFieldName)).click();
           WebElement drptotalCount = driver.findElements(by.xpath(//a[@class='button ng-binding']));
            int numberOfDropDown = drptotalCount.size();
            List<String> allDropDownValues = Arrays.asList(values.split("#"));
     for (int colCount = 1; colCount <= numberOfDropDown; colCount++) {
           boolean flag = false;
          Actualvalue = driver.findElement(By.xpath(drptotalCount + "[.=" + allDropDownValues.get(colCount)  +"]"])).getText();
     String expectedValues = allDropDownValues.get(colCount);
           if(expectedValues.equalIgnoreCase(Actualvalue))
           {
                    flag = true;
           }
     Assert.assertTrue("Column values doesn't match", flag);
     }
     }
    

答案 3 :(得分:0)

  1. 点击按钮(应该是直接的)
    • 看来你有一些异步电话或延迟
  2. 等待使用WebDriverWait显示下拉“div.hpDropDownButton”:

    WebElement myDynamicDropDown =(new WebDriverWait(driver,10))。until(ExpectedConditions.presenceOfElementLocated(By.CssSelector(“div.myDynamicDropDown”)))

  3. 继续..

  4. http://www.seleniumhq.org/docs/04_webdriver_advanced.jsp