How to run Firefox in a Scheduled Task in Windows 2012 R2 - selenium

Context: Microsoft Azure / Windows 2012 R2; C#; Selenium.Webdriver 3.4.0; Selenium.Support 3.4.0; Selenium.WebDriver.GeckoDriver.Win64 0.18.0; Firefox 54.0.1 (64-bit); geckodriver 0.18.0
I'm having enormous difficulty getting the Firefox session to launch inside a Windows Service Scheduled Task. I have no problem launching a Selenium-driven Firefox session from a C# exe interactively.
The Scheduled Task is set to "Run whether user is logged on or not", and "Run with highest privilege" is checked.
Part of the code in this boiled-down exe:
FirefoxProfile fp = new FirefoxProfile(#"C:\web\FirefoxProfiles\Thingo");
fp.AcceptUntrustedCertificates = true;
FirefoxOptions fo = new FirefoxOptions();
fo.Profile = fp;
fo.LogLevel = FirefoxDriverLogLevel.Debug;
fo.BrowserExecutableLocation = #"C:\Program Files\Mozilla Firefox\firefox.exe";
var ts = TimeSpan.FromMinutes(1);
IWebDriver driver = null;
try
{
driver = new FirefoxDriver(fo);
}
catch (Exception E)
{
Console.WriteLine(E.Message);
Environment.Exit(255);
}
After a minute of running that code I get the following Message:
The HTTP request to the remote WebDriver server for URL http://localhost:51289/session timed out after 60 seconds.
Interestingly, according to Task Manager, geckodriver.exe has --port 51289 on its command line.
I've been trying various combinations of FirefoxDriver invocations. None work as expected giving the above error or
Unable to find a matching set of capabilities
which makes even less sense. I've also tried a couple of different versions of Firefox, all to no avail.
Is there some Selenium, Firefox or, for that matter, Windows setting that might be affecting the running of the scheduled task? The task launches, and the binary runs, but Firefox is not launched.

Related

C# : Cannot run selenium tests in parallel while using a Chrome profile

I am trying to run tests in parallel , however I am not able to. It triggers only one failing the other. I am using Chrome profiling option ("user-data-dir") to launch Chrome. If I disable this option, the tests run just fine in parallel. Can somebody tell me what I could be missing?
The argument that I have added is
public string ChromeProfilePath= "C:\Users\myuser\AppData\Local\Google\Chrome\User Data";
ChromeOptions options = new ChromeOptions();
options.AddArgument(string.Format("user-data-dir={0}", ChromeProfilePath));
ChromeDriver driver = new ChromeDriver(options);
It invokes the driver session with the current chrome profile successfully when a test is run but fails when run in parallel (invokes one instance, test it and fails the rest).
The error I receives for the failed one is
OpenQA.Selenium.WebDriverException : unknown error: failed to write prefs file
Note: I am using NUnit as test framework, C# and Selenium

ChromeDriverService and Azure DevOps Agents

I am currently running Selenium tests through an Azure DevOps pipeline using C#.
I made a change to my code to write ChromeDriver logs, which necessitated a change to using the ChromeDriverService.
string binaryDir = Manager.GetWebDriver(Manager.BrowserType.CHROME,os,browser);
if (options == null) options = new ChromeOptions();
options.AddUserProfilePreference("download.default_directory", ExtentReporter.ReportDir);
options.SetLoggingPreference(LogType.Browser, LogLevel.Info);
options.UnhandledPromptBehavior = UnhandledPromptBehavior.Ignore;
Console.WriteLine("Chrome Logging path: "+chromeLogPath);
var service = ChromeDriverService.CreateDefaultService(binaryDir);
service.LogPath = chromeLogPath;
service.EnableVerboseLogging = true;
_driver = new ChromeDriver(service, options);
_driver.Manage().Window.Maximize();
return _driver;
After making this change the chromedriver is no longer able to open with the error:
OpenQA.Selenium.WebDriverException : Cannot start the driver service on http://localhost:52257/
I have also copied the compiled code out of the Agent directory and executed it via command line and it was able to run all tests. BUT when executed via command line within the Agent work directory the ChromeDriverService threw the previous exception.
I believe the issue is related to the creation of the ChromeDriver logs, but I am also generating files in the Agent Work directory without any errors.
Any previous experience of the WebDriveService or generating ChromeDriver logs through Azure DevOps will be much obliged.
I solved this shortly after eventually posting.
Run the agent as administrator resolves it.

Selenium navigate().to() stuck without error

I am running tests using TestNG, chromium and Selenium, in Java, on two machines:
my own laptop
a Mac Mini I do not have physical access to, that I connect via SSH, that has the same identical project structure as the project on my laptop.
Both machines operate behind the same corporate proxy.
The issue I am having is the following:
while the tests run smoothly on my machine, when I execute the command to start them on the Mac Mini, the execution stops when telling the Selenium WebDriver to navigate to any specified url using driver.navigate().to(url) or driver.get(url); causing the program to freeze without throwing any exception, essentially remaining on hold.
The code resembles the following:
in the #BeforeClass beforeClass() driver is initialized with an instance of ChromeDriver using ChromeOptions. Among other settings, I add the following arguments to the ChromeOptions like such chromeOptions.addArguments(options): where options is things like --disable-web-security, --allow-running-insecure-content, --allow-insecure-localhost and most importantly: --proxy-server=address:port (e.g. --proxy-server=172.26.44.146:3128).
in the #BeforeMethod beforeMethod() after checking that the driver is not null and that the URL url is a valid one, I execute something similar to the stylized following code:
try {
log.debug("Navigating to: " + url);
driver.navigate().to(url);
log.debug("Navigated to url: " + driver.getCurrentUrl());
} catch (Exception e) {
log.error("Unable to navigate at page: " + url.toString());
throw e;
}
As I said, when running the test on the Mac Mini via SSH using the command line in a similar fashion to: java -cp WORKSPACE org.testng.TestNG testng.xml the code stops executing and freezes at log.debug("Navigating to: " + url); essentially not producing other output and forcing me to manually stop the execution of the test.
As additional information, tests are run using chromium version 72.0.3609.0.
The ChromeOptions also receive the following proxy setting (like such: chromeOptions.setProxy(p);) where p is a Proxy with
p.setProxyType(ProxyType.MANUAL);
p.setAutodetect(false);
I suspect an issue with the proxy (and am doubtful I have correctly preconfigured the program), but, quite frankly, I am puzzled by the fact that the same script runs locally on my machine and is unable to run locally on the Mac Mini when the tests are launched via SSH.

Cannot open Firefox browser selenium automation script - Error: cannot open display: :99 in Jenkins Ubuntu

I have a simple automation script written in selenium webdriver. I need to execute this script through Jenkins. So I've configured the job in Jenkins and when tried to execute, I'm unable to open Firefox browser.
The console output is as follows:
Running TestSuite
org.openqa.selenium.firefox.NotConnectedException: Unable to connect to host 127.0.0.1 on port 7055 after 45000 ms. Firefox console output:
Error: cannot open display: :99
Failed tests:
NewTest.testEasy:25 ยป WebDriver Failed to connect to binary FirefoxBinary(/usr...
This is my code:
#Test
public void testEasy() {
// WebDriver driver = new FirefoxDriver();
WebDriver driver = null;
FirefoxBinary binary = new FirefoxBinary(new File("/usr/bin/firefox"));
binary.setEnvironmentProperty("DISPLAY",System.getProperty("lmportal.xvfb.id",":99"));
driver = new FirefoxDriver(binary,null);
driver.get("https://www.google.co.in/?gfe_rd=cr&ei=drM-V8PiHPPI8AevjriQDA&gws_rd=ssl");
// String title = driver.getTitle();
// AssertJUnit.assertTrue(title.contains("Free Selenium Tutorials"));
// System.out.println("TEST");
}
Error: cannot open display: :99
When we run selenium scripts in jenkins they run in headless mode i.e. without any display. Hence they need some kind of virtual buffer for example Xvfb or alike.
In this case, script is looking for some virtual display on port :99, which is not running. If the jenkins box is linux try before running the selenium script.
Xvfb :99 &
export DISPLAY=:99
Actually I'm able to execute the script. But only browser is not visible. I'm ok with it. Also I tested with failing test script, and Jenkins returned with that error.

Jenkins + Selenium WebDriver + MSTest issue

I have created a test method in Visual Studio 2010 Ultimate that checks for the existance of two text boxes. I instantiate "InternetExplorerDriver" in the AssemblyInitialize() method and have setup Jenkins (on Windows 7) to run the MSTest method using the MSTestRunner plugin. The test seem to pass but I'm not seeing the DOS command window that I see when the "InternetExplorerDriver" instantiates and also I'm not seeing the Internet Explorer browser from loading the web page at all even though the test has passed. I also call the Quit() method on the webdriver in AssemblyCleanup. I'm running Jenkins service as my own NT account on my local box as running the service as "Local System Account" has issues. Here's my code snippet:
InternetExplorerOptions ieOptions = new InternetExplorerOptions();
ieOptions.IntroduceInstabilityByIgnoringProtectedModeSettings = true;
webDriver = new InternetExplorerDriver(ieDriverDirectory, ieOptions);
The ieDriverDirectory has the InternetExplorerServer.exe running in 32bit mode. When I run the same test method from Visual Studio 2010 IDE or from the mstest.exe /testcontainer:, I'm able to see the DOS command window loading with the port number, the browser loading, the test method passing and the browser closing at the end when Quit() is called.