zerocode icon indicating copy to clipboard operation
zerocode copied to clipboard

Execute tests from a relative path of a scenario file from the PWD(Present Working Directory)

Open authorjapps opened this issue 4 months ago • 8 comments

Currently the Zerocode runner can execute a test from the Classpath. That's fine and we will retain this as this is given default to us. See example: https://github.com/authorjapps/zerocode/blob/master/http-testing-examples/src/test/java/org/jsmart/zerocode/testhelp/tests/helloworld/JustHelloWorldTest.java

@TargetEnv("github_host.properties")
@RunWith(ZeroCodeUnitRunner.class)
public class JustHelloWorldTest {

    @Test
    @Scenario("helloworld/hello_world_status_ok_assertions.json") <---------- picks from Classapth
    public void testGet() throws Exception {
    }

}

AC1:

But, we need a mechanism to make it flexible to run even if

  • relative path is provided to the scenario file

Example 1:

    $pwd
    /home/authorj/code/zerocode
    $

    then, I should be able to run using below:
    @Test
    //  should pick from relative-apth from PWD //
    @Scenario("./http-testing-examples/src/test/resources/helloworld/hello_world_status_ok_assertions.json")
    public void testGet() throws Exception {
    }

   // in the above PWD =  /home/authorj/code/zerocode, which is represented by "."

Example 2:

    $pwd
    /home/authorj/code/zerocode/http-testing-examples/src
    $

    then, I should be able to run using below:
    @Test
    //  should pick from relative-apth from PWD //
    @Scenario("./test/resources/helloworld/hello_world_status_ok_assertions.json")
    public void testGet() throws Exception {
    }

   // in the above PWD =  /home/authorj/code/zerocode/http-testing-examples/src, which is represented by "."

Linked Issue #722

AC = Acceptance Criteria

authorjapps avatar Aug 11 '25 18:08 authorjapps

Hello, I am a first time contributor and interested in making a change to resolve this issue. Can I take this up? Thanks

pratyushkumar211 avatar Aug 23 '25 19:08 pratyushkumar211

Hello, I am a first time contributor and interested in making a change to resolve this issue. Can I take this up? Thanks

@pratyushkumar211 , Yes please go ahead. Thanks for your interest.

When you're ready or just before you implement the solution, can you put a summary of what approach or solution you would be implementing? This will make everyone to be in sync for PR reviews.

authorjapps avatar Aug 24 '25 07:08 authorjapps

Thanks @authorjapps !

I was thinking that the Scenario path gets resolved in the function public static String readJsonAsString(String scenarioFile) in this file: https://github.com/authorjapps/zerocode/blob/master/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java

So I can modify the readJsonAsString function with the following additions in the try statement.

  String pwd = System.getProperty("user.dir");
  if (scenarioFile.startsWith("./") || scenarioFile.startsWith("../")) {  
     // Relative path: Resolve from PWD
     scenarioFile = Paths.get(pwd, scenarioFile.substring(2)).normalize().toString(); // Adjust for ../
  }

Could you let me know if this makes sense? And are you aware of any other files / function where a change needs to be made to resolve this issue?

pratyushkumar211 avatar Aug 24 '25 17:08 pratyushkumar211

Thanks @authorjapps !

I was thinking that the Scenario path gets resolved in the function public static String readJsonAsString(String scenarioFile) in this file: https://github.com/authorjapps/zerocode/blob/master/core/src/main/java/org/jsmart/zerocode/core/utils/SmartUtils.java

So I can modify the readJsonAsString function with the following additions in the try statement.

  String pwd = System.getProperty("user.dir");
  if (scenarioFile.startsWith("./") || scenarioFile.startsWith("../")) {  
     // Relative path: Resolve from PWD
     scenarioFile = Paths.get(pwd, scenarioFile.substring(2)).normalize().toString(); // Adjust for ../
  }

Could you let me know if this makes sense? And are you aware of any other files / function where a change needs to be made to resolve this issue?

Hello @pratyushkumar211 , Thanks for asking. Looks good to me. But, can you also have a look this has not already implemented like the other ticket for full abs path?

If not, then your solution approach should work I think.

  • Another query, will this "user.dir" work for Windows OS? worth checking
  • Is there no other way to work out the PWD for all OS in general ?

Anyway give it a go, and go ahead with the PR.

nirmalchandra avatar Aug 30 '25 21:08 nirmalchandra

Thanks @nirmalchandra. I double checked and did not found this relative path feature implemented already like the abs path, and the String pwd = System.getProperty("user.dir"); command should work for all OS in general as well.

I have submitted the following PR https://github.com/authorjapps/zerocode/pull/727 to resolve this. I will look forward to hearing feedback on my proposed changes and getting the updates incorporated in the main repo. Thanks.

pratyushkumar211 avatar Sep 06 '25 04:09 pratyushkumar211

Thanks @nirmalchandra. I double checked and did not found this relative path feature implemented already like the abs path, and the String pwd = System.getProperty("user.dir"); command should work for all OS in general as well.

I have submitted the following PR #727 to resolve this. I will look forward to hearing feedback on my proposed changes and getting the updates incorporated in the main repo. Thanks.

Thanks @pratyushkumar211 , Can you check the PR comments and fix them?

nirmalchandra avatar Sep 06 '25 10:09 nirmalchandra

Thanks @nirmalchandra. However, I don't see any comments in my PR (https://github.com/authorjapps/zerocode/pull/727) and it only says "Review Required" or "1 workflow awaiting approval" like in the screenshot below. Could you let me know how could I check whether those builds are successful or not?

Thank you

Image

pratyushkumar211 avatar Sep 06 '25 16:09 pratyushkumar211

Here is the screenshot of the comment. Let me check why GitHub is not showing you

Image

nirmalchandra avatar Sep 06 '25 21:09 nirmalchandra