Some time ago, I wrote some Selenium unit tests and these used the Firefox browser (headless). After a few years, I have returned to these (and cannot now remember how they worked) and would like to update them to use the Edge browser (in headless mode). How do I do this ? Specifically, when I ask the IWebDriver to "Navigate", how does it know the root URL - specifically the port ? I presume that my URL would be "http://localhost:nnnn/blah...", but how do I determine the port (which I also presume I need) ?
My code is based upon the following:
protected IWebDriver GetBrowserEdge(bool headless = true)
{
var service = SeleniumTools.EdgeDriverService.CreateChromiumService();
service.UseVerboseLogging = true;
var options = new SeleniumTools.EdgeOptions { PageLoadStrategy = PageLoadStrategy.Normal, UseChromium = true };
if (headless) options.AddArgument("headless");
return new SeleniumTools.EdgeDriver(service, options);
}
I wasn't using "CreateChromiumService" before (and I note it is disposable - which gives me a design issue), but I note that the service it returns has a "port" property. Do I need to leverage that in my "IWebDriver.Navigate" calls ?
I am using Edge v92.0.902.73 and Microsoft.Edge.SeleniumTools v3.141.2
Related
Been testing the Kameleo.LocalApiClient now whole day. Noticed something strange.
var client = new KameleoLocalApiClient(new Uri(KameleoBaseUrl));
client.SetRetryPolicy(null);
// Search Chrome Base Profiles
var baseProfileList = await client.SearchBaseProfilesAsync("desktop", "windows", "chrome", "ru-ru");
Random rnd = new Random();
var baseId = rnd.Next(1, baseProfileList.Count);
// Create a new profile with recommended settings
// Choose one of the Firefox BaseProfiles
// You can set your proxy up in the setProxy method
var requestBody = BuilderForCreateProfile
.ForBaseProfile(baseProfileList[baseId].Id)
.SetRecommendedDefaults()
.SetProxy("http", new Server("zproxy.lum-superproxy.io", 22225, "lum-customer-joshua901-zone-zone2", "5inhblkgrzxj"))
//.SetProxy("http", new Server("zproxy.lum-superproxy.io", 22225, "lum-customer-joshua901-zone-russiashared-ip-181.214.180.215", "lqoz0ee2hbvb"))
.SetLauncher("chrome")
//.SetScreen("--window-size", 500, 783)
.Build();
var profile = await client.CreateProfileAsync(requestBody);
// Start the browser
await client.StartProfileAsync(profile.Id);
// Connect to the running browser instance using WebDriver
var uri = new Uri($"{KameleoBaseUrl}/webdriver");
var opts = new ChromeOptions();
opts.AddAdditionalOption("kameleo:profileId", profile.Id.ToString());
opts.AddExcludedArgument("enable-automation");
opts.AddArgument("--window-size=500,783");
opts.AddArgument("disable-infobars");
opts.AddArgument("--incongito");
opts.AddArgument("--disable-extensions");
opts.AddArgument("disable-gpu");
opts.AddArgument("headless");
opts.AddArgument("--ignore-certificate-errors");
opts.AddArgument("no-sandbox");
opts.AddArgument("--silent-launch");
opts.AddArgument("--no-startup-window");
//var webdriver = new RemoteWebDriver(uri, opts);
_driver = new RemoteWebDriver(uri, opts)
I want to add my additional ChromeOptions to my driver, especially be able to run things in "headless" mode.
Even if i define the ChromeOptions above and create the RemoteWebDriver with these options, the chrome browser does still popup and not run as headless.
How come and how do i add my additional options?
The browser is started by Kameleo
The RemoteWebDriver is not starting any browser, just connecting to the already started browser
Because of these, you cannot pass any arguments to the browser with the RemoteWebDriver's options.
There is way to provide additional web driver options. There is a POST /profiles/{guid}/start endpoint which can process additional arguments, options, and preferences when Kameleo is starting the browser. See the documentation or see the example code.
await client.StartProfileWithWebDriverSettingsAsync(profile.Id, new WebDriverSettings
{
Arguments = new List<string> { "mute-audio" },
Preferences = new List<Preference>
{
new Preference("profile.managed_default_content_settings.images", 2),
new Preference("profile.password_manager_enabled.images", 2),
},
AdditionalOptions = new List<Preference>
{
new Preference("pageLoadStrategy", "eager"),
}
});
There are arguments that are not supported and that may cause issues so it is better to contact the team about the arguments.
For example:
opts.AddArgument("--disable-extensions");
This cannot be added, since Kameleo's extension is needed in the browser. If you remove it, the spoofing won't work.
I see you want to use this flag as well:
opts.AddArgument("disable-gpu");
You can simply set WebGL to Block. IT will result in the same.
Currently, there is no opportunity to start the browser in headless mode. But there will be a solution soon.
I'm trying to run some selenium C# end-to-end tests on Edge Chromium browser. One of the tests does a download check, basically it downloads a xml file and check whether it exists in the downloaded location.
I'm using Microsoft.Edge.SeleniumTools EdgeOptions to construct options for the EdgeDriver.
But the issue right now is that Edge blocks downloads.
Tried different sorts of things but none of them worked.
Same issue can be solved on Chrome by disabling "safebrowsing" on UserProfilePreferences in ChromeOptions.
I know for a fact that SmartScreen does the blocking, if that is so is there any profile preference that I can use to disable SmartScreen ?
Or any other workaround to force download without the block would be very helpful.
For testing purposes, I suggest you create HashMap objects and try to set Edge preferences like below.
static void Main(string[] args)
{
HashMap<String, Object> edgePrefs = new HashMap<String, Object>();
edgePrefs.put("profile.default_content_settings.popups", 0);
edgePrefs.put("profile.default_content_setting_values.notifications", 2);
edgePrefs.put("profile.default_content_setting_values.automatic_downloads" , 1);
edgePrefs.put("profile.content_settings.pattern_pairs.*,*.multiple-automatic-downloads",1);
var options = new EdgeOptions();
egdeOptions.setExperimentalOption("prefs",edgePrefs);
options.UseChromium = true;
options.BinaryLocation = #"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe"; // Here add the Edge browser exe path.
var driver = new EdgeDriver(#"Edge_driver_path_here...", options); // Here modify the selenium web driver path.
driver.Navigate().GoToUrl("http://website_URL_here...");
driver.FindElementById("lnk1").Click();
}
If the issue persists then I suggest you set the default download directory like below.
options.AddUserProfilePreference("download.default_directory", #"D://Folder1");
See whether it help to download the file.
I was trying to find the answer with the usage of W3C WebDriver Protocol, but it seems like the only possible way is to turn off XML Edge SmartScreen policies using Windows Registry:
public void SetEdgeXmlDownloadPolicy()
{
var keyName = #"HKEY_CURRENT_USER\Software\Policies\Microsoft\Edge\ExemptDomainFileTypePairsFromFileTypeDownloadWarnings";
var valueName = "AllowXmlDownload";
var valueData = #"{""domains"": ["" * ""], ""file_extension"": ""xml""}";
var currentValue = Registry.GetValue(keyName, valueName, string.Empty).ToString();
if (currentValue == string.Empty || !currentValue.Contains("xml"))
Registry.SetValue(keyName, valueName, valueData, RegistryValueKind.String);
}
Credits to this workaround goes to IndianCodeMaster
So I have a test that was written, and the test runs and works using the Selenium ChromeDriver just fine. I was tasked with getting the tests working on the FirefoxDriver as well.
Part of the test we input text into a login field, and then check the login field to make sure it was input. The way we check the field is like this
public virtual string Text => WebElement.GetAttribute("value");
while (!Text.Equals(inputText) && count++ < 3)
This works perfectly fine in Chrome. However it does not in Firefox. When I debug the test, it shows that Text is "" or empty/blank. If I open Firefox, I can do this document.getElementById("login").value and it returns the correct value.
Is WebElement.GetAttribute implemented differently in the FirefoxDriver or am I just missing something?
It's hard to say in your case why is not working on Firefox, there is no different implementation between browsers. You can try alternate solution using IJavascriptExecutor instead as below :-
IJavaScriptExecutor js = driver as IJavaScriptExecutor;
string Text = (string)js.ExecuteScript("return arguments[0].value", WebElement);
The Selenium protocol to get an attribute/property has evolved with Selenium 3.
With Selenium 2, the method WebElement.GetAttribute(...) returns the HTMLElement property when present and the attribute otherwise.
With Selenium 3, there's a distinctive command to get the property and one for the attribute :
https://www.w3.org/TR/webdriver/#get-element-property
In your case it seems that you are using the geckodriver (Selenium 3) with a client limited to the Selenium 2 specs. It would explain why the property is not returned.
To make it work, you can either upgrade your C# client to V3.0.0-beta2:
https://github.com/SeleniumHQ/selenium/blob/master/dotnet/CHANGELOG
https://github.com/SeleniumHQ/selenium/commit/a573338f7f575ccb0069575700f2c059dc94c3b8
Or you can implement your own GetProperty in a method extension:
static string GetProperty(this IWebElement element, string property) {
var driver = (RemoteWebDriver)((RemoteWebElement)element).WrappedDriver;
var result = (IList)driver.ExecuteScript(#"
var element = arguments[0], property = arguments[1];
if (property in element) return [true, '' + element[property]];
return [false, 'Missing property: ' + property];
", element, property);
bool succeed = (bool)result[0];
if (!succeed) throw new WebDriverException((string)result[1]);
return (string)result[1];
}
Usage:
string value = driver.FindElement(...).GetProperty("value");
I installed RFT 8.5 and JRE 7. When I run the scripts it's not finding browser object.
Below is the code which I have used in RFt to find the brwoser object.
Dim Allobjects() as TestObeject
Allobjects=RootTestObject.GetRootTestObject.Find(".class","Html.HtmlBrowser"))
Here it's is returning Allbects.lenth=0. Because of the I am getting struck.
Anybody can help me how to resolve this issue.
Note: I am using IE8
I was not able to find the browsers using RootTestObject either. But it is possible to find the browser windows using the Html domains:
startApp("Google");
startApp("asdf");
sleep(5);
DomainTestObject[] dtos = getDomains();
List<DomainTestObject> htmlDomains = new ArrayList<DomainTestObject>();
for (DomainTestObject dto : dtos) {
if (dto.getName().equals("Html")) {
htmlDomains.add(dto);
}
}
List<BrowserTestObject> browsers = new ArrayList<BrowserTestObject>();
for (DomainTestObject htmlDomain : htmlDomains) {
TestObject[] tos = htmlDomain.getTopObjects();
for (TestObject to : tos) {
if (to.getProperty(".class").equals("Html.HtmlBrowser")) {
browsers.add((BrowserTestObject) to);
}
}
}
System.out.println("Found " + browsers.size() + " browsers:");
for (BrowserTestObject browser : browsers) {
System.out.println(browser.getProperty(".documentName"));
}
Output:
Found 2 browsers:
https://www.google.ch/
http://www.asdf.com/
First, I start 2 browsers. Then I get all Html domain test objects. After that, I get all top objects and check whether their class is Html.HtmlBrowser.
I hope there is a simpler solution—looking forward to seeing one :)
Try the below code Snippet:
Dim Allobjects() As TestObject
Allobjects = Find(AtDescendant(".class", "Html.HtmlBrowser"))
Hope it helps.
Browser is a toplevel window so what you can do is :
Dim Allobjects() as TestObeject
Allobjects=Find(AtChild(".class","Html.HtmlBrowser"))
'The above code expects the browser to be statically enabled , also RootTestObject is not needed as implicitly RFT will use the RootTestObject if no anchor is provided.
Also if the browser is not statically enabled then you could also use:
DynamicEnabler.HookBrowsers() API so that browsers get enabled.
I opened a browser(any) with selenium tool and applying proxy to that browser by this below posted code, below is for Firefox
//LINE 1 FirefoxProfile profile = new FirefoxProfile();
//LINE 2 profile.setPreference("network.proxy.http", configuration
.getProxyConfiguration().getHostname());
//LINE 3 profile.setPreference("network.proxy.http_port", configuration
.getProxyConfiguration().getPort());
//LINE 4 profile.setPreference("network.proxy.type", configuration
.getProxyConfiguration().getType().toInt());
//LINE 5 return new FirefoxDriver(profile);
Now, I want to apply another proxy configuration for the same browser(Because, If I use another browser, session will be get change, So.... I want to apply my changes to that browser itself). How to apply my proxy configuration to the same browser. When I use same code I've to return driver which uses "NEW". I showed in my code(//LINE 5). Please help me to solve this issue.
Thanks:
Ramakrishna K.C
You can create Proxy with Kind = ProxyKind.System
new Proxy { Kind = ProxyKind.System};
Then update the internet settings in the registry
var proxyServer = string.Format("http={0};https={0}", ipAddressAndPort);
var proxyEnable = enableProxy ? 1 : 0;
const string subKeyPath = #"Software\Microsoft\Windows\CurrentVersion\Internet Settings";
using (var subKey = Registry.CurrentUser.CreateSubKey(subKeyPath))
{
if (subKey == null)
{
throw new Exception(string.Format("Failed to create or open subKey. SubKeyPath: {0} ", subKeyPath));
}
subKey.SetValue("ProxyServer", proxyServer, RegistryValueKind.String);
subKey.SetValue("ProxyEnable", proxyEnable, RegistryValueKind.DWord);
}
otherwise use PAC file in firefox profile.
http://findproxyforurl.com/