How to match a specific java file with specific fe

2019-08-17 16:47发布

问题:

I've created three java and feature files within cucumber and there's following code part for each java file.

@Before
public void startbrowser() {
 System.setProperty("webdriver.gecko.driver","/Users/firatkaymaz/eclipse-workspace/SeleniumTest/drivers/geckodriver/geckodriver");
 driver = new FirefoxDriver();

so once i try to run one of my cucumber files (run as ->cucumber feature), three browser is opening because all @Before annotations located in three java file are being executed. how can i get rid of that ? I want to match the feature file named TestCase1.feature with Case1.java so when i execute concerned feature file, Case1 is gonna be executed so just one browser can be opened as expected. any idea ?

files

回答1:

In Cucumber you can use picocontainer in your pom file, then you can separate common method "startbrowser" in one class. Refer https://www.toolsqa.com/selenium-cucumber-framework/sharing-test-context-between-cucumber-step-definitions/



回答2:

An alternative approach is to use the @ScenarioScoped annotation for a class. You need to add cucumber-guice to your pom file or whatever. Then you can add this annotation to the class your driver is created by and there will be single instance of it for the whole scenario. http://www.thinkcode.se/blog/2017/08/16/sharing-state-between-steps-in-cucumberjvm-using-guice