0

I am following the link: How Can I write an AWS Lambda Script that Runs a Protractor / Selenium Browser Automation Script? And I am implementing the same code in handler as given in the answer:

'use strict';
module.exports.runtest = (event, context, callback) => {

  var npm = require('npm');
  var path = require('path');
  var childProcess = require('child_process');
  var args = ['conf.js'];

  npm.load({}, function() {
    var child = childProcess
    .fork(path.join(npm.root, 'protractor/bin/protractor'), args)
    .on('close', function(errorCode) {
      const response = {
        statusCode: 200,
        body: JSON.stringify({
          message: `Selenium Test executed on BrowserStack!  Child process Error Code: ${errorCode}`,
        }),
      };
      callback(null, response);
    });
    process.on('SIGINT', child.kill);
  });
};

But on invoking lambda function, its giving me following error:

{
  "errorType": "TypeError",
  "errorMessage": "callback must be a function if provided",
  "trace": [
    "TypeError: callback must be a function if provided",
    "    at Object.load (/var/task/node_modules/npm/lib/npm.js:163:13)",
    "    at Runtime.module.exports.runtest [as handler] (/var/task/handler.js:10:7)",
    "    at Runtime.handleOnce (/var/runtime/Runtime.js:66:25)"
  ]
}

Any help here?

Shivi
  • 3
  • 2

1 Answers1

0

The npm.load function only takes in a single argument, which is a callback function. However, you pass in {} as the first argument, hence you get a TypeError saying that the passed value is not a function.

The fact that you're using the exact code from the other page that works, is probably due to different versions of npm, especially since the post you're referring to is quite old.

stijndepestel
  • 3,076
  • 2
  • 18
  • 22
  • Hey, when I ran the test again after fixing the code, its giving me following error: [15:53:49] I/launcher - Running 1 instances of WebDriver [15:53:49] I/direct - Using ChromeDriver directly... [15:53:49] E/direct - Error code: 135 [15:53:49] E/direct - Error message: Could not find chromedriver at /var/task/node_modules/webdriver-manager/selenium/chromedriver_92.0.4515.107.exe. Run 'webdriver-manager update' to download binaries. How can I run 'webdriver-manager update' on lambda function? If not, How can I use the chromedriver and headless chromium layer existing in my lambda func? – Shivi Aug 05 '21 at 16:00
  • You have to upload the necessary binaries yourself. So you have to ensure that you run an `npm install` (ensuring that you install binaries for the Amazon Linux 2 environment and not necessarily your develop environment) and include the node_modules folder in the source artifact that you upload to lambda. – stijndepestel Aug 05 '21 at 19:42
  • First of all thanks a lot for all the useful answers. They are helping me a lot :) Exactly, what you said in above comment, I tried uploading node_modules in the source artifact earlier but I got this issue: "AWS Lambda Error: Unzipped size must be smaller than 262144000 bytes" Hence, I am not able to upload my artifacts along with node_modules due to lamda size limitations.. Please help here if you have any solution to this. – Shivi Aug 06 '21 at 07:12
  • Ensure you're only uploading the production dependencies, and not the development dependencies, this can already have a huge impact on the size of the folder. However, if you're still having trouble with that limitation, you should look into using [container images](https://docs.aws.amazon.com/lambda/latest/dg/images-create.html) to upload to lambda, for which the limitations are less strict regarding size. If the above answered your question, please mark the answer as accepted so the question is marked as answered. – stijndepestel Aug 06 '21 at 07:50
  • Thanks for your suggestions. As suggested, I am now using container images following the docs properly. After running container, when I run my tests locally by hitting the cmd: curl -XPOST "http://localhost:9000/2015-03-31/functions/function/invocations" -d '{}' I am getting : {"statusCode":200,"body":"{\"message\":\"Selenium Test executed on Chrome! Child process Error Code: 199\"}"}% I tried to debug issue but not able to find any concrete resolution. Any help here? – Shivi Aug 07 '21 at 09:34
  • That's pretty far of the original topic of the question and does not seems a problem correlated to AWS Lambda. You're better of creating a new question I think – stijndepestel Aug 07 '21 at 13:13