TestNG 断言类重写

断言失败截图,且不退出用例,提供结算方法结算断言

public class SoftAssert extends Assertion {

    private WebDriver driver;

    private final String SCREENSHOT_PATH =System.getProperty("user.dir")+"/target/testreport/screenshot/";
    public SoftAssert(WebDriver driver){
        this.driver=driver;
    }
    private Map<AssertionError, IAssert> m_errors = Maps.newLinkedHashMap();
    private List<String> screenshotList =new ArrayList<>();

    /**
     * 重写方法,断言失败不退出用例,加入失败截图
     * @param a
     */
    @Override
    public void executeAssert(IAssert a) {
        try {
            a.doAssert();
        } catch(AssertionError ex) {
            onAssertFailure(a, ex);
            m_errors.put(ex, a);
            BasisUtil util=new BasisUtil();
            screenshotList.add(util.getScreenshot(driver, SCREENSHOT_PATH));

        }
    }


    /**
     * 结算
     */
    public void assertAll() {
        if (! m_errors.isEmpty()) {
            StringBuilder sb = new StringBuilder("The following asserts failed:\n");
            boolean first = true;
            for (Map.Entry<AssertionError, IAssert> ae : m_errors.entrySet()) {
                if (first) {
                    first = false;
                } else {
                    sb.append(", ");
                }
                sb.append(ae.getKey().getMessage());
            }
            throw new AssertionError(sb.toString());
        }
    }
}

 

posted @ 2015-03-19 00:17  脱氧  阅读(1019)  评论(0编辑  收藏  举报