How to SetUp Object Repository in Selenium?

2019-09-21 08:40发布

问题:

I am trying to setup an Object Repository in Selenium Webdriver in order to make sure that future changes in objects don't impact my script.

回答1:

From my point of view PageObjects are the way to go.

That simple!



回答2:

  1. Have a CSV with all the object locator's....
  2. A CSV parser and store all the values in a multidimensional array with column and and row as index..
  3. Use that array wherever you need to use the locater in the code...
  4. If you don't want to have the hard-coded index values for the array then you will need to have some increment mechanism...


回答3:

  try{
    while(loc_index<=loc_type.size()-1){
    //  System.out.println("1sy");
    while(loc_val_index<=loc_val.size()){
        while(obj_index<=obj_value.size()-1){   
          String loc_data = loc_type.get(loc_index);
          if(loc_data.equals("name")){           
     WebElement element = driver.findElement(By.name(loc_val.get(loc_val_index)));
    if (element!=null){
        try {
       element.sendKeys(obj_value.get(obj_index)); } catch (Exception e){}
       obj_index++;}}else if(loc_data.equals("xpath")){
      WebElement element = driver.findElement(By.xpath(loc_val.get(loc_val_index)));
      if (element!=null){                            
          element.sendKeys(obj_value.get(obj_index));  
           Log("Data Entered");
          obj_index++;}}
      else if(loc_data.equals("id")){ 
      try{
          WebElement element = driver.findElement(By.id(loc_val.get(loc_val_index))); 
          if (element!=null){
             element.sendKeys(obj_value.get(obj_index));                                
               obj_index++;}} catch (Exception e) {}}
             break;}
        loc_val_index++;
        break;}
    loc_index++;
    }
        }catch (Exception e){}
        finally{
            obj_index=0;
            loc_index=0;
            loc_val_index=0;
        }