How can we use Edge IE Mode in Selenium Grid(RemoteWebDriver)? - c#

MS will stop supporting IE from June 2022, we are trying to find out a workaround to use Edge IE mode to do health checks we have for our IE supported applications. However, I can't see any supporting documents which show how to use Edge IE mode in Selenium Grid.

You can refer to the steps below to use Edge IE mode in Selenium Grid:
Download selenium-server-4.1.2.jar and IE Driver Server from here.
Download the corresponding version of Edge WebDriver from here (the same version as your Edge browser).
Put the paths of IE Driver Server and Edge WebDriver in Environment PATH.
Using the command prompt, navigate to the path of selenium server, then type on the command prompt to launch a hub:
java -jar selenium-server-4.1.2.jar hub
Using the command prompt, navigate to the same path and type on the command prompt to launch a node:
java -jar selenium-server-4.1.2.jar node
By default, the server will be listening on http://localhost:4444, and that’s the URL you should point your RemoteWebDriver tests.
Use the C# code below to run the automation test. The site will be opened in IE mode:
using System;
using System.IO;
using OpenQA.Selenium;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Remote;
using System.Threading;
namespace WebDriverTest
{
class seleniumgrid
{
static void Main(string[] args)
{
var dir = #"E:\webdriver_path";
var driver = "IEDriverServer.exe";
if (!Directory.Exists(dir) || !File.Exists(Path.Combine(dir, driver)))
{
Console.WriteLine("Failed to find {0} in {1} folder.", dir, driver);
return;
}
var ieOptions = new InternetExplorerOptions { };
ieOptions.AttachToEdgeChrome = true;
ieOptions.EdgeExecutablePath = #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe";
WebDriver webdriver = new RemoteWebDriver(new Uri("http://localhost:4444"), ieOptions);
webdriver.Navigate().GoToUrl("https://www.google.com");
Thread.Sleep(3000);
webdriver.Close();
webdriver.Quit();
}
}
}
Note: Please change the paths and URLs to your owns.
Reference link: Getting started with Selenium Grid

Related

Is there a way to activate IE mode in Edge Options?

Hello,
I want to achieve this by having an option inside the EdgeDriver but I cant seem to find it anywhere on the map?
I am trying to open a page in IE mode inside Edge with Selenium and EdgeDriver.
Is there a way to achieve this great thing? [pun intented]
I can see 2 questions in this thread.
Is there a way to activate IE mode in Edge Options?
There is no way to activate IE mode bypassing the Edge options parameter in the Selenium Edge driver.
I am trying to open a page in IE mode inside Edge with Selenium and EdgeDriver. Is there a way to achieve this great thing?
Yes, it is possible to automate the IE mode in the new MS Edge browser using the Selenium web driver.
The new Microsoft Edge allows you to run IE11 validation for legacy sites in addition to your modern experiences. To run your IE11 tests in Microsoft Edge, download the IEDriverServer from Selenium. Then you must pass in a capability to put Microsoft Edge into IE Mode and then run your tests.
Because this capability puts the whole browser into IE11 Mode, you cannot simultaneously test content that should render in the modern Chromium engine, but you should be able to run all of your IE11 tests and validate the rendering in Microsoft Edge. Note that this code requires an update to IEDriverServer which should be included in the next release of Selenium.
After you download the new IEDriverServer from SeleniumHQ and follow the directions for the “Required Configuration” as documented here, you can run the following code to launch the new Microsoft Edge in IE11 mode and run some tests:
static void Main(string[] args)
{
var dir = "{FULL_PATH_TO_IEDRIVERSERVER}";
var driver = "IEDriverServer.exe";
if (!Directory.Exists(dir) || !File.Exists(Path.Combine(dir, driver)))
{
Console.WriteLine("Failed to find {0} in {1} folder.", dir, driver);
return;
}
var ieService = InternetExplorerDriverService.CreateDefaultService(dir, driver);
var ieOptions = new InternetExplorerOptions{};
ieOptions.AddAdditionalCapability("ie.edgechromium", true);
ieOptions.AddAdditionalCapability("ie.edgepath", #"\\msedge.exe");
var webdriver = new InternetExplorerDriver(ieService, ieOptions, TimeSpan.FromSeconds(30));
webdriver.Url = "http://www.example.com";
}
Output:
Notes:
Make sure you are using the latest version of the IE driver server.
I suggest making a test with the latest version of the Stable Edge browser.
Try to pass the full path of the Edge browser in the 'ie.edgepath' capability. For example:
ieOptions.AddAdditionalCapability("ie.edgepath", #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe");
Make sure you close all the already opened instances and tabs of the Edge browser before running the code. Otherwise, it will generate an error.
References:
Scroll to the Automating Internet Explorer mode point in this link.
kypflug/webdriver-edge-ie-mode.cs
Below code (which is in VB.NET, but you can easily modify it to C#) will start Chromium Edge in IE Mode
Dim ieService = InternetExplorerDriverService.CreateDefaultService("DIRECTORY_PATH_HAVING_IEDriverServer.exe", "IEDriverServer.exe")
Dim ieOptions = New InternetExplorerOptions
ieOptions.IgnoreZoomLevel = True
ieOptions.AddAdditionalCapability("ie.edgechromium", True)
ieOptions.AddAdditionalCapability("ie.edgepath", "C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe")
Dim driver = New InternetExplorerDriver(ieService, ieOptions, TimeSpan.FromSeconds(60))
driver.Navigate().GoToUrl("https://example.com")
You can download IEDriverServer from https://www.selenium.dev/downloads/

C# selenium wont find webdriver from directory

I cannot get selenium to find my webdrivers, wether it be firefox or chrome. I have tried downloading the dirvers manually and with the NuGet package manager, neither of them are working for me in C# but with python they work fine.
C# code that does not work. Gives out error:
Exception thrown: 'OpenQA.Selenium.DriverServiceNotFoundException' in WebDriver.dll
An exception of type 'OpenQA.Selenium.DriverServiceNotFoundException' occurred in WebDriver.dll but was not handled in user code
The file C:/webdrivers/geckodriver.exe does not exist. The driver can be downloaded at https://github.com/mozilla/geckodriver/releases
Commeted code, found on some old stackoverflow question did not work either
using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
class Scraper
{
/*
private static IWebDriver Driver { get; set; }
public static IWebDriver Init()
{
//System.Environment.SetEnvironmentVariable("webdriver.gecko.driver", #"C:\webdrivers\geckodriver.exe");
FirefoxDriverService service = FirefoxDriverService.CreateDefaultService(#"C:\webdrivers");
service.FirefoxBinaryPath = #"C:\Program Files\Mozilla Firefox\firefox.exe"; // May not be necessary
FirefoxOptions options = new FirefoxOptions();
TimeSpan time = TimeSpan.FromSeconds(10);
Driver = new FirefoxDriver(service, options, time);
return Driver;
}
*/
IWebDriver driver;
public void StartScraping()
{
//driver = Init();
driver = new FirefoxDriver(#"C:/webdrivers/");
}
}
Calling the StartScraping() from UWP applications MainPage.xaml.cs constructor
Python code that works just fine
import selenium
from selenium.webdriver import Firefox, Chrome
def startf():
driverf = Firefox("C:/webdrivers/")
driverf.get("https://www.google.com")
startf()
Some have said that I should add the webdrivers directory to PATH, that I've done as well but nothing changed
EDIT:
After returning to this problem a couple of days later and testing selenium with just a console application and it working properly there.
This would seem to be a problem with me using a blank Universal Windows Platform application instead of a Console App. Is there a way to "attach" or open a console window inside a UWP application? Or could that even fix this issue? if not, can I somehow open similar XML form window from a console application?
The best way to get rid of this is to use Path.DirectorySeparatorChar in the path instead of using / or \\.
You can use it like:
separator = Path.DirectorySeparatorChar;
path = $"C:{separator}webdrivers{separator}";
And now you can use this path while running firefox.

Run tests implemented in C# on a multiple browsers using Selenium Grid

I have created test suite with 10 test cases using Visual Studio 2015. Tests are implemented in C# using WebDriver.
Now, my aim is to make a concept of distributed testing in Selenium using Grid technology.The Test suite to be run in 3 different browsers:IE, Chrome, Firefox parallely.
I think my grid setup is successful, but I need to expand my tests implemented in WebDriver. I found just a few tutorials in C# but none of them work for me ex- site.
I would appreciate if anyone assist me in creating the tests or send me a site with tutorial in C#
My err from executed TC in VS-GoogleSearctTestErr.
I have one hub on a win10 machine, 2 nodes localy and 3rd node started on a VM Win 8.1 Grid console preview too-GridConsole
Thank you very much.
I will add my code here:
using System;
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
using OpenQA.Selenium.Remote;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using NUnit;
using NUnit.Framework;
namespace TestOfGrid
{
[TestFixture]
public class Driver
{
IWebDriver driver;
[SetUp]
public void Setup()
{
// Create a new instance of the Firefox driver
driver = new FirefoxDriver();
DesiredCapabilities capabilities = new DesiredCapabilities();
capabilities = DesiredCapabilities.Firefox();
capabilities.SetCapability(CapabilityType.BrowserName, "firefox");
capabilities.SetCapability(CapabilityType.Platform, new Platform(PlatformType.Windows));
driver = new RemoteWebDriver(new Uri("http://192.168.56.1:4444/wd/hub"), capabilities);
}
[TearDown]
public void Teardown()
{
driver.Quit();
}
[Test]
public void GoogleSearch()
{
string homepage = "http://www.google.co.uk";
//Navigate to the site
driver.Manage().Timeouts().ImplicitlyWait(TimeSpan.FromSeconds(10));
driver.Navigate().GoToUrl(homepage);
}
}
}
This test should be run in 3 browsers parallely. If I comment desired capabilities, the site is successfully opened, but I want to start my test on a separate browsers parallely.

Cannot start the driver service on localhost when using geckodriver with Firefox 50.0.1 in .NET

I'm learning Selenium from the scratch and trying to run a test case on Firefox 50.0.1 using geckodriver I installed in VS2015 by selecting Selenium.WebDriver.GeckoDriver.Win64
However, when running the test I got an exception
Cannot start the driver service on localhost
What am I missing?
I was following some tutorials and performed step by step walk-through.
This is my code:
[TestMethod]
public void WebDriverSample()
{
IWebDriver webDriver;
//IWebDriver webDriver = new InternetExplorerDriver();
//Thread.Sleep(1000);
//webDriver.Dispose();
//webDriver = new ChromeDriver();
//Thread.Sleep(1000);
//webDriver.Dispose();
webDriver = new FirefoxDriver();
Thread.Sleep(1000);
webDriver.Dispose();
}
When using Firefox 47.0.2, I did not need to use geckodriver at all and it worked just fine.
Now, since browsers are getting updated, at some point I need to start using new versions. So, I need to find out what to do in order to be able to adapt to new changes and use geckodriver
Any suggestons?

c# headless browser with javascript support for crawler

Could anyone suggest headless browser for .NET that supports cookies and authomatically javascript execution?
Selenium+HtmlUnitDriver/GhostDriver is exactly what you are looking for. Oversimplified, Selenium is library for using variety of browsers for automation purposes - testing, scraping, task automation.
There are different WebDriver classes with which you can operate an actual browser. HtmlUnitDriver is a headless one. GhostDriver is a WebDriver for PhantomJS, so you can write C# while actually PhantomJS will do the heavy lifting.
Code snippet from Selenium docs for Firefox, but code with GhostDriver (PhantomJS) or HtmlUnitDriver is almost identical.
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Support.UI;
class GoogleSuggest
{
static void Main(string[] args)
{
// driver initialization varies across different drivers
// but they all support parameter-less constructors
IWebDriver driver = new FirefoxDriver();
driver.Navigate().GoToUrl("http://www.google.com/");
IWebElement query = driver.FindElement(By.Name("q"));
query.SendKeys("Cheese");
query.Submit();
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until((d) => { return d.Title.ToLower().StartsWith("cheese"); });
System.Console.WriteLine("Page title is: " + driver.Title);
driver.Quit();
}
}
If you run this on Windows machine you can use actual Firefox/Chrome driver because it will open an actual browser window which will operate as programmed in your C#. HtmlUnitDriver is the most lightweight and fast.
I have successfully ran Selenium for C# (FirefoxDriver) on Linux using Mono. I suppose HtmlUnitDriver will also work as fine as the others, so if you require speed - I suggest you go for Mono (you can develop, test and compile with Visual Studio on Windows, no problem) + Selenium HtmlUnitDriver running on Linux host without desktop.
I am not aware of a .NET based headless browser but there is always PhantomJS which is C/C++ and it works fairly well for assisting in unit testing of JS with QUnit.
There is also another relevant question here which might help you - Headless browser for C# (.NET)?

Categories