使用 TestNG 执行 Cucumber - 有未定义的步骤

Cucumber execution with TestNG - There were undefined steps

我想 运行 我的黄瓜功能与 TestNG 并行执行。我继续 运行 关注 testNG 无法找到匹配的胶水代码的问题。虽然代码是用Steps正确编写和映射的。

测试执行被跳过,错误指出有未定义的步骤需要定义。 然后我将步骤定义 class 移动到我的测试运行器 class 所在的同一个包中,并且执行成功。

我不明白为什么testNG无法识别不同包中的胶水代码。我也尝试处理不同的依赖项,但效果不佳。有没有其他方法可以确保 testNG 在正确的位置查找步骤定义?

下面是 POM:

<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/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>CucumberPractice</groupId>
  <artifactId>CucumberPractice</artifactId>
  <packaging>jar</packaging>
  <version>1.0-SNAPSHOT</version>
  <name>CucumberPractice</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-core</artifactId>
    <version>4.7.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-java -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>4.7.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-junit -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-junit</artifactId>
    <version>4.7.2</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-jvm-deps -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-jvm-deps</artifactId>
    <version>1.0.6</version>
    <scope>provided</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.hamcrest/hamcrest-core -->
<dependency>
    <groupId>org.hamcrest</groupId>
    <artifactId>hamcrest-core</artifactId>
    <version>2.1</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/org.seleniumhq.selenium/selenium-java -->
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>selenium-java</artifactId>
    <version>3.141.59</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/gherkin -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>gherkin</artifactId>
    <version>5.1.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-html -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-html</artifactId>
    <version>0.2.7</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.mockito/mockito-all -->
<dependency>
    <groupId>org.mockito</groupId>
    <artifactId>mockito-all</artifactId>
    <version>1.10.19</version>
    <scope>test</scope>
</dependency>
<!-- https://mvnrepository.com/artifact/io.cucumber/cucumber-testng -->
<dependency>
    <groupId>io.cucumber</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>4.7.2</version>
</dependency>
</dependencies>
</project>

下面是步骤定义 class :

package CucumberPageLogic;

import java.util.Set;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.Select;
import org.openqa.selenium.support.ui.WebDriverWait;

import io.cucumber.java.After;
import io.cucumber.java.Before;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.Then;
import io.cucumber.java.en.When;
import junit.framework.Assert;



public class PageLogic {

    public WebDriver driver;
    public WebDriverWait wait;

    @Before("@Chrome")
    public void driverSetup() {
        System.out.println("In Before");
        System.setProperty("webdriver.chrome.driver", "driverPath");
        driver= new ChromeDriver();
        wait= new WebDriverWait(driver,20);
    }

    @After
    public void tearDown() {
        driver.quit();
    }
    @Given("User opens Salesforce.com")
    public void userOpensSalesforceCom() {
       driver.get("https://www.salesforce.com/in/?ir=1");
       driver.manage().window().maximize();
    }

    @Given("Home Page is sucessfully loaded")
    public void homePageIsSucessfullyLoaded() {
       wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='btn-container hidden-xs']//span[contains(text(),'Try')]")));
    }

    @When("User clicks on Try For Free button")
    public void userClicksOnTryForFreeButton() {
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='btn-container hidden-xs']//span[contains(text(),'Try')]"))).click();
    }

    @When("^User enters SignUp \"(.*)\" \"(.*)\" \"(.*)\" information$")
    public void userEntersSignUpInformation(String firstname, String lastname, String jobtitle) {
       driver.findElement(By.name("UserFirstName")).sendKeys(firstname);
       driver.findElement(By.name("UserLastName")).sendKeys(lastname);
       Select select = new Select(driver.findElement(By.name("UserTitle")));
       select.selectByVisibleText(jobtitle); 
    }


    @Then("New Tab is opened")
    public void newTabIsOpened() {
        Set<String> handles = driver.getWindowHandles();
        if(handles.isEmpty()) {
            Assert.fail();
        }

        System.out.println("these are the handles :"+handles);
        System.out.println("Current Handle :"+driver.getWindowHandle());
        for(String handle:handles) {
            if(!handle.equals(driver.getWindowHandle())) {
                    System.out.println("Switching to "+handle);
                    driver.switchTo().window(handle);
                    break;
                }
        }

    }

    @Then("User goes back to home page")
    public void userGoesBackToHomeTab() {
        Set<String> handles = driver.getWindowHandles();
        if(handles.isEmpty()) {
            Assert.fail();
        }
        System.out.println("these are the handles :"+handles);
        System.out.println("Current Handle :"+driver.getWindowHandle());
        for(String handle:handles) {
            if(!handle.equals(driver.getWindowHandle())) {
                System.out.println("Switching to "+handle);
                    driver.switchTo().window(handle);
                    break;
                }
        }
    }

    @Then("Sign up form is visible")
    public void signUpFormIsVisible() {
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.name("UserFirstName")));
    }

    @Then("validate home page content")
    public void validateHomePageContent() {
        wait.until(ExpectedConditions.elementToBeClickable(By.xpath("//div[@class='btn-container hidden-xs']//span[contains(text(),'Try')]")));
    }

}

下面是测试运行器 Class :

package CucumberPractice;

import org.testng.annotations.DataProvider;
import io.cucumber.junit.CucumberOptions;
import io.cucumber.junit.CucumberOptions.SnippetType;
import io.cucumber.testng.AbstractTestNGCucumberTests;

//@RunWith(Cucumber.class)


@CucumberOptions(features= {"C:\Users\komehta\eclipse-workspace\CucumberPractice\src\test\java\CucumberPractice"},
        dryRun=false,
        glue= {"CucumberPageLogic"},
        snippets= SnippetType.CAMELCASE,
        tags= {"@NewTabFeature"},
        strict=false,
         plugin= {
                    "pretty","html:test-outout", 
                    "json:json_output/cucumber.json", 
                    "junit:junit_xml/cucumber.xml"
                })


public class CucumberTestRunner extends AbstractTestNGCucumberTests  {
    @Override
    @DataProvider(parallel = true)
    public Object[][] scenarios() {
        return super.scenarios();
    }
}

专题文件:

@NewTabFeature @Chrome
Feature: Opening the WebPage in New Tab


  @NewTabSnr1
  Scenario: Open the link in a New WebPage and Validate the Content
    Given User opens Salesforce.com
    And Home Page is sucessfully loaded
    When User clicks on Try For Free button
    Then New Tab is opened
    And Sign up form is visible

  @NewTabSnr2
  Scenario Outline: Validate the Content on HomePage after entering details on new tab
    Given User opens Salesforce.com
    And Home Page is sucessfully loaded
    When User clicks on Try For Free button
    And New Tab is opened
    And User enters SignUp "<firstname>" "<lastname>" "<jobtitle>" information
    Then User goes back to home page
    And validate home page content

    Examples: 
      | firstname | lastname | jobtitle  |
      | Kovid |Mehta | Sales Manager |
      | Vikas |Bhat | IT Manager |

如果您删除不使用的依赖项或传递性引入并让 Maven 为您进行依赖项管理,您将看到您必须替换:

import io.cucumber.junit.CucumberOptions;
import io.cucumber.junit.CucumberOptions.SnippetType;

与:

import io.cucumber.testng.CucumberOptions;
import io.cucumber.testng.CucumberOptions.SnippetType;

目前您可以删除:

junit
cucumber-junit
cucumber-jvm-dep
gherkin
cucumber-html