Java 如何在五个列表框中选择选项而不重复代码(Web元素-列表框)?
共有5个列表框,我在其中分别选择了1个选项:Java 如何在五个列表框中选择选项而不重复代码(Web元素-列表框)?,java,selenium,selenium-webdriver,Java,Selenium,Selenium Webdriver,共有5个列表框,我在其中分别选择了1个选项: WebElement listBox=driver.findElement(By.id("country"));//click on country text box Select select1=new Select(listBox); select1.selectByVisibleText("India");//select india WebElement listBox1=driver.findElement(By.id("state"))
WebElement listBox=driver.findElement(By.id("country"));//click on country text box
Select select1=new Select(listBox);
select1.selectByVisibleText("India");//select india
WebElement listBox1=driver.findElement(By.id("state"));//click on state text box
Select select2=new Select(listBox1);
select2.selectByVisibleText("delhi");//select delhi
WebElement listBox2=driver.findElement(By.id("district"));//click on district text box
Select select3=new Select(listBox2);
select3.selectByVisibleText("NCR");//select NCR
WebElement listBox3=driver.findElement(By.id("block"));//click on block
Select select4=new Select(listBox3);
select4.selectByVisibleText("Block1");//select Block1
WebElement listBox4=driver.findElement(By.id("village"));//click on village text box
Select select5=new Select(listBox4);
select5.selectByVisibleText("south");//select south
上面的代码太长
但是我必须通过组合列表框的代码来减少代码长度。创建一个方法来查找下拉列表并选择一个选项
public void chooseFromDropdown(By locator, String text) {
WebElement listBox = driver.findElement(locator);
Select select = new Select(listBox);
select.selectByVisibleText(text);
}
这样称呼它
chooseFromDropdown(By.id("country"), "india");
chooseFromDropdown(By.id("state"), "delhi");
// ...
您可以创建元素一次,如下所示:-
WebElement dropboxelement=driver.findElement(By.id("country"));
Select select1=new Select(dropboxelement);
select1.selectByVisibleText("India");
dropboxelement = driver.findElement(By.id("state"));
select1.selectByVisibleText("delhi");
以此类推
试一试
希望它能帮助您:)我的建议是,为您的硒测试使用一个框架,如Cucumber或Specflow。当然,这需要一些学习,但比单独使用Selenium要有效得多。这样,您的测试将如下所示:
When user selects items in dropdownbox
| dropdownboxID| selectOption|
| country | india |
| state | delhi |
| district | NCR |
| block | Block1 |
| village | south |
创建表格后,在代码(在本例中为C#)中定义以下代码:
[When(@"user selects items in dropdownbox")]
public void UserSelectsItemsInDropdownbox(Table table){
var parameters = table.CreateSet<Parameters>();
foreach (var parameter in parameters) {
var dropdownbox = Driver.FindElement(By.Name(dropdownboxID));
var selectElement = new SelectElement(dropdownbox);
selectElement.SelectByText(parameters.selectOption);
}
}
[当(@“用户选择下拉框中的项目时”)]
public void UserSelectsItemsInDropdownbox(表){
var参数=table.CreateSet();
foreach(参数中的var参数){
var dropdownbox=Driver.FindElement(By.Name(dropdownboxID));
var selectElement=新的selectElement(下拉框);
selectElement.SelectByText(parameters.selectOption);
}
}
这样,这是您需要的唯一代码。其余数据可以通过specflow插入。您需要在不同的类中定义参数在本例中,我将我的类命名为parameters.cs以使用多个参数进行迭代:
String[] items = new String[] {
"country", "India",
"state", "delhi",
"district", "NCR",
"block", "Block1",
"village", "south",
};
for (int i = 0; i < items.length; i += 2) {
String name = items[i];
String text = items[i + 1];
WebElement element = driver.findElement(By.name(name));
new Select(element).selectByVisibleText(text);
}
String[]项=新字符串[]{
“国家”、“印度”,
“国家”、“德里”,
“地区”、“NCR”,
“区块”、“区块1”,
“村庄”、“南部”,
};
对于(int i=0;i
谢谢你的帮助,代码减少了一点。这对我很有效,非常感谢,代码减少了很多@ana,我将确保学习cucumber和specflow,因为这对测试工程师来说至关重要