0

无法将 json 测试数据文件读入我的 stepDef。当我尝试在 Maven 中执行此操作时,我收到以下错误消息。

我的场景场景大纲:JSON 关键字示例给定我在 github 登录页面 #And I enter "" and "" And I enter users and passwords

Examples: {'datafile':'./src/main/resources/data/testData.json'}

我的 testData.json

[
  { "username" : "jsonuser1" , "password":"jsonpass1" , "isvalid":"false" }
]

步骤定义

@And("^I enter usernames and passwords$")
public void iEnterUsernamesAndPasswords(Map<Object, Object> data) {
    System.out.println("*************************"+ data.get("username") + data.get("password"));
    
}

错误信息:

init done 
08:48:10:169: Executing get parameters: {"url":"https://github.com/login"} 
08:48:10:778: command: get[ param-1: {url=https://github.com/login}] Result: null 
[WINDOWS  90.0.4430.212]: ▀ ▀ ▀ END STEP: Given I am on github login page ▀ ▀ ▀
08:48:10:781: Completed Teststep: Given I am on github login page 
08:48:10:787: Invoking Teststep: And I enter usernames and passwords 
[WINDOWS  90.0.4430.212]: ▄ ▄ ▄ BEGIN STEP: And I enter usernames and passwords ▄ ▄ ▄
08:48:10:787: Failure Teststep: And I enter usernames and passwords 
java.lang.RuntimeException: Wrong number of parameters, Expected 1 parameters but Actual is 0
    at com.qmetry.qaf.automation.step.JavaStep.processArgs(JavaStep.java:206) ~[qaf-2.1.15.jar:?]
    at com.qmetry.qaf.automation.step.JavaStep.doExecute(JavaStep.java:149) ~[qaf-2.1.15.jar:?]
    at com.qmetry.qaf.automation.step.BaseTestStep.execute(BaseTestStep.java:146) [qaf-2.1.15.jar:?]
    at com.qmetry.qaf.automation.step.StringTestStep.execute(StringTestStep.java:126) [qaf-2.1.15.jar:?]
    at com.qmetry.qaf.automation.step.client.Scenario.execute(Scenario.java:174) [qaf-2.1.15.jar:?]
    at com.qmetry.qaf.automation.step.client.DataDrivenScenario.scenario(DataDrivenScenario.java:66) [qaf-2.1.15.jar:?]
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:?]
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:64) ~[?:?]
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:?]
    at java.lang.reflect.Method.invoke(Method.java:564) ~[?:?]
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104) [testng-6.10.jar:?]
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:645) [testng-6.10.jar:?]
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851) [testng-6.10.jar:?]
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177) [testng-6.10.jar:?]
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) [testng-6.10.jar:?]
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) [testng-6.10.jar:?]
    at org.testng.TestRunner.privateRun(TestRunner.java:778) [qaf-2.1.15.jar:?]
    at org.testng.TestRunner.run(TestRunner.java:632) [qaf-2.1.15.jar:?]
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:387) [testng-6.10.jar:?]
    at org.testng.SuiteRunner.access$000(SuiteRunner.java:39) [testng-6.10.jar:?]
    at org.testng.SuiteRunner$SuiteWorker.run(SuiteRunner.java:421) [testng-6.10.jar:?]
    at org.testng.internal.thread.ThreadUtil$2.call(ThreadUtil.java:64) [testng-6.10.jar:?]
    at java.util.concurrent.FutureTask.run(FutureTask.java:264) [?:?]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130) [?:?]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:630) [?:?]
    at java.lang.Thread.run(Thread.java:832) [?:?]
[WINDOWS  90.0.4430.212]: ▀ ▀ ▀ END STEP: And I enter usernames and passwords ▀ ▀ ▀
08:48:10:787: Completed Teststep: And I enter usernames and passwords 
08:48:10:787: Competed scenario: JSON Keyword Example with status FAILURE 
08:48:10:806: Executing screenshot parameters: {} 
08:48:11:023: Executing quit parameters: {} 
08:48:11:025: command: quit[ param-1: {}] Result: Cannot invoke "String.toLowerCase()" because the return value of "com.qmetry.qaf.automation.util.PropertyUtil.getString(String)" is null 
08:48:11:026: Cannot invoke "String.toLowerCase()" because the return value of "com.qmetry.qaf.automation.util.PropertyUtil.getString(String)" is null 
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 4.093 sec <<< FAILURE! - in TestSuite
JSON Keyword Example(com.qmetry.qaf.automation.step.client.DataDrivenScenario)  Time elapsed: 2.984 sec  <<< FAILURE!
java.lang.RuntimeException: Wrong number of parameters, Expected 1 parameters but Actual is 0
    at com.qmetry.qaf.automation.step.client.DataDrivenScenario.scenario(DataDrivenScenario.java:66)
08:48:11:225: Stopping chrome driver service. 
Results :
Failed tests: 
  DataDrivenScenario.scenario:66->Scenario.execute:174 » Runtime Wrong number of...
Tests run: 1, Failures: 1, Errors: 0, Skipped: 0
4

1 回答 1

0

对于任何需要参数的步骤,您需要在定义步骤时指定参数占位符,并且需要提供调用时间的值。在您的情况下,尝试如下更新步骤定义:

@And("^I enter usernames and passwords \"([^\"]*)\"$")
public void iEnterUsernamesAndPasswords(Map<Object, Object> data) {
System.out.println("*************************"+ data.get("username") + data.get("password"));
}

在步骤调用时传递数据,

My Scenario
  Scenario Outline: JSON Keyword Example
    Given I am on github login page
    And I enter usernames and passwords "${args[0]}"

    Examples: {'datafile':'./src/main/resources/data/testData.json'}
于 2021-05-31T19:30:42.727 回答