我是Selenium Cucumber Maven集成的新手。 我正在使用Cucumber 3.0.2。我的TestRunnerTest代码如下:
package runner;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.runner.RunWith;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import cucumber.api.CucumberOptions;
import cucumber.api.cli.Main;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(
glue= {"stepDefinition"} ,
plugin={"html:reports/report"}
, features = "features"
, tags= {"@Valid or @Invalid or emptyCredentials"}
)
public class TestRunnerTest {
public static WebDriver driver;
private static TestRunnerTest sharedInstance = new TestRunnerTest();
public static TestRunnerTest getInstance() {
return sharedInstance;
}
@BeforeClass
public static void before() {
System.setProperty("webdriver.chrome.driver",
"E:\\ChromeDriverNew\\chromedriver.exe");
driver=new ChromeDriver();
driver.manage().window().maximize();
}
@AfterClass
public static void after() {
Runtime.getRuntime().addShutdownHook(new Thread()
{
@Override
public void run()
{
try {
Files.move(Paths.get("reports/report"), Paths.get("reports/report_"+
LocalDateTime.now().format(DateTimeFormatter.ofPattern("YYYYLd_HHmmss"))),
StandardCopyOption.REPLACE_EXISTING);
} catch (IOException e) {
e.printStackTrace();
}
}
});
driver.quit();
}
}
我的功能文件如下:
功能:“测试登录”页面
Scenario: Verify whether user is able to redirect to the Home URL
When I go to "https://abcd/home"
@Valid
Scenario: Verify whether user is able to Login with valid Email and Password
When I go to "/login"
And I enter username ""
And I enter password ""
And I click on submit
还给出了我的baseDefinition文件:
package stepDefinition;
import org.junit.Assert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.Select;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
import runner.TestRunnerTest;
import support.Locators;
import support.TestData;
public class baseDefinition {
public Boolean beforsuit=true;
public String baseurl = "https://abcd.in";
private static TestRunnerTest runner_TestObj = TestRunnerTest.getInstance();
public WebDriver driver = runner_TestObj.driver;
@When("^I go to \"([^\"]*)\"$")
public void i_go_to(String url) {
driver.get(baseurl+url);
}
@When("^I enter username \"([^\"]*)\$")
public void i_enter_in(String arg1) {
driver.findElement(By.id("username")).sendKeys(email);
}
@When("^I enter password \"([^\"]*)\$")
public void i_enter_in(String arg1) {
driver.findElement(By.id("password")).sendKeys(pass);
}
@When("^I click on submit$")
public void i_click_on(String arg1) {
driver.findElement(By.id("submitbutton")).click();
}
将其作为Maven测试运行后,出现此错误:
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running runner.TestRunnerTest
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.55 sec <<<
FAILURE!
initializationError(runner.TestRunnerTest) Time elapsed: 0.031 sec <<<
ERROR!
cucumber.runtime.CucumberException: java.util.regex.PatternSyntaxException:
Illegal repetition near index 7
I enter {string} is present
^
at cucumber.runtime.java.JavaBackend.addStepDefinition(JavaBackend.java:156)
at cucumber.runtime.java.MethodScanner.scan(MethodScanner.java:68)
at cucumber.runtime.java.MethodScanner.scan(MethodScanner.java:41)
at cucumber.runtime.java.JavaBackend.loadGlue(JavaBackend.java:86)
at cucumber.runtime.Runtime.<init>(Runtime.java:92)
at cucumber.runtime.Runtime.<init>(Runtime.java:70)
at cucumber.runtime.Runtime.<init>(Runtime.java:66)
at cucumber.api.junit.Cucumber.createRuntime(Cucumber.java:80)
at cucumber.api.junit.Cucumber.<init>(Cucumber.java:59)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
我是这个黄瓜硒框架的新手,不了解我要去哪里。谢谢。
答案 0 :(得分:0)
错误消息I enter {string} is present
中的文本似乎未链接到您发布的代码。首先尝试一个简化的示例,并在运行它时对其进行扩展。
根据您在下面发布的代码找到一个简化的示例。
假设您在pom.xml
<properties>
<version.cucumber>3.0.2</version.cucumber>
</properties>
<dependencies>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-java</artifactId>
<version>${version.cucumber}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.cucumber</groupId>
<artifactId>cucumber-junit</artifactId>
<version>${version.cucumber}</version>
<scope>test</scope>
</dependency>
</dependencies>
以及以下两个文件
src / test / java / features / login-page.feature
Feature: cucumber
Scenario: Verify whether user is able to redirect to the Home URL
When I go to "https://abcd/home"
Scenario: Verify whether user is able to Login with valid Email and Password
When I go to "/login"
And I enter username "user"
And I enter password "password"
And I click on submit
src / test / java / runner / TestRunnerTest.java
package runner;
import org.junit.runner.RunWith;
import cucumber.api.CucumberOptions;
import cucumber.api.junit.Cucumber;
@RunWith(Cucumber.class)
@CucumberOptions(
features = "src/test/java/features/login-page.feature",
glue = { "stepDefinition" }
)
public class TestRunnerTest {
}
使用mvn compile test
运行测试将产生以下输出
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running runner.TestRunnerTest
Undefined scenarios:
src/test/java/features/login-page.feature:3 # Verify whether user is able to redirect to the Home URL
src/test/java/features/login-page.feature:6 # Verify whether user is able to Login with valid Email and Password
2 Scenarios (2 undefined)
5 Steps (5 undefined)
0m0.079s
You can implement missing steps with the snippets below:
@When("I go to {string}")
public void i_go_to(String string) {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("I enter username {string}")
public void i_enter_username(String string) {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("I enter password {string}")
public void i_enter_password(String string) {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
@When("I click on submit")
public void i_click_on_submit() {
// Write code here that turns the phrase above into concrete actions
throw new PendingException();
}
将缺少的步骤方法复制到src/test/java/stepDefinition/baseDefinition.java
中,然后再次运行测试。测试将失败,并显示以下错误。
-------------------------------------------------------
T E S T S
-------------------------------------------------------
Running runner.TestRunnerTest
Pending scenarios:
src/test/java/features/login-page.feature:3 # Verify whether user is able to redirect to the Home URL
src/test/java/features/login-page.feature:6 # Verify whether user is able to Login with valid Email and Password
2 Scenarios (2 pending)
5 Steps (3 skipped, 2 pending)
0m0.028s
cucumber.api.PendingException: TODO: implement me
at stepDefinition.baseDefinition.i_go_to(baseDefinition.java:34)
at ✽.I go to "https://abcd/home"(src/test/java/features/login-page.feature:4)
cucumber.api.PendingException: TODO: implement me
at stepDefinition.baseDefinition.i_go_to(baseDefinition.java:34)
at ✽.I go to "/login"(src/test/java/features/login-page.feature:7)
Tests run: 2, Failures: 0, Errors: 0, Skipped: 2, Time elapsed: 0.474 sec