如何使用Java-Selenium中的Cucumber Bdd框架处理Bootstrap下拉列表

苏马

我在BDD框架中构建了脚本。我尝试了几种方法来处理正在测试的应用程序中使用的Bootstrap下拉菜单。

但是没有运气。

如何使用框架处理我的应用程序的引导程序下拉列表?

UIAction类文件:

我已经编写了将通常执行的所有UI操作的代码,并在必要时在另一个类中调用此函数方法。

因此,对于下拉菜单,我尝试使用在UIAction中编码的select方法,如下所示。

UIAction类中的Select方法:

public class UIActions  {

public static void select(String UIName, String objTechName, String data) throws Exception{ 
        try{

            Log.info("Selecting item in Combo " + objTechName);
            //driver.findElement(By.xpath(OR.getProperty(object))).sendKeys(data);

            //final Select selectBox = new Select(driver.findElement(By.xpath(OR.getProperty(object))));
            final Select selectBox = new Select(driver.findElement(ObjectMap.getLocator(objTechName)));

            //selectBox.selectByValue(data);
            selectBox.selectByVisibleText(data);
         }catch(Exception e){
             Log.error("Not able to Select an item --- " + e.getMessage());
             throw new UserDefinedException("<<< Unable to Select '" + data +  "' in Field '" + UIName + "' >>> "+e.getMessage());           
             //DriverScript.bTestStepResult = false;

            }
        }
}

在单独的类中定义了对象:

public class ObjectsDefinition extends UIActions {

    public static final String OBJ_Origin_DROPDOWN = "|xpath|//span[contains(text(),'BRANCH')]";

}

在我的框架中的stepdefinition类文件中调用了select函数的方法:

public class StepDefinitions extends UIActions {

@And("^User captures Origin (.*)$") 

    public void User_captures_Origin(String arg7) throws Throwable {

        UIActions.select("OBJ_Origin_DROPDOWN", VASTObjectsDefinition.OBJ_Origin_DROPDOWN, arg7);
        //UIActions.driver.findElements(By.xpath("//span[contains(text(),'BRANCH')]"));
        Thread.sleep(50);       
    }
}

当上面的代码不起作用时,我按照博客1的建议尝试了处理bootstrap下拉菜单,但这对我也不起作用:

@And("^User captures Origin (.*)$")
    public void User_captures_Origin(String arg7) throws Exception{

          // this is using action class to set focus on any element
          // below line is just for clicking on perticular dropdownlist

             WebElement ele= driver.findElement(By.xpath(".//*[@id='content']/div/div[1]/div[2]/div[1]/div[2]/div[2]/div/div/div/button"));
        // To count the number of value in drop down

        Actions act= new Actions(driver);

        act.moveToElement(ele).perform();

        // below line xpath is to select the value
       List<WebElement> Dropdownvalues= driver.findElements(By.xpath("//span[contains(text(),'BRANCH')]"));

       int total_links=Dropdownvalues.size();

       System.out.println("Total values count are:"+total_links);


       for(int i=0;i<total_links;i++){

            // To get the name of all links:

            WebElement element=Dropdownvalues.get(i);

           String dropdownvalues_names= element.getAttribute("innerHTML");
    //   boolean status= element.isEnabled();

          // System.out.println("Links name are:"+links_names +"Link status is "+ status);       


           // To clicking on perticular link:

                       if (dropdownvalues_names.equalsIgnoreCase("BRANCH")){

                             element.click();
                            break;


                       }

       }


      //  Actions act= new Actions(driver);

       // act.moveToElement(ele).perform();

      //  driver.findElement(By.xpath("//div[2]/div/select")).click();


      //  new Actions(driver).moveToElement(ele).click().perform();



        Thread.sleep(3000);



          }

当我运行脚本时,出现如下错误:

org.openqa.selenium.ElementNotVisibleException: Cannot click on element (WARNING: The server did not provide any stacktrace information)
Command duration or timeout: 85 milliseconds
Build info: version: '2.48.2', revision: '41bccdd', time: '2015-10-09 19:55:52'
System info: host: '0967JNBPBB010L', ip: '10.5.70.125', os.name: 'Windows 7', os.arch: 'x86', os.version: '6.1', java.version: '1.8.0_76-ea'
Driver info: org.openqa.selenium.ie.InternetExplorerDriver
Capabilities [{browserAttachTimeout=0, ie.enableFullPageScreenshot=true, enablePersistentHover=true, ie.forceCreateProcessApi=false, ie.forceShellWindowsApi=false, pageLoadStrategy=normal, ignoreZoomSetting=false, ie.fileUploadDialogTimeout=3000, version=9, platform=WINDOWS, proxy={proxyType=}, nativeEvents=true, ie.ensureCleanSession=false, elementScrollBehavior=0, ie.browserCommandLineSwitches=, requireWindowFocus=false, browserName=internet explorer, initialBrowserUrl=http://localhost:24652/, javascriptEnabled=true, ignoreProtectedModeSettings=false, enableElementCacheCleanup=true, unexpectedAlertBehaviour=dismiss}]
Session ID: df6bf10c-51d0-46b3-ab24-95897f29223f
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at org.openqa.selenium.remote.ErrorHandler.createThrowable(ErrorHandler.java:206)
    at org.openqa.selenium.remote.ErrorHandler.throwIfResponseFailed(ErrorHandler.java:158)
    at org.openqa.selenium.remote.RemoteWebDriver.execute(RemoteWebDriver.java:647)
    at org.openqa.selenium.remote.RemoteWebElement.execute(RemoteWebElement.java:326)
    at org.openqa.selenium.remote.RemoteWebElement.click(RemoteWebElement.java:84)
    at com.org.frameworkFiles.stepDefinitions.StepDefinitions.User_captures_Origin(StepDefinitions.java:173)
    at ?.And User captures Origin BRANCH(1_Create Application.feature:12)

以下是在我的applicationn中为下拉字段编写的Bootstrap代码:

这部分代码用于单击下拉列表:

<div class="btn-group bootstrap-select show-tick">
<button class="btn dropdown-toggle bs-placeholder btn-default" role="button" data-toggle="dropdown" type="button" data-id="ContentPlaceHolder1_ddlOrigin" title="Choose Origin Code">
<span class="filter-option pull-left">Choose Origin Code</span>
<span class="bs-caret">
<span class="caret"/>
</span>
</button> 

这部分代码用于选择下拉字段:

<div class="dropdown-menu open" role="combobox">
<ul class="dropdown-menu inner" aria-expanded="false" role="listbox">
<li data-original-index="0">
<a class="" role="option" data-tokens="null" style="" tabindex="0" aria-disabled="false" aria-selected="false">
<span class="text">BRANCH</span>
<span class="glyphicon glyphicon-ok check-mark"/>
</a>
</li>
<li data-original-index="1">
<a class="" role="option" data-tokens="null" style="" tabindex="0" aria-disabled="false" aria-selected="false">
<span class="text">CALL CENTRE</span>
<span class="glyphicon glyphicon-ok check-mark"/>
</a>
</li>
<li data-original-index="2">
<a class="" role="option" data-tokens="null" style="" tabindex="0" aria-disabled="false" aria-selected="false">
<span class="text">INTERNET</span>
<span class="glyphicon glyphicon-ok check-mark"/>
</a>
</li>
<li data-original-index="3">
<a class="" role="option" data-tokens="null" style="" tabindex="0" aria-disabled="false" aria-selected="false">
<span class="text">PRIVATE BANKING</span>
<span class="glyphicon glyphicon-ok check-mark"/>
</a>
</li>
</ul>
</div>

再次,选择HTML代码是用相同的字段编写的,但是,当我尝试通过萤火虫对此进行高亮显示时,则没有高亮显示:字段的高亮仅发生在上面的代码部分:

<select id="ContentPlaceHolder1_ddlOrigin" class="selectpicker" title="Choose Origin Code" data-max-options="1" multiple="" name="ctl00$ContentPlaceHolder1$ddlOrigin" tabindex="-98">
<option value="BRH">BRANCH</option>
<option value="CC">CALL CENTRE</option>
<option value="INT">INTERNET</option>
<option value="PB">PRIVATE BANKING</option>
</select>
</div>
<span id="ContentPlaceHolder1_RequiredFieldValidator8" class="zmdi zmdi-alert-triangle form-control-feedback" style="visibility:hidden;"/>
</div>
穆尔蒂

您没有单击下拉按钮。这是选择下拉选项的最少代码。请尝试让我知道。

   @And("^User captures Origin (.*)$")              
   public void User_captures_Origin(String origin) throws Exception{ 

    // click on drop down button
    WebElement DropdownButton = driver.findElement(By.xpath("//button[@data-id='ContentPlaceHolder1_ddlOrigin']"));
    DropdownButton.click();

    // select a drop down option
     WebElement DropdownOption= driver.findElement(By.xpath("//ul/li/a/span[text()="+origin+"]"));
     DropdownOption.click();
    }

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

我们如何知道在 BDD 方法中运行 Selenium Java 需要哪些 Cucumber jar 文件?

如何使用 Selenium 处理年份和月份的下拉列表?

使用 Selenium 从另一个框架中查找与下拉列表相关的元素 -- Python

如何使用Java中的Selenium WebDriver单击列表中的链接

如何使用Selenium Java从网站中的框架切换

如何使用Selenium Java从多选下拉列表中显示所选选项?

如何使用 Selenium 和 Java 从非选择下拉列表中单击并选择一个选项

如何使用Java处理Selenium WebDriver中的新窗口?

如何使用Java处理Selenium WebDriver中的iframe

如何使用Java处理Selenium WebDriver中的弹出窗口

如何等待Ajax调用正在处理Selenium中的下拉列表

如何处理 div 类型的下拉列表而不是 selenium/python 中的选择

如何在 selenium python 中处理没有选择的下拉列表

使用Java和Selenium使用输入标记构建下拉列表时如何填充下拉列表

使用 Java 处理 Selenium 列表

如何在带有JAVA的Cucumber框架中使用TestNG?

如何获取存储在 Cypress 框架中 Cucumber Feature File 数据表中的数字列表的总和?

如何使用python(Selenium)处理Selenium中的Windows身份验证弹出窗口(加Java)

如何在Selenium Webdriver中使用Java验证HTML代码中是否存在用于下拉列表的标记

如何使用selenium webdriver java从城市字段的GoIbibo中的自动选择下拉列表中选择一个元素

如何从 Selenium + Cucumber + Ruby 中的 H1 标签中获取文本

如何处理Reactstrap下拉列表中的选择

如何使用 Selenium 选择下拉列表?

Selenium-如何使用Java获取下拉列表的选项列表?

使用Java在Selenium WebDriver(Selenium 2)中处理警报

如何在Cucumber + Selenium Webdriver + Page-Object中重新启动浏览器?

在使用 Selenium 的 Cucumber 测试中未发现任何特征异常

如何使用Selenium Webdriver和Java在框架内的iframe中单击链接

如何使用Java在Selenium WebDriver中的框架之间切换