How to use GalenTest class of com.galenframework.tests package

Best Galen code snippet using com.galenframework.tests.GalenTest

Source:GalenActionTest.java Github

copy

Full Screen

...15******************************************************************************/16package com.galenframework.actions;17import com.galenframework.TestRunnable;18import com.galenframework.config.GalenConfig;19import com.galenframework.reports.GalenTestInfo;20import com.galenframework.reports.HtmlReportBuilder;21import com.galenframework.reports.JunitReportBuilder;22import com.galenframework.reports.TestNgReportBuilder;23import com.galenframework.reports.json.JsonReportBuilder;24import com.galenframework.reports.model.FileTempStorage;25import com.galenframework.runner.CombinedListener;26import com.galenframework.runner.CompleteListener;27import com.galenframework.runner.EventHandler;28import com.galenframework.runner.JsTestCollector;29import com.galenframework.runner.events.TestFilterEvent;30import com.galenframework.suite.reader.GalenSuiteReader;31import com.galenframework.tests.GalenTest;32import org.slf4j.Logger;33import org.slf4j.LoggerFactory;34import java.io.File;35import java.io.FileNotFoundException;36import java.io.IOException;37import java.io.PrintStream;38import java.util.Collections;39import java.util.LinkedList;40import java.util.List;41import java.util.concurrent.ExecutorService;42import java.util.concurrent.Executors;43import java.util.regex.Pattern;44import static java.util.Arrays.asList;45public class GalenActionTest extends GalenAction {46 private final static Logger LOG = LoggerFactory.getLogger(GalenActionTest.class);47 private final GalenActionTestArguments testArguments;48 private final CombinedListener listener;49 public GalenActionTest(String[] arguments, PrintStream outStream, PrintStream errStream, CombinedListener listener) {50 super(arguments, outStream, errStream);51 this.testArguments = GalenActionTestArguments.parse(arguments);52 this.listener = createListeners(listener);53 }54 @Override55 public void execute() throws Exception {56 loadConfigIfNeeded(getTestArguments().getConfig());57 List<File> basicTestFiles = new LinkedList<>();58 List<File> jsTestFiles = new LinkedList<>();59 for (String path : testArguments.getPaths()) {60 File file = new File(path);61 if (file.exists()) {62 if (file.isDirectory()) {63 searchForTests(file, testArguments.getRecursive(), basicTestFiles, jsTestFiles);64 } else if (file.isFile()) {65 String name = file.getName().toLowerCase();66 if (name.endsWith(GalenConfig.getConfig().getTestSuffix())) {67 basicTestFiles.add(file);68 } else if (name.endsWith(".js")) {69 jsTestFiles.add(file);70 }71 }72 } else {73 throw new FileNotFoundException(path);74 }75 }76 if (basicTestFiles.size() > 0 || jsTestFiles.size() > 0) {77 runTestFiles(basicTestFiles, jsTestFiles);78 } else {79 throw new RuntimeException("Couldn't find any test files");80 }81 }82 private void runTestFiles(List<File> basicTestFiles, List<File> jsTestFiles) throws IOException {83 GalenSuiteReader reader = new GalenSuiteReader();84 List<GalenTest> tests = new LinkedList<>();85 for (File file : basicTestFiles) {86 tests.addAll(reader.read(file));87 }88 JsTestCollector testCollector = new JsTestCollector(tests);89 for (File jsFile : jsTestFiles) {90 testCollector.execute(jsFile);91 }92 testCollector.getEventHandler().invokeBeforeTestSuiteEvents();93 runTests(testCollector.getEventHandler(), tests, testArguments, listener);94 testCollector.getEventHandler().invokeAfterTestSuiteEvents();95 }96 public static void runTests(EventHandler eventHandler, List<GalenTest> tests, GalenActionTestArguments testArguments, CombinedListener listener) {97 if (testArguments.getParallelThreads() > 1) {98 runTestsInThreads(eventHandler, tests, testArguments.getParallelThreads(), testArguments, listener);99 } else {100 runTestsInThreads(eventHandler, tests, 1, testArguments, listener);101 }102 }103 private static void runTestsInThreads(final EventHandler eventHandler, List<GalenTest> tests,104 int amountOfThreads, GalenActionTestArguments testArguments, CombinedListener listener) {105 ExecutorService executor = Executors.newFixedThreadPool(amountOfThreads);106 Pattern filterPattern = createTestFilter(testArguments.getFilter());107 List<GalenTest> filteredTests = filterTests(tests, eventHandler);108 tellBeforeTestSuite(listener, filteredTests);109 List<GalenTestInfo> testInfos = Collections.synchronizedList(new LinkedList<GalenTestInfo>());110 for (final GalenTest test : filteredTests) {111 if (matchesPattern(test.getName(), filterPattern)112 && matchesSelectedGroups(test, testArguments.getGroups())113 && doesNotMatchExcludedGroups(test, testArguments.getExcludedGroups())) {114 executor.execute(new TestRunnable(test, listener, eventHandler, testInfos));115 }116 }117 executor.shutdown();118 while (!executor.isTerminated()) {119 }120 tellAfterTestSuite(testInfos, listener);121 createAllReports(testInfos, testArguments);122 cleanData(testInfos);123 }124 private void searchForTests(File file, boolean recursive, List<File> files, List<File> jsFiles, int level) {125 String fileName = file.getName().toLowerCase();126 if (file.isFile()) {127 if (fileName.endsWith(GalenConfig.getConfig().getTestSuffix())) {128 files.add(file);129 } else if (fileName.endsWith(GalenConfig.getConfig().getTestJsSuffix())) {130 jsFiles.add(file);131 }132 } else if (file.isDirectory() && (level == 0 || recursive)) {133 for (File childFile : file.listFiles()) {134 searchForTests(childFile, recursive, files, jsFiles, level + 1);135 }136 }137 }138 private static void cleanData(List<GalenTestInfo> testInfos) {139 for (GalenTestInfo testInfo : testInfos) {140 if (testInfo.getReport() != null) {141 FileTempStorage storage = testInfo.getReport().getFileStorage();142 if (storage != null) {143 storage.cleanup();144 }145 }146 }147 }148 private static boolean doesNotMatchExcludedGroups(GalenTest test, List<String> excludedGroups) {149 if (excludedGroups != null && excludedGroups.size() > 0) {150 return !matchesSelectedGroups(test, excludedGroups);151 }152 return true;153 }154 private static boolean matchesSelectedGroups(GalenTest test, List<String> selectedGroups) {155 if (selectedGroups != null && selectedGroups.size() > 0) {156 List<String> testGroups = test.getGroups();157 if (testGroups != null && testGroups.size() > 0) {158 for (String testGroup : testGroups) {159 if (selectedGroups.contains(testGroup)) {160 return true;161 }162 }163 }164 return false;165 }166 return true;167 }168 private static List<GalenTest> filterTests(List<GalenTest> tests, EventHandler eventHandler) {169 List<TestFilterEvent> filters = eventHandler.getTestFilterEvents();170 if (filters != null && filters.size() > 0) {171 GalenTest[] arrTests = tests.toArray(new GalenTest[]{});172 for (TestFilterEvent filter : filters) {173 arrTests = filter.execute(arrTests);174 }175 if (arrTests == null) {176 arrTests = new GalenTest[]{};177 }178 return asList(arrTests);179 } else {180 return tests;181 }182 }183 private static void tellBeforeTestSuite(CompleteListener listener, List<GalenTest> tests) {184 if (listener != null) {185 try {186 listener.beforeTestSuite(tests);187 } catch (Exception ex) {188 LOG.error("Unknow error before running testsuites.", ex);189 }190 }191 }192 private static void tellAfterTestSuite(List<GalenTestInfo> testInfos, CombinedListener listener) {193 if (listener != null) {194 try {195 listener.afterTestSuite(testInfos);196 } catch (Exception ex) {197 LOG.error("Unknow error after running testsuites.", ex);198 }199 }200 }201 private static void createAllReports(List<GalenTestInfo> testInfos, GalenActionTestArguments testArguments) {202 if (testArguments.getTestngReport() != null) {203 createTestngReport(testArguments.getTestngReport(), testInfos);204 }205 if (testArguments.getJunitReport() != null) {206 createJunitReport(testArguments.getJunitReport(), testInfos);207 }208 if (testArguments.getHtmlReport() != null) {209 createHtmlReport(testArguments.getHtmlReport(), testInfos);210 }211 if (testArguments.getJsonReport() != null) {212 createJsonReport(testArguments.getJsonReport(), testInfos);213 }214 }215 private static void createJsonReport(String jsonReport, List<GalenTestInfo> testInfos) {216 try {217 new JsonReportBuilder().build(testInfos, jsonReport);218 } catch (IOException e) {219 LOG.error("Failed generating json report", e);220 }221 }222 private static void createHtmlReport(String htmlReportPath, List<GalenTestInfo> testInfos) {223 try {224 new HtmlReportBuilder().build(testInfos, htmlReportPath);225 } catch (Exception ex) {226 LOG.error("Unknown error during creating HTML report.", ex);227 }228 }229 private static void createJunitReport(String junitReport, List<GalenTestInfo> testInfos) {230 try {231 new JunitReportBuilder().build(testInfos, junitReport);232 } catch (Exception ex) {233 LOG.error("Unknown error during creating Junit report.", ex);234 }235 }236 private static void createTestngReport(String testngReport, List<GalenTestInfo> testInfos) {237 try {238 new TestNgReportBuilder().build(testInfos, testngReport);239 } catch (Exception ex) {240 LOG.error("Unknown error during creating TestNG report.", ex);241 }242 }243 private static boolean matchesPattern(String name, Pattern filterPattern) {244 if (filterPattern != null) {245 return filterPattern.matcher(name).matches();246 } else247 return true;248 }249 private static Pattern createTestFilter(String filter) {250 return filter != null ? Pattern.compile(filter.replace("*", ".*")) : null;...

Full Screen

Full Screen

Source:TestRunnable.java Github

copy

Full Screen

...13* See the License for the specific language governing permissions and14* limitations under the License.15******************************************************************************/16package com.galenframework;17import com.galenframework.reports.GalenTestInfo;18import com.galenframework.reports.TestReport;19import com.galenframework.runner.CompleteListener;20import com.galenframework.runner.EventHandler;21import com.galenframework.runner.TestListener;22import com.galenframework.tests.GalenTest;23import com.galenframework.tests.TestSession;24import com.galenframework.reports.GalenTestInfo;25import com.galenframework.reports.TestReport;26import com.galenframework.runner.CompleteListener;27import com.galenframework.runner.EventHandler;28import com.galenframework.runner.TestListener;29import com.galenframework.runner.events.TestRetryEvent;30import com.galenframework.tests.GalenTest;31import com.galenframework.tests.TestSession;32import java.util.Date;33import java.util.List;34import org.slf4j.Logger;35import org.slf4j.LoggerFactory;36/**37 * Used for running the test and invoking all test related events like: before, after, testRetry38 */39public class TestRunnable implements Runnable {40 private final static Logger LOG = LoggerFactory.getLogger(TestRunnable.class);41 42 private final GalenTest test;43 private final CompleteListener listener;44 private final EventHandler eventHandler;45 private final List<GalenTestInfo> testInfos;46 public TestRunnable(GalenTest test, CompleteListener listener, EventHandler eventHandler, List<GalenTestInfo> testInfos) {47 this.test = test;48 this.listener = listener;49 this.eventHandler = eventHandler;50 this.testInfos = testInfos;51 }52 private GalenTestInfo runTest() {53 GalenTestInfo info = new GalenTestInfo(test.getName(), test);54 TestReport report = new TestReport();55 info.setStartedAt(new Date());56 info.setReport(report);57 TestSession session = TestSession.register(info);58 session.setReport(report);59 session.setListener(listener);60 eventHandler.invokeBeforeTestEvents(info);61 tellTestStarted(listener, test);62 try {63 test.execute(report, listener);64 }65 catch(Exception ex) {66 info.setException(ex);67 report.error(ex);68 if (listener != null) {69 listener.onGlobalError(ex);70 }71 }72 info.setEndedAt(new Date());73 eventHandler.invokeAfterTestEvents(info);74 tellTestFinished(listener, test);75 TestSession.clear();76 return info;77 }78 @Override79 public void run() {80 GalenTestInfo info = null;81 boolean shouldRetry = true;82 int tries = 1;83 while (shouldRetry) {84 info = runTest();85 if (info.isFailed()) {86 shouldRetry = checkIfShouldRetry(info.getTest(), tries);87 }88 else {89 shouldRetry = false;90 }91 tries++;92 }93 testInfos.add(info);94 }95 private boolean checkIfShouldRetry(GalenTest test, int tries) {96 for (TestRetryEvent retryEvent : eventHandler.getTestRetryEvents()) {97 if (retryEvent.shouldRetry(test, tries)) {98 return true;99 }100 }101 return false;102 }103 private void tellTestFinished(TestListener testListener, GalenTest test) {104 try {105 if (testListener != null) {106 testListener.onTestFinished(test);107 }108 }109 catch (Exception e) {110 LOG.error("Unkown error during test finishing", e);111 }112 }113 private void tellTestStarted(TestListener testListener, GalenTest test) {114 try {115 if (testListener != null) {116 testListener.onTestStarted(test);117 }118 }119 catch (Exception e) {120 LOG.error("Unkown error during test start", e);121 }122 }123}...

Full Screen

Full Screen

Source:JsTestCollector.java Github

copy

Full Screen

...21import java.util.LinkedList;22import java.util.List;23import com.galenframework.runner.events.TestFilterEvent;24import com.galenframework.runner.events.TestSuiteEvent;25import com.galenframework.tests.GalenTest;26import com.galenframework.runner.events.TestFilterEvent;27import com.galenframework.runner.events.TestRetryEvent;28import com.galenframework.javascript.GalenJsExecutor;29import com.galenframework.runner.events.TestEvent;30import com.galenframework.runner.events.TestSuiteEvent;31import com.galenframework.tests.GalenTest;32public class JsTestCollector {33 private List<GalenTest> collectedTests = new LinkedList<>();34 private EventHandler eventHandler = new EventHandler();35 36 private GalenJsExecutor js = createExecutor();37 38 public JsTestCollector(List<GalenTest> tests) {39 this.collectedTests = tests;40 }41 private GalenJsExecutor createExecutor() {42 GalenJsExecutor jsExector = new GalenJsExecutor();43 jsExector.putObject("_galenCore", this);44 45 jsExector.eval(GalenJsExecutor.loadJsFromLibrary("GalenCore.js"));46 jsExector.eval(GalenJsExecutor.loadJsFromLibrary("GalenApi.js"));47 jsExector.eval(GalenJsExecutor.loadJsFromLibrary("GalenPages.js"));48 return jsExector;49 }50 public JsTestCollector() {51 }52 public void execute(File file) throws IOException {53 Reader scriptFileReader = new FileReader(file);54 js.eval(scriptFileReader, file.getAbsolutePath());55 }56 public void addTest(GalenTest test) {57 this.collectedTests.add(test);58 }59 60 public List<GalenTest> getCollectedTests() {61 return this.collectedTests;62 }63 public EventHandler getEventHandler() {64 return eventHandler;65 }66 public void setEventHandler(EventHandler eventHandler) {67 this.eventHandler = eventHandler;68 }69 70 71 public void addBeforeTestSuiteEvent(TestSuiteEvent event) {72 eventHandler.getBeforeTestSuiteEvents().add(event);73 }74 ...

Full Screen

Full Screen

GalenTest

Using AI Code Generation

copy

Full Screen

1import com.galenframework.tests.GalenTest;2import org.testng.annotations.Test;3import java.io.IOException;4public class 1 extends GalenTest {5public void sampleTest() throws IOException {6load("/path/to/specfile.spec");7checkLayout("/path/to/pagefile.html", "desktop");8}9}10import com.galenframework.testng.GalenTestBase;11import org.testng.annotations.Test;12import java.io.IOException;13public class 2 extends GalenTestBase {14public void sampleTest() throws IOException {15load("/path/to/specfile.spec");16checkLayout("/path/to/pagefile.html", "desktop");17}18}

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 Galen automation tests on LambdaTest cloud grid

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

Most used methods in GalenTest

Test Your Web Or Mobile Apps On 3000+ Browsers

Signup for free

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful