user2377299
user2377299

Reputation: 103

Run Selenium Chrome WebDriver on Azure Cloud Service?

I have : ASP.NET Core2 App + Selenium to automate some actions with browser.

It works perfect on local.Use the latest versions of all nuget and exe.

After deploy to Azure have problems on create Webdriver.

I tried:

new ChromeDriver(ChromeDriverService.CreateDefaultService("./CORE/ExeFiles"), chromeOptions);

Read about some Firewall or Antivirus blocking stuff but cant find where to configure necessary properties on Azure.

How can I use Selenium on Azure? Some simplest example pls??, I'm fighting with this for 3 days =(

P.S. Also find this article https://github.com/projectkudu/kudu/wiki/Azure-Web-App-sandbox#unsupported-frameworks and THIS in the end:

Unsuported: PhantomJS/Selenium: tries to connect to local address, and also uses GDI+.

Alternatives? How to use Selenium on Azure?

Upvotes: 7

Views: 11793

Answers (4)

Shivam Bhojani
Shivam Bhojani

Reputation: 19

I was facing the same errors and I tried this and Its working for me. I added the chromedriver.exe in the repo itself and gave relative path in the code. It is working fine as it works in local env.

Chromedriver

Upvotes: -3

Sangeet
Sangeet

Reputation: 422

string apikey = ConfigurationManager.AppSettings["BROWSERLESS_API_KEY"];
ChromeOptions chromeOptions = new ChromeOptions();
// Set launch args similar to puppeteer's for best performance
chromeOptions.AddArgument("--disable-background-timer-throttling");
chromeOptions.AddArgument("--disable-backgrounding-occluded-windows");
chromeOptions.AddArgument("--disable-breakpad");
chromeOptions.AddArgument("--disable-component-extensions-with-background-pages");
chromeOptions.AddArgument("--disable-dev-shm-usage");
chromeOptions.AddArgument("--disable-extensions");
chromeOptions.AddArgument("--disable-features=TranslateUI,BlinkGenPropertyTrees");
chromeOptions.AddArgument("--disable-ipc-flooding-protection");
chromeOptions.AddArgument("--disable-renderer-backgrounding");
chromeOptions.AddArgument("--enable-features=NetworkService,NetworkServiceInProcess");
chromeOptions.AddArgument("--force-color-profile=srgb");
chromeOptions.AddArgument("--hide-scrollbars");
chromeOptions.AddArgument("--metrics-recording-only");
chromeOptions.AddArgument("--mute-audio");
chromeOptions.AddArgument("--headless");
chromeOptions.AddArgument("--no-sandbox");
chromeOptions.AddAdditionalCapability("browserless.token", apikey, true);
using (var driver = new RemoteWebDriver(new Uri("https://chrome.browserless.io/webdriver"), chromeOptions.ToCapabilities()))
{
//Your selenium code
}

Upvotes: 1

Sangeet
Sangeet

Reputation: 422

WELL, GOOD NEWS!!

I got the following working

Azure Web job hosted along a website in Azure App Service (S1 App Service plan) + Selenium C# + Browserless.io for running remote headless chrome.

Make sure you install only 2 the following 2 Nuget Packages -

  1. Selenium.WebDriver
  2. Selenium.Support

That's it.

I had issues when I installed ChromeDriver package also and then the ChromeDriver.exe was the cause of problems on Azure. So stay away from running the browser on Azure. Just think of it as the controller which runs the browser as service remotely.

Upvotes: 4

evilSnobu
evilSnobu

Reputation: 26314

It won't work on App Service and you already found the limits and limitations page that explains it.

That being said, it works just fine on a Cloud Service (with Roles), yes the good ol' Cloud Services.

WebRole sample —

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Web.Http;
using OpenQA.Selenium;
using OpenQA.Selenium.PhantomJS;
using OpenQA.Selenium.Support.UI;

namespace WebRole1.Controllers
{
    public class PhantomController : ApiController
    {
        /// <summary>
        /// Run PhantomJS UI tests against the specified URL
        /// </summary>
        /// <param name="URL">URL to test</param>
        public string Get(string URL)
        {
            string result = UITests.Test(URL);
            return result;
        }
    }

    /// <summary>
    /// UITests class
    /// </summary>
    public class UITests
    {
        /// <summary>
        /// Test implementation
        /// </summary>
        /// <param name="URL">URL to test</param>
        /// <returns></returns>
        public static string Test(string URL)
        {
            // Initialize the Chrome Driver
            // Place phantomjs.exe driver in the project root,
            // meaning same folder as WebRole.cs
            using (var driver = new PhantomJSDriver())
            {
                try
                {
                    // Go to the home page
                    driver.Navigate().GoToUrl(URL);

                    IWebElement input;
                    WebDriverWait wait = new WebDriverWait(
                        driver, TimeSpan.FromSeconds(2));

                    Func<IWebDriver, IWebElement> _emailInputIsVisible =
                        ExpectedConditions.ElementIsVisible(By.Id("email"));
                    wait.Until(_emailInputIsVisible);
                    input = driver.FindElementById("email");
                    input.SendKeys("[email protected]");
                    driver.FindElementById("submit").Click();
                    var alertbox = driver.FindElementById("alert");
                    if (alertbox.Text.Contains("disposable"))
                    {
                        return "PASS";
                    }
                    else
                    {
                        return "FAIL: alertbox.Text should contain " + 
                            "the word 'disposable'";
                    }
                }

                catch (Exception ex)
                {
                    return $"FAIL: {ex.Message}";
                }
            }
        }
    }
}

Alternatively you can look at Azure Container Instances with Headless Chrome. There's a .NET SDK as well.

Upvotes: 2

Related Questions