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

如何在java中使用selenium验证按钮是否具有下拉菜单,java,html,selenium,selenium-webdriver,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()

有一个按钮。单击此按钮时,将出现一个具有两个选项的下拉菜单。如何在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>

假期运作
  • 上传假期
  • 删除假日
点击按钮

现在:-

        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");
        }

希望它能帮助您:)

您要求验证整个场景。您需要首先了解什么是
SeleniumWebDriver
。有关更多说明,请参阅教程

但是,您可以遵循以下代码

     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;
}
  • 首先收集列表中的所有下拉值,列表值=上传假日#删除假日
  • 然后使用
    DropdownFieldName=driver.findElement(by.xpath(//button[@class='button ng binding'])单击下拉WebElement。单击()
  • 通过
    drptotalCount=driver.findElements(by.xpath(//a[@class='button ng binding']),获取下拉列表值的数量
  • 现在您有了期望的下拉值和下拉值计数
  • 您可以从以下代码中获取参考:

    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);
     }
     }
    
    checkDropDownValues(字符串DropdownFieldName,字符串值){
    driver.findElement(By.xath(DropdownFieldName)).click();
    WebElement drptotalCount=driver.findElements(by.xpath(//a[@class='button ng binding']);
    int numberOfDropDown=drptotalCount.size();
    List allDropDownValues=Arrays.asList(values.split(“#”);
    for(int colCount=1;colCount
    
  • 单击按钮(应该是直接向前的)
    • 似乎您有一些异步调用或延迟
  • 等待使用WebDriverWait显示下拉列表“div.hpDropDownButton”:

    WebElement myDynamicDropDown=(新的WebDriverWait(driver,10))。直到(ExpectedConditions.presenceOfElementLocated(By.CssSelector(“div.myDynamicDropDown”))

  • 继续


  • 启动webdriver,找到按钮,单击它,找到这两个菜单,然后断言属性isDisplayed()=true。