@BeforeMethod public void before(Method m, ITestResult r) { m_testResult = r; r.setAttribute("before", 10); }
static void copyAttributes(ITestResult source, ITestResult target) { source .getAttributeNames() .forEach(name -> target.setAttribute(name, source.getAttribute(name))); }
@BeforeMethod public void setUp(ITestResult tr) { tr.setAttribute("class", getClass().getName()); tr.setAttribute("toString", tr.toString()); }
@AfterMethod(alwaysRun = true) public void childClassAfterMethod(Method method, ITestResult result) { result.setAttribute( getClass().getSimpleName() + "-childClassAfterMethod", getClass().getName() + ".childClassAfterMethod()"); } }
@BeforeMethod(alwaysRun = true) public void parentClassBeforeMethod(Method method, ITestResult result) { result.setAttribute( getClass().getSimpleName() + "-parentClassBeforeMethod", getClass().getName() + ".parentClassBeforeMethod()"); throw new RuntimeException("Forcing a failure"); }
@AfterMethod(alwaysRun = true) public void parentClassAfterMethod(Method method, ITestResult result) { result.setAttribute( getClass().getSimpleName() + "-parentClassAfterMethod", getClass().getName() + ".parentClassAfterMethod()"); throw new RuntimeException("Forcing a failure"); } }
@BeforeMethod(alwaysRun = true) public void childClassBeforeMethod(Method method, ITestResult result) { result.setAttribute( getClass().getSimpleName() + "-childClassBeforeMethod", getClass().getName() + ".childClassBeforeMethod()"); throw new RuntimeException("Forcing a failure"); }
@Override public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) { if (iInvokedMethod.isTestMethod()) { String attribute = Github1600Analyzer.NO; if (iTestResult.getStatus() == ITestResult.SUCCESS) { iTestResult.setStatus(ITestResult.FAILURE); attribute = Github1600Analyzer.YES; } iTestResult.setAttribute(Github1600Analyzer.RETRY, attribute); } }
/** * Sets the appenders. * * @param tr the ITestresult * @param appenders the appenders */ public static void setAppenders(ITestResult tr, Appenders appenders) { tr.setAttribute(NAME, appenders); }
/** * Sets the attachment files. * * @param tr the ITestResult * @param files the list of Files */ public static void setAttachmentFiles(ITestResult tr, List<File> files) { tr.setAttribute(NAME, files); }
/** * Sets the log file. * * @param tr the tr * @param file the file */ public static void setLogFile(ITestResult tr, File file) { tr.setAttribute(NAME, file); }
/** * Sets the "request" and "response" attributes of a test result. The value * of these attributes is a string that contains information about the * content of an outgoing or incoming message: target resource, status code, * headers, entity (if present). The entity is represented as a String with * UTF-8 character encoding. * * @param result A description of a test result (with a fail verdict). */ @Override public void onTestFailure(ITestResult result) { super.onTestFailure(result); Object instance = result.getInstance(); if (CommonFixture.class.isInstance(instance)) { CommonFixture fixture = CommonFixture.class.cast(instance); result.setAttribute("request", getRequestMessageInfo(fixture.request)); result.setAttribute("response", getResponseMessageInfo(fixture.response)); } }
/** * Sets the "request" and "response" attributes of a test result. The value * of these attributes is a string that contains information about the * content of an outgoing or incoming message: target resource, status code, * headers, entity (if present). The entity is represented as a String with * UTF-8 character encoding. * * @param result A description of a test result (with a fail verdict). */ @Override public void onTestFailure(ITestResult result) { super.onTestFailure(result); Object instance = result.getInstance(); if (CommonFixture.class.isInstance(instance)) { CommonFixture fixture = CommonFixture.class.cast(instance); result.setAttribute("request", getRequestMessageInfo(fixture.request)); result.setAttribute("response", getResponseMessageInfo(fixture.response)); } }
@Override public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { logger.entering(new Object[] { method, testResult }); try { if (ListenerManager.isCurrentMethodSkipped(this)) { logger.exiting(ListenerManager.THREAD_EXCLUSION_MSG); return; } // Initialize soft asserts for this test method instance. SeLionSoftAssert softAsserts = new SeLionSoftAssert(); testResult.setAttribute(SeLionSoftAssert.SOFT_ASSERT_ATTRIBUTE_NAME, softAsserts); } catch (Exception e) { // NOSONAR logger.log(Level.WARNING, "An error occurred while processing beforeInvocation: " + e.getMessage(), e); } }
@Override public void beforeInvocation(IInvokedMethod method, ITestResult testResult) { try { if (ListenerManager.isCurrentMethodSkipped(this)) { logger.exiting(ListenerManager.THREAD_EXCLUSION_MSG); return; } Test testMethod = method.getTestMethod().getConstructorOrMethod().getMethod().getAnnotation(Test.class); if (testMethod != null) { String testName = testMethod.testName(); if (StringUtils.isNotEmpty(testName)) { testResult.setAttribute(TEST_NAME_KEY, testName); } } } catch (Exception e) { //NOSONAR logger.log(Level.WARNING, "An error occurred while processing beforeInvocation: " + e.getMessage(), e); } }
/** * Retry logic. We retry a test if * - it has not reached the maximum retry number * - the error of the test is not an AssertionError. These are functional errors and so should not be retried * @param result * @param logRetry * @return */ private boolean retry(final ITestResult result, boolean logRetry) { String testClassName = String.format("%s.%s", result.getMethod().getRealClass().toString(), result.getMethod().getMethodName()); if (count <= maxCount) { if (result.getThrowable() instanceof AssertionError) { return false; } if (logRetry) { result.setAttribute("RETRY", new Integer(count)); TestLogging.log("[RETRYING] " + testClassName + " FAILED, " + "Retrying " + count + " time"); count += 1; } return true; } return false; }
@Override public void startTestMethod(ITestResult testResult) { StartTestItemRQ rq = buildStartStepRq(testResult); if (rq == null) { return; } Maybe<String> stepMaybe = launch.get().startTestItem(this.<Maybe<String>>getAttribute(testResult.getTestContext(), RP_ID), rq); testResult.setAttribute(RP_ID, stepMaybe); }
/** * get SR context stored in test result if it exists. Else, create a new one (happens when a test method has been skipped for example) * called from reporters only * @param testNGCtx * @param testName * @param testResult */ public static void setThreadContextFromTestResult(ITestContext testNGCtx, String testName, String className, ITestResult testResult) { if (testResult == null) { throw new ConfigurationException("Cannot set context from testResult as it is null"); } if (testResult.getAttribute(SeleniumRobotTestListener.TEST_CONTEXT) != null) { setThreadContext((SeleniumTestsContext)testResult.getAttribute(SeleniumRobotTestListener.TEST_CONTEXT)); } else { logger.error("Result did not contain thread context, initializing a new one"); initThreadContext(testNGCtx, testName, className, testResult); testResult.setAttribute(SeleniumRobotTestListener.TEST_CONTEXT, getThreadContext()); } }
@Override public void startConfiguration(ITestResult testResult) { TestMethodType type = TestMethodType.getStepType(testResult.getMethod()); StartTestItemRQ rq = buildStartConfigurationRq(testResult, type); Maybe<String> parentId = getConfigParent(testResult, type); final Maybe<String> itemID = launch.get().startTestItem(parentId, rq); testResult.setAttribute(RP_ID, itemID); }
@Override public void onTestStart( ITestResult paramITestResult ) { Object instance = paramITestResult.getInstance(); ScenarioBase scenario; if( instance instanceof ScenarioTestBase<?, ?, ?> ) { ScenarioTestBase<?, ?, ?> testInstance = (ScenarioTestBase<?, ?, ?>) instance; scenario = testInstance.createNewScenario(); } else { scenario = new ScenarioBase(); } ScenarioHolder.get().setScenarioOfCurrentThread( scenario ); paramITestResult.setAttribute(SCENARIO_ATTRIBUTE, scenario); ReportModel reportModel = getReportModel( instance.getClass() ); scenario.setModel( reportModel ); scenario.getExecutor().injectStages( instance ); Method method = paramITestResult.getMethod().getConstructorOrMethod().getMethod(); scenario.startScenario( instance.getClass(), method, getArgumentsFrom( method, paramITestResult ) ); // inject state from the test itself scenario.getExecutor().readScenarioState( instance ); }