How to use getTestListeners method of com.consol.citrus.context.TestContext class

Best Citrus code snippet using com.consol.citrus.context.TestContext.getTestListeners

Source:TestCase.java Github

copy

Full Screen

...80 /**81 * Starts the test case.82 */83 public void start(final TestContext context) {84 context.getTestListeners().onTestStart(this);85 try {86 if (log.isDebugEnabled()) {87 log.debug("Initializing test case");88 }89 /* Debug print global variables */90 if (context.hasVariables() && log.isDebugEnabled()) {91 log.debug("Global variables:");92 for (final Entry<String, Object> entry : context.getVariables().entrySet()) {93 log.debug(entry.getKey() + " = " + entry.getValue());94 }95 }96 // add default variables for test97 context.setVariable(Citrus.TEST_NAME_VARIABLE, getName());98 context.setVariable(Citrus.TEST_PACKAGE_VARIABLE, packageName);99 for (final Entry<String, Object> paramEntry : parameters.entrySet()) {100 if (log.isDebugEnabled()) {101 log.debug(String.format("Initializing test parameter '%s' as variable", paramEntry.getKey()));102 }103 context.setVariable(paramEntry.getKey(), paramEntry.getValue());104 }105 /* build up the global test variables in TestContext by106 * getting the names and the current values of all variables */107 for (final Entry<String, Object> entry : variableDefinitions.entrySet()) {108 final String key = entry.getKey();109 final Object value = entry.getValue();110 if (value instanceof String) {111 //check if value is a variable or function (and resolve it accordingly)112 context.setVariable(key, context.replaceDynamicContentInString(value.toString()));113 } else {114 context.setVariable(key, value);115 }116 }117 /* Debug print all variables */118 if (context.hasVariables() && log.isDebugEnabled()) {119 log.debug("Test variables:");120 for (final Entry<String, Object> entry : context.getVariables().entrySet()) {121 log.debug(entry.getKey() + " = " + entry.getValue());122 }123 }124 beforeTest(context);125 } catch (final Exception | AssertionError e) {126 testResult = TestResult.failed(getName(), testClass.getName(), e);127 throw new TestCaseFailedException(e);128 }129 }130 /**131 * Method executes the test case and all its actions.132 */133 public void doExecute(final TestContext context) {134 if (!getMetaInfo().getStatus().equals(TestCaseMetaInfo.Status.DISABLED)) {135 try {136 start(context);137 for (final TestAction action: actions) {138 executeAction(action, context);139 }140 testResult = TestResult.success(getName(), testClass.getName());141 } catch (final TestCaseFailedException e) {142 throw e;143 } catch (final Exception | AssertionError e) {144 testResult = TestResult.failed(getName(), testClass.getName(), e);145 throw new TestCaseFailedException(e);146 } finally {147 try {148 if (contextContainsExceptions(context)) {149 final CitrusRuntimeException ex = context.getExceptions().remove(0);150 testResult = TestResult.failed(getName(), testClass.getName(), ex);151 throw new TestCaseFailedException(ex);152 }153 } finally {154 finish(context);155 }156 }157 } else {158 testResult = TestResult.skipped(getName(), testClass.getName());159 context.getTestListeners().onTestSkipped(this);160 }161 }162 /**163 * Sequence of test actions before the test case.164 */165 public void beforeTest(final TestContext context) {166 if (beforeTest != null) {167 for (final SequenceBeforeTest sequenceBeforeTest : beforeTest) {168 try {169 if (sequenceBeforeTest.shouldExecute(getName(), packageName, groups))170 sequenceBeforeTest.execute(context);171 } catch (final Exception e) {172 throw new CitrusRuntimeException("Before test failed with errors", e);173 }174 }175 }176 }177 /**178 * Sequence of test actions after test case. This operation does not raise andy errors - exceptions179 * will only be logged as warning. This is because we do not want to overwrite errors that may have occurred180 * before in test execution.181 */182 public void afterTest(final TestContext context) {183 if (afterTest != null) {184 for (final SequenceAfterTest sequenceAfterTest : afterTest) {185 try {186 if (sequenceAfterTest.shouldExecute(getName(), packageName, groups)) {187 sequenceAfterTest.execute(context);188 }189 } catch (final Exception | AssertionError e) {190 log.warn("After test failed with errors", e);191 }192 }193 }194 }195 /**196 * Executes a single test action with given test context.197 * @param action198 * @param context199 */200 public void executeAction(final TestAction action, final TestContext context) {201 if (contextContainsExceptions(context)) {202 throw context.getExceptions().remove(0);203 }204 try {205 if (!action.isDisabled(context)) {206 testActionListeners.onTestActionStart(this, action);207 setActiveAction(action);208 action.execute(context);209 testActionListeners.onTestActionFinish(this, action);210 } else {211 testActionListeners.onTestActionSkipped(this, action);212 }213 } catch (final Exception | AssertionError e) {214 testResult = TestResult.failed(getName(), testClass.getName(), e);215 throw new TestCaseFailedException(e);216 }217 }218 /**219 * Method that will be executed in any case of test case result (success, error)220 * Usually used for clean up tasks.221 */222 public void finish(final TestContext context) {223 CitrusRuntimeException runtimeException = null;224 if (testCaseWasSuccessful(context)) {225 final ScheduledExecutorService scheduledExecutor =226 Executors.newSingleThreadScheduledExecutor(this::createFinisherThread);227 try {228 waitForNestedTestActions(context, scheduledExecutor);229 } catch (final InterruptedException | ExecutionException | TimeoutException e) {230 runtimeException =231 new CitrusRuntimeException("Failed to wait for nested test actions to finish properly", e);232 } finally {233 scheduledExecutor.shutdown();234 if (contextContainsExceptions(context)) {235 final CitrusRuntimeException ex = context.getExceptions().remove(0);236 testResult = TestResult.failed(getName(), testClass.getName(), ex);237 runtimeException = ex;238 }239 }240 }241 context.getTestListeners().onTestFinish(this);242 try {243 if (!finalActions.isEmpty()) {244 log.debug("Entering finally block in test case");245 /* walk through the finally chain and execute the actions in there */246 for (final TestAction action : finalActions) {247 if (!action.isDisabled(context)) {248 testActionListeners.onTestActionStart(this, action);249 action.execute(context);250 testActionListeners.onTestActionFinish(this, action);251 } else {252 testActionListeners.onTestActionSkipped(this, action);253 }254 }255 }256 if (testResult == null) {257 testResult = TestResult.success(getName(), testClass.getName());258 }259 if (runtimeException != null) {260 throw runtimeException;261 }262 } catch (final Exception | AssertionError e) {263 testResult = TestResult.failed(getName(), testClass.getName(), e);264 throw new TestCaseFailedException(e);265 } finally {266 if (testResult != null) {267 if (testResult.isSuccess()) {268 context.getTestListeners().onTestSuccess(this);269 } else {270 context.getTestListeners().onTestFailure(this, testResult.getCause());271 }272 }273 afterTest(context);274 }275 }276 private void waitForNestedTestActions(final TestContext context,277 final ScheduledExecutorService scheduledExecutor)278 throws InterruptedException, ExecutionException, TimeoutException {279 final CompletableFuture<Boolean> finished = new CompletableFuture<>();280 scheduledExecutor.scheduleAtFixedRate(() -> {281 if (isDone(context)) {282 finished.complete(true);283 } else {284 log.debug("Wait for test actions to finish properly ...");...

Full Screen

Full Screen

Source:DefaultTestCase.java Github

copy

Full Screen

...39 /** Time to wait for nested actions to finish */40 private long timeout = 10000L;41 @Override42 public void start(final TestContext context) {43 context.getTestListeners().onTestStart(this);44 try {45 if (log.isDebugEnabled()) {46 log.debug("Initializing test case");47 }48 debugVariables("Global", context);49 initializeTestParameters(parameters, context);50 initializeTestVariables(variableDefinitions, context);51 debugVariables("Test", context);52 beforeTest(context);53 } catch (final Exception | AssertionError e) {54 testResult = TestResult.failed(getName(), testClass.getName(), e);55 throw new TestCaseFailedException(e);56 }57 }58 @Override59 public void doExecute(final TestContext context) {60 if (!getMetaInfo().getStatus().equals(TestCaseMetaInfo.Status.DISABLED)) {61 try {62 start(context);63 for (final TestActionBuilder<?> actionBuilder: actions) {64 TestAction action = actionBuilder.build();65 executeAction(action, context);66 }67 testResult = TestResult.success(getName(), testClass.getName());68 } catch (final TestCaseFailedException e) {69 throw e;70 } catch (final Exception | AssertionError e) {71 testResult = TestResult.failed(getName(), testClass.getName(), e);72 throw new TestCaseFailedException(e);73 } finally {74 finish(context);75 }76 } else {77 testResult = TestResult.skipped(getName(), testClass.getName());78 context.getTestListeners().onTestSkipped(this);79 }80 }81 @Override82 public void beforeTest(final TestContext context) {83 for (final BeforeTest sequenceBeforeTest : context.getBeforeTest()) {84 try {85 if (sequenceBeforeTest.shouldExecute(getName(), packageName, groups))86 sequenceBeforeTest.execute(context);87 } catch (final Exception e) {88 throw new CitrusRuntimeException("Before test failed with errors", e);89 }90 }91 }92 @Override93 public void afterTest(final TestContext context) {94 for (final AfterTest sequenceAfterTest : context.getAfterTest()) {95 try {96 if (sequenceAfterTest.shouldExecute(getName(), packageName, groups)) {97 sequenceAfterTest.execute(context);98 }99 } catch (final Exception | AssertionError e) {100 log.warn("After test failed with errors", e);101 }102 }103 }104 @Override105 public void executeAction(final TestAction action, final TestContext context) {106 if (context.hasExceptions()) {107 throw context.getExceptions().remove(0);108 }109 try {110 if (!action.isDisabled(context)) {111 setActiveAction(action);112 context.getTestActionListeners().onTestActionStart(this, action);113 action.execute(context);114 context.getTestActionListeners().onTestActionFinish(this, action);115 } else {116 context.getTestActionListeners().onTestActionSkipped(this, action);117 }118 } catch (final Exception | AssertionError e) {119 testResult = TestResult.failed(getName(), testClass.getName(), e);120 throw new TestCaseFailedException(e);121 }122 }123 /**124 * Method that will be executed in any case of test case result (success, error)125 * Usually used for clean up tasks.126 */127 public void finish(final TestContext context) {128 try {129 CitrusRuntimeException contextException = null;130 if (testResult == null) {131 if (context.hasExceptions()) {132 contextException = context.getExceptions().remove(0);133 testResult = TestResult.failed(getName(), testClass.getName(), contextException);134 } else {135 testResult = TestResult.success(getName(), testClass.getName());136 }137 }138 if (context.isSuccess(testResult)) {139 TestUtils.waitForCompletion(this, context, timeout);140 }141 context.getTestListeners().onTestFinish(this);142 executeFinalActions(context);143 if (contextException != null) {144 throw new TestCaseFailedException(contextException);145 }146 } catch (final TestCaseFailedException e) {147 throw e;148 } catch (final Exception | AssertionError e) {149 testResult = TestResult.failed(getName(), testClass.getName(), e);150 throw new TestCaseFailedException(e);151 } finally {152 if (testResult != null) {153 if (testResult.isSuccess()) {154 context.getTestListeners().onTestSuccess(this);155 } else {156 context.getTestListeners().onTestFailure(this, testResult.getCause());157 }158 }159 afterTest(context);160 }161 }162 /**163 * Run final test actions.164 * @param context165 */166 private void executeFinalActions(TestContext context) {167 if (!finalActions.isEmpty()) {168 log.debug("Entering finally block in test case");169 /* walk through the finally chain and execute the actions in there */170 for (final TestActionBuilder<?> actionBuilder : finalActions) {...

Full Screen

Full Screen

Source:Template.java Github

copy

Full Screen

...72 innerContext.getVariables().putAll(context.getVariables());73 innerContext.setMessageStore(context.getMessageStore());74 innerContext.setMessageValidatorRegistry(context.getMessageValidatorRegistry());75 innerContext.setValidationMatcherRegistry(context.getValidationMatcherRegistry());76 innerContext.setTestListeners(context.getTestListeners());77 innerContext.setMessageListeners(context.getMessageListeners());78 innerContext.setGlobalMessageConstructionInterceptors(context.getGlobalMessageConstructionInterceptors());79 innerContext.setEndpointFactory(context.getEndpointFactory());80 innerContext.setNamespaceContextBuilder(context.getNamespaceContextBuilder());81 innerContext.setApplicationContext(context.getApplicationContext());82 }83 84 for (Entry<String, String> entry : parameter.entrySet()) {85 String param = entry.getKey();86 String paramValue = entry.getValue();87 paramValue = VariableUtils.replaceVariablesInString(paramValue, innerContext, false);88 if (context.getFunctionRegistry().isFunction(paramValue)) {89 paramValue = FunctionUtils.resolveFunction(paramValue, context);90 }...

Full Screen

Full Screen

getTestListeners

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus;2import java.util.ArrayList;3import java.util.List;4import org.testng.ITestContext;5import org.testng.ITestListener;6import org.testng.ITestResult;7import org.testng.annotations.Test;8public class TestContextDemo {9 public void getTestListeners(ITestContext testContext) {10 TestContext context = new TestContext();11 List<ITestListener> listeners = new ArrayList<ITestListener>();12 listeners.add(new ITestListener() {13 public void onTestStart(ITestResult result) {14 System.out.println("onTestStart");15 }16 public void onTestSuccess(ITestResult result) {17 System.out.println("onTestSuccess");18 }19 public void onTestFailure(ITestResult result) {20 System.out.println("onTestFailure");21 }22 public void onTestSkipped(ITestResult result) {23 System.out.println("onTestSkipped");24 }25 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {26 System.out.println("onTestFailedButWithinSuccessPercentage");27 }28 public void onStart(ITestContext context) {29 System.out.println("onStart");30 }31 public void onFinish(ITestContext context) {32 System.out.println("onFinish");33 }34 });35 context.setTestListeners(listeners);36 System.out.println(context.getTestListeners());37 }38}

Full Screen

Full Screen

getTestListeners

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.dsl.testng;2import com.consol.citrus.testng.AbstractTestNGCitrusTest;3import org.testng.Assert;4import org.testng.annotations.Test;5import java.util.ArrayList;6import java.util.List;7public class GetTestListenersTest extends AbstractTestNGCitrusTest {8 public void getTestListenersTest() {9 List<String> testListeners = new ArrayList<String>();10 testListeners.add("com.consol.citrus.report.TestListener");11 testListeners.add("com.consol.citrus.report.TestActionListener");12 testListeners.add("com.consol.citrus.report.TestSuiteListener");13 testListeners.add("com.consol.citrus.report.TestReporter");14 testListeners.add("com.consol.citrus.report.TestLogger");15 testListeners.add("com.consol.citrus.report.TestResultLogger");16 testListeners.add("com.consol.citrus.report.TestFailureLogger");17 Assert.assertEquals(testContext.getTestListeners(), testListeners);18 }19}20package com.consol.citrus.dsl.testng;21import com.consol.citrus.testng.AbstractTestNGCitrusTest;22import org.testng.Assert;23import org.testng.annotations.Test;24import java.util.ArrayList;25import java.util.List;26public class GetTestListenersTest extends AbstractTestNGCitrusTest {27 public void getTestListenersTest() {28 List<String> testListeners = new ArrayList<String>();29 testListeners.add("com.consol.citrus.report.TestListener");30 testListeners.add("com.consol.citrus.report.TestActionListener");31 testListeners.add("com.consol.citrus.report.TestSuiteListener");32 testListeners.add("com.consol.citrus.report.TestReporter");33 testListeners.add("com.consol.citrus.report.TestLogger");34 testListeners.add("com.consol.citrus.report.TestResultLogger");35 testListeners.add("com.consol.citrus.report.TestFailureLogger");36 Assert.assertEquals(testContext.getTestListeners(), testListeners);37 }38}39package com.consol.citrus.dsl.testng;40import com.consol.citrus.testng.AbstractTestNGCitrusTest;41import org.testng

Full Screen

Full Screen

getTestListeners

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus;2import java.util.List;3public class TestContextTest {4public static void main(String[] args) {5TestContext testContext = new TestContext();6List<TestListener> listeners = testContext.getTestListeners();7System.out.println(listeners);8}9}

Full Screen

Full Screen

getTestListeners

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus;2import java.util.List;3public class TestContextGetTestListeners {4 public static void main(String args[]) {5 TestContext testContext = new TestContext();6 List<TestListener> testListeners = testContext.getTestListeners();7 System.out.println("Test Listeners: " + testListeners);8 }9}

Full Screen

Full Screen

getTestListeners

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus;2import org.testng.annotations.Test;3import org.testng.ITestListener;4import org.testng.ITestContext;5import org.testng.ITestResult;6import org.testng.TestNG;7import org.testng.annotations.BeforeMethod;8import org.testng.annotations.AfterMethod;9import org.testng.annotations.BeforeClass;10import org.testng.annotations.AfterClass;11import java.util.List;12public class NewTest {13 public void f() {14 TestNG testng = new TestNG();15 TestContext context = new TestContext();16 List<ITestListener> listeners = context.getTestListeners();17 System.out.println("List of listeners: " + listeners);18 }19}20package com.consol.citrus;21import org.testng.annotations.Test;22import org.testng.ITestListener;23import org.testng.ITestContext;24import org.testng.ITestResult;25import org.testng.TestNG;26import org.testng.annotations.BeforeMethod;27import org.testng.annotations.AfterMethod;28import org.testng.annotations.BeforeClass;29import org.testng.annotations.AfterClass;30import java.util.List;31public class NewTest {32 public void f() {33 TestNG testng = new TestNG();34 TestContext context = new TestContext();35 context.addTestListener(new ITestListener() {36 public void onTestStart(ITestResult result) {37 System.out.println("Test started");38 }39 public void onTestSuccess(ITestResult result) {40 System.out.println("Test success");41 }42 public void onTestFailure(ITestResult result) {43 System.out.println("Test failed");44 }45 public void onTestSkipped(ITestResult result) {46 System.out.println("Test skipped");47 }48 public void onTestFailedButWithinSuccessPercentage(ITestResult result) {49 System.out.println("Test failed but within success percentage");50 }51 public void onStart(ITestContext context) {52 System.out.println("Test started");53 }

Full Screen

Full Screen

getTestListeners

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus;2import java.util.ArrayList;3import java.util.List;4import java.util.Map;5import java.util.HashMap;6import org.testng.Assert;7import org.testng.annotations.Test;8public class TestContextTest {9 public void testGetTestListeners() {10 TestContext context = new TestContext();11 Map<String, TestListener> testListeners = new HashMap<String, TestListener>();12 List<TestListener> testListenerList = new ArrayList<TestListener>();13 testListenerList.add(new TestListener());14 testListeners.put("testListener1", testListenerList.get(0));15 testListeners.put("testListener2", testListenerList.get(0));16 testListeners.put("testListener3", testListenerList.get(0));17 testListeners.put("testListener4", testListenerList.get(0));18 testListeners.put("testListener5", testListenerList.get(0));19 testListeners.put("testListener6", testListenerList.get(0));20 testListeners.put("testListener7", testListenerList.get(0));21 testListeners.put("testListener8", testListenerList.get(0));22 testListeners.put("testListener9", testListenerList.get(0));23 testListeners.put("testListener10", testListenerList.get(0));24 testListeners.put("testListener11", testListenerList.get(0));25 context.setTestListeners(testListeners);26 Assert.assertEquals(context.getTestListeners().size(), 11);27 }28}29package com.consol.citrus;30import org.testng.Assert;31import org.testng.annotations.Test;32public class TestContextTest {33 public void testGetTestListeners() {34 TestContext context = new TestContext();35 Assert.assertEquals(context.getTestListeners().size(), 0);36 }37}38package com.consol.citrus;39import org.testng.Assert;40import org.testng.annotations.Test;41public class TestContextTest {42 public void testGetTestListeners() {43 TestContext context = new TestContext();44 Assert.assertEquals(context.getTestListeners().size(), 0);45 }46}47package com.consol.citrus;48import org.testng.Assert;49import org.testng.annotations.Test;50public class TestContextTest {51 public void testGetTestListeners() {

Full Screen

Full Screen

getTestListeners

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus;2import java.util.List;3import org.testng.annotations.Test;4public class TestContextTest {5@Test(description = "getTestListeners method")6public void testGetTestListeners() {7TestContext testContext = new TestContext();8List<TestListener> listeners = testContext.getTestListeners();9System.out.println("Test listeners: " + listeners);10}11}12package com.consol.citrus;13import java.util.List;14import org.testng.annotations.Test;15public class TestContextTest {16@Test(description = "addTestListener method")17public void testAddTestListener() {18TestContext testContext = new TestContext();19List<TestListener> listeners = testContext.getTestListeners();20System.out.println("Test listeners: " + listeners);21TestListener testListener = new TestListener() {22public void onTestFailure(TestContext context, Throwable cause) {23System.out.println("Test listener on test failure");24}25public void onTestSuccess(TestContext context) {26System.out.println("Test listener on test success");27}28public void onTestFinish(TestContext context) {29System.out.println("Test listener on test finish");30}31public void onTestStart(TestContext context) {32System.out.println("Test listener on test start");33}34};35testContext.addTestListener(testListener);36listeners = testContext.getTestListeners();37System.out.println("Test listeners: " + listeners);38}39}40package com.consol.citrus;41import java.util.List;42import org.testng.annotations.Test;43public class TestContextTest {44@Test(description = "removeTestListener method")45public void testRemoveTestListener() {46TestContext testContext = new TestContext();47List<TestListener> listeners = testContext.getTestListeners();48System.out.println("Test listeners: " + listeners);49TestListener testListener = new TestListener() {

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful