3

Trying to run selenium tests in AWS Lambda. Read below links and felt confident to implement similar tests with NET Core 2.1.

Here is my complete lambda function code.

    using System;
    using System.Collections.Generic;
    using System.IO;
    using Amazon.Lambda.Core;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;

    // Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]

namespace selenium_lambda_poc1
{
    public class Function
    {

        /// <summary>
        /// A simple function that takes a string and does a ToUpper
        /// </summary>
        /// <param name="input"></param>
        /// <param name="context"></param>
        /// <returns></returns>
        public void FunctionHandler(string input, ILambdaContext context)
        {
            context.Logger.LogLine("Starting...");
            var driver = GetDriver(context);
            driver.Navigate().GoToUrl(input);
            var title = driver.Title;
            LambdaLogger.Log("Window Title: " + title);
            driver.Quit();
            context.Logger.LogLine("Ending...");
        }

        public IWebDriver GetDriver(ILambdaContext context)
        {
            var lambdaTaskRootPath = Environment.GetEnvironmentVariable("LAMBDA_TASK_ROOT");
            context.Logger.LogLine(lambdaTaskRootPath);
            Environment.SetEnvironmentVariable("webdriver.chrome.driver", lambdaTaskRootPath + @"/Lib/chromedriver");

            ChromeOptions options = new ChromeOptions();
            options.BinaryLocation = lambdaTaskRootPath + "/Lib/chrome";
            var chromeBinaryPath = options.BinaryLocation;
            var chromeDriverPath = lambdaTaskRootPath + @"/Lib/chromedriver";

            context.Logger.LogLine("Chrome Path? " + chromeBinaryPath);
            context.Logger.LogLine("ChromeDriver Path? " + chromeDriverPath);
            context.Logger.LogLine("Chrome - Available? " + File.Exists(chromeBinaryPath).ToString());
            context.Logger.LogLine("ChromeDriver - Available? " + File.Exists(chromeDriverPath).ToString());


            options.AddArguments(new List<string>() {
                "--headless",
                "--disable-gpu",
                "--single-process",
                "--no-sandbox",
                "--data-path=/tmp/data-path",
                "--homedir=/tmp/homedir",
                "--disk-cache-dir=/tmp/cache-dir",
                "--allow-file-access-from-files",
                "--disable-web-security",
                "--disable-extensions",
                "--ignore-certificate-errors",
                "--disable-ntp-most-likely-favicons-from-server",
                "--disable-ntp-popular-sites",
                "--disable-infobars",
                "--disable-dev-shm-usage",
                "--window-size=1366,1024",
                "--enable-logging"
            });

            var driver = new ChromeDriver(lambdaTaskRootPath + "/Lib/", options, TimeSpan.FromMinutes(1));
            return driver;
        }
    }
}

When I test my lambda function, getting the below and I've been struggling with same issue for the last 3 days. Would be grateful if someone can help me out here

Starting ChromeDriver 2.39.562737 (dba483cee6a5f15e2e2d73df16968ab10b38a2bf) on port 38471
Only local connections are allowed.
The HTTP request to the remote WebDriver server for URL http://127.0.0.1:38471/session timed out after 60 seconds.: WebDriverException
at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.DriverServiceCommandExecutor.Execute(Command commandToExecute)
at OpenQA.Selenium.Remote.RemoteWebDriver.Execute(String driverCommandToExecute, Dictionary`2 parameters)
at OpenQA.Selenium.Remote.RemoteWebDriver.StartSession(ICapabilities desiredCapabilities)
at OpenQA.Selenium.Remote.RemoteWebDriver..ctor(ICommandExecutor commandExecutor, ICapabilities desiredCapabilities)
at OpenQA.Selenium.Chrome.ChromeDriver..ctor(ChromeDriverService service, ChromeOptions options, TimeSpan commandTimeout)
at selenium_lambda_poc.Function.GetDriver(ILambdaContext context) in C:\Users\sureshraja.s\source\repos\selenium-lambda-poc\selenium-lambda-poc\Function.cs:line 97
at selenium_lambda_poc.Function.FunctionHandler(String input, ILambdaContext context) in C:\Users\sureshraja.s\source\repos\selenium-lambda-poc\selenium-lambda-poc\Function.cs:line 34

at System.Net.HttpWebRequest.GetResponse()
at OpenQA.Selenium.Remote.HttpCommandExecutor.MakeHttpRequest(HttpRequestInfo requestInfo)
Suresh Raja
  • 411
  • 6
  • 23

0 Answers0