当前位置: 首页 > 知识库问答 >
问题:

TestNG@Test方法在功能级运行,如何在场景级运行

徐友樵
2023-03-14

我想在功能文件的场景级别(就在每个场景之后)执行@AfterMethod。但是@AfterMethod在功能文件的功能级别(在所有场景之后)执行。我怎样才能做到这一点。下面是我的代码片段。

>

  • 跑步课。

    @SuppressWarnings(“unused”)@CucumberOptions(features=“features”,glue={“com.dell.clouddam.stepdefinitions”},dryRun=false,plugin={“com.cucumber.listener.extentcumberFormatter:target/cumber reports/report.html”,“pretty”,“json:target/cumber reports/cumber.json”},monology=true)公共类MyTestRunner{

     private TestNGCucumberRunner testNGCucumberRunner;
    
     private DriverFactory driverFactory;    
     private WebDriver driver;   //these are made private because these are specific to this class only.
     private ConfigReader configReader;
     Properties properties;
    
    
     @BeforeSuite(alwaysRun = true)
     public void getProperty() {
         configReader = new ConfigReader();
         properties = configReader.initProp();
         System.out.println("@BeforeClass getpropeety ");
    
     }
    
     @BeforeClass(alwaysRun = true)
     public void setUpClass() throws Exception {
         testNGCucumberRunner = new TestNGCucumberRunner(this.getClass());
     }
    
     @BeforeMethod(alwaysRun = true)
     public void launchBrowser() throws FileNotFoundException, IOException, Exception {
         String browserName = properties.getProperty("environment");
         driver = driverFactory.initDriver(browserName); 
         driver.get(properties.getProperty("author_url"));
         ((RemoteWebDriver) driver).getSessionId();
    
         driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
     }
    
    
    
     @DataProvider
     public Object[][] features() {
         return testNGCucumberRunner.provideFeatures();
     }
    
     @Test(groups = "cucumber", description = "Runs Cucumber Feature", dataProvider = "features")
     public void feature(CucumberFeatureWrapper cucumberFeature) {
         testNGCucumberRunner.runCucumber(cucumberFeature.getCucumberFeature());
     }
    
     @AfterMethod(description = "Executes After Each Iteration")
     public void afterMethod(ITestResult res) throws Exception {
         driver.quit();
     }
    
     @AfterTest(alwaysRun = true)
     public void afterTest() throws Throwable {
    
     }
    
     @AfterClass(alwaysRun = true)
     public void tearDownClass() throws Throwable {
         testNGCucumberRunner.finish();
     }
    

    }

    司机工厂。JAVA

    公共类驱动工厂{

    公共静态WebDriver驱动程序;HashMap

    >>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>

    公共WebDriver initDriver(String环境)抛出FileNotFoundExcture、IOExcure、Exc0019{System.out.println("Browser value is-"环境);

     if (environment.equals("chrome")) {
         WebDriverManager.chromedriver().setup();
             driver = new ChromeDriver();
     }
    
     else if (environment.equals("firefox")) {
         WebDriverManager.firefoxdriver().setup();
         driver = new FirefoxDriver();
     }
    
     else if (environment.equals("safari")) {
         driver = new SafariDriver();
     } 
     else {
         System.out.println("Please select the correct browser value");
     }
     getDriver().manage().deleteAllCookies();
     getDriver().manage().window().maximize();
     return getDriver();
    

    }

    公共静态同步WebDriver getDriver(){return driver;}}

    波姆。xml

    <project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
     <modelVersion>4.0.0</modelVersion>
     <groupId>com.sample</groupId>
     <artifactId>SampleDAM</artifactId>
     <version>1.0</version>
     <dependencies>
         <dependency>
             <groupId>io.github.bonigarcia</groupId>
             <artifactId>webdrivermanager</artifactId>
             <version>4.2.2</version>
         </dependency>
         <!-- https://mvnrepository.com/artifact/io.rest-assured/rest-assured -->
         <dependency>
             <groupId>io.rest-assured</groupId>
             <artifactId>rest-assured</artifactId>
             <version>4.2.0</version>
         </dependency>
         <dependency>
             <groupId>log4j</groupId>
             <artifactId>log4j</artifactId>
             <version>1.2.17</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi</artifactId>
             <version>3.15</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-ooxml</artifactId>
             <version>3.15</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-scratchpad</artifactId>
             <version>3.15</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-ooxml-schemas</artifactId>
             <version>3.15</version>
         </dependency>
         <dependency>
             <groupId>org.apache.poi</groupId>
             <artifactId>poi-excelant</artifactId>
             <version>3.15</version>
         </dependency>
         <dependency>
             <groupId>org.apache.xmlbeans</groupId>
             <artifactId>xmlbeans</artifactId>
             <version>2.6.0</version>
         </dependency>
         <dependency>
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-collections4</artifactId>
             <version>4.1</version>
         </dependency>
         <dependency>
             <groupId>org.apache.commons</groupId>
             <artifactId>commons-lang3</artifactId>
             <version>3.12.0</version>
         </dependency>
         <dependency>
             <groupId>com.paulhammant</groupId>
             <artifactId>ngwebdriver</artifactId>
             <version>1.1.6</version>
         </dependency>
         <dependency>
             <groupId>org.seleniumhq.selenium</groupId>
             <artifactId>selenium-remote-driver</artifactId>
             <version>2.48.2</version>
         </dependency>
         <!-- https://mvnrepository.com/artifact/com.github.jesg/phantomjsdriver -->
         <dependency>
             <groupId>com.github.jesg</groupId>
             <artifactId>phantomjsdriver</artifactId>
             <version>2.0.0</version>
         </dependency>
         <dependency>
             <groupId>com.saucelabs</groupId>
             <artifactId>sauce_bindings</artifactId>
             <version>1.0.0</version>
         </dependency>
         <dependency>
             <groupId>org.testng</groupId>
             <artifactId>testng</artifactId>
             <version>6.14.3</version>
         </dependency>
         <dependency>
             <groupId>com.googlecode.json-simple</groupId>
             <artifactId>json-simple</artifactId>
             <version>1.1</version>
         </dependency>
         <dependency>
             <groupId>org.seleniumhq.selenium</groupId>
             <artifactId>selenium-java</artifactId>
             <version>2.53.0</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-jvm</artifactId>
             <version>1.2.5</version>
             <type>pom</type>
         </dependency>
         <dependency>
             <groupId>com.googlecode.json-simple</groupId>
             <artifactId>json-simple</artifactId>
             <version>1.1.1</version>
         </dependency>
         <dependency>
             <groupId>org.json</groupId>
             <artifactId>json</artifactId>
             <version>20180130</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-junit</artifactId>
             <version>1.2.5</version>
             <scope>test</scope>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-core</artifactId>
             <version>1.2.5</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-html</artifactId>
             <version>0.2.3</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-java</artifactId>
             <version>1.2.5</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-jvm-deps</artifactId>
             <version>1.0.5</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>gherkin</artifactId>
             <version>2.12.2</version>
         </dependency>
         <dependency>
             <groupId>org.hamcrest</groupId>
             <artifactId>hamcrest-all</artifactId>
             <version>1.3</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-picocontainer</artifactId>
             <version>1.2.5</version>
         </dependency>
         <dependency>
             <groupId>info.cukes</groupId>
             <artifactId>cucumber-testng</artifactId>
             <version>1.2.5</version>
         </dependency>
         <dependency>
             <groupId>com.aventstack</groupId>
             <artifactId>extentreports</artifactId>
             <version>3.1.2</version>
         </dependency>
         <dependency>
             <groupId>org.freemarker</groupId>
             <artifactId>freemarker</artifactId>
             <version>2.3.26-incubating</version>
         </dependency>
         <dependency>
             <groupId>com.vimalselvam</groupId>
             <artifactId>cucumber-extentsreport</artifactId>
             <version>3.0.1</version>
         </dependency>
     </dependencies>
    
     <build>
         <plugins>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-compiler-plugin</artifactId>
                 <configuration>
                     <source>1.8</source>
                     <target>1.8</target>
                 </configuration>
             </plugin>
             <plugin>
                 <groupId>org.apache.maven.plugins</groupId>
                 <artifactId>maven-surefire-plugin</artifactId>
                 <version>3.0.0-M5</version>
                 <configuration>
                     <parallel>methods</parallel>
                     <useUnlimitedThreads>true</useUnlimitedThreads>
                     <suiteXmlFiles>
                         <suiteXmlFile>CloudDAM.xml</suiteXmlFile>
                     </suiteXmlFiles>
                 </configuration>
             </plugin>
         </plugins>
     </build>
    
  • 共有1个答案

    钱朝明
    2023-03-14

    对于cucumber,您需要使用cucumber jvm的注释,例如(io.cucumber.java.After)@After、@Before-在每个场景之前和之后运行。没有支持要素结束的注释。还有@AfterAll-它在所有场景的所有功能之后运行。如果您想要更细粒度的控制,则需要实现ConcurrentEventListener。

     类似资料:
    • 我正在使用Maven SureFire,TestNG(扩展AbstractTestNGCucumber测试)和Cucumber,并且有几个功能文件,每个文件都有几个场景。我希望能够在一个功能文件中并行运行每个场景,但一次只能运行一个功能文件。这有可能吗? 举个例子: 我希望场景1a、1b和1c在功能文件1中并行运行。一旦这些完成,运行场景2a和功能2等的2b。 这是当前一次从所有功能文件运行所有方

    • jhbhhjhj当我点击登录并输入有效的“kumar.rakesh@yopmail.com”和有效的“admin@123”,然后点击登录,用户应该可以成功登录 测试转轮

    • 是否可以通过右键单击功能文件中的各个场景而不是使用命令文件或直接运行 TestNG 运行器文件来使用 TestNG 运行器运行cucumber场景? 我使用Intellij在maven测试框架中运行cucumber场景。在POM.xml文件中,我有一个引用testNG.xml文件的Surefire插件,该文件指向TestNG runner类。 当我从终端运行“mvn test”时,它会调用 Tes

    • 如何运行一个测试用例的一个方法来调试Gradle?我试过: 但最终会出现以下错误: test类有、等。

    • 我有一套为Cucumber-JVM编写的验收测试。为了减少反馈时间,我想并行运行(功能)的场景。如何以最简单、最方便的方式做到这一点? (我更希望能够在Java代码中表达这一点,作为一个常规的JUnit测试/运行程序,即我不希望使用maven-sureFire或maven-故障安全插件来解决一些问题,这将需要(?)之后对Cucumber报告进行旋转和合并。)

    • 这是驱动程序类,它将为每个测试方法创建驱动程序实例。