for循环使用jxl将断言结果发送到excel

时间:2017-09-02 06:25:38

标签: selenium selenium-webdriver

我正在尝试编写基于assert写入pass / fail的selenium脚本。

我面临的问题是断言结果没有正确插入excel。我写的for循环有一些问题。请帮我修理它。

提前感谢。

这是我写的代码

package DDT;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Map;

import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import org.testng.asserts.SoftAssert;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;
import jxl.read.biff.BiffException;
import jxl.write.Label;
import jxl.write.WriteException;
import jxl.write.biff.RowsExceededException;

public class TestPass extends ExeclUtility
{
    WebDriver driver;
    Map<String,Object[]> Exp;
    @BeforeClass
    public void openBrowser()//opening of the browser is done only 1s before executing the test data
    {
        System.setProperty("webdriver.chrome.driver","C:\\Users\\DELL\\Desktop\\shree\\qsp_java_selenium\\selenium\\selenium_web_driver\\chrome_driver\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.get("file:///C:/Users/DELL/Desktop/USN%20and%20Sem.html");
    }
    @Test(dataProvider="TestData")
    public void datasend(String username,String phnum,String Exp) throws InterruptedException, RowsExceededException, WriteException

    {
        WebElement name=driver.findElement(By.id("field1"));
        name.clear();
        name.sendKeys(username);
        WebElement pwd=driver.findElement(By.id("field2"));
        pwd.clear();
        pwd.sendKeys(phnum);
        WebElement but=driver.findElement(By.id("field4"));
        but.click();
        Alert alert=driver.switchTo().alert();
        SoftAssert assertion=new SoftAssert();

            for(int i=0;i<6;i++){
            try
            {
                assertion.assertEquals(Exp, alert.getText());// compares the expected result in excel v/s the actual o/p
                Testcase="pass";

                Label l3=new Label(3,i,Testcase);
                writablesh.addCell(l3);

            }
            catch(Exception e)
            {
                Testcase="fail";

                Label l3=new Label(3,i,Testcase);
                writablesh.addCell(l3);

            }}

        assertion.assertEquals(Exp, alert.getText());
        alert.accept();
        assertion.assertAll();
        Thread.sleep(3000);
    }
    @AfterClass
    public void closeBrowser()//browser is closed 1s after all the test data are executed
    {
        driver.quit();
    }
    @DataProvider(name = "TestData")
    public Object[][] ExcelData() throws BiffException, IOException
    {
        String Filepath="D:\\selenium_pgms\\intern_practice\\Book1.xls";
        FileInputStream Excelfile=new FileInputStream(Filepath);
        Workbook Exbook=Workbook.getWorkbook(Excelfile);
        Sheet Exsheet=Exbook.getSheet("Sheet1");
        int Rows=Exsheet.getRows();
        int Columns=Exsheet.getColumns();
        String Testdata[][]= new String[Rows-1][Columns];
        int count=0;
        for(int i=1;i<Rows;i++)
        {
            for(int j=0;j<Columns;j++)
            {
                Cell Excell = Exsheet.getCell(j,i);
                Testdata[count][j]=Excell.getContents();
            }
            count++;

        }
        Excelfile.close();
        return Testdata;
    }
}

0 个答案:

没有答案