可以使用Cucumber Runner执行TestNG跨浏览器测试吗?

Can a TestNG cross-browser test be executed with Cucumber runner?(可以使用Cucumber Runner执行TestNG跨浏览器测试吗?)

本文介绍了可以使用Cucumber Runner执行TestNG跨浏览器测试吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在与Cucumber一起使用Selenium Webdriver。我的测试就像预期的那样,有了这个组合。为了实现跨浏览器测试,我添加了TestNG框架。为了验证我的跨浏览器测试是否工作良好,我只使用TestNG运行它,没有使用Cucumber。它在Chrome和Firefox浏览器上都能完美运行。

public class WebTest {


  WebDriver driver = null;
  BasePageWeb basePage;
  public String browser;


  @Parameters({ "Browser" })
  public WebTest(String browser) {
    this.browser = browser;
  }

  
  @BeforeClass
  public void navigateToUrl() {
    switch (browser) {

      case "CHROME":
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        break;

      case "FF":
        WebDriverManager.firefoxdriver().setup();
        driver = new FirefoxDriver();
        break;

      default:
        driver = null;
        break;
    }
    driver.get("https://demosite.executeautomation.com/Login.html");

  }

  @Test
  public void loginToWebApp() {

    basePage = new BasePageWeb(driver);
    basePage.enterUsername("admin")
            .enterPassword("admin")
            .clickLoginButton();

    driver.quit();
  }

}

testng.xml文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite" parallel="tests" thread-count="5">

    <test name="Chrome Test">
        <parameter name="Browser" value="CHROME"/>
        <classes>
            <class name="tests.web.WebTest"/>
        </classes>
    </test>
    <test name="Firefox Test">
        <parameter name="Browser" value="FF"/>
        <classes>
            <class name="tests.web.WebTest"/>
        </classes>
    </test>
</suite>

我需要将TestNG测试与我的Cucumber设置集成,以便可以使用Cucumber运行整个测试。为此,我向POM添加了cucumber-testng依赖项,并创建了一个扩展AbstractCucumberTestNG类的Cucumber运行器。我指定了特征文件和STEP定义的位置。步骤定义映射到TestNG测试。

黄瓜切割者:

@CucumberOptions(
                plugin = {"pretty", "html:target/surefire-reports/cucumber",
                        "json:target/surefire-reports/cucumberOriginal.json"},
                glue = {"stepdefinitions"},
                tags = "@web-1",
                features = {"src/test/resources/features/web.feature"})
          
   

 public class RunCucumberNGTest extends AbstractTestNGCucumberTests {
    }

步骤定义:

public class WebAppStepDefinitions {
      private final WebTest webTest = new WebTest("CHROME"); //create an object of the class holding the testng test. If I change the argument to FF, the test will run only on Firefox
      static boolean prevScenarioFailed = false;
    
    
      @Before
      public void setUp() {
        if (prevScenarioFailed) {
          throw new IllegalStateException("Previous scenario failed!");
        }
    
      }
    
      @After()
      public void stopExecutionAfterFailure(Scenario scenario) throws Exception {
        prevScenarioFailed = scenario.isFailed();
      }
    
      @Given("^I have navigated to the web url "([^"]*)"$")
      public void navigateToUrl(String url) {  test
        webTest.navigateToUrl(url); //calling the first method holding the testng
      }
    
    
      @When("^I log into my web account with valid credentials as specicified in (.*) and (.*)$")
      public void logintoWebApp(String username, String password) {
        webTest.loginToWebApp(username, password); //calling the second method holding the testng
      }
    }

在运行该类时,测试仅在一个浏览器(Chrome)中执行。不知何故,火狐在筹备过程中迷失了方向。我怀疑我从另一个类错误地调用了参数化的TestNG方法。如何成功进行呼叫?

推荐答案

若要使用Cucumber运行TestNG测试,您必须在testng.xml中定义测试运行器类。

您的Test Runner类是RunCucumberNGTest

因此XML应该如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">

<suite name="Suite" parallel="tests" thread-count="5">

    <test name="Chrome Test">
        <parameter name="Browser" value="CHROME"/>
        <classes>
            <class name="some.package.name.RunCucumberNGTest"/>
        </classes>
    </test>
    <test name="Firefox Test">
        <parameter name="Browser" value="FF"/>
        <classes>
            <class name="some.package.name.RunCucumberNGTest"/>
        </classes>
    </test>
</suite>

从这个XML我看到以下要求:

  1. 运行同一组测试,但参数值不同。

  2. 这应该并行工作,所以这应该是线程安全的。


1为Test Runner类引入TestNG参数

@CucumberOptions(
                plugin = {"pretty", "html:target/surefire-reports/cucumber",
                        "json:target/surefire-reports/cucumberOriginal.json"},
                glue = {"stepdefinitions"},
                tags = "@web-1",
                features = {"src/test/resources/features/web.feature"})
public class RunCucumberNGTest extends AbstractTestNGCucumberTests {

    // static thread-safe container to keep the browser value
    public final static ThreadLocal<String> BROWSER = new ThreadLocal<>();

    @BeforeTest
    @Parameters({"Browser"})
    public void defineBrowser(String browser) {
        //put browser value to thread-safe container
        RunCucumberNGTest.BROWSER.set(browser);
        System.out.println(browser);
    }

}

2使用步骤定义类中的值

public class WebAppStepDefinitions {
      private WebTest webTest;
      static boolean prevScenarioFailed = false;
    
      @Before
      public void setUp() {
        if (prevScenarioFailed) {
          throw new IllegalStateException("Previous scenario failed!");
        }
        //get the browser value for current thread
        String browser = RunCucumberNGTest.BROWSER.get();
        System.out.println("WebAppStepDefinitions: " + browser);
        //create an object of the class holding the testng test. If I change the argument to FF, the test will run only on Firefox
        webTest = new WebTest(browser);
      }
    
      @After
      public void stopExecutionAfterFailure(Scenario scenario) throws Exception {
        prevScenarioFailed = scenario.isFailed();
      }
    
      @Given("^I have navigated to the web url "([^"]*)"$")
      public void navigateToUrl(String url) {
        webTest.navigateToUrl(url); //calling the first method holding the testng
      }
    
    
      @When("^I log into my web account with valid credentials as specicified in (.*) and (.*)$")
      public void logintoWebApp(String username, String password) {
        webTest.loginToWebApp(username, password); //calling the second method holding the testng
      }

}

注意:所有TestNG批注都应从WebTest类中删除,它们将不起作用且不是必需的。WebTestWebAppStepDefinitions类显式使用,所有方法都显式调用,而不是由TestNG调用。

因此,根据您的初始要求:

public class WebTest {

  WebDriver driver = null;
  BasePageWeb basePage;
  public String browser;

  public WebTest(String browser) {
    this.browser = browser;
  }

  public void navigateToUrl(String url) {
    switch (browser) {

      case "CHROME":
        WebDriverManager.chromedriver().setup();
        driver = new ChromeDriver();
        break;

      case "FF":
        WebDriverManager.firefoxdriver().setup();
        driver = new FirefoxDriver();
        break;

      default:
        driver = null;
        break;
    }
    driver.get(url);

  }

  public void loginToWebApp(String username, String password) {

    basePage = new BasePageWeb(driver);
    basePage.enterUsername(username)
            .enterPassword(password)
            .clickLoginButton();

    driver.quit();
  }

这篇关于可以使用Cucumber Runner执行TestNG跨浏览器测试吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:可以使用Cucumber Runner执行TestNG跨浏览器测试吗?

基础教程推荐