简体   繁体   中英

How to match a specific java file with specific feature file in cucumber

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

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/

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM