Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/320.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Selenium2Java中生成测试报告_Java_Eclipse_Selenium_Testng - Fatal编程技术网

如何在Selenium2Java中生成测试报告

如何在Selenium2Java中生成测试报告,java,eclipse,selenium,testng,Java,Eclipse,Selenium,Testng,我正在使用eclipse对selenium 2测试进行编码,它从excel的行和列中获取值。现在在excel工作表中,我想添加一个新的列,称为“RESULT”。现在,在测试运行时,如何针对每一行的该列写入测试结果(通过/失败) if (TestContext.CurrentTestOutcome != UnitTestOutcome.Passed) { writeToExcel(); } 您没有将Excel作为标记,因此我假设您知道如何在那里写入值,但不知道如何判断测试是否通过。我相信上

我正在使用eclipse对selenium 2测试进行编码,它从excel的行和列中获取值。现在在excel工作表中,我想添加一个新的列,称为“RESULT”。现在,在测试运行时,如何针对每一行的该列写入测试结果(通过/失败)

if (TestContext.CurrentTestOutcome != UnitTestOutcome.Passed)
{
   writeToExcel();
}

您没有将Excel作为标记,因此我假设您知道如何在那里写入值,但不知道如何判断测试是否通过。我相信上面的方法可以做到这一点,但我没有在这台机器上安装Selenium,因此您可能只想尝试一下…

testNG提供了添加侦听器的方法,这些侦听器将告诉您测试是通过还是失败。您可以重写
onTestFailure、onTestSuccess
方法,并定义将值写入excel工作表的方法。您可以看到监听器的详细信息,我正在使用ApachePOI将数据读写到Excel。 要检查测试用例是否通过或失败,可以使用try-catch-around断言。 如果任何断言失败,这意味着您的测试用例已经失败,然后您可以在catch块内的Excel中将该测试用例标记为失败

确保在catch块末尾使用以下命令

Assert.fail(e)  // where e is the exception captured
这将使TestNG将此执行报告为失败。如果未使用此选项,则TestNG将此执行报告为PASS

以下是我用来将结果写入Excel的函数:

可变细节 功能详细信息
FilePath = excel file path
_Sheet = Worksheet name
result = Pass / Fail string that you want to print in excel
static int _TC01_ROWCOUNTER = 1; // Defines the row number of first data element in test data excel sheet.
    static int _TC01_COLUMNCOUNTER = 3; // Defines the Column position in which Result needs to be printed in test data excel sheet.
private void writeToExcel(String FilePath, String _Sheet, String result) throws Exception
    {
        try
        {
            _testdatastream = new FileInputStream(FilePath);  

            try
            {
                wb = new HSSFWorkbook(_testdatastream);  
                ws = wb.getSheet(_Sheet);

                try
                {
                    HSSFRow row = ws.getRow(_TC01_ROWCOUNTER);
                    HSSFCell cell = row.createCell(_TC01_COLUMNCOUNTER);
                    cell.setCellValue(result);
                    _testdatastream.close();

                    fileOut = new FileOutputStream(FilePath);  
                    wb.write(fileOut);  
                    fileOut.close();

                    _TC01_ROWCOUNTER++;
                }
                catch(Exception e)
                {
                    Logger.logger("Could not write result to test data file because of following reason"+"\n"+e,"ERR:");
                }

            }
            catch(Exception e)
            {
                Logger.logger("Mentioned worksheet"+_Sheet+" not found in test data file"+"\n"+e,"ERR:");
            }
        }
        catch(Exception e)
        {
            Logger.logger("Test Data file not found at location:"+FilePath+"\n"+e,"ERR:");
        }
    }