From 79044f2f8c917d43e867815d57457696105ed42e Mon Sep 17 00:00:00 2001 From: AutomatedOwl Date: Thu, 7 Jun 2018 02:30:13 +0300 Subject: [PATCH] Added support for TestNG to use non-annotated tests. --- .../automatedowl/tools/JSCollectorJUnitTest.java | 10 ++++++++++ testng/pom.xml | 2 +- .../automatedowl/tools/JSErrorsCollectorListener.java | 10 +++++++++- .../automatedowl/tools/JSCollectorTestNGTest.java | 8 ++++++++ 4 files changed, 28 insertions(+), 2 deletions(-) diff --git a/junit5/src/test/java/com/github/automatedowl/tools/JSCollectorJUnitTest.java b/junit5/src/test/java/com/github/automatedowl/tools/JSCollectorJUnitTest.java index baf8c38..84bb613 100644 --- a/junit5/src/test/java/com/github/automatedowl/tools/JSCollectorJUnitTest.java +++ b/junit5/src/test/java/com/github/automatedowl/tools/JSCollectorJUnitTest.java @@ -1,5 +1,7 @@ package com.github.automatedowl.tools; +import static org.junit.jupiter.api.Assertions.assertTrue; + import com.github.automatedowl.tools.drivers.junitholder.JSErrorsDriverHolder; import com.github.automatedowl.tools.pages.BlankPage; import com.github.automatedowl.tools.pages.Site88Page; @@ -72,6 +74,14 @@ void noJSErrorsTest(TestInfo testInfo) throws InterruptedException { waitBeforeClosingBrowser(); } + /** Test method. + * It should not use JSErrorsCollectorJUnit annotation. */ + @Test + void noAnnotationTest() { + driver = new ChromeDriver(); + assertTrue(true); + } + @AfterEach void closeDriver() { driver.quit(); diff --git a/testng/pom.xml b/testng/pom.xml index 9e4d149..652a0ad 100644 --- a/testng/pom.xml +++ b/testng/pom.xml @@ -6,7 +6,7 @@ com.github.automatedowl chromedriver-js-errors-collector-testng - 1.0.2 + 1.0.3 ${project.groupId}:${project.artifactId} Java library which allows to easily collect JS errors received in Chromedriver session, using annotations on test methods. diff --git a/testng/src/main/java/com/github/automatedowl/tools/JSErrorsCollectorListener.java b/testng/src/main/java/com/github/automatedowl/tools/JSErrorsCollectorListener.java index 2d736fe..b41cce8 100644 --- a/testng/src/main/java/com/github/automatedowl/tools/JSErrorsCollectorListener.java +++ b/testng/src/main/java/com/github/automatedowl/tools/JSErrorsCollectorListener.java @@ -38,7 +38,7 @@ public void beforeInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestRes public void afterInvocation(IInvokedMethod iInvokedMethod, ITestResult iTestResult) { // Check if test method had invoked. - if (iInvokedMethod.getTestMethod().isTest()) { + if (iInvokedMethod.getTestMethod().isTest() && isTestAnnotated(iInvokedMethod)) { // Get log entries from Chromedriver session. List logEntries = getLogEntriesForTest(iInvokedMethod); @@ -69,6 +69,14 @@ else if (isAssertJSErrorsEnabled( } } + private boolean isTestAnnotated(IInvokedMethod method) { + return method + .getTestMethod() + .getConstructorOrMethod() + .getMethod() + .getAnnotation(JSErrorsCollectorTestNG.class) != null; + } + private boolean isJSErrorContained(String message) { Pattern pattern = Pattern.compile(JS_ERRORS_REGEX); Matcher matcher = pattern.matcher(message); diff --git a/testng/src/test/java/com/github/automatedowl/tools/JSCollectorTestNGTest.java b/testng/src/test/java/com/github/automatedowl/tools/JSCollectorTestNGTest.java index bed4d16..2d59b95 100644 --- a/testng/src/test/java/com/github/automatedowl/tools/JSCollectorTestNGTest.java +++ b/testng/src/test/java/com/github/automatedowl/tools/JSCollectorTestNGTest.java @@ -6,6 +6,7 @@ import com.github.automatedowl.tools.pages.Site88Page; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; +import org.testng.Assert; import org.testng.annotations.*; /** Class that contains TestNG unit tests for Chromedriver JS errors collector. */ @@ -64,6 +65,13 @@ void noJSErrorsTest() throws InterruptedException { waitBeforeClosingBrowser(); } + /** Test method. + * It should not use JSErrorsCollectorTestNG annotation. */ + @Test + void noAnnotationTest() { + Assert.assertTrue(true); + } + @AfterMethod void closeDriver() { driver.quit();