How to use XmlTestLoader method of com.consol.citrus.common.XmlTestLoader class

Best Citrus code snippet using com.consol.citrus.common.XmlTestLoader.XmlTestLoader

Source:CitrusBaseExtension.java Github

copy

Full Screen

...17import com.consol.citrus.Citrus;18import com.consol.citrus.TestCase;19import com.consol.citrus.annotations.*;20import com.consol.citrus.common.TestLoader;21import com.consol.citrus.common.XmlTestLoader;22import com.consol.citrus.context.TestContext;23import com.consol.citrus.exceptions.CitrusRuntimeException;24import org.junit.jupiter.api.DynamicTest;25import org.junit.jupiter.api.extension.*;26import org.springframework.core.io.Resource;27import org.springframework.core.io.support.PathMatchingResourcePatternResolver;28import org.springframework.util.Assert;29import org.springframework.util.StringUtils;30import java.io.File;31import java.io.IOException;32import java.lang.reflect.Method;33import java.util.ArrayList;34import java.util.List;35import java.util.stream.Stream;36/**37 * JUnit5 extension supports Citrus Xml test extension that also allows to load test cases from external Spring configuration files. In addition to that Citrus annotation based resource injection38 * and lifecycle management such as before/after suite is supported.39 *40 * Extension resolves method parameter of type {@link TestContext} and injects endpoints and resources coming from Citrus Spring application context that is automatically loaded at suite start up.41 * After suite automatically includes Citrus report generation.42 *43 * @author Christoph Deppisch44 */45public class CitrusBaseExtension implements BeforeAllCallback,46 BeforeEachCallback, BeforeTestExecutionCallback, AfterTestExecutionCallback, ParameterResolver, TestInstancePostProcessor {47 /** Test suite name */48 private static final String SUITE_NAME = "citrus-junit5-suite";49 private static Citrus citrus;50 private static boolean beforeSuite = true;51 private static boolean afterSuite = true;52 /**53 * {@link ExtensionContext.Namespace} in which Citrus related objects are stored keyed by test class.54 */55 protected static final ExtensionContext.Namespace NAMESPACE = ExtensionContext.Namespace.create(CitrusBaseExtension.class);56 @Override57 public void afterTestExecution(ExtensionContext extensionContext) throws Exception {58 extensionContext.getRoot().getStore(NAMESPACE).remove(getBaseKey(extensionContext) + TestContext.class.getSimpleName());59 extensionContext.getRoot().getStore(NAMESPACE).remove(getBaseKey(extensionContext) + TestCase.class.getSimpleName());60 }61 @Override62 public void beforeAll(ExtensionContext extensionContext) throws Exception {63 if (beforeSuite) {64 beforeSuite = false;65 getCitrus(extensionContext).beforeSuite(SUITE_NAME);66 }67 if (afterSuite) {68 afterSuite = false;69 Runtime.getRuntime().addShutdownHook(new Thread(() -> getCitrus(extensionContext).afterSuite(SUITE_NAME)));70 }71 }72 @Override73 public void beforeEach(ExtensionContext extensionContext) throws Exception {74 getTestContext(extensionContext);75 getXmlTestCase(extensionContext);76 }77 @Override78 public void beforeTestExecution(ExtensionContext extensionContext) throws Exception {79 getCitrus(extensionContext).run(getXmlTestCase(extensionContext), getTestContext(extensionContext));80 }81 @Override82 public void postProcessTestInstance(Object testInstance, ExtensionContext extensionContext) throws Exception {83 CitrusAnnotations.injectAll(testInstance, getCitrus(extensionContext));84 }85 @Override86 public boolean supportsParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {87 return parameterContext.getParameter().isAnnotationPresent(CitrusResource.class);88 }89 @Override90 public Object resolveParameter(ParameterContext parameterContext, ExtensionContext extensionContext) throws ParameterResolutionException {91 if (TestContext.class.isAssignableFrom(parameterContext.getParameter().getType())) {92 return getTestContext(extensionContext);93 }94 throw new CitrusRuntimeException(String.format("Failed to resolve parameter %s", parameterContext.getParameter()));95 }96 /**97 * Gets base key for store.98 * @param extensionContext99 * @return100 */101 protected static String getBaseKey(ExtensionContext extensionContext) {102 return extensionContext.getRequiredTestClass().getName() + "." + extensionContext.getRequiredTestMethod().getName() + "#";103 }104 /**105 * Creates new test loader which has TestNG test annotations set for test execution. Only106 * suitable for tests that get created at runtime through factory method. Subclasses107 * may overwrite this in order to provide custom test loader with custom test annotations set.108 * @param extensionContext109 * @return110 */111 protected static TestLoader createTestLoader(ExtensionContext extensionContext) {112 Method method = extensionContext.getRequiredTestMethod();113 String testName = extensionContext.getRequiredTestClass().getSimpleName();114 String packageName = method.getDeclaringClass().getPackage().getName();115 if (method.getAnnotation(CitrusXmlTest.class) != null) {116 CitrusXmlTest citrusXmlTestAnnotation = method.getAnnotation(CitrusXmlTest.class);117 String[] packagesToScan = citrusXmlTestAnnotation.packageScan();118 if (StringUtils.hasText(citrusXmlTestAnnotation.packageName())) {119 packageName = citrusXmlTestAnnotation.packageName();120 }121 if (citrusXmlTestAnnotation.name().length > 0) {122 testName = citrusXmlTestAnnotation.name()[0];123 } else if (packagesToScan.length == 0) {124 testName = method.getName();125 }126 }127 return new XmlTestLoader(extensionContext.getRequiredTestClass(), testName, packageName, getCitrus(extensionContext).getApplicationContext());128 }129 /**130 * Get the {@link Citrus} associated with the supplied {@code ExtensionContext}.131 * @return the {@code Citrus} (never {@code null})132 */133 protected static Citrus getCitrus(ExtensionContext extensionContext) {134 Assert.notNull(extensionContext, "ExtensionContext must not be null");135 return extensionContext.getRoot().getStore(NAMESPACE).getOrComputeIfAbsent(Citrus.class.getName(), key -> {136 if (citrus == null) {137 citrus = Citrus.newInstance();138 }139 140 return citrus;141 }, Citrus.class);142 }143 /**144 * Get the {@link TestContext} associated with the supplied {@code ExtensionContext} and its required test class name.145 * @return the {@code TestContext} (never {@code null})146 */147 protected static TestContext getTestContext(ExtensionContext extensionContext) {148 Assert.notNull(extensionContext, "ExtensionContext must not be null");149 return extensionContext.getRoot().getStore(NAMESPACE).getOrComputeIfAbsent(getBaseKey(extensionContext) + TestContext.class.getSimpleName(), key -> getCitrus(extensionContext).createTestContext(), TestContext.class);150 }151 /**152 * Get the {@link TestCase} associated with the supplied {@code ExtensionContext} and its required test class name.153 * @return the {@code TestCase} (never {@code null})154 */155 protected static TestCase getXmlTestCase(ExtensionContext extensionContext) {156 Assert.notNull(extensionContext, "ExtensionContext must not be null");157 return extensionContext.getRoot().getStore(NAMESPACE).getOrComputeIfAbsent(getBaseKey(extensionContext) + TestCase.class.getSimpleName(), key -> createTestLoader(extensionContext).load(), TestCase.class);158 }159 /**160 * Creates stream of dynamic tests based on package scan. Scans package for all Xml test case files and creates dynamic test instance for it.161 * @param packagesToScan162 * @return163 */164 public static Stream<DynamicTest> packageScan(String ... packagesToScan) {165 List<DynamicTest> tests = new ArrayList<>();166 for (String packageScan : packagesToScan) {167 try {168 for (String fileNamePattern : Citrus.getXmlTestFileNamePattern()) {169 Resource[] fileResources = new PathMatchingResourcePatternResolver().getResources(packageScan.replace('.', File.separatorChar) + fileNamePattern);170 for (Resource fileResource : fileResources) {171 String filePath = fileResource.getFile().getParentFile().getCanonicalPath();172 if (packageScan.startsWith("file:")) {173 filePath = "file:" + filePath;174 }175 filePath = filePath.substring(filePath.indexOf(packageScan.replace('.', File.separatorChar)));176 String testName = fileResource.getFilename().substring(0, fileResource.getFilename().length() - ".xml".length());177 XmlTestLoader testLoader = new XmlTestLoader(DynamicTest.class, testName, filePath, citrus.getApplicationContext());178 tests.add(DynamicTest.dynamicTest(testName, () -> citrus.run(testLoader.load())));179 }180 }181 } catch (IOException e) {182 throw new CitrusRuntimeException("Unable to locate file resources for test package '" + packageScan + "'", e);183 }184 }185 return tests.stream();186 }187 /**188 * Creates dynamic test that executes Xml test given by name and package.189 * @param packageName190 * @param testNames191 * @return192 */193 public static Stream<DynamicTest> dynamicTests(String packageName, String ... testNames) {194 return Stream.of(testNames).map(testName -> {195 XmlTestLoader testLoader = new XmlTestLoader(DynamicTest.class, testName, packageName, citrus.getApplicationContext());196 return DynamicTest.dynamicTest(testName, () -> citrus.run(testLoader.load()));197 });198 }199 /**200 * Creates dynamic test that executes Xml test given by name and package.201 * @param testClass202 * @return203 */204 public static Stream<DynamicTest> dynamicTests(Class<?> testClass, String ... testNames) {205 return Stream.of(testNames).map(testName -> {206 XmlTestLoader testLoader = new XmlTestLoader(DynamicTest.class, testName, testClass.getPackage().getName(), citrus.getApplicationContext());207 return DynamicTest.dynamicTest(testName, () -> citrus.run(testLoader.load()));208 });209 }210 /**211 * Creates dynamic test that executes Xml test given by name and package.212 * @param packageName213 * @param testName214 * @return215 */216 public static DynamicTest dynamicTest(String packageName, String testName) {217 XmlTestLoader testLoader = new XmlTestLoader(DynamicTest.class, testName, packageName, citrus.getApplicationContext());218 return DynamicTest.dynamicTest(testName, () -> citrus.run(testLoader.load()));219 }220}...

Full Screen

Full Screen

Source:AbstractJUnit4CitrusTest.java Github

copy

Full Screen

...17import com.consol.citrus.Citrus;18import com.consol.citrus.TestCase;19import com.consol.citrus.annotations.CitrusResource;20import com.consol.citrus.common.TestLoader;21import com.consol.citrus.common.XmlTestLoader;22import com.consol.citrus.config.CitrusSpringConfig;23import com.consol.citrus.context.TestContext;24import com.consol.citrus.exceptions.CitrusRuntimeException;25import org.junit.runner.RunWith;26import org.slf4j.Logger;27import org.slf4j.LoggerFactory;28import org.springframework.test.context.ContextConfiguration;29import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;30import org.springframework.util.ReflectionUtils;31import java.lang.annotation.Annotation;32/**33 * Abstract base test implementation for test cases that rather use JUnit testing framework. Class also provides 34 * test listener support and loads the root application context files for Citrus.35 * 36 * @author Christoph Deppisch37 */38@RunWith(CitrusJUnit4Runner.class)39@ContextConfiguration(classes = CitrusSpringConfig.class)40public abstract class AbstractJUnit4CitrusTest extends AbstractJUnit4SpringContextTests {41 /** Logger */42 protected final Logger log = LoggerFactory.getLogger(getClass());43 /** Citrus instance */44 protected Citrus citrus;45 /**46 * Reads Citrus test annotation from framework method and executes test case.47 * @param frameworkMethod48 */49 protected void run(CitrusJUnit4Runner.CitrusFrameworkMethod frameworkMethod) {50 if (citrus == null) {51 citrus = Citrus.newInstance(applicationContext);52 }53 TestContext ctx = prepareTestContext(citrus.createTestContext());54 TestLoader testLoader = createTestLoader(frameworkMethod.getTestName(), frameworkMethod.getPackageName());55 TestCase testCase = testLoader.load();56 citrus.run(testCase, ctx);57 }58 /**59 * Resolves method arguments supporting TestNG data provider parameters as well as60 * {@link CitrusResource} annotated methods.61 *62 * @param frameworkMethod63 * @param testCase64 * @param context65 * @return66 */67 protected Object[] resolveParameter(CitrusJUnit4Runner.CitrusFrameworkMethod frameworkMethod, TestCase testCase, TestContext context) {68 Object[] values = new Object[frameworkMethod.getMethod().getParameterTypes().length];69 Class<?>[] parameterTypes = frameworkMethod.getMethod().getParameterTypes();70 for (int i = 0; i < parameterTypes.length; i++) {71 final Annotation[] parameterAnnotations = frameworkMethod.getMethod().getParameterAnnotations()[i];72 Class<?> parameterType = parameterTypes[i];73 for (Annotation annotation : parameterAnnotations) {74 if (annotation instanceof CitrusResource) {75 values[i] = resolveAnnotatedResource(frameworkMethod, parameterType, context);76 }77 }78 }79 return values;80 }81 /**82 * Resolves value for annotated method parameter.83 *84 * @param frameworkMethod85 * @param parameterType86 * @return87 */88 protected Object resolveAnnotatedResource(CitrusJUnit4Runner.CitrusFrameworkMethod frameworkMethod, Class<?> parameterType, TestContext context) {89 if (TestContext.class.isAssignableFrom(parameterType)) {90 return context;91 } else {92 throw new CitrusRuntimeException("Not able to provide a Citrus resource injection for type " + parameterType);93 }94 }95 /**96 * Execute the test case.97 */98 protected void executeTest() {99 run(new CitrusJUnit4Runner.CitrusFrameworkMethod(ReflectionUtils.findMethod(this.getClass(), "executeTest"),100 this.getClass().getSimpleName(), this.getClass().getPackage().getName()));101 }102 /**103 * Prepares the test context.104 *105 * Provides a hook for test context modifications before the test gets executed.106 *107 * @param testContext the test context.108 * @return the (prepared) test context.109 */110 protected TestContext prepareTestContext(final TestContext testContext) {111 return testContext;112 }113 /**114 * Creates new test loader which has TestNG test annotations set for test execution. Only115 * suitable for tests that get created at runtime through factory method. Subclasses116 * may overwrite this in order to provide custom test loader with custom test annotations set.117 * @param testName118 * @param packageName119 * @return120 */121 protected TestLoader createTestLoader(String testName, String packageName) {122 return new XmlTestLoader(getClass(), testName, packageName, applicationContext);123 }124 /**125 * Constructs the test case to execute.126 * @return127 */128 protected TestCase getTestCase() {129 return createTestLoader(this.getClass().getSimpleName(), this.getClass().getPackage().getName()).load();130 }131}...

Full Screen

Full Screen

Source:XmlTestLoader.java Github

copy

Full Screen

...28 *29 * @author Christoph Deppisch30 * @since 2.131 */32public class XmlTestLoader implements TestLoader {33 private TestCase testCase;34 private Class<?> testClass;35 private String testName;36 private String packageName;37 private ApplicationContext parentContext;38 private String contextFile;39 /**40 * Default constructor with context file and parent application context field.41 * @param testClass42 * @param testName43 * @param packageName44 * @param parentContext45 */46 public XmlTestLoader(Class<?> testClass, String testName, String packageName, ApplicationContext parentContext) {47 this.testClass = testClass;48 this.testName = testName;49 this.packageName = packageName;50 this.parentContext = parentContext;51 }52 @Override53 public TestCase load() {54 if (testCase == null) {55 ApplicationContext ctx = loadApplicationContext();56 try {57 testCase = ctx.getBean(testName, TestCase.class);58 testCase.setTestClass(testClass);59 testCase.setPackageName(packageName);60 } catch (NoSuchBeanDefinitionException e) {...

Full Screen

Full Screen

XmlTestLoader

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.common;2import java.io.File;3import java.io.IOException;4import java.util.ArrayList;5import java.util.List;6import java.util.Map;7import javax.xml.parsers.DocumentBuilder;8import javax.xml.parsers.DocumentBuilderFactory;9import javax.xml.parsers.ParserConfigurationException;10import org.springframework.core.io.Resource;11import org.springframework.core.io.support.PathMatchingResourcePatternResolver;12import org.springframework.util.StringUtils;13import org.w3c.dom.Document;14import org.w3c.dom.Element;15import org.w3c.dom.Node;16import org.w3c.dom.NodeList;17import org.xml.sax.SAXException;18public class XmlTestLoader {19public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {20PathMatchingResourcePatternResolver resolver = new PathMatchingResourcePatternResolver();21Resource[] resources = resolver.getResources("classpath*:/*.xml");22for (Resource resource : resources) {23System.out.println("Resource : " + resource.getFilename());24File file = resource.getFile();25DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();26DocumentBuilder db = dbf.newDocumentBuilder();27Document doc = db.parse(file);28Element root = doc.getDocumentElement();29NodeList list = root.getChildNodes();30for (int i = 0; i < list.getLength(); i++) {31Node node = list.item(i);32if (node.getNodeType() == Node.ELEMENT_NODE) {33Element element = (Element) node;34if (element.getTagName().equals("test")) {35String name = element.getAttribute("name");36System.out.println("Test name : " + name);37String description = element.getAttribute("description");38System.out.println("Test description : " + description);39String author = element.getAttribute("author");40System.out.println("Test author : " + author);41String status = element.getAttribute("status");42System.out.println("Test status : " + status);43String enabled = element.getAttribute("enabled");44System.out.println("Test enabled : " + enabled);45String packageName = element.getAttribute("package");46System.out.println("Test package : " + packageName);47String className = element.getAttribute("class");48System.out.println("Test class : " + className);49String methodName = element.getAttribute("method");50System.out.println("Test method : " + methodName);51String groups = element.getAttribute("groups");52System.out.println("Test groups : " + groups);53String dependsOnMethods = element.getAttribute("depends-on-methods");54System.out.println("Test depends on methods : " + dependsOnMethods);

Full Screen

Full Screen

XmlTestLoader

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.common;2import java.io.File;3import java.io.FileNotFoundException;4import java.io.IOException;5import java.util.HashMap;6import java.util.Map;7import java.util.logging.Level;8import java.util.logging.Logger;9import org.testng.annotations.Test;10import com.consol.citrus.exceptions.CitrusRuntimeException;11import com.consol.citrus.testng.AbstractTestNGCitrusTest;12import com.consol.citrus.xml.XsdSchemaRepository;13import com.consol.citrus.xml.namespace.NamespaceContextBuilder;14import com.consol.citrus.xml.schema.XsdSchema;15import com.consol.citrus.xml.schema.XsdSchemaRepositoryImpl;16import com.consol.citrus.xml.xpath.XPathUtils;17import com.consol.citrus.xml.xpath.XPathValidation

Full Screen

Full Screen

XmlTestLoader

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.common;2import java.io.File;3import java.io.IOException;4import java.util.Map;5import org.testng.annotations.Test;6import org.xml.sax.SAXException;7public class XmlTestLoaderTest {8public void testXmlTestLoader() throws SAXException, IOException {9XmlTestLoader loader = new XmlTestLoader();10Map<String, Object> test = loader.load(new File("C:\\Users\\user\\Desktop\\xmltest.xml"));11System.out.println(test);12}13}

Full Screen

Full Screen

XmlTestLoader

Using AI Code Generation

copy

Full Screen

1package com.consol.citrus.common;2import com.consol.citrus.dsl.testng.TestNGCitrusTestDesigner;3import com.consol.citrus.dsl.testng.TestNGCitrusTestRunner;4import org.testng.annotations.DataProvider;5import org.testng.annotations.Test;6public class TestNGCitrusTestRunner extends TestNGCitrusTestDesigner {7 @Test(dataProvider = "testDataProvider")8 public void testSample(Object[][] testAction) {9 run(testAction);10 }11 public Object[][] testDataProvider() {12 return new Object[][] {13 { new XmlTestLoader().load("classpath:com/consol/citrus/common/4.xml") }14 };15 }16}17[main] INFO org.springframework.context.support.ClassPathXmlApplicationContext - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5b4d4f4: startup date [Sun Feb 12 22:30:39 IST 2017]; root of context hierarchy

Full Screen

Full Screen

XmlTestLoader

Using AI Code Generation

copy

Full Screen

1import org.apache.log4j.Logger;2import org.apache.log4j.PropertyConfigurator;3import org.springframework.context.ApplicationContext;4import org.springframework.context.support.ClassPathXmlApplicationContext;5import com.consol.citrus.common.XmlTestLoader;6import com.consol.citrus.TestCase;7import com.consol.citrus.exceptions.CitrusRuntimeException;8public class 4 {9 private static Logger log = Logger.getLogger(4.class);10 public static void main(String[] args) {11 PropertyConfigurator.configure("log4j.properties");12 ApplicationContext ctx = new ClassPathXmlApplicationContext("citrus-context.xml");13 XmlTestLoader loader = ctx.getBean("xmlTestLoader", XmlTestLoader.class);14 try {15 TestCase testCase = loader.load("classpath:4.xml");16 log.info(testCase);17 } catch (CitrusRuntimeException e) {18 log.error("Error loading test case", e);19 }20 }21}

Full Screen

Full Screen

XmlTestLoader

Using AI Code Generation

copy

Full Screen

1import java.util.*;2import com.consol.citrus.common.*;3import com.consol.citrus.common.XmlTestLoader;4import com.consol.citrus.common.TestAction;5public class 4 {6public static void main(String[] args) {7XmlTestLoader xmlTestLoader = new XmlTestLoader();8List<TestAction> testActions = xmlTestLoader.load("testcase.xml");9for (TestAction testAction : testActions) {10testAction.execute();11}12}13}

Full Screen

Full Screen

XmlTestLoader

Using AI Code Generation

copy

Full Screen

1import com.consol.citrus.common.XmlTestLoader;2import com.consol.citrus.TestCase;3import com.consol.citrus.TestSuite;4import org.apache.tools.ant.Project;5import org.apache.tools.ant.taskdefs.Ant;6import org.apache.tools.ant.types.FileSet;7public class 4 {8 public static void main(String[] args) {9 TestSuite suite = new TestSuite();10 suite.addTest(XmlTestLoader.load("4.xml"));11 Project project = new Project();12 project.init();13 Ant ant = new Ant();14 ant.setProject(project);15 ant.setTaskName("ant");16 ant.setAntfile("4.xml");17 ant.execute();18 }19}20import com.consol.citrus.common.XmlTestLoader;21import com.consol.citrus.TestCase;22import com.consol.citrus.TestSuite;23import org.apache.tools.ant.Project;24import org.apache.tools.ant.taskdefs.Ant;25import org.apache.tools.ant.types.FileSet;26public class 4 {27 public static void main(String[] args) {28 TestSuite suite = new TestSuite();29 suite.addTest(XmlTestLoader.load("4.xml"));30 Project project = new Project();31 project.init();32 Ant ant = new Ant();33 ant.setProject(project);34 ant.setTaskName("ant");

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.

Run Citrus automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful