Java Code Examples for org.testng.ITestResult#setAttribute()

The following examples show how to use org.testng.ITestResult#setAttribute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: QAFTestNGListener.java    From qaf with MIT License 7 votes vote down vote up
public void beforeInvocation(IInvokedMethod method, ITestResult tr,
		ITestContext context) {
	QAFTestBase stb = TestBaseProvider.instance().get();
	stb.getLog().clear();
	stb.clearVerificationErrors();
	stb.getCheckPointResults().clear();
	logger.debug("beforeInvocation: " + method.getTestMethod().getMethodName());
	tr.setAttribute("context", context);
	ConfigurationManager.getBundle().setProperty(ApplicationProperties.CURRENT_TEST_CONTEXT.key, context);

	ConfigurationManager.getBundle().setProperty(ApplicationProperties.CURRENT_TEST_NAME.key,
			tr.getName());
	ConfigurationManager.getBundle().setProperty(ApplicationProperties.CURRENT_TEST_DESCRIPTION.key,
			tr.getMethod().getDescription());
	ConfigurationManager.getBundle().setProperty(ApplicationProperties.CURRENT_TEST_RESULT.key,
			tr);
}
 
Example 2
Source File: TestNgPlatformBase.java    From Selenium-Foundation with Apache License 2.0 5 votes vote down vote up
@Override
@SuppressWarnings("serial")
public void activatePlatform(WebDriver driver, P platform) throws PlatformActivationFailedException {
    ITestResult testResult = Reporter.getCurrentTestResult();
    if (testResult != null) {
        String description = testResult.getMethod().getDescription();
        PlatformIdentity<P> identity = DataUtils.fromString(description, new TypeToken<PlatformIdentity<P>>(){}.getType());
        if (identity != null) {
            testResult.setAttribute(PLATFORM, identity.deserialize());
        }
    }
}
 
Example 3
Source File: RetryTest.java    From allure-java with Apache License 2.0 5 votes vote down vote up
@Override
public boolean retryMethod(ITestResult result) {
    boolean willRetry = !result.isSuccess();
    if (willRetry) {
        result.setAttribute("retry", true);
    }
    return willRetry;
}
 
Example 4
Source File: QAFTestNGListener.java    From qaf with MIT License 5 votes vote down vote up
protected void report(ITestResult tr) {
	String[] groups = tr.getMethod().getGroups();
	if (null != groups && Arrays.asList(groups).contains("cucumber"))
		return;
	QAFTestBase stb = TestBaseProvider.instance().get();
	tr.setAttribute("browser", stb.getBrowser());

	org.testng.Reporter.setCurrentTestResult(tr);
	if (getBundle().getBoolean("report.log.testngoutput", false)) {
		HtmlCheckpointResultFormatter checkpointResultFormatter = new HtmlCheckpointResultFormatter();
		org.testng.Reporter.log(checkpointResultFormatter.getResults(stb.getCheckPointResults()));
	}
}
 
Example 5
Source File: ReporterUtil.java    From qaf with MIT License 5 votes vote down vote up
/**
 * should be called on test method completion
 * 
 * @param context
 * @param result
 */
public static void createMethodResult(ITestContext context, ITestResult result,
		List<LoggingBean> logs, List<CheckpointResultBean> checkpoints) {

	try {
		String dir = getClassDir(context, result);

		MethodResult methodResult = new MethodResult();

		methodResult.setSeleniumLog(logs);
		methodResult.setCheckPoints(checkpoints);
		methodResult.setThrowable(result.getThrowable());

		updateOverview(context, result);
		String fileName = getMethodIdentifier(result);//StringUtil.toTitleCaseIdentifier(getMethodName(result));
		String methodResultFile = dir + "/" + fileName;

		File f = new File(methodResultFile + ".json");
		if (f.exists()) {
			// if file already exists then it will append some random
			// character as suffix
			String suffix = "_"+indexer.incrementAndGet();
			fileName += suffix;
			// add updated file name as 'resultFileName' key in metaData
			methodResultFile = dir + "/" + fileName;
			result.setAttribute(QAF_TEST_IDENTIFIER,fileName);

			updateClassMetaInfo(context, result, fileName);
		} else {
			updateClassMetaInfo(context, result, fileName);
		}

		writeJsonObjectToFile(methodResultFile + ".json", methodResult);
	} catch (Exception e) {
		logger.warn(e.getMessage(), e);
	}

}
 
Example 6
Source File: ReporterUtil.java    From qaf with MIT License 5 votes vote down vote up
@SuppressWarnings("unchecked")
private static String getMethodIdentifier(ITestResult result){

	if(result.getAttribute(QAF_TEST_IDENTIFIER)!=null){
		return (String) result.getAttribute(QAF_TEST_IDENTIFIER);
	}
	
	String id = getMethodName(result);
	String identifierKey = ApplicationProperties.TESTCASE_IDENTIFIER_KEY.getStringVal("testCaseId");

	Map<String, Object> metadata =
			new TreeMap<String, Object>(String.CASE_INSENSITIVE_ORDER);

	if (result.getMethod() instanceof TestNGScenario) {
		TestNGScenario scenario = (TestNGScenario) result.getMethod();
		metadata.putAll(scenario.getMetaData());
	}
	if(result.getParameters()!=null && result.getParameters().length>0){
		if(result.getParameters()[0] instanceof Map<?, ?>){
			metadata.putAll((Map<String, Object>)result.getParameters()[0]);
		}
	}
	String idFromMetaData = metadata.getOrDefault(identifierKey,"").toString();
	if (StringUtil.isNotBlank(idFromMetaData) ) {
		id = idFromMetaData;
	}
	id=StringUtil.toTitleCaseIdentifier(id);
	
	if(id.length()>45){
		id=id.substring(0, 45);
	}
	result.setAttribute(QAF_TEST_IDENTIFIER,id);
	return (String) result.getAttribute(QAF_TEST_IDENTIFIER);
}
 
Example 7
Source File: ReportListener.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private void logMeasurements(ITestResult tr) {
    Object[] parameters = tr.getParameters();
    MeasuredTestContext measuredTestContext;
    if (parameters == null || parameters.length == 0) {
        return;
    } else if (parameters[0] instanceof MeasuredTestContext) {
        measuredTestContext = (MeasuredTestContext) parameters[0];
    } else {
        return;
    }
    tr.setAttribute(tr.getName() + MEASUREMENTS, measuredTestContext.getMeasure());
}
 
Example 8
Source File: Log.java    From cloudbreak with Apache License 2.0 5 votes vote down vote up
private static TestContextReporter getReporter() {
    ITestResult res = Reporter.getCurrentTestResult();
    if (res == null) {
        return null;
    }
    TestContextReporter reporter = (TestContextReporter) res.getAttribute(TEST_CONTEXT_REPORTER);
    if (reporter == null) {
        reporter = new TestContextReporter();
        res.setAttribute(TEST_CONTEXT_REPORTER, reporter);
    }

    return reporter;
}
 
Example 9
Source File: Log.java    From cloudbreak with Apache License 2.0 4 votes vote down vote up
private static void publishReport(TestContextReporter testContextReporter) {
    ITestResult res = Reporter.getCurrentTestResult();
    if (res != null) {
        res.setAttribute(ALTERNATE_LOG, testContextReporter.print());
    }
}
 
Example 10
Source File: CarinaListener.java    From carina with Apache License 2.0 4 votes vote down vote up
private void onTestFinish(ITestResult result) {
    try {
        // clear all kind of temporary properties
        R.CONFIG.clearTestProperties();
        R.TESTDATA.clearTestProperties();
        R.DATABASE.clearTestProperties();
        R.EMAIL.clearTestProperties();
        R.REPORT.clearTestProperties();
        R.ZAFIRA.clearTestProperties();
        
        LOGGER.debug("Test result is : " + result.getStatus());
        // result status == 2 means failure, status == 3 means skip. We need to quit driver anyway for failure and skip
        if ((automaticDriversCleanup && !hasDependencies(result)) || result.getStatus() == 2 || result.getStatus() == 3) {
            quitDrivers(Phase.BEFORE_METHOD, Phase.METHOD);
        }

        // TODO: improve later removing duplicates with AbstractTestListener
        // handle Zafira already passed exception for re-run and do nothing.
        // maybe return should be enough
        if (result.getThrowable() != null && result.getThrowable().getMessage() != null
                && result.getThrowable().getMessage().startsWith(SpecialKeywords.ALREADY_PASSED)) {
            // [VD] it is prohibited to release TestInfoByThread in this
            // place.!
            return;
        }

        // handle CarinaListener->SkipExecution
        if (result.getThrowable() != null && result.getThrowable().getMessage() != null
                && result.getThrowable().getMessage().startsWith(SpecialKeywords.SKIP_EXECUTION)) {
            // [VD] it is prohibited to release TestInfoByThread in this
            // place.!
            return;
        }

        List<String> tickets = Jira.getTickets(result);
        result.setAttribute(SpecialKeywords.JIRA_TICKET, tickets);
        Jira.updateAfterTest(result);

        // we shouldn't deregister info here as all retries will not work
        // TestNamingUtil.releaseZafiraTest();

    } catch (Exception e) {
        LOGGER.error("Exception in CarinaListener->onTestFinish!", e);
    }
}