Additional chrome options for headless mode with Kameleo - selenium

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.

Related

How to run Selenium test using Edge headless

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

How to disable SmartScreen (safebrowsing in edge) on C# Selenium Edge Chromium?

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

How to add flags to webdrive using JS?

I want to add this attribute
chrome://flags/#mark-non-secure-as
Part of the webdriver
I saw that I can add with
var capabilities = webdriver.Capabilities.chrome();
Is it the right way ?
If yes which attribute do I need to add this attribute
https://sites.google.com/a/chromium.org/chromedriver/capabilities
The value that I want to pass is Always mark Http as neural , Does it have any constant ?
var TestConfiguration = require("./globalConfiguration"),
webdriver = require('selenium-webdriver'),
proxy = require('selenium-webdriver/proxy');
module.exports = {
createDriver: function () {
var capabilities = webdriver.Capabilities.chrome();
capabilities.chromeOptions = {
args: ['mark-non-secure-as=NEUTRAL']
};
var manualProxy = TestConfiguration.getParam(TestConfiguration.KEYS.PROXY);
var proxyToSet = proxy.system();
if (manualProxy) {
proxyToSet = proxy.manual({http: manualProxy, https: manualProxy});
}
var driver = new webdriver.Builder().withCapabilities(capabilities).
usingServer(TestConfiguration.getParam(TestConfiguration.KEYS.SELENIUM_HOST))
.setProxy(proxyToSet)
.build();
return driver;
}
};
You did not specify whether you are using C# or Java version of the selenium driver but the code is basically the same either way. The c# code below opens chrome with the attribute 'chrome://flags/#mark-non-secure-as' set.
You just need to create a ChromeOptions object set the desired options and pass it to the driver constructor.
https://sites.google.com/a/chromium.org/chromedriver/capabilities
ChromeOptions options = new ChromeOptions();
options.AddArgument("--mark-non-secure-as");
IWebDriver driver = new ChromeDriver(options);
I used version 3.3 of Webdriver.dll and the Chrome driver from: https://sites.google.com/a/chromium.org/chromedriver/getting-started

selenium proxy configuration multiple times in same browser

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/

how would I run Selenium test through the GRID using C#

i have just set-up the selenium grid on my local machine and everything seems to be up and running.
my question is, is there a way I can run the test case from selenium grid node (command prompt)?
I am using WebDriver for creating my testcase using .Net
Sample code from here
using OpenQA.Selenium;
using OpenQA.Selenium.Firefox;
// Requires reference to WebDriver.Support.dll
using OpenQA.Selenium.Support.UI;
class GoogleSuggest
{
static void Main(string[] args)
{
// Create a new instance of the Firefox driver.
// Notice that the remainder of the code relies on the interface,
// not the implementation.
// Further note that other drivers (InternetExplorerDriver,
// ChromeDriver, etc.) will require further configuration
// before this example will work. See the wiki pages for the
// individual drivers at http://code.google.com/p/selenium/wiki
// for further information.
WebDriver driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"),
DesiredCapabilities.FirefoxDriver());
//Notice navigation is slightly different than the Java version
//This is because 'get' is a keyword in C#
driver.Navigate().GoToUrl("http://www.google.com/");
// Find the text input element by its name
IWebElement query = driver.FindElement(By.Name("q"));
// Enter something to search for
query.SendKeys("Cheese");
// Now submit the form. WebDriver will find the form for us from the element
query.Submit();
// Google's search is rendered dynamically with JavaScript.
// Wait for the page to load, timeout after 10 seconds
WebDriverWait wait = new WebDriverWait(driver, TimeSpan.FromSeconds(10));
wait.Until((d) => { return d.Title.ToLower().StartsWith("cheese"); });
// Should see: "Cheese - Google Search"
System.Console.WriteLine("Page title is: " + driver.Title);
//Close the browser
driver.Quit();
}
}
WebDriver driver = new RemoteWebDriver(new Uri("http://127.0.0.1:4444/wd/hub"),
DesiredCapabilities.FirefoxDriver());
OR in c#
IWebDriver driver;
DesiredCapabilities capability = new DesiredCapabilities();
driver = new RemoteWebDriver(
new Uri("http://hub-cloud.com/wd/hub/"), capability);
driver.Navigate().GoToUrl("http://www.google.com");