Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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:

  • Include .exe files to folder and use it like :

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

  • Run Job with standalone RemoteWebServer: Cant connect to it + Job disappears after Start-Stop site.
  • Run .exe files as service - dead end;
  • Run .exe file from CMD with code: RemoteWebServer on 4444 port OK, but I can't connect to it.

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?

like image 877
user2377299 Avatar asked Aug 22 '18 01:08

user2377299


People also ask

Is Selenium compatible with Chrome?

It supports a number of browsers (Google Chrome 12+, Internet Explorer 7,8,9,10, Safari 5.1+, Opera 11.5, Firefox 3+) and operating systems (Windows, Mac, Linux/Unix). Selenium also provides compatibility with different programming languages – C#, Java, JavaScript, Ruby, Python, PHP.

How do I set Chrome capabilities in selenium?

Initially, you need to set the path to the chromedriver.exe file using set property method since you are using Chrome Browser for testing. You need to set the path to CRX File to add extensions method. Then you need to create an object of Chrome Desired Capabilities in Selenium class and pass it to web driver instance.


2 Answers

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
}
like image 50
Sangeet Avatar answered Oct 19 '22 18:10

Sangeet


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.

like image 35
evilSnobu Avatar answered Oct 19 '22 19:10

evilSnobu