无法从步骤定义类

时间:2019-04-29 04:08:35

标签: automation cucumber testng

我正在使用test ng并行运行测试用例,但是无法从步骤定义类中获取参数。有人知道我该如何实现吗?目前正在黄瓜 @注解之前,我得到这个异常。如果我使用@BeforeTest / @BeforeClass(测试ng注释)而不是@Before代码,则不会执行任何操作(不会检测到该注释)

2 Steps ([36m2 skipped[0m)
0m0.003s

0.003s

cucumber.runtime.CucumberException: Hooks must declare 0 or 1 arguments. public void com.zucchini.mobile.StartPageSteps.init(java.lang.String,java.lang.String)
    at cucumber.runtime.java.JavaHookDefinition.execute(JavaHookDefinition.java:57)
    at cucumber.runtime.Runtime.runHookIfTagsMatch(Runtime.java:224)
    at cucumber.runtime.Runtime.runHooks(Runtime.java:212)
    at cucumber.runtime.Runtime.runBeforeHooks(Runtime.java:202)
    at cucumber.runtime.model.CucumberScenario.run(CucumberScenario.java:40)
    at cucumber.runtime.model.CucumberFeature.run(CucumberFeature.java:165)
    at cucumber.api.testng.TestNGCucumberRunner.runCucumber(TestNGCucumberRunner.java:63)
    at cucumber.api.testng.AbstractTestNGCucumberTests.feature(AbstractTestNGCucumberTests.java:21)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
    at org.testng.TestRunner.privateRun(TestRunner.java:774)
    at org.testng.TestRunner.run(TestRunner.java:624)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
    at org.testng.SuiteRunner.access$000(SuiteRunner.java:39)
    at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:393)
    at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64)
    at java.util.concurrent.FutureTask.run(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

如果我使用@BeforeTest / @BeforeClass(测试ng注释)而不是@Before代码,则不会执行任何操作(不会检测到该注释)

    <?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Testng Cucumber Suite" parallel="tests">
    <test name="Run All Tests">
       <parameter name="deviceName" value="Google Nexus 6" />
            <parameter name="platformVersion" value="6.0" />
        <classes>
            <class name="com.zucchini.mobile.runner.FirstZucchiniTest">
            </class>
        </classes>

    </test>

     <test name="Run All TestsB">
        <parameter name="deviceName" value="Google Pixel" />
        <parameter name="platformVersion" value="7.1" />
        <classes>
            <class name="com.zucchini.mobile.runner.FirstZucchiniTest"></class>
        </classes>
    </test>
</suite>

步骤定义

    public class StartPageSteps{
    AndroidDriver<MobileElement> androidDriver;
    String USERNAME = "";
    String ACCESS_KEY = "";
    String url = "https://" + USERNAME + ":" + ACCESS_KEY + "@hub.browserstack.com/wd/hub";
    String pVersion;


     @Before
     @Parameters({"deviceName", "platformVersion"})
    public void init(String deviceName,String platformVersion){
        System.out.println(deviceName  + "  :  " + platformVersion);
    }



    public StartPageSteps(){

    }

}

Pom文件:

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>zucchini-mobile-parallel</groupId>
    <artifactId>com.zucchini.mobile</artifactId>
    <version>0.0.1-SNAPSHOT</version>



    <properties>
        <cucumber.version>1.2.5</cucumber.version>
    </properties>


    <dependencies>
    <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>1.2.5</version>
        </dependency>
    <dependency>
            <groupId>org.testng</groupId>
            <artifactId>testng</artifactId>
            <version>6.9.9</version>
        </dependency>
<!--    <dependency>
    <groupId>com.comcast.zucchini</groupId>
    <artifactId>zucchini</artifactId>
    <version>2.3.1</version>
</dependency> -->
        <dependency>
            <groupId>io.appium</groupId>
            <artifactId>java-client</artifactId>
            <version>5.0.4</version>
            <!--<exclusions>-->
            <!--<exclusion>-->
            <!--<groupId>com.google.guava</groupId>-->
            <!--<artifactId>guava</artifactId>-->
            <!--</exclusion>-->
            <!--</exclusions>-->
        </dependency>
          <dependency>
            <groupId>org.seleniumhq.selenium</groupId>
            <artifactId>selenium-java</artifactId>
            <version>3.0.1</version>
            <scope>test</scope>
        </dependency>

          <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-java</artifactId>
            <version>${cucumber.version}</version>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-picocontainer</artifactId>
            <version>${cucumber.version}</version>
            <scope>test</scope>
        </dependency>

         <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-junit</artifactId>
            <version>1.2.5</version>
        </dependency>


        <!-- https://mvnrepository.com/artifact/info.cukes/cucumber-testng -->
        <dependency>
            <groupId>info.cukes</groupId>
            <artifactId>cucumber-testng</artifactId>
            <version>${cucumber.version}</version>
        </dependency>


        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>

    </dependencies>

    <build>
        <pluginManagement>
            <plugins>

            <!--   <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.6.0</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                </configuration>
            </plugin> -->

            <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                    <suiteXmlFiles>
            <suiteXmlFile>testng.xml</suiteXmlFile>
          </suiteXmlFiles>
                        <!-- <parallel>classes</parallel> -->
                        <threadCount>2</threadCount>
                        <reuserForks>false</reuserForks>
                        <!-- <includes>
                            <include>**/*Test.java</include>
                        </includes> -->
                    </configuration>
                </plugin>
            </plugins>
        </pluginManagement>
    </build>
</project>

Runner文件:

    @CucumberOptions(features = { "src/test/resources/StartPage.feature" }, glue = { "com.zucchini.mobile" })
public class FirstZucchiniTest extends AbstractTestNGCucumberTests {


}

2 个答案:

答案 0 :(得分:0)

使用以下代码:

public class StartPageSteps{
AndroidDriver<MobileElement> androidDriver;
String USERNAME = "";
String ACCESS_KEY = "";
String url = "https://" + USERNAME + ":" + ACCESS_KEY + "@hub.browserstack.com/wd/hub";
String pVersion;


 @Before
 public void setUp(){
    System.out.println("Inside Setup");
 }

 @Parameters({"deviceName", "platformVersion"})
 public void init(String deviceName,String platformVersion){
    System.out.println(deviceName  + "  :  " + platformVersion);
 }

 public StartPageSteps(){

 }

}

答案 1 :(得分:0)

让我们理解为什么您会遇到此异常...

拇指规则- 您不能在@Before(挂钩)内部传递超过1个参数,而该参数是从cumul.api.java.Before调用的。

您应使用@BeforeTest(org.testng.annotations.BeforeTest)而不是使用@Before