繁体   English   中英

如何使用Selenium WebDriver TestNG数据提供程序方法从Excel工作表中读取特定值?

[英]How to read a specific value from Excel sheet using Selenium WebDriver TestNG Data Provider Method?

以下是代码: -

package sanityTests;

import java.io.File;
import java.io.IOException;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;


import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;

public class DataDrivenTest2 {


    @Test (dataProvider= "testdata")  //attribute

    public void add(String x , String y){
        int a= Integer.parseInt(x);   //Convert String to integer
        int b = Integer.parseInt(y);
        int z=a+b;
        System.out.println(z);
    }


    @DataProvider(name="testdata")   //annotation for TestNG


    public Object [] [] readExcel() throws BiffException, IOException   //Way of Object Declaration of Array.Main Method Removed because of test NG Framework
{       

        File f = new File("C:/Users/Ishan/Desktop/Input2.xls");  //Create File Object to locate file on system
        Workbook w = Workbook.getWorkbook(f); //Activate the Workbook
        Sheet s = w.getSheet(0);  //Activate Sheet (either by name or id). Id starts with 0
        int rows= s.getRows() ; //Get Row Count
        System.out.println(rows); //Get Column Count
        int columns = s.getColumns();
        System.out.println(columns);

    String InputData[] [] = new String [rows] [columns];  //Array to read data from excel file

    for(int i=0; i<rows; i++){
        for(int j=0; j<columns;j++){
            Cell c =s.getCell(j, i);  // Getting location of cell

            InputData[i][j] =c.getContents();
        System.out.println(InputData[i][j]);


    }

}
    return InputData;
}
}

现在它返回输入Excel中提到的所有值和值的总和。 以下是输入值

1 2 3 4 5 6

现在,我只想从特定的行和列(例如行1,列1)中选择值,然后将它们相加并显示总和。 我试图在输入数据字段中输入1,1但我没有得到输出。 请帮忙。

如果您想要来自特定单元格的数据,那么您可以这样做:

        File f = new File("/Users/Pankaj/Desktop/QA_Automation/qascripting/Stacks/files/Input2.xls");  //Create File Object to locate file on system
        Workbook w = Workbook.getWorkbook(f); //Activate the Workbook
        Sheet s = w.getSheet(0);  //Activate Sheet (either by name or id). Id starts with 0
        int rows= s.getRows() ; //Get Row Count
        int columns = s.getColumns();

        //sheet's columns and rows are started with (column = 0, row = 0)
        int firstColumn = 0;
        int secondColumn = 1;
        int secondRow = 1;

        int firstColumnData = Integer.parseInt(s.getCell(firstColumn, secondRow).getContents());
        int secondColumnData = Integer.parseInt(s.getCell(secondColumn, secondRow).getContents());

        int desiredData = firstColumnData + secondColumnData;

        System.out.println("First Column: "+firstColumnData + " Second Column Data: "+secondColumnData + " Sum Data: "+desiredData);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM