Results 1 to 4 of 4

Thread: relative paths do not work in ST?

  1. #1

    Default Answered: relative paths do not work in ST?

    I have written a short test that fetches login credentials from a local file using the fs-module and logs in.

    Code:
    describe("loginAndNavigate", function() {
        const fs = require('fs');
    
        // Reading the JSON config file and converting it to a JS object
        let configData = fs.readFileSync("C:\\Users\\JohnDoe\\SenchaTest\\Tests\\database_integration\\test\\basic_stuff\\lib\\config.json");
        configData = JSON.parse(configData);
    
        ....
    

    This works perfectly fine, but as soon as I want to use the relative path (see below for every notation I tried) the following error occurs.

    [ERROR] [model/browser/SandboxBrowser-14] [ERROR] [orion] Error: ENOENT: no such file or directory, open 'C:\Program Files\Sencha\Test\2_2_1_83\test\basic_stuff\lib\config.json' at Error (native) at Object.fs.openSync (fs.js:640:18) at Object.module.(anonymous function) [as openSync] (ELECTRON_ASAR.js:173:20) at Object.fs.readFileSync (fs.js:508:33) at Object.fs.readFileSync (ELECTRON_ASAR.js:506:29) at Suite.<anonymous>


    (C:\Users\JohnDoe\SenchaTest\Tests\database_integration\test\basic_stuff\loginAndNavigate.js:5:22) at addSpecsToSuite (C:\Program Files\Sencha\Test\2_2_1_83\resources\app.asar\node_modules\orion-core\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:864:25) at Env.describe (C:\Program Files\Sencha\Test\2_2_1_83\resources\app.asar\node_modules\orion-core\node_modules\jasmine-core\lib\jasmine-core\jasmine.js:833:7) at doDescribe (C:\Program Files\Sencha\Test\2_2_1_83\resources\app.asar\node_modules\orion-core\serve\jasmine-post-extensions.js:52:26) at Object.fn (C:\Program Files\Sencha\Test\2_2_1_83\resources\app.asar\node_modules\orion-core\serve\jasmine-post-extensions.js:90:20)


    For every one of these lines (of course not all of them at once)

    Code:
    let configData = fs.readFileSync("test\\basic_stuff\\lib\\config.json");
    let configData = fs.readFileSync("test/basic_stuff/lib/config.json");
    let configData = fs.readFileSync("./test/basic_stuff/lib/config.json");


    Note that every one of these commands work a regular js file located in the same folder as my test spec.




  2. Thank you so much!

    A small addition to your solution:
    module.id gets the file location using '\' instead of '/' so the subtstring method has to be altered the following:

    Code:
    describe('File path', function() {
        it('should do something with a file path', function() {
              // Strip off test suite file name from the file path. You can then tag on the specific folder/file you want to reference, relative to the test project.
    
              contextPath = contextPath.substring(0, contextPath.lastIndexOf('\\')); /*Two '\' are needed in order to escape one.*/
    
              // Add rest of test logic here.
         });
    });
    
    // Gets full file path of this individual test suite file. This needs to be placed here.
    var contextPath = module.id;

  3. #2
    Sencha - Sales Team daniel.gallo's Avatar
    Join Date
    Apr 2009
    Location
    London, England
    Posts
    352
    Answers
    59

    Default

    Using relative paths will give you a path that's relative to the Sencha Test executable path, rather than the test project itself.

    In order to generate a path relative to the test project, you have to do something like this:

    Code:
    describe('File path', function() {
        it('should do something with a file path', function() {
            // Strip off test suite file name from the file path.  You can then tag on the specific folder/file you want to reference, relative to the test project.
            contextPath = contextPath.substring(0, contextPath.lastIndexOf('/'));
            
            // Add rest of test logic here.
        });
    });
    
    // Gets full file path of this individual test suite file.  This needs to be placed here.
    var contextPath = module.id;
    "module.id" gets the full file path of the current test suite file, but in order for it to work you have to place this outside of the test suite, at the end of the file. Just assign that value to a variable, so it can then be referenced within your test suite and used in the generation of other file paths.
    Daniel Gallo
    Sales Engineer
    Sencha

  4. #3

    Default

    Thank you so much!

    A small addition to your solution:
    module.id gets the file location using '\' instead of '/' so the subtstring method has to be altered the following:

    Code:
    describe('File path', function() {
        it('should do something with a file path', function() {
              // Strip off test suite file name from the file path. You can then tag on the specific folder/file you want to reference, relative to the test project.
    
              contextPath = contextPath.substring(0, contextPath.lastIndexOf('\\')); /*Two '\' are needed in order to escape one.*/
    
              // Add rest of test logic here.
         });
    });
    
    // Gets full file path of this individual test suite file. This needs to be placed here.
    var contextPath = module.id;

  5. #4
    Sencha - Sales Team daniel.gallo's Avatar
    Join Date
    Apr 2009
    Location
    London, England
    Posts
    352
    Answers
    59

    Default

    Quote Originally Posted by freightgateone View Post
    A small addition to your solution:
    module.id gets the file location using '\' instead of '/' so the subtstring method has to be altered the following:
    Correct - I mostly use Mac, so the path separator on that OS uses forward slashes. Windows uses backslashes.

    Thanks.
    Daniel Gallo
    Sales Engineer
    Sencha

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •