0

I ran into a situation whereas I defined a customize annotation class (SeleniumTest.java), and use that annotation in another class(AppSeleniumTest.java). However, I need pass in a constructor parameter to one of the @Autowired field (AppSeleniumTest.driver). Anyone knows the best way to do that?

Code snippet:

SeleniumTest.java

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface SeleniumTest {
  Class<? extends WebDriver> driver() default PhantomJSDriver.class;
}

AppSeleniumTest.java

@RunWith(SpringRunner.class)
@SeleniumTest
public class AppSeleniumTest {

  @Autowired
  private WebDriver driver; // Basically here, we want driver=new PhantomJSDriver(NEED_GIVE_PARAMETER) instead of just driver=new PhatomJSDriver(); by default.

} 

one clue I might have is instead of @SeleniumTest in AppSeleniumTest.java, we might give @SeleniumTest(driver = PhantomJSDriver.class). But how can we pass in parameters to this PhantomJSDriver's constructor?

Any hints much appreciated. Thanks

MG.
  • 449
  • 6
  • 15

1 Answers1

0

Can't you use driverParam in your annotation like following.

@Documented
@Inherited
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface SeleniumTest {
    Class<? extends WebDriver> driver() default PhantomJSDriver.class;

    Object driverParam();
}
shazin
  • 21,379
  • 3
  • 54
  • 71
  • Sorry, maybe I didn't quite follow your answer. How can this solve my situation? Basically, where is the place to plugin your driverParam() to the instatiation of driver() in spring that makes the same effect as `new PhatomJSDriver(driverParam())`? – MG. Dec 07 '16 at 15:00