3

使用下面提到的代码,如果测试用例通过截图成功并显示在报告中。但是当测试失败时——不显示截图。甚至截图超链接也不显示在报告中。任何人都可以在代码中解决错误?

package listeners;

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

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.apache.commons.io.FileUtils;
import org.openqa.selenium.By;
import org.openqa.selenium.OutputType;
import org.openqa.selenium.TakesScreenshot;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;    
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.annotations.Listeners;
import org.testng.annotations.Test;
import org.testng.ITestResult;
import org.testng.Reporter;
import org.testng.TestListenerAdapter;
import java.util.logging.Logger;


@Listeners

public class CountryChoserLayer extends TestListenerAdapter {


  @Test(priority=1)
  public void choseCountry() throws Exception{

      driver.findElement(By.id("intselect")).sendKeys("India");
        driver.findElement(By.xpath(".//*[@id='countryChooser']/a/img")).click();
        //window.onbeforeunload = null;
        Date date=new Date();
        Format formatter = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss");        
        File scrnsht = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
        String NewFileNamePath=("C://Documents and Settings//vlakshm//workspace//MyTNG//test-output//Screenshots"+"//SearsINTL_"+ formatter.format(date)+".png");  
        FileUtils.copyFile(scrnsht, new File(NewFileNamePath));
        System.out.println(NewFileNamePath);
        Reporter.log("<a href=\"" + NewFileNamePath + "\">Passed Screenshot</a>");
        System.out.println("---------------------------------------");
        System.out.println("Country choser layer test case-Success");
        System.out.println("---------------------------------------");

  }


  public String baseurl="http://www.sears.com/shc/s/CountryChooserView?storeId=10153&catalogId=12605";
  public WebDriver driver;
  public int Count = 0;

  @Test(priority=0)
  public void openBrowser() {

      driver = new FirefoxDriver();
      driver.manage().deleteAllCookies();
      driver.get(baseurl);
  }
  @Test(priority=2)
  public void closeBrowser() {
     driver.quit(); 
  }

@Override
public void onTestFailure(ITestResult result){

    Reporter.log("Fail");
    System.out.println("BBB");
    //Reporter.setCurrentTestResult(result);     
      Date date=new Date();
      Format formatter = new SimpleDateFormat("yyyy-MM-dd_hh-mm-ss");    


      File scrnsht = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
      //File scrFile = ((TakesScreenshot) WebDriver.globalDriverInstance).getScreenshotAs(OutputType.FILE);
      String NewFileNamePath=("C://Documents and Settings//vlakshm//workspace//MyTNG//test-output//Screenshots"+"//SearsINTL_"+ formatter.format(date)+".png"); 
      //System.out.println("AAA" + NewFileNamePath);
      try {
          //System.out.println("CCC");
        FileUtils.copyFile(scrnsht,new File(NewFileNamePath));
          System.out.println(NewFileNamePath);


    } catch (IOException e) {
        // TODO Auto-generated catch block
        System.out.println("DDD");
        e.printStackTrace();
    }
      Reporter.log("<a href=\"" + NewFileNamePath + "\">Failed Screenshot</a>");
      Reporter.setCurrentTestResult(null);
    System.out.println("---------------------------------------");
    System.out.println("Country choser layer test case Failed");
    System.out.println("---------------------------------------");

      }

      @Override
      public void onTestSkipped(ITestResult result) {
      // will be called after test will be skipped
        Reporter.log("Skip");
      }
      @Override
      public void onTestSuccess(ITestResult result) {
      // will be called after test will pass
        Reporter.log("Pass");
      }

}
4

1 回答 1

2

你的 onTestFailure 方法没有被调用,因为你没有为你的测试类指定监听器。您在 @Listeners 注释中缺少一个值。它应该是这样的

@Listeners({CountryChoserLayer.class})

您可以在TestNg 的官方文档中找到更多指定侦听器的方法。

您可能会遇到的另一个问题是在 onTestFailure 方法中尝试截屏时出现 NullPointerException。最简单的解决方法是将驱动程序字段的声明更改为静态。我使用这些修复程序运行代码,并获得了带有屏幕截图的报告。

我必须补充一点,我认为将测试和侦听器方法放在一个类中并不是一个好习惯。

于 2013-06-09T20:21:04.723 回答