Search code examples
opentest

How can i read data from a text file in opentest


I tried the below code but it throws an error that

Caused by: :1 ReferenceError: "require" is not defined

              fs.readFile('test.txt', 'utf-8',function(err,data){
                  if (err) {
                      return  $log(err);
                    }
                    $log(data);
              })

Solution

  • You are trying to use the Node.js API, which is not available in OpenTest. The proper way to read a text file is by using the ReadTextFile keyword:

    - description: Read text file from disk
      action: org.getopentest.actions.files.ReadTextFile
      args:
        file: $tempDir + "/test.txt"
        encoding: UTF-8
    
    - script: |
        var fileContents = $output.text;
        $log(fileContents)