Java - 将数组的第1列传递给driver.get()并断言到第2列

时间:2018-05-13 01:02:29

标签: java selenium testng testng-dataprovider

我正在编写一个验证网址重定向的脚本。我有一个包含两列的电子表格。一列是我需要传递给driver.get()的URL,第二列是URL是预期的URL。

我的问题是使用VerifyRedirectURL。我如何通过列[0]传递迭代通过它传递给driver.get()。然后用列[1]断言。

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Iterator;

import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class UrlRedirectCheck {

public class ReadExcelDataProvider {
    public WebDriver driver;
    public WebDriverWait wait;

    @BeforeClass
    public void testSetup() {
        driver=new FirefoxDriver ();
        driver.manage().window().maximize();
        wait = new WebDriverWait(driver, 5);
    }


    @Test(dataProvider="Urls")
    public void VerifyRedirectURL(String RedirectedUrl, String ExpectedURL) {

        // For loop for RedirectURL (column 1 from spreadsheet)
        {
            driver.get ();
            driver.getCurrentUrl ();
            // driver.getCurrentURL, column 2 from array
            assert.assertEquals()
        }

    }


    @DataProvider(name="Urls")
    public Object[][] MilbURL() {
        Object[][] arrayObject = getExcelData("<URL path>","Sheet1");
        return arrayObject;
    }

    /**
     * @param File Name
     * @param Sheet Name
     * @return
     */
    @Test
    public String[][] getExcelData(String fileName, String sheetName) {
        String[][] arrayExcelData = null;
        try {
            FileInputStream fs = new FileInputStream (fileName);
            Workbook wb = Workbook.getWorkbook(fs);
            Sheet sh = wb.getSheet(sheetName);

            int totalNoOfCols = sh.getColumns();
            int totalNoOfRows = sh.getRows();

            arrayExcelData = new String[totalNoOfRows-1][totalNoOfCols];

            for (int i= 1 ; i < totalNoOfRows; i++) {

                for (int j=0; j < totalNoOfCols; j++) {
                    arrayExcelData[i-1][j] = sh.getCell(j, i).getContents();
                }

            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
            e.printStackTrace();
        } catch (BiffException e) {
            e.printStackTrace();
        }
        return arrayExcelData;
    }

    @Test
    public void tearDown() {
        driver.quit();
    }
}

}

0 个答案:

没有答案