Java 执行TestRunner类时发生初始化错误

Java 执行TestRunner类时发生初始化错误,java,selenium,cucumber,Java,Selenium,Cucumber,当我尝试运行TestRunner.java时,我看到以下错误: 初始化错误(0.000s) java.lang.IllegalArgumentException:baseDir必须存在: C:\com\newtours\qa\features位于 io.cucumber.core.resource.PathScanner.findResourcesForPath(PathScanner.java:42) 在 io.cumber.core.resource.PathScanner.findReso

当我尝试运行TestRunner.java时,我看到以下错误:

初始化错误(0.000s)

java.lang.IllegalArgumentException:baseDir必须存在: C:\com\newtours\qa\features位于 io.cucumber.core.resource.PathScanner.findResourcesForPath(PathScanner.java:42) 在 io.cumber.core.resource.PathScanner.findResourcesForUri(PathScanner.java:26) 在 io.cumber.core.resource.ResourceScanner.findResourcesForUri(ResourceScanner.java:111) 在 io.cumber.core.resource.ResourceScanner.scanForResourcesUri(ResourceScanner.java:88) 在 io.cucumber.core.runtime.FeaturePathFeatureSupplier.loadFeatures(FeaturePathFeatureSupplier.java:62) 在 io.cumber.core.runtime.FeaturePathFeatureSupplier.get(FeaturePathFeatureSupplier.java:46) at io.cumber.junit.cumber.(cumber.java:156)at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(本机 方法)在 java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62) 在 java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45) 在 java.base/java.lang.reflect.Constructor.newInstanceWithCaller(Constructor.java:500) 在 java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:481) 在 org.junit.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:104) 在 org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:86) 在 org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) 在 org.junit.internal.builders.AllDefaultPossibilityBuilder.runnerForClass(AllDefaultPossibilityBuilder.java:26) 在 org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59) 在 org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:33) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createUnfilteredTest(JUnit4TestLoader.java:90) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.createTest(JUnit4TestLoader.java:76) 在 org.eclipse.jdt.internal.junit4.runner.JUnit4TestLoader.loadTests(JUnit4TestLoader.java:49) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:525) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:763) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:463) 在 org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:209)

我试图给出特征文件和步骤定义的完整路径,但仍然出现相同的问题

以下是正在使用的文件:

1。Login.java

package com.newtours.qa.stepdefinition;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;

public class Login
{
    WebDriver driver;
    String expectedTitle = "ToolsQA – Demo Website to Practice Automation – Demo Website to Practice Automation";

    @Given("user is already on login page")
    public void user_is_already_on_login_page() throws Throwable
    {
        System.setProperty("webdriver.chrome.driver", "..\\CucumberMavenProject\\Drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.phptravels.org/clientarea.php");
        System.out.println("User navigates to URL");
        throw new io.cucumber.java.PendingException();
    }

    @And("username and password are entered")
    public void username_and_password_are_entered() throws Throwable
    {
        //String actualtitle = driver.getTitle();
        driver.findElement(By.cssSelector("#inputEmail")).sendKeys("abe");
        driver.findElement(By.cssSelector("#inputPassword")).sendKeys("aaa");
        System.out.println("User enters login details ");
        throw new io.cucumber.java.PendingException();  
    }
    @When("user clicks on signin button")
    public void user_clicks_on_signin_button() throws Throwable
    {

        driver.findElement(By.cssSelector("#login")).click();
        System.out.println("User clicks login button");
        throw new io.cucumber.java.PendingException();
    }
}
package com.newtours.qa.runner;

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)

@CucumberOptions
(
        strict = true,
        dryRun = false,
        monochrome = true,
        features = "\\com\\newtours\\qa\\features",
        glue    = "\\com\\newtours\\qa\\stepdefinition"
)

public class TestRunner
{

}
2。登录功能

Feature: Demo Tours Test

@SmokeTest
Scenario: Demo Tours Login with valid credentials

Given user is already on login page
And username and password are entered
When user clicks on signin button
Then user should see home page
3。TestRunner.java

package com.newtours.qa.stepdefinition;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import io.cucumber.java.en.And;
import io.cucumber.java.en.Given;
import io.cucumber.java.en.When;

public class Login
{
    WebDriver driver;
    String expectedTitle = "ToolsQA – Demo Website to Practice Automation – Demo Website to Practice Automation";

    @Given("user is already on login page")
    public void user_is_already_on_login_page() throws Throwable
    {
        System.setProperty("webdriver.chrome.driver", "..\\CucumberMavenProject\\Drivers\\chromedriver.exe");
        driver = new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.phptravels.org/clientarea.php");
        System.out.println("User navigates to URL");
        throw new io.cucumber.java.PendingException();
    }

    @And("username and password are entered")
    public void username_and_password_are_entered() throws Throwable
    {
        //String actualtitle = driver.getTitle();
        driver.findElement(By.cssSelector("#inputEmail")).sendKeys("abe");
        driver.findElement(By.cssSelector("#inputPassword")).sendKeys("aaa");
        System.out.println("User enters login details ");
        throw new io.cucumber.java.PendingException();  
    }
    @When("user clicks on signin button")
    public void user_clicks_on_signin_button() throws Throwable
    {

        driver.findElement(By.cssSelector("#login")).click();
        System.out.println("User clicks login button");
        throw new io.cucumber.java.PendingException();
    }
}
package com.newtours.qa.runner;

import org.junit.runner.RunWith;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;

@RunWith(Cucumber.class)

@CucumberOptions
(
        strict = true,
        dryRun = false,
        monochrome = true,
        features = "\\com\\newtours\\qa\\features",
        glue    = "\\com\\newtours\\qa\\stepdefinition"
)

public class TestRunner
{

}
项目结构:


[

功能的路径应该是相对的,并且您所拥有的似乎来自根。 它正在查找
C:\com\newtours\qa\features
,而不是查看您实际拥有
com/newtours/qa/features
的文件夹

因此,我建议您使用相对路径,如:
src/main/java/com/newtours/qa/features

类似地,对胶水执行此操作。

路径看起来不正确。请重试

features = "src/main/java/com/newtours/qa/features",
glue = "com.newtours.qa.stepdefinition"
在运行程序文件中:

features = "absolute path to feature file or package name of feature file"
glue = "package name of step definition"
  • 如果存在多个功能文件(C:\Users\CucumberMavenProject\src\main\java\com\newtours\qa\features\login.feature),则功能文件的路径可以是绝对路径,也可以是包名(“src/main/java/com/newtours/qa/features”)

  • 在glue中,指定存储步骤定义的包名称。步骤定义包下不能有多个步骤定义文件。您需要为每个功能文件创建新的步骤定义包- “com.newtours.qa.stepdefinition”


它起了作用,但出现了新问题io.cucumber.junit.UndefinedStepException:未定义“用户已登录页面”的步骤。您可以使用下面的代码段来实现它:@Given(“用户已登录页面”)public void user_已登录页面(){//在此处编写代码,将上述短语转换为具体操作抛出新io.cumber.java.PendingException();}给定(“用户已在登录页上,()->{//在此处编写代码,将上述短语转换为具体操作抛出新io.cumber.java8.PendingException();});