本文介绍了在Cucumber 6中初始化Webdriver的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Cucumber中一次初始化webdrivermanager java和DAO/Spring/?

How to initialize webdrivermanager java and DAO/Spring/ once in Cucumber?

@BeforeSuite或@AfterSuite有类似的机制吗?

Any @BeforeSuite or @AfterSuite similar mechanism?

import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;

@RunWith(Cucumber.class)
@CucumberOptions(
        features = "src/main/resources/Features",
        glue = "com.cimb.StepsDef",
        plugin = {"com.aventstack.extentreports.cucumber.adapter.ExtentCucumberAdapter:",
                "json:target/cucumber-report.json"})
public class ParallelRunner {
}

黄瓜支持JUnits @ ClassRule,@ BeforeClass和@AfterClass批注.这些将在所有方案之前和之后执行.不建议使用它们,因为它会限制不同流道之间的可移植性.使用命令行,IntelliJ IDEA或Cucumber-Eclipse时,它们可能无法正确执行.相反,建议使用黄瓜之前和之后钩子.

Cucumber supports JUnits @ClassRule, @BeforeClass and @AfterClass annotations. These will executed before and after all scenarios. Using these is not recommended, as it limits the portability between different runners; they may not execute correctly when using the commandline, IntelliJ IDEA or Cucumber-Eclipse. Instead it is recommended to use Cucumbers Before and After hooks.

请提供一些示例代码.十亿感谢您的帮助.

Please provide some sample code. A billion thanks for your help.

推荐答案

您可以使用 cucumber-guice 初始化Webdriver.这还将使您的测试方案独立,因为为每个方案都创建了一个新的驱动程序实例.这是我使用它的方法

You can utilize cucumber-guice to initialize webdriver. This will also make your tests scenarios independent as a new driver instance is created for every scenario. Here is what I do to use it

要使用以下代码,您将需要 cucumber-guice 依赖项

To use below code you will need cucumber-guice dependency

 <dependency>
      <groupId>io.cucumber</groupId>
      <artifactId>cucumber-guice</artifactId>
      <version>${cucumber.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.google.inject</groupId>
      <artifactId>guice</artifactId>
      <version>4.2.3</version>
      <scope>test</scope>
    </dependency>

在chromemanager类中

In chromemanager class

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeDriverService;

public class ChromeManager extends DriverManager {

     protected ChromeDriver driver;

         @Override
         protected WebDriver createDriver() {

                    System.setProperty("webdriver.chrome.driver", "/Users/../chromedriver");
                    return driver = new ChromeDriver(ChromeDriverService.createDefaultService());
        }

        @Override
         public WebDriver getDriver() {
                 if (driver == null) {
                             this.createDriver();
                }
                return driver;
        }

}

Driverfactory类

Driverfactory class

import org.openqa.selenium.WebDriver;

public class DriverFactory {
        public WebDriver getManager() {

            return new ChromeManager().getDriver();
        }
}

Drivermanager类

Drivermanager class

import org.openqa.selenium.WebDriver;

     public abstract class DriverManager {

                     protected abstract WebDriver createDriver();
                     public abstract WebDriver getDriver();
     }

然后在全局类中

import org.openqa.selenium.WebDriver;
import io.cucumber.guice.ScenarioScoped;

 @ScenarioScoped

          public class Global {
          public WebDriver driver;
           public WebDriverWait wait;

          public Global() {
                driver = new DriverFactory().getManager();
                wait = new WebDriverWait(driver, 3000);

          }

    }

最后是您的测试班

import com.google.inject.Inject;
import com.test.support.Global;


public class InitialIT  {

         public static  ChromeDriver driver ;

        @Inject
        Global global;

    @Test
       @Given("I open the site {string}")
        public void OpenSite(String site) throws MalformedURLException {
                global.driver.get(site);
       }

         @After
        public void closeBrowser() {
             global.driver.quit();
      }

}

如果您正在使用junit jupiter.您还可以使用 硒木星 来管理webdriver

If you are using junit jupiter. You can also use selenium-jupiter to manage webdriver

这篇关于在Cucumber 6中初始化Webdriver的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-03 04:30