Selenium: Chrome doesn't maximize under IIS - c#

I have a web API application. When I call some URL, selenium starts and goes to some site and makes screenshots and saves them to files.
When I run code in Visual Studio by pressing F5, the application works well.
But after I'd publish my application to IIS, I noticed that browser doesn't maximize and I got small screenshots.
I use a chrome driver, because IE driver and Firefox driver throw error
unable to connect.
I tried this code:
var chromeOptions = new ChromeOptions()
{
};
chromeOptions.AddArgument("--start-maximized");
using (IWebDriver driver = new ChromeDriver(chromeOptions))
{
driver.Manage().Window.Maximize();
/*driver.Manage().Window.Size = new System.Drawing.Size(1920, 1040); this doesn't work too*/
I notice when selenium starts under IIS I don't see a browser window. I only see a process of chrome in the task manager.
How to make maximize window of a browser under IIS?

Instead of :
chromeOptions.AddArgument("--start-maximized");
Can you try :
chromeOptions.AddArgument("start-maximized");
And remove :
driver.Manage().Window.Maximize();

Related

Using Custom Firefox Binary Location with Headless Firefox Options

I am attempting to write C# code that will launch Mozilla Firefox, browse to a website and automate form entry. I can get this to function correctly without being headless, but now I am looking to convert my code to run a headless Firefox Browser.
The following code will function if the latest version of Selenium and Firefox driver is installed via NuGet and also the latest version of the geckodriver is at the appropriate folder location.
What needs to be done to make this code open a headless Mozilla Firefox?
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
namespace GoogleSearch
{
class LaunchFirefox
{
static void Main(string[] args)
{
//Start Firefox Gecko Driver Service from Custom Location
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(#"C:\GoogleSearch");
//Force the CMD prompt window to automatically close and suppress diagnostic information
service.HideCommandPromptWindow = true;
service.SuppressInitialDiagnosticInformation = true;
//Launch Mozilla Firefox from custom location
service.FirefoxBinaryPath = #"C:\Firefox\firefox.exe";
//Initialize new Firefox Driver with the above service arguments
FirefoxDriver driver = new FirefoxDriver(service);
//Navigate to the Google website
driver.Navigate().GoToUrl("https://www.google.com");
//Automate custom Google Search Submission
driver.FindElement(By.Name("q")).SendKeys("Stack Overflow");
}
}
}
I tried inserting Firefox options, but that option doesn't seem to be available.
I get the following error when I attempt adding options to the firefox driver initialization line:
Error CS1503 Argument 2: cannot convert from 'OpenQA.Selenium.Firefox.FirefoxOptions' to 'OpenQA.Selenium.Firefox.FirefoxProfile'
Any assistance would be appreciated.
I am running the following software:
Windows 7
Visual Studio Community Edition 2017
Mozilla Firefox 61.0.1
Gecko Driver 0.21.0
Since using Firefox in headless mode is accomplished by passing a --headless option to the Firefox command line, the code you want is similar to the following:
// DISCLAIMER WARNING! The below code was written from
// memory, without benefit of an IDE. It may not be entirely
// correct, and may not even compile without modification.
//Start Firefox Gecko Driver Service from Custom Location
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(#"C:\GoogleSearch");
//Force the CMD prompt window to automatically close and suppress diagnostic information
service.HideCommandPromptWindow = true;
service.SuppressInitialDiagnosticInformation = true;
//Launch Mozilla Firefox from custom location
FirefoxOptions options = new FirefoxOptions();
options.BrowserExecutableLocation = #"C:\Firefox\firefox.exe";
options.AddArgument("--headless");
//Initialize new Firefox Driver with the above service and options
FirefoxDriver driver = new FirefoxDriver(service, options);
//Navigate to the Google website
driver.Navigate().GoToUrl("https://www.google.com");
//Automate custom Google Search Submission
driver.FindElement(By.Name("q")).SendKeys("Stack Overflow”);
As long as you’re using 3.14 or later of the .NET bindings, that code, or something similar to it, should work.
I finally found the answer to this question. The stack overflow page here helped a lot in finding the answer to this question:
Setting BrowserExecutableLocation in FirefoxOptions in Selenium doesn't prevent an "Unable to find a matching set of capabilities" error
Here is my code for headless firefox that works very well when Firefox is used in a non-default location:
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
namespace GoogleSearch
{
class LaunchFirefox
{
static void Main(string[] args)
{
//Start Firefox Gecko Driver Service from Custom Location
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(#"C:\FirefoxDriver");
//Force the CMD prompt window to automatically close and suppress diagnostic information
service.HideCommandPromptWindow = true;
service.SuppressInitialDiagnosticInformation = true;
//Initialize Firefox Options class
FirefoxOptions options = new FirefoxOptions();
//Set Custom Firefox Options
options.BrowserExecutableLocation = #"C:\Mozilla Firefox\Firefox.exe";
//options.AddArgument("--headless");
//Start Firefox Driver with service, options, and timespan arguments
FirefoxDriver driver = new FirefoxDriver(service, options, TimeSpan.FromMinutes(1));
//Navigate to the Google website
driver.Navigate().GoToUrl("https://www.google.com");
//Automate custom Google Search Submission
driver.FindElement(By.Name("q")).SendKeys("Stack Overflow");
}
}
}
Hope the helps someone else using C# code and trying to run headless firefox.
Blessings,
Seth

chromedriver.exe has stopped working when driver.Navigate().GoToUrl("http://www.example.com/")

Upon reaching navigate.GoToUrl("http://www.example.com/") chromedriver.exe will stop working, but it's working when the FirefoxDriver is being used:
using (IWebDriver driver = new ChromeDriver(DRIVER_PATH))
{
// driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
INavigation navigate = driver.Navigate();
navigate.GoToUrl("http://www.example.com/");
}
Chrome browser will successfully open.
Then a few second "chromedriver.exe has stopped working" will appear.
Here's my debug.log file:
[0508/115012.911:ERROR:process_reader_win.cc(114)] NtOpenThread: {Access Denied} A process has requested access to an object, but has not been granted those access rights. (0xc0000022)
[0508/115012.912:ERROR:exception_snapshot_win.cc(87)] thread ID 7968 not found in process
[0508/115012.912:WARNING:crash_report_exception_handler.cc(60)] ProcessSnapshotWin::Initialize failed
ChromeDriver v2.9.248315 (chromedriver_win32.zip)
Google Chrome Version 58.0.3029.96 (64-bit)
Can anyone guess how to make it work in C#?
To work with Selenium 3.4.0 you need to have latest ChromeDriver 2.29.x from here & latest Google Chrome 58.0
I don't see any issues in your code as such.
You might need to check if navigate have GoToUrl method implemented or not in C#.
As in Java we do it like this:
WebDriver driver1 = new ChromeDriver(c1);
Navigation navigate = driver1.navigate();
navigate.to("https://gmail.com");

All open Chrome windows using Selenium Chrome driver

How do I identify an existing open Chrome Window with a specific url in its address bar, and open a new tab in that window using Selenium web driver in C#? All examples I see shows how to open new tabs in a window that is opened within Selenium ChromeDriver.
IWebDriver driver = null;
var chromeDriverService = ChromeDriverService.CreateDefaultService();
chromeDriverService.HideCommandPromptWindow = true;
driver = new ChromeDriver(chromeDriverService);
List<string> tabs = new List<string>(driver.WindowHandles);
driver.WindowHandles always return the windows opened by the ChromeDriver. But, I am looking for all windows. As soon as the line that instantiates ChromeDriver is executed, a new window opens up. But, I need a new URL to be opened in a new tab in an existing window.
WebDriver can't control browser windows that it didn't open. This is in part a security measure to prevent WebDriver-based malware. Additionally, to communicate with a browser instance, the browser must be listening on a port for incoming driver commands. Unless WebDriver started the browser, the browser has no way to know to listen on that port.

Through windows service open browser to render and screenshot webgl page in selenium .Net

I have made an application in which i have used selenium in .Net. In the code, What i did was, using chrome driver, i opened up a url in chrome, where the page contains webgl and took a snapshot of the page. This works fine, when i run the code in visual studio, the chrome browser opens the webpage and renders the webgl page perfectly and takes a screenshot and saves to disk.
However, when i wrap this code in a windows service and invoke the service from other application , the browser window does not seem to open and even the snapshot taken says "Webgl not supported" (i have added code to show this message if webgl is not supported by the browser).
I am really confused , as the code works fine when running from visual studio, the webgl page renders ok, and when invoked from windows service say's webgl not supported.
I tried running the windows service with administrator rights as well as Local System, with interact with desktop option enabled, but didnt help.
Has anyone any idea, as what might be happening?
Here is the Code
Logger.Log("Step 001");
DesiredCapabilities capabilities = new DesiredCapabilities();
Logger.Log("Step 002");
capabilities = DesiredCapabilities.HtmlUnitWithJavaScript();
Logger.Log("Step 003");
ChromeOptions options = new ChromeOptions();
Logger.Log("Step 004");
options.AddArguments("start-maximized", "no-default-browser-check", "--ignore-certificate-errors","--enable-webgl-image-chromium", "--ignore-gpu-blacklist", "--use-gl", "--no-sandbox", "--disable-web-security");
Logger.Log("Step 005");
capabilities.SetCapability(ChromeOptions.Capability, options);
Logger.Log("Step 006");
capabilities.SetCapability(CapabilityType.IsJavaScriptEnabled, true);
Logger.Log("Step 007");
capabilities.SetCapability(CapabilityType.AcceptSslCertificates, true);
Logger.Log("Step 008");
IWebDriver WebDriver = new ChromeDriver(options);
Logger.Log("Step 009");
WebDriver.Manage().Timeouts().ImplicitlyWait(new TimeSpan(0, 0, 30));
Logger.Log("Step 010");
Logger.Log("Navigating to Url");
Logger.Log("Step 011");
WebDriver.Navigate().GoToUrl("http://localhost:8088/index.html");
Logger.Log("Step 012");
Logger.Log("Navigating to Url Complete");
Logger.Log("Sleeping for 10 sec.");
System.Threading.Thread.Sleep(10000);
Logger.Log("Sleep complete");
Logger.Log("Taking screenshot");
Screenshot screenshot = ((ITakesScreenshot)WebDriver).GetScreenshot();
Logger.Log("Step 013");
screenshot.SaveAsFile("c:\\temp\\acd.jpeg", System.Drawing.Imaging.ImageFormat.Jpeg);
Logger.Log("Step 014");
Logger.Log("Snapshot Complete");
Logger.Log("Webdriver quit");
WebDriver.Quit();
You can try using PhantomJS. It will provide you with a headless browser for Chrome.

How to stop invoking of Firefox browser before opening any othre browser in selenium web driver cross browser testing

I am using selenium WD in C# for cross browser testing but facing a strange problem that when ever i run my test using Nunit firstly Firefox window will open & then my desired browser window will open & run the test on it(desired browser).
As per my knowledge if any system is not having Firefox installed in it then it fails the script.
So is there any way to change this default value of browser in selenium.
I am able to run tests on different browser, my problem is only that before opening my desired browser by default first system is opening firefox. which create issue for me & my tests.
public void SetupTest()
{
driver = new SafariDriver();
baseURL = "http://google.com/";
verificationErrors = new StringBuilder();
}
Most probably, somewhere in your code you are initializing the Firefox Driver. Search for this within you code:
new FirefoxDriver();
You could also debug to the line
driver = new SafariDriver();
and see if it has an assigned value already.
But I am also pretty certain that you are initializing a FirefoxDriver somewhere.

Categories

Resources