0

Let's say I have some guice module/provider which supposed to create bindings based on parameters received from TestNg suite file. e.g.

<test name="Test">
    <parameter name="profile" value="chrome"></parameter>
    <classes>
        <class name="com.apc.ui.tests.TestClass">
        </class>
    </classes>
</test>

What I wanted to achieve is a possibility to access parameter value from withing above mentioned module. e.g.

public class MyModule extends AbstractModule {
    @Inject
    ITestContext context;

    @Override
    protected void configure() {
        ...
    }
}

So, I'm wondering whether it's possible. Any alternatives are also really welcomed. Thanks.

user1058106
  • 530
  • 3
  • 12

1 Answers1

1

Eventually, did manage to find a solution in testNg sources. There is a possibility to set a so called parent module in a suite file.

<suite name="Suite1" verbose="1" parallel="false"
    parent-module="org.my.tests.ParentModule">
...

the module can receive ITestContext as a constructor parameter which means it can be then injected to other classes:

public class ParentModule extends AbstractModule {

  private ITestContext context;

  public GuiceParentModule(ITestContext context) {
    this.context = context;
  }

  @Override
  protected void configure() {
    bind(ITestContext.class).toInstance(context);
  }
...
user1058106
  • 530
  • 3
  • 12
  • But wont it inject the same context for other all other tests? – KitKarson Apr 14 '19 at 15:51
  • it will, but the context has access to all tests in a suite. So, it actually possible to access currently running test entry in xml suite. – user1058106 Apr 19 '19 at 11:21
  • I tried this - it works great by injecting the appropriate test context - https://stackoverflow.com/questions/55677112/testng-accessing-itestcontext-in-guice-module – KitKarson Apr 19 '19 at 13:59
  • @KitKarson Can you out one basic example here? So suppose, I want to pass few parameter values via context, we I achieve with this approach? – Ashish Nov 27 '19 at 04:33