Search code examples
c#azureseleniumgoogle-chromewebjob

Azure Webjob - Timeout when trying to open ChromeDriver


I'm trying to use Azure WebApps, with Webjobs, to Test applications. Locally, it works great. But when I build to cloud it happens the problem listed above.

Apperently the file was found, but it stays wainting until timeout.

Does anybody knows how to solve this? I'm using C# Selenium WebDriver.

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using System;
using System.Diagnostics;
using System.IO;

namespace Ololu_WebJob
{
    class Program
    {
        static void Main()
        {
            IWebDriver driver = new ChromeDriver();
            driver.Url = "my_url";

            Console.WriteLine(driver.Title);

            driver.Quit();
        }
    }
}

/*
    LOG:
    [02/10/2019 12:27:51 > 7f9c27: SYS INFO] Status changed to Initializing
    [02/10/2019 12:27:51 > 7f9c27: SYS INFO] Job directory change detected: Job file 'Ololu_WebJob.exe' timestamp differs between source and working directories.
    [02/10/2019 12:27:53 > 7f9c27: SYS INFO] Run script 'chromedriver.exe' with script host - 'WindowsScriptHost'
    [02/10/2019 12:27:53 > 7f9c27: SYS INFO] Status changed to Running
    [02/10/2019 12:27:53 > 7f9c27: INFO] Starting ChromeDriver 2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1) on port 9515
    [02/10/2019 12:27:53 > 7f9c27: INFO] Only local connections are allowed.
    [02/10/2019 12:27:53 > 7f9c27: INFO] Please protect ports used by ChromeDriver and related test frameworks to prevent access by malicious code.
    [02/10/2019 12:31:55 > 7f9c27: ERR ] Command 'cmd /c ""chromedriv ...' was aborted due to no output nor CPU activity for 121 seconds. You can increase the SCM_COMMAND_IDLE_TIMEOUT app setting (or WEBJOBS_IDLE_TIMEOUT if this is a WebJob) if needed.
    cmd /c ""chromedriver.exe""
    [02/10/2019 12:31:55 > 7f9c27: SYS INFO] Status changed to Failed
    [02/10/2019 12:31:55 > 7f9c27: SYS ERR ] System.AggregateException: One or more errors occurred. ---> Kudu.Core.Infrastructure.CommandLineException: Command 'cmd /c ""chromedriv ...' was aborted due to no output nor CPU activity for 121 seconds. You can increase the SCM_COMMAND_IDLE_TIMEOUT app setting (or WEBJOBS_IDLE_TIMEOUT if this is a WebJob) if needed.
    cmd /c ""chromedriver.exe""
       at Kudu.Core.Infrastructure.IdleManager.WaitForExit(IProcess process) in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\IdleManager.cs:line 45
       at Kudu.Core.Infrastructure.ProcessExtensions.<Start>d__12.MoveNext() in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\ProcessExtensions.cs:line 212
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Kudu.Core.Infrastructure.Executable.<ExecuteAsync>d__31.MoveNext() in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\Executable.cs:line 255
       --- End of inner exception stack trace ---
       at System.Threading.Tasks.Task.ThrowIfExceptional(Boolean includeTaskCanceledExceptions)
       at System.Threading.Tasks.Task`1.GetResultCore(Boolean waitCompletionNotification)
       at System.Threading.Tasks.Task`1.get_Result()
       at Kudu.Core.Infrastructure.Executable.ExecuteInternal(ITracer tracer, Func`2 onWriteOutput, Func`2 onWriteError, Encoding encoding, String arguments, Object[] args) in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\Executable.cs:line 216
       at Kudu.Core.Infrastructure.Executable.ExecuteReturnExitCode(ITracer tracer, Action`1 onWriteOutput, Action`1 onWriteError, String arguments, Object[] args) in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\Executable.cs:line 165
       at Kudu.Core.Jobs.BaseJobRunner.RunJobInstance(JobBase job, IJobLogger logger, String runId, String trigger, ITracer tracer, Int32 port) in C:\Kudu Files\Private\src\master\Kudu.Core\Jobs\BaseJobRunner.cs:line 272
    ---> (Inner Exception #0) ExitCode: -1, Output: Command 'cmd /c ""chromedriv ...' was aborted due to no output nor CPU activity for 121 seconds. You can increase the SCM_COMMAND_IDLE_TIMEOUT app setting (or WEBJOBS_IDLE_TIMEOUT if this is a WebJob) if needed., Error: Command 'cmd /c ""chromedriv ...' was aborted due to no output nor CPU activity for 121 seconds. You can increase the SCM_COMMAND_IDLE_TIMEOUT app setting (or WEBJOBS_IDLE_TIMEOUT if this is a WebJob) if needed., Kudu.Core.Infrastructure.CommandLineException: Command 'cmd /c ""chromedriv ...' was aborted due to no output nor CPU activity for 121 seconds. You can increase the SCM_COMMAND_IDLE_TIMEOUT app setting (or WEBJOBS_IDLE_TIMEOUT if this is a WebJob) if needed.
    cmd /c ""chromedriver.exe""
       at Kudu.Core.Infrastructure.IdleManager.WaitForExit(IProcess process) in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\IdleManager.cs:line 45
       at Kudu.Core.Infrastructure.ProcessExtensions.<Start>d__12.MoveNext() in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\ProcessExtensions.cs:line 212
    --- End of stack trace from previous location where exception was thrown ---
       at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
       at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
       at Kudu.Core.Infrastructure.Executable.<ExecuteAsync>d__31.MoveNext() in C:\Kudu Files\Private\src\master\Kudu.Core\Infrastructure\Executable.cs:line 255<---

*/

Solution

  • Per my experience, you can not directly use selenium and chromedriver without an installed instance of Chrome or Chromium on Azure WebApp, as the requirement of ChromeDriver Setup said below.

    • Ensure Chromium/Google Chrome is installed in a recognized location

    ChromeDriver expects you to have Chrome installed in the default location for your platform. You can also force ChromeDriver to use a custom location by setting a special capability.

    However, due to some restricts of Azure WebApp sandbox which include Win32k.sys (User32/GDI32) Restrictions and Local Address Requests, you could not start Chrome or Chromium without GDI support via chromedriver even if you uploaded a portable distribution of Chrome or Chromium into Azure WebApp and configured environment variables.

    Although your error information did not point out the real reason of the issue in your C# code, I got the error information in Python on Azure WebApp as below.

    enter image description here

    So a workaround way is to directly use selenium and chromedriver on Azure VM. Or considering for your scenario, you can start chromedriver on default port 9515 on Azure VM and configure the VM NSG role to allow the inbound of port 9515, then to use class OpenQA.Selenium.Remote.RemoteWebDriver to connect the remote webdriver in Azure WebJob.

    Here is a sample code for using RemoteWebDriver which comes from C# (CSharp) OpenQA.Selenium.Remote RemoteWebDriver.FindElement Examples

    IWebDriver webDriver = new RemoteWebDriver(new Uri("http://<vm host ip>:9515"), DesiredCapabilities.Chrome());