java - 当每个下一个生成的元素都有不同的名称时,如何在 selenium webdriver java 中使用循环

标签 java loops for-loop selenium-webdriver

UI Project

上图的场景是用户可以填写每个合作伙伴的所有详细信息,用户可以通过点击添加合作伙伴按钮最多添加20个合作伙伴,每次用户都会添加一个新的合作伙伴详细信息网页元素将生成一个新元素,例如:

合作伙伴 1 的名字文本框是 = partner[0][person][given_name]。

如果用户添加另一组合作伙伴详细信息,则合作伙伴 2 给定名称的元素将是:

合作伙伴 2 的名字文本框是 = partner[ 1 ][person][given_name] .

因此每次用户添加合作伙伴详细信息时,元素的索引都会增加。

这是我使用 selenium webdriver 使用 java 和混合自动化框架的代码。我想在这段代码上使用循环,是否可以在这种框架中使用循环?因为如果我不必使用循环,我必须编写我的 Action 类 20 次,我将需要为每个合作伙伴找到所有元素,这是不合逻辑的事情。请指导我。关于如何让这些代码更简单,特别是让我的代码更短。

//这是我的页面对象存储库(它是我存储网页元素的地方) //我在我的元素索引上放置了一个参数(值)

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

//等等..对于其他领域

这是我的 Action 类(它是我放置 ActionScript 的地方,您还将在本节中看到我获取数据的地方,我从 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++;

}

我也在使用模块化框架,它是我的主类所在的位置。如果我错了,请纠正我,我认为我会把我的 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);
  }

最佳答案

可能的解决方案是将合作伙伴值作为参数传递,因为您使用页面 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;
}

并将该值作为0传递,并在向用户添加新伙伴后将其递增。

int val=0; //initialize a variable
com.businessname.pageobjects.RegistrationPage.txtbx_GivenName(val).sendKeys(sGivenName);
//add a new partner
val++; //increment the value

关于java - 当每个下一个生成的元素都有不同的名称时,如何在 selenium webdriver java 中使用循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25737493/

相关文章:

java - 如何克服访问者实现的类型删除问题

具有多线程的 Java 解析器

loops - 为交互式 IO : problems with do-notation and layout 编写循环

python - 在 Pandas 中获得滚动平均值的最快方法?

R 增量 - 避免循环

python - 嵌套 for 循环和 if 条件 pandas 替换

javascript - 对于语句和 img 问题

java - 关于不可变类防御性复制

JavaScript - 刚刚学习

java - 我想将 Facebook 帖子从公共(public)页面提取到 Java 应用程序