简体   繁体   中英

I have configured Selenium Cucumber Maven project and getting Initialization error while executing my runner.TestRunnerTest_Test.java file

I am new to Selenium Cucumber Maven integration. I am using Cucumber 3.0.2 . My TestRunnerTest code is given below :

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();
    }
}

And my feature file is given below :

Feature: Test Login page

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

Also my baseDefinition file is given :

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();
}

After running this as Maven Test, I am getting this error :

-------------------------------------------------------
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)

I am new to this cucumber-selenium framework and not understanding where i am going wrong.Thanks in Advance.

The text in your error message I enter {string} is present seems not to be linked to the code you posted. Try first with a stripped down example and expand it when you got it running.

Find a simplified example based on the code you posted below.

Assuming you have following dependencies in the 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>

and only the two following files

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 {
}

running the test with mvn compile test will produce the following output

-------------------------------------------------------
 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();
}

copy the missing steps methods into your src/test/java/stepDefinition/baseDefinition.java and run the test again. The test will fail with following error.

-------------------------------------------------------
 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM