私はセレン2テストのコーディングにEclipseを使用しています.Excelの行単位と列単位から値を取得します.Excelシートで、「RESULT」という新しい列を追加したかったのですが、どうすればテスト結果を書くことができますか(合格/失敗) ) テスト ng の実行中に各行のこの列に対して
3 に答える
testNG は、テストが成功したか失敗したかを通知するリスナーを追加するメソッドを提供します。メソッドをオーバーライドしてonTestFailure, onTestSuccess
メソッドを定義し、値を Excel シートに書き込むことができます。リスナーの詳細はこちら
if (TestContext.CurrentTestOutcome != UnitTestOutcome.Passed)
{
writeToExcel();
}
Excel をタグとして入れていないので、そこに値を書き込む方法は知っていると思いますが、テストに合格したかどうかを判断する方法はわかりません。上記でうまくいくと思いますが、このマシンにはSeleniumがインストールされていないので、試してみてください...
さて、私は Apache POI を使用して Excel にデータを読み書きしています。テスト ケースが成功したか失敗したかを確認するには、アサーションの周りでtry - catchを使用できます。いずれかのアサーションが失敗した場合、それはテスト ケースが失敗したことを意味し、そのテスト ケースを Excel の catch ブロック内で FAIL としてマークできます。
catch ブロックの最後で以下のコマンドを必ず使用してください
Assert.fail(e) // where e is the exception captured
これにより、TestNG はこの実行を FAIL として報告します。これが使用されていない場合、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:");
}
}