Skip to content

Commit

Permalink
Merge pull request #325 from Xceptance/322-bug-browser-is-not-started…
Browse files Browse the repository at this point in the history
…-for-after-method-if-its-declared-in-different-class-than-the-test-itself

[#322] Bug: Browser is not started for @after method if it's declared in different class than the test itself
  • Loading branch information
wurzelkuchen authored Jan 23, 2025
2 parents e7e0c28 + 5acfe02 commit 1942624
Show file tree
Hide file tree
Showing 11 changed files with 130 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public void run(Supplier<Throwable> afterMethodInvocation, Method after, boolean

// if browserConfiguration is null, the browser should not be started for this method and browserTag and
// browserRunner are therefore not required
BrowserMethodData browserTag = browserConfiguration != null ? BrowserData.addKeepBrowserOpenInformation(browserConfiguration.getBrowserTag(), after)
BrowserMethodData browserTag = browserConfiguration != null ? BrowserData.addKeepBrowserOpenInformationForBeforeOrAfter(browserConfiguration.getBrowserTag(),
after)
: null;
BrowserRunner browserRunner = browserTag != null ? new BrowserRunner(browserTag, after.getName()) : null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ public void run(Supplier<Throwable> beforeMethodInvocation, Method before, boole

// if browserConfiguration is null, the browser should not be started for this method and browserTag and
// browserRunner are therefore not required
BrowserMethodData browserTag = browserConfiguration != null ? BrowserData.addKeepBrowserOpenInformation(browserConfiguration.getBrowserTag(), before)
BrowserMethodData browserTag = browserConfiguration != null ? BrowserData.addKeepBrowserOpenInformationForBeforeOrAfter(browserConfiguration.getBrowserTag(),
before)
: null;
BrowserRunner browserRunner = browserTag != null ? new BrowserRunner(browserTag, before.getName()) : null;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ public class BrowserData extends Data

private List<RandomBrowsers> classRandomBrowsersAnnotation;

private Class<?> testClass;

private static final String SYSTEM_PROPERTY_BROWSERDEFINITION = "browserdefinition";

public BrowserData(Class<?> testClass)
Expand All @@ -42,6 +44,7 @@ public BrowserData(Class<?> testClass)

public void initClassAnnotationsFor(Class<?> testClass)
{
this.testClass = testClass;
classRandomBrowsersAnnotation = getAnnotations(testClass, RandomBrowsers.class);

if (getAnnotations(testClass, SuppressBrowsers.class).isEmpty())
Expand Down Expand Up @@ -206,7 +209,7 @@ else if (!classRandomBrowsersAnnotation.isEmpty() && methodBrowsers.isEmpty())
.collect(Collectors.toList());
}

public static BrowserMethodData addKeepBrowserOpenInformation(String browserTag, Method method)
public static BrowserMethodData addKeepBrowserOpenInformationForBeforeOrAfter(String browserTag, Method method)
{
List<KeepBrowserOpen> methodKeepBrowserOpenAnnotations = getAnnotations(method, KeepBrowserOpen.class);
List<KeepBrowserOpen> classKeepBrowserOpenAnnotations = getAnnotations(method.getDeclaringClass(), KeepBrowserOpen.class);
Expand Down Expand Up @@ -243,15 +246,22 @@ public static BrowserMethodData addKeepBrowserOpenInformation(String browserTag,
keepOpenOnFailure = false;
}
}

return new BrowserMethodData(browserTag, keepOpen, keepOpenOnFailure, false, false, new ArrayList<Method>());
}

private BrowserMethodData addKeepBrowserOpenInformation(String browserTag, Method method)
{
BrowserMethodData browserMethodData = addKeepBrowserOpenInformationForBeforeOrAfter(browserTag, method);
boolean junit5 = method.getAnnotation(NeodymiumTest.class) != null;
Class<?> testClass = method.getDeclaringClass();
List<Method> afterMethodsWithTestBrowser = List.of(testClass.getMethods()).stream()
.filter(classMethod -> (junit5 ? classMethod.getAnnotation(AfterEach.class)
: classMethod.getAnnotation(After.class)) != null)
.collect(Collectors.toList());
if (!(Neodymium.configuration().startNewBrowserForSetUp() && Neodymium.configuration().startNewBrowserForCleanUp()))
{
return new BrowserMethodData(browserTag, keepOpen, keepOpenOnFailure, false, false, afterMethodsWithTestBrowser);
browserMethodData.setAfterMethodsWithTestBrowser(afterMethodsWithTestBrowser);
return browserMethodData;

}
boolean separateBrowserForSetupRequired = false;
Expand Down Expand Up @@ -284,7 +294,10 @@ public static BrowserMethodData addKeepBrowserOpenInformation(String browserTag,
separateBrowserForCleanupRequired = afterMethodsWithTestBrowser.isEmpty() && !afterMethods.isEmpty();
}

return new BrowserMethodData(browserTag, keepOpen, keepOpenOnFailure, separateBrowserForSetupRequired, separateBrowserForCleanupRequired, afterMethodsWithTestBrowser);
browserMethodData.setStartBrowserOnSetUp(separateBrowserForSetupRequired);
browserMethodData.setStartBrowserOnCleanUp(separateBrowserForCleanupRequired);
browserMethodData.setAfterMethodsWithTestBrowser(afterMethodsWithTestBrowser);
return browserMethodData;
}

private List<String> computeRandomBrowsers(final Method method, final List<RandomBrowsers> randomBrowsersAnnotation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,19 @@ public List<Method> getAfterMethodsWithTestBrowser()
{
return afterMethodsWithTestBrowser;
}

public void setStartBrowserOnSetUp(boolean startBrowserOnSetUp)
{
this.startBrowserOnSetUp = startBrowserOnSetUp;
}

public void setStartBrowserOnCleanUp(boolean startBrowserOnCleanUp)
{
this.startBrowserOnCleanUp = startBrowserOnCleanUp;
}

public void setAfterMethodsWithTestBrowser(List<Method> afterMethodsWithTestBrowser)
{
this.afterMethodsWithTestBrowser = afterMethodsWithTestBrowser;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public void evaluate() throws Throwable
{
if (!param.isStartBrowserOnCleanUp() && Neodymium.getWebDriverStateContainer() != null)
{
new BrowserRunner().teardown(testFailed, param, Neodymium.getWebDriverStateContainer());
browserRunner.teardown(testFailed, param, Neodymium.getWebDriverStateContainer());
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.xceptance.neodymium.junit4.testclasses.browser.inheritance;

import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;

import com.xceptance.neodymium.junit4.tests.NeodymiumWebDriverTest;
import com.xceptance.neodymium.util.Neodymium;

public class BrowserChildTest extends BrowserParent
{
@Before
public void before()
{
Assert.assertNotNull("No browser started for @Before method", Neodymium.getDriver());
NeodymiumWebDriverTest.assertWebDriverAlive(Neodymium.getDriver());
}

@Test
public void test()
{
Assert.assertNotNull("No browser started for @Test method", Neodymium.getDriver());
NeodymiumWebDriverTest.assertWebDriverAlive(Neodymium.getDriver());
}

@After
public void after()
{
Assert.assertNotNull("No browser started for @After method", Neodymium.getDriver());
NeodymiumWebDriverTest.assertWebDriverAlive(Neodymium.getDriver());
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,23 @@
package com.xceptance.neodymium.junit4.testclasses.browser.inheritance;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;

import com.xceptance.neodymium.common.browser.Browser;
import com.xceptance.neodymium.junit4.NeodymiumRunner;
import com.xceptance.neodymium.junit5.tests.NeodymiumWebDriverTest;
import com.xceptance.neodymium.util.Neodymium;

@Browser("Chrome_1024x768")
@Browser("Chrome_1500x1000")
@RunWith(NeodymiumRunner.class)
public abstract class BrowserParent
{

@Test
public void testParent()
{
Assert.assertNotNull(Neodymium.getDriver());
NeodymiumWebDriverTest.assertWebDriverAlive(Neodymium.getDriver());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -476,11 +476,12 @@ public void testBrowserOverwrittingInheritance() throws Throwable
{
String[] expected = new String[]
{
"test :: Browser Chrome_1024x768"
"test :: Browser Chrome_1024x768",
"testParent :: Browser Chrome_1024x768"
};
checkDescription(BrowserOverwrittingChild.class, expected);
Result result = JUnitCore.runClasses(BrowserOverwrittingChild.class);
checkPass(result, 1, 0);
checkPass(result, 2, 0);
}

private void checkChrome(BrowserConfiguration config)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.xceptance.neodymium.junit5.testclasses.browser.inheritance;

import org.junit.Assert;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;

import com.xceptance.neodymium.junit5.NeodymiumTest;
import com.xceptance.neodymium.junit5.tests.NeodymiumWebDriverTest;
import com.xceptance.neodymium.util.Neodymium;

public class BrowserChildTest extends BrowserParent
{
@BeforeEach
public void before()
{
Assert.assertNotNull("No browser started for @BeforeEach method", Neodymium.getDriver());
NeodymiumWebDriverTest.assertWebDriverAlive(Neodymium.getDriver());
}

@NeodymiumTest
public void test()
{
Assert.assertNotNull("No browser started for @NeodymiumTest method", Neodymium.getDriver());
NeodymiumWebDriverTest.assertWebDriverAlive(Neodymium.getDriver());
}

@AfterEach
public void after()
{
Assert.assertNotNull("No browser started for @AfterEach method", Neodymium.getDriver());
NeodymiumWebDriverTest.assertWebDriverAlive(Neodymium.getDriver());
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,20 @@
package com.xceptance.neodymium.junit5.testclasses.browser.inheritance;

import org.junit.Assert;

import com.xceptance.neodymium.common.browser.Browser;
import com.xceptance.neodymium.junit5.NeodymiumTest;
import com.xceptance.neodymium.junit5.tests.NeodymiumWebDriverTest;
import com.xceptance.neodymium.util.Neodymium;

@Browser("Chrome_1024x768")
@Browser("Chrome_1500x1000")
public abstract class BrowserParent
{

@NeodymiumTest
public void testParent()
{
Assert.assertNotNull(Neodymium.getDriver());
NeodymiumWebDriverTest.assertWebDriverAlive(Neodymium.getDriver());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -458,11 +458,12 @@ public void testBrowserOverwrittingInheritance() throws Throwable
{
String[] expected = new String[]
{
"test :: Browser Chrome_1024x768"
"test :: Browser Chrome_1024x768",
"testParent :: Browser Chrome_1024x768"
};
checkDescription(BrowserOverwrittingChild.class, expected);
NeodymiumTestExecutionSummary summary = run(BrowserOverwrittingChild.class);
checkPass(summary, 1, 0);
checkPass(summary, 2, 0);
}

private void checkChrome(BrowserConfiguration config)
Expand Down

0 comments on commit 1942624

Please sign in to comment.