当每个下一个生成的元素都有不同的名称时,如何在 selenium webdriver java 中使用循环
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/25737493/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to use looping in selenium webdriver java when every next generated element will have different names
提问by user3713453
The scenario of the image above is the user can fill in all the details per partner, the user can add a maximum of 20 partners by clicking the add partner button, every time the user will add a new partner details the webpage elements will generate a new elements for example:
上图的场景是用户可以填写每个合作伙伴的所有详细信息,用户最多可以通过点击添加合作伙伴按钮添加20个合作伙伴,每次用户添加新的合作伙伴详细信息网页元素会生成一个新元素例如:
given name textbox for partner 1 is = partner[0][person][given_name] .
合作伙伴 1 的给定名称文本框是 = partner[0][person][given_name]。
if the user adds another set of partner details the element for partner 2 given name will be:
如果用户添加另一组合作伙伴详细信息,则合作伙伴 2 给定名称的元素将是:
given name textbox for partner 2 is = partner[1][person][given_name] .
合作伙伴 2 的给定名称文本框是 = partner[ 1][person][given_name] 。
so every time the user adds a partner details the index for elements will increment.
因此,每次用户添加合作伙伴详细信息时,元素的索引都会增加。
Here is my code im using selenium webdriver using java and hybrid automation framework. I want to use looping on this code, is it possible to use looping in this kind of framework? cause if i don't have to use loop, i have to write my action class like 20 times and i will need to find all the elements for each partner which is not logical thing to do. Please guide me. on how to make these code easier specially on making my code shorter.
这是我使用 java 和混合自动化框架使用 selenium webdriver 的代码。我想在这段代码上使用循环,是否可以在这种框架中使用循环?因为如果我不必使用循环,我必须编写我的动作类 20 次,我需要为每个合作伙伴找到所有元素,这不是合乎逻辑的事情。请指导我。关于如何使这些代码更容易特别是使我的代码更短。
// Here's my Page Object repository (its where i store my web page elements) // I have put a parameter (value) on my elements index
// 这是我的页面对象存储库(我存储网页元素的地方) // 我在元素索引上放置了一个参数(值)
public class RegistrationPage extends BaseClass {
private static WebElement element;
public RegistrationPage(WebDriver driver){
super(driver);
}
public static WebElement txtbx_GivenName(int value){
try{
element = driver.findElement(By.name("partner["+value+"][person][given_name]"));
Log.info("Given name textbox found");
}catch (Exception e){
Log.error("Given name textbox is not found on the Register Page");
throw(e);
}
return element;
}
public static WebElement txtbx_LastName(int value){
try{
element = driver.findElement(By.name("partner["+value+"][person][last_name]"));
Log.info("Last name textbox found");
}catch (Exception e){
Log.error("Last name textbox is not found on the Register Page");
throw(e);
}
return element;
}
public static WebElement txtbx_Email(int value){
try{
element = driver.findElement(By.name("partner["+value+"][person][email]"));
Log.info("Email textbox found");
}catch (Exception e){
Log.error("Email textbox is not found on the Register Page");
throw(e);
}
return element;
}
public static WebElement txtbx_ContactNumber(int value){
try{
element = driver.findElement(By.name("partner["+value+"][contact_number]"));
Log.info("Contact number textbox found");
}catch (Exception e){
Log.error("Contact number textbox is not found on the Register Page");
throw (e);
}
return element;
}
// and so on.. for other fields
// 依此类推.. 对于其他领域
Here is my action class (its where i put my action script and you will also see in this section is where i get my data, i get my data from excel)
这是我的动作类(我将动作脚本放在这里,您还将在本节中看到我获取数据的位置,我从 excel 获取数据的位置)
public class RegisterPartnerDetails_Action {
public static void Execute (int iTestCaseRow) throws Exception {
int val = 0;
String sGivenName = ExcelUtils.getCellData(iTestCaseRow, Constant.COL_GIVEN_NAME);
Log.info("Given name picked from Excel is " + sGivenName);
com.businessname.pageobjects.RegistrationPage.txtbx_GivenName(val).sendKeys(sGivenName);
Log.info("Given name entered in the Given name text box");
String sLastName = ExcelUtils.getCellData(iTestCaseRow, Constant.COL_LAST_NAME);
Log.info("Last name picked from Excel is " + sLastName);
com.businessname.pageobjects.RegistrationPage.txtbx_LastName(val).sendKeys(sLastName);
Log.info("Last name entered in the Lastname text box");
String sEmail = ExcelUtils.getCellData(iTestCaseRow, Constant.COL_EMAIL);
Log.info("email picked from Excel is " + sEmail);
com.businessname.pageobjects.RegistrationPage.txtbx_Email(val).sendKeys(sEmail);
Log.info("Email entered in the email text box");
String sContactNumber = ExcelUtils.getCellData(iTestCaseRow, Constant.COL_CONTACT_NUMBER);
Log.info("Contactnumber picked from Excel is " + sContactNumber);
com.businessname.pageobjects.RegistrationPage.txtbx_ContactNumber(val).sendKeys(sContactNumber);
Log.info("Contact number entered in the contact number text box");
//etc for other details
val++;
} }
} }
I'm using also modular framework, its where my main class is located. Correct me if im wrong im thinking that i will put my for loop here right?
我也在使用模块化框架,它是我的主类所在的位置。如果我错了,请纠正我,我认为我会将我的 for 循环放在这里,对吗?
@Test
public void main() throws Exception {
com.businessname.modules.SearchBusinessname_Action.Execute(iTestCaseRow);
com.businessname.modules.InputAustralianBusinessNumber_Action.Execute(iTestCaseRow)
// Here is the registration script part, i will need to put for loop on this right?
com.businessname.modules.RegisterPartnerDetails_Action.Execute(iTestCaseRow);
}
回答by Vignesh Paramasivam
possible solution will be passing the partner value as a parameter, since you use page obj repository this will be a better approach,
可能的解决方案是将合作伙伴值作为参数传递,因为您使用页面 obj 存储库,这将是更好的方法,
public static WebElement txtbx_GivenName(int value){
try{
element = driver.findElement(By.name("partner["+value+"][person][given_name]"));
Log.info("Given name textbox found");
}catch (Exception e){
Log.error("Given name textbox is not found on the Register Page");
throw(e);
}
return element;
}
And pass the value as 0, and increment it after adding new partner to the users.
并将该值传递为0,并在向用户添加新合作伙伴后增加它。
int val=0; //initialize a variable
com.businessname.pageobjects.RegistrationPage.txtbx_GivenName(val).sendKeys(sGivenName);
//add a new partner
val++; //increment the value
回答by Abhinav Kumar Singh
In case if loop doesn't work my solution is as follows:
如果循环不起作用,我的解决方案如下:
1) Create an xml suppose name is "abc.xml" file which will contain all of the predictable ids could be as
1)创建一个 xml 假设名称是“abc.xml”文件,该文件将包含所有可预测的 id 可以是
<dynamicid>
<id>
<textboxid>
partner[0][person][given_name]
</textboxid>
</id>
<id>
<textboxid>
partner[1][person][given_name]
</textboxid>
</id>
....
....
<id>
<textboxid>
partner[20][person][given_name]
</textboxid>
</id>
....
....
</dynamicid>
2) after that parse the xml as
2)之后将xml解析为
File file = new File(abc_xml);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(file);
document.getDocumentElement().normalize();
NodeList emailNodeElementList = document.getElementsByTagName("id");
for(int j=0;j<emailNodeElementList.getLength(); j++)//loop for the multiple data
{
//parsing the xml
Node nNode = emailNodeElementList.item(j);
Element eElement = (Element) nNode;
//Now we get the values from the xml
String Textboxid=eElement.getElementsByTagName("textboxid").item(0).getTextContent();
WebElement xyz=driver.findElements(By.cssselector("input[name='+Textboxid+']")).sendkeys("abcdef");
}
Hope this will help you.
希望这会帮助你。
please note that I have an interface from where I am getting the interface name example:
请注意,我有一个从中获取接口名称示例的接口:
public interface interface1
{
String abcde = "Foldername/nameofxmlfile";
}
There are various ways to parse Xml you can view at http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/
有多种解析 Xml 的方法,您可以在http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/查看