I am currently working on migrating my existing cucumber framework from cucumber info:cukes to io.cucumber with selenium 4. But getting below error for my Generic Hook class, can someone please help me understand and resolve the issue:
- [Guice/ErrorInCustomProvider]: OutOfScopeException: Cannot access Key[type=ContextObject, annotation=[none]] outside of a scoping block
at DependencyModule.configure(DependencyModule.java:26)
at GenericHooks.(GenericHooks.java:57)
_ for 1st parameter
at GenericHooks.class(GenericHooks.java:44)
while locating GenericHooks
Code for DependencyModule:
import com.google.inject.Module;
import com.google.inject.Binder;
import com.google.inject.name.Names;
import proj.beacon.data.ContextObject;
import io.cucumber.guice.CucumberScopes;
import org.openqa.selenium.WebDriver;
import static io.cucumber.guice.CucumberScopes.createScenarioScope;
public class DependencyModule implements Module {
public void configure(Binder binder) {
binder.bind(ContextObject.class).in(createScenarioScope());
binder.bind(WebDriver.class)
.toProvider(DriverProvider.class).in(createScenarioScope());
binder.bind(String.class).annotatedWith(Names.named("Default Excel File
Path")).toInstance("src/test/resources/");
}
}
Code for GenericHooks:
@ScenarioScoped
public class GenericHooks {
private static AppiumDriverLocalService appiumService;
private ContextObject contextObject;
private WebDriver driver;
@Inject
public GenericHooks(ContextObject contextObject) {
this.contextObject = contextObject;
}
@Before(order = 1)
public void addScenario(Scenario scenario) {
contextObject.set(Scenario.class.toString(), scenario);
logAutoRuns=new LogAutoRuns();
setLogger(scenario);
if (scenario.getSourceTagNames().contains(Constants.SCENARIO_DEVICE_TAG)) {
launchAppium();
}
}
private void launchAppium() {
String APPIUMPORT = contextObject.getConfig().getProperty("APPIUMPORT");
String APPIUMSERVER = contextObject.getConfig().getProperty("APPIUMSERVER");
platform= contextObject.getConfig().getProperty("PLATFORM_NAME");
//Make sure appium is installed using 'npm install -g appium' from terminal
//appiumService = AppiumDriverLocalService.buildDefaultService();
AppiumServiceBuilder appiumServiceBuilder=new AppiumServiceBuilder();
appiumServiceBuilder.withIPAddress(APPIUMSERVER);
appiumServiceBuilder.withTimeout(Duration.ofSeconds(40));
appiumServiceBuilder.usingPort(Integer.parseInt(APPIUMPORT));
appiumServiceBuilder.withArgument(BASEPATH,"/wd/hub/");
appiumService=AppiumDriverLocalService.buildService(appiumServiceBuilder);
if (appiumService != null) {
appiumService.start();
}
int timeout = 10;
String url = String.format("http://%s:%s/wd/hub/status", APPIUMSERVER, APPIUMPORT);
if (!Utility.ping(url, timeout, TimeUnit.SECONDS)) {
throw new RuntimeException("Appium server node is not started!");
}
}
}