On AWS farm getting file not found exception, when

2019-03-05 10:57发布

问题:

file not found exception

TestNG] INVOKING: "test" - testscripts.LoginTest.loginWithValidCredentialsTest()
[Invoker 1915058446] Invoking testscripts.LoginTest.loginWithValidCredentialsTest
2018-01-22 15:09:25 INFO  testpages.LoginPage:46 - clicked on skip button
2018-01-22 15:09:27 INFO  testpages.LoginPage:50 - clicked on log in button
2018-01-22 15:09:27 INFO  testpages.LoginPage:96 - Enter user name and password
java.io.FileNotFoundException: src/test/resources/LoginPage.json (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)

Please help me, i am facing this issue from last 2-3 days. Not found any solution yet.

回答1:

I was able to solve above problem. Simply we have to keep the files under resources directory. like in the below image

Now add the following dependency to the pom.xml :

<!-- https://mvnrepository.com/artifact/org.codehaus.jackson/jackson-mapper-asl -->
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

Use following code and you are ready to test it on AWS ;

 Test test = method.getAnnotation(Test.class);
    URL resource = ClassLoader.getSystemResource("test.json");
    InputStream stream = resource.openStream();
    String data_all = IOUtils.toString(stream, "UTF-8");
    ObjectMapper mapper = new ObjectMapper();
    JsonNode rootNode = mapper.readTree(data_all);
    JsonNode testNode = rootNode.get(test.testName());
    if (!testNode.get("Execute").asText().equals("Yes")) {
        throw new SkipException("Skipping the test as per Configuration");
    }
    if (testNode.get("Obsolete").asText().equals("Yes")) {
        throw new SkipException("Skipping the test as test is obsolete.");
    }
    userName = testNode.get("testData").get("userName").toString();
    password = testNode.get("testData").get("password").toString();

My test.json file is structured as below :

{
  "test_case_name": {
                     "testName": "test_case_name",
                     "testDescription": "test description goes here",
                     "testDataType": "test data type",
                     "Execute": "Yes",
                     "Obsolete": "No",
                     "testData": {
                                 "userName": "usernmae",
                                 "password": "Password"
                      },
                      "validations": {
                      "validation_value1": "NA",
                      "validation_value2": "NA"
   }
}