无法实例化类TestRunner:将Cucumber与TestNG一起用于Selenium代码

时间:2018-06-01 06:19:41

标签: selenium-webdriver cucumber testng

从TestNG运行我的Cucumber TestRunner.java类时出现以下错误。在将方法替换为 psvm 之后,我尝试运行 Selenium.java 文件,它运行正常,但如果我将该方法替换为 Selenium_Setup()然后它在尝试从** TestNG(Cucumber Execute.java类)执行时提供问题**所以,我无法找到初始化webdriver的任何问题。共享错误和所有相关文件: -

Can anyone please help

错误说明: -

    [RemoteTestNG] detected TestNG version 6.8.2
    org.testng.TestNGException: 
    Cannot instantiate class testRunner.Execute
        at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
        at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
        at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
        at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
        at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
        at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
        at org.testng.TestRunner.initMethods(TestRunner.java:409)
        at org.testng.TestRunner.init(TestRunner.java:235)
        at org.testng.TestRunner.init(TestRunner.java:205)
        at org.testng.TestRunner.<init>(TestRunner.java:160)
        at org.testng.remote.support.RemoteTestNG6_5$1.newTestRunner(RemoteTestNG6_5.java:27)
        at org.testng.remote.support.RemoteTestNG6_5$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG6_5.java:63)
        at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561)
        at org.testng.SuiteRunner.init(SuiteRunner.java:157)
        at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
        at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
        at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
        at org.testng.TestNG.run(TestNG.java:1057)
        at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:114)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
    Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
        at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
        ... 21 more
    Caused by: cucumber.runtime.CucumberException: java.lang.reflect.InvocationTargetException
        at cucumber.runtime.Reflections.newInstance(Reflections.java:43)
        at cucumber.runtime.Reflections.instantiateSubclasses(Reflections.java:29)
        at cucumber.runtime.Runtime.loadBackends(Runtime.java:96)
        at cucumber.runtime.Runtime.<init>(Runtime.java:63)
        at cucumber.api.testng.AbstractTestNGCucumberTests.<init>(AbstractTestNGCucumberTests.java:33)
        at testRunner.Execute.<init>(Execute.java:14)
        ... 26 more
    Caused by: java.lang.reflect.InvocationTargetException
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
        at java.base/jdk.internal.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
        at java.base/java.lang.reflect.Constructor.newInstance(Constructor.java:488)
        at cucumber.runtime.Reflections.newInstance(Reflections.java:37)
        ... 31 more
    Caused by: java.lang.NoSuchFieldError: INSTANCE
        at cucumber.runtime.java.JavaBackend.<init>(JavaBackend.java:65)
        ... 36 more


    Below is the code for **Execute.java**(Cucumber TestRunner) file

    package testRunner;

    @CucumberOptions(

    features="D:\\eclipse-workspace\\CucumberWithTestNGForSelenium\\src\\test\\java\\featureFiles\\CreateAccount.feature",glue="stepDefinition"
    )
    public class Execute extends AbstractTestNGCucumberTests {

    }


    Below is the code for **Selenium.java** file 

    package testRunner;
    public class Selenium {
        public  WebDriver driver;

        public void Selenium_Setup() throws IOException
        //public static void main(String args[]) throws IOException
        {
            System.setProperty("webdriver.chrome.driver", "C:\\Users\\Mehak\\Downloads\\chromedriver.exe");
            driver=new ChromeDriver();
              Properties prop=new Properties();
                FileInputStream fis=new FileInputStream("D:\\eclipse-workspace\\CucumberWithTestNGForSelenium\\src\\test\\java\\ObjectRepository\\Configuration.properties");
                prop.load(fis);
                String WebsiteURL=prop.getProperty("URL");
                driver.get(WebsiteURL);
                driver.manage().window().maximize();
                driver.manage().timeouts().implicitlyWait(5000,TimeUnit.SECONDS);
        }   

    }

    And,below is my **CreateAccount.featur**e feature file:-
    @tag
    Feature: Create New Account
     I want to use this template for my feature file

    @Test
       Scenario Outline: Create New Account by adding Multiple Users
        Given Enter the URL and Click on SignIn button
        When The EMailId is provided and Click on Create an account
        Then Fill the Personal Information Details for the "<email>" in step
        And Click on Register
        And After Clicking on Register goahead and verify the "<status>" in step


        Examples: 
          |email|status|
          | abc@gmail.com |failure for Incorrect Mobile number|
          |akapo@yahoo.in|failure for validation errors|

以下是我的StepDefinition文件 CreateAccount.java

    public class CreateAccount extends Selenium{
        ObjectLocatorsForHomePage obj=new ObjectLocatorsForHomePage();


        @Given("^Enter the URL and Click on SignIn button$")
        @Test
        public void enter_the_URL_and_Click_on_SignIn_button() throws IOException  {
            Selenium sel=new Selenium();
            sel.Selenium_Setup();
          System.out.println(driver.getCurrentUrl());
          driver.findElement(By.xpath(obj.SignInbutton)).click();
           }

 Sharing the **pom.xml** file also for dependencies version of Cucumber,testNG and Selenium:-


  <dependencies>
     <dependency>
    <groupId>info.cukes</groupId>
    <artifactId>gherkin</artifactId>
    <version>2.12.2</version>
    <scope>provided</scope>
</dependency>
<dependency>
        <groupId>org.seleniumhq.selenium</groupId>
        <artifactId>selenium-server</artifactId>
        <version>3.12.0</version>
    </dependency>
     <dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-java</artifactId>
    <version>1.2.5</version>
</dependency>

<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-core</artifactId>
    <version>1.1.5</version>
</dependency>
  <dependency>
  <groupId>org.testng</groupId>
  <artifactId>testng</artifactId>
  <version>6.8.7</version>
  <scope>test</scope>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-jvm</artifactId>
    <version>1.2.5</version>
    <type>pom</type>
</dependency>
<dependency>
    <groupId>info.cukes</groupId>
    <artifactId>cucumber-testng</artifactId>
    <version>1.1.5</version>
</dependency>
  </dependencies>

任何人都可以请帮助

0 个答案:

没有答案