如何正确使用TestNG注解与Cucumber框架。

huangapple go评论69阅读模式
英文:

How to use correctly TestNG annotations with Cucumber framework

问题

我正在构建TestNG和Cucumber框架,并希望使用TestNG注释,如@BeforeMethod@AfterMethod,它们将在每个场景之前和之后运行setUptearDown方法。如果我将所有的Cucumber场景都放在单独的特性文件中,那么它可以正常工作。但如果我在同一个特性文件中有多个场景,那么@AfterMethod在每个场景后面不会运行。

另外,如果使用Cucumber的Before和After,它们只会在我将它们放在StepDefinition文件中时正常工作:

public class StepDefinition extends Base {
    
    @Before
    public void setUp() throws Exception {
        openBrowser();
        maximizeWindow();
        deleteAllCookies();
    }
    
    @After
    public void quit() throws IOException, InterruptedException {
        driver.quit();
    }

    // 其他步骤定义方法
}

但如果我将它们放在单独的类中(如下所示),它们就不起作用了(文档中说它们可以放在项目的任何地方):

package com.act.hooks;

import cucumber.api.java.After;
import cucumber.api.java.Before;
import java.io.IOException;
import com.moodys.act.base.Base;

public class Hooks extends Base {
    
    @Before
    public void setUp() throws Exception {
        openBrowser();
        maximizeWindow();
        deleteAllCookies();
    }
    
    @After
    public void quit() throws IOException, InterruptedException {
        driver.quit();
    }
}
英文:

I am building TestNG and Cucumber framework, and want to use TestNG annotations like @BeforeMethod, @AfterMethod which will run setUp and tearDown methods before and after each scenario. It works fine if I keep all my Cucumber scenarios in separate feature files. But if I have multiple scenarios in the same feature file @AfterMethod is not running after each scenario.

Also have issues if using cucumber Before and After, they work fine only if I put them in StepDefinition file below:

public class StepDefinition extends Base {
	
	@Before
	public void setUp() throws Exception {
		openBrowser();
		maximizeWindow();
//		implicitWait(30);
		deleteAllCookies();
//		setEnv();
	}
	
	@After
	public void quit() throws IOException, InterruptedException {
		driver.quit();
	}

	@Given("^Navigate to ACT landing page$")
	public void navigate_to_ACT_landing_page() throws Throwable {
		LandingPage landingPage = new LandingPage();
		landingPage.navigateToLandingPage();
	}

	@Then("^Verify landing page is rendered$")
	public void verify_landing_page_is_rendered() throws Throwable {
		LandingPage landingPage = new LandingPage();
		landingPage.landingPageRendered();
	}

}

But if I put them in separate class (like below) they don't work (in documentation says that they can be put anywhere in the project)

package com.act.hooks;


import cucumber.api.java.After;
import cucumber.api.java.Before;

import java.io.IOException;

import com.moodys.act.base.Base;

public class Hooks extends Base {
	
	@Before
	public void setUp() throws Exception {
		openBrowser();
		maximizeWindow();
//		implicitWait(30);
		deleteAllCookies();
//		setEnv();
	}
	
	@After
	public void quit() throws IOException, InterruptedException {
		driver.quit();
	}

}

答案1

得分: 1

有提供的黄瓜标签/钩子@Before和@After,在每个场景之前和之后运行,您可以在其中包括设置和拆卸代码。您是否有特定的原因要使用testng注释来执行此操作?

这个链接可能对黄瓜@Before和@After标签有帮助。

https://www.google.com/amp/www.automationtestinghub.com/cucumber-hooks-before-after/amp/

英文:

There are cucumber provided tags/hooks @Before and @After which runs just before and after each scenario where you can have your setup and teardown code included. Is there any specific reason you want to use testng annotations for doing so ?

This link may help for cucumber @Before and @ After tags.

https://www.google.com/amp/www.automationtestinghub.com/cucumber-hooks-before-after/amp/

答案2

得分: 0

黄瓜不建议使用TestNG注解来准备和清理状态。为了充分利用TestNG,您可以尝试使用纯TestNG实现来进行BDD测试,它将每个场景视为TestNG方法。

如果您想要使用黄瓜,可以添加qaf-cucumber依赖并使用TestNG工厂。这将允许您使用TestNG监听器和生命周期方法(@BeforeMethod,@AfterMethod)。在使用黄瓜与qaf时,您可以选择使用TestNG生命周期或黄瓜生命周期。

此外,如果您正在使用webdriver或appium,它具有内置的线程安全驱动程序管理,因此您不需要担心驱动程序管理。

英文:

Cucumber does not recommend to prepare and clean states using TestNG annotations.
In order to take maximum benefits of TestNG, you can try using pure TestNG implementation for BDD with QAF. It considers each scenario as TestNGMethod.

If you want to use cucumber then you can add qaf-cucumber dependency and use TestNG factory. It will allow to use TestNG listeners and lifecycle methods (@BeforeMethod, @AfterMethod). When using cucumber with qaf you can choose either to utilize TestNG life-cycle or cucumber life-cycle.

Furthermore, if you are working with webdriver or appium it has inbuilt thread-safe driver management so you don't need to worry about driver management.

huangapple
  • 本文由 发表于 2020年7月30日 01:32:25
  • 转载请务必保留本文链接:https://go.coder-hub.com/63159289.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定