1111import org .junit .jupiter .api .AfterEach ;
1212import org .junit .jupiter .api .BeforeAll ;
1313import org .junit .jupiter .api .BeforeEach ;
14+ import org .junit .jupiter .api .Nested ;
1415import org .junit .jupiter .api .extension .AfterAllCallback ;
1516import org .junit .jupiter .api .extension .AfterEachCallback ;
1617import org .junit .jupiter .api .extension .BeforeAllCallback ;
2526import info .novatec .testit .webtester .browser .Browser ;
2627import info .novatec .testit .webtester .browser .BrowserFactory ;
2728import info .novatec .testit .webtester .browser .proxy .ProxyConfiguration ;
28- import info .novatec .testit .webtester .junit5 .extensions .NoTestClassException ;
2929import info .novatec .testit .webtester .junit5 .extensions .BaseExtension ;
3030
3131
@@ -84,7 +84,9 @@ public class ManagedBrowserExtension extends BaseExtension
8484
8585 @ Override
8686 public void beforeAll (ContainerExtensionContext context ) throws Exception {
87- executeHandlingUndeclaredThrowables (context , this ::initializeAndInjectStaticBrowsers );
87+ if (isRootContext (context )) {
88+ executeHandlingUndeclaredThrowables (context , this ::initializeAndInjectStaticBrowsers );
89+ }
8890 }
8991
9092 @ Override
@@ -99,7 +101,13 @@ public void afterEach(TestExtensionContext context) {
99101
100102 @ Override
101103 public void afterAll (ContainerExtensionContext context ) {
102- getManagedStaticBrowsers (context ).forEach (this ::closeAndLogErrors );
104+ if (isRootContext (context )) {
105+ getManagedStaticBrowsers (context ).forEach (this ::closeAndLogErrors );
106+ }
107+ }
108+
109+ private boolean isRootContext (ContainerExtensionContext context ) {
110+ return !context .getParent ().isPresent ();
103111 }
104112
105113 private void closeAndLogErrors (Browser browser ) {
@@ -112,10 +120,9 @@ private void closeAndLogErrors(Browser browser) {
112120 }
113121
114122 private void initializeAndInjectStaticBrowsers (ContainerExtensionContext context ) {
115- Class <?> testClass = context .getTestClass ().orElseThrow (NoTestClassException ::new );
116123 List <Browser > managedBrowsers = getManagedStaticBrowsers (context );
117124 getModel (context ).getBrowserFields ().stream ().filter (isStaticField ).forEach (field -> {
118- Browser browser = createBrowserFor (field , testClass );
125+ Browser browser = createBrowserFor (field );
119126 managedBrowsers .add (browser );
120127 setValue (field , null , browser );
121128 });
@@ -125,36 +132,57 @@ private void initializeAndInjectInstanceBrowsers(TestExtensionContext context) {
125132 Object testInstance = context .getTestInstance ();
126133 List <Browser > managedBrowsers = getManagedInstanceBrowsers (context );
127134 getModel (context ).getBrowserFields ().stream ().filter (isInstanceField ).forEach (field -> {
128- Browser browser = createBrowserFor (field , testInstance . getClass () );
135+ Browser browser = createBrowserFor (field );
129136 managedBrowsers .add (browser );
130137 setValue (field , testInstance , browser );
131138 });
132139 }
133140
134- private Browser createBrowserFor (Field field , Class <?> testClass ) {
135-
136- Class <? extends ProxyConfiguration > proxyConfigurationClass ;
137- Class <? extends BrowserFactory > factoryClass ;
138-
139- if (field .isAnnotationPresent (CreateUsing .class )) {
140- CreateUsing annotation = field .getAnnotation (CreateUsing .class );
141- proxyConfigurationClass = annotation .proxy ();
142- factoryClass = annotation .value ();
143- } else if (testClass .isAnnotationPresent (CreateBrowsersUsing .class )) {
144- CreateBrowsersUsing annotation = testClass .getAnnotation (CreateBrowsersUsing .class );
145- proxyConfigurationClass = annotation .proxy ();
146- factoryClass = annotation .value ();
147- } else {
148- throw new NoBrowserFactoryException ();
141+ private Browser createBrowserFor (Field field ) {
142+
143+ CreateUsing createUsing = getFieldLevelAnnotation (field );
144+ if (createUsing != null ) {
145+ Class <? extends ProxyConfiguration > proxyConfigurationClass = createUsing .proxy ();
146+ Class <? extends BrowserFactory > factoryClass = createUsing .value ();
147+ return createBrowser (factoryClass , proxyConfigurationClass );
148+ }
149+
150+ CreateBrowsersUsing createBrowsersUsing = getClassLevelAnnotation (field );
151+ if (createBrowsersUsing != null ) {
152+ Class <? extends ProxyConfiguration > proxyConfigurationClass = createBrowsersUsing .proxy ();
153+ Class <? extends BrowserFactory > factoryClass = createBrowsersUsing .value ();
154+ return createBrowser (factoryClass , proxyConfigurationClass );
155+ }
156+
157+ throw new NoBrowserFactoryException ();
158+
159+ }
160+
161+ private CreateUsing getFieldLevelAnnotation (Field field ) {
162+ return field .getAnnotation (CreateUsing .class );
163+ }
164+
165+ private CreateBrowsersUsing getClassLevelAnnotation (Field field ) {
166+ Class <?> testClass = field .getDeclaringClass ();
167+ CreateBrowsersUsing isPresent = testClass .getAnnotation (CreateBrowsersUsing .class );
168+ if (isPresent == null && testClass .isAnnotationPresent (Nested .class )) {
169+ Class <?> declaringClass = testClass .getDeclaringClass ();
170+ while (declaringClass != null && isPresent == null ) {
171+ isPresent = declaringClass .getAnnotation (CreateBrowsersUsing .class );
172+ declaringClass = declaringClass .getDeclaringClass ();
173+ }
149174 }
175+ return isPresent ;
176+ }
150177
178+ private Browser createBrowser (Class <? extends BrowserFactory > factoryClass ,
179+ Class <? extends ProxyConfiguration > proxyConfigurationClass ) {
151180 try {
152181 ProxyConfiguration proxyConfiguration = proxyConfigurationClass .newInstance ();
153182 return factoryClass .newInstance ().withProxyConfiguration (proxyConfiguration ).createBrowser ();
154183 } catch (InstantiationException | IllegalAccessException e ) {
155184 throw new UndeclaredThrowableException (e , "error while creating browser factory" );
156185 }
157-
158186 }
159187
160188 @ SuppressWarnings ("unchecked" )
0 commit comments