【发布时间】:2018-08-21 19:35:17
【问题描述】:
我是 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)
我是这个黄瓜硒框架的新手,不知道我哪里出错了。在此先感谢。
【问题讨论】:
-
你真的在同一个类文件中有两倍的方法
public void i_enter_in(String arg1)吗?尝试注释掉所有粘合方法并运行测试。 Cucumber 默认会抱怨错过的方法并打印它正在寻找的正确签名。 -
感谢您的回复。我已经更新了文件并重新运行。但仍然收到此初始化错误。