我在BDD框架中構建了我的腳本。我嘗試了幾種方法來處理我正在測試的應用程序中使用的Bootstrap下拉菜單。如何在java-selenium中使用黃瓜bdd框架處理bootstrap下拉框
但沒有運氣。
如何使用我的框架爲我的應用程序處理引導程序下拉列表?
UIAction類文件:
我編寫這將常見的表現,我在另一個類在以往任何時候需要調用這個函數法對所有的UI操作。
因此,對於下拉列表,我嘗試使用UIAction中編寫的select方法,如下所示。
在UIAction類選擇方法:
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類文件選擇功能在我的框架的方法:
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的建議處理引導下拉列表中,但是,這也因此未爲我工作:
@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:
這部分代碼是點擊下拉:
<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代碼相同的字段,但是,當我嘗試過螢火蟲自認倒黴到higlight是突出寫與上面的部分代碼:
<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>
在此猜測 - HTML選擇標記不可見,因此使用「選擇」編寫的代碼將不起作用。你的代碼應該與ul和li交互,因爲它們是可見的組件。 – Grasshopper
@Grasshopper,在我的腳本中,我試圖只與ul和li組件交互,你可以檢查我的xpath - // span [contains(text(),'BRANCH')]。我從Firebug複製的第一個xpath點擊該下拉列表。但是當我運行我的腳本時,點擊本身並沒有發生。 – SAUMARS
您是否嘗試過使用更簡單的CSS選擇器而不是長xpaths ...在您的腳本中,您正在移動到該元素,但未點擊。您打印的鏈接數量的值是多少?哪一行是第173行? – Grasshopper