从不同类中的Hashmap获取值

时间:2016-10-17 10:38:45

标签: java selenium hashmap

我已经编写了一段代码,用于在我的selenium scipt中使用定位器在hashmap中也编写了一个入口集来从地图中获取值。

public class ReadDatafromExcel {

    @Test
    public void putIntoHashmap() throws IOException     
    {

        FileInputStream inputStream = new FileInputStream("/home/suvin/Documents/SeleniumWebdriver/Script/PaytmAutomationFramework/src/testUtilities/TestData.xlsx");

            XSSFWorkbook workbook= new XSSFWorkbook(inputStream);

            XSSFSheet firstSheet = workbook.getSheetAt(0);
            Iterator<Row> rowiterator = firstSheet.iterator();

            HashMap<String,String> map= new HashMap<String, String>();
            String key= null;
            String value= null;

            while (rowiterator.hasNext()) {
                Row nextRow = rowiterator.next();

                Iterator<Cell> cellIterator = nextRow.cellIterator();
                int i;  
                for (i=0;i<=1;i++)
                {
                    Cell cell = cellIterator.next();

                    if(i==0)
                    { 
                        key =cell.getStringCellValue();
                    }
                    if(i==1)
                    {
                        value=cell.getStringCellValue();
                        break;
                    }

                }
                map.put(key, value);    
            }

           // String Retrieved_value= map.get("Test11");
          //  System.out.println(Retrieved_value);

            Set set= (Set)map.entrySet();
            Iterator itr= set.iterator();
            while(itr.hasNext())
            {
                Map.Entry entry= (Entry)itr.next();    
                System.out.println(entry.getKey()+":"+entry.getValue());

            }

        }
}

现在我想在另一个类中使用这个hasmap。我在新类

中创建了一个ReadDatafromExcel类的对象rd
public class ccTransactions {

    public ReadDatafromExcel rd = new ReadDatafromExcel();

}

我想在此类中使用在ReadDatafromExcel中创建的hasmap的值。我该怎么办?

类似

driver.findelement(By.Xpath(USE VALUE FROM HASHMAP)).sendkeys();

2 个答案:

答案 0 :(得分:0)

map更改为班级ReadDatafromExcel的字段 像private HashMap<String,String> map= new HashMap<String, String>(); 并从方法putIntoHashmap()中删除它,然后添加另一种方法

public String getValue(String key){
return map.get(key);
}

然后您可以将其用作rd.get(key);

答案 1 :(得分:0)

按如下方式修改方法:

   public Map<String, String> putIntoHashmap(String filePath) throws IOException 

    {

            FileInputStream inputStream = new FileInputStream(filePath);

            XSSFWorkbook workbook= new XSSFWorkbook(inputStream);

            XSSFSheet firstSheet = workbook.getSheetAt(0);
            Iterator<Row> rowiterator = firstSheet.iterator();

            HashMap<String,String> map= new HashMap<String, String>();
            String key= null;
            String value= null;

            while (rowiterator.hasNext()) {
                Row nextRow = rowiterator.next();

                Iterator<Cell> cellIterator = nextRow.cellIterator();

                key = nextRow.getCell(0).getStringCellValue();
                value = nextRow.getCell(1).getStringCellValue();

                map.put(key, value);    
            }

         return map;
    }

现在,在创建类的对象后调用该方法,如:

ReadDatafromExcel rd = new ReadDatafromExcel();
Map<String, String> data = rd.putIntoHashmap("Path to your excel file");

然后,使用测试方法中的数据:

driver.findelement(By.Xpath(data.get("**your desired key**"))).sendkeys();