What is the C# equivalent to Java ChromeOptions.setExperimentalOptions()? - selenium

I have automated android chrome browser with the below code:
DesiredCapabilities capabilities=DesiredCapabilities.chrome();
ChromeOptions options=new ChromeOptions();
options.setExperimentalOptions("androidPackage", "com.android.chrome");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver=new ChromeDriver(capabilities);
System.setProperty("webdriver.chrome.driver", "C://Users//Documents//Appium//ChromeDriver//chromedriver.exe");
String url="http://yahoo.com";
driver.get(url);
I am trying to automate android chrome browser using C# (Visual Studio) but can't find the equivalent code. I am using this but not working:
Capabilities = DesiredCapabilities.Chrome();
ChromeOptions options1=new ChromeOptions();
options1.AddAdditionalCapability("androidPackage", "com.android.chrome",);
Driver = new ChromeDriver(Chrome_Driver, options1);

I believe this is what you're looking for:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddAdditionalCapability("androidPackage", "com.android.chrome");
driver = new RemoteWebDriver(new Uri("http://seleniumhubaddress:4444/wd/hub"), chromeOptions.ToCapabilities());

Related

HtmlUnit set download location

Currently I am executing selenium script with HtmlUnit. How can I set the Download location for the zip file downloaded at the time of test script execution.
You can use ChromeDriver in headless mode. You just need to add options as headless as below:
chromeOptions.addArguments("--headless");
The full code in Java will appear as below:
System.setProperty("webdriver.chrome.driver","D:\\Workspace\\JmeterWebdriverProject\\src\\lib\\chromedriver.exe");
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.addArguments("--headless");
chromeOptions.addArguments("--start-maximized");
WebDriver driver = new ChromeDriver(chromeOptions);
driver.get("https://google.com");
You can use ChromeDriver to set download in a specific path by below code:
String downloadFilepath = "/path/to/download";
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadFilepath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
DesiredCapabilities cap = DesiredCapabilities.chrome();
cap.setCapability(CapabilityType.ACCEPT_SSL_CERTS, true);
cap.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(cap);

How to maximise the window in chrome browser in incognito mode using Selenium WebDriver?

How to maximise the window in chrome browser in incognito mode using Selenium WebDriver
I am using the below code:
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(capabilities);
driver.manage().window().maximize();
But in the last line code I am getting error as Exception in thread "main" org.openqa.selenium.WebDriverException: unknown error: No current window
To maximize the Chrome Browser in incognito mode you need to use the ChromeOptions class as follows:
ChromeOptions options = new ChromeOptions();
options.addArguments("--incognito");
options.addArguments("start-maximized");
WebDriver driver = new ChromeDriver(options);

Possible to add Chrome option and capabilities together?

Possible to add Chrome option and capabilities together?
In need to combine the following listed below together, is it even possible?
System.setProperty("webdriver.chrome.driver", Base_Page.getConstant(Constant.CHROME_DRIVER_DIRECTORY));
ChromeOptions options = new ChromeOptions();
string[] switches = {"user-data-dir=C:\\Users\\AppData\\Local\\Google\\Chrome\\User Data"};
options.addArguments("user-data-dir=C:\\Users\\AppData\\Local\\Google\\Chrome\\User Data");
options.addArguments("test-type");
options.addArguments("--start-maximized");
options.addArguments("--disable-extensions");
options.addArguments("no-sandbox");
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
options.(CapabilityType.LOGGING_PREFS, logPrefs);
DesiredCapabilities caps = DesiredCapabilities.chrome();
caps.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new ChromeDriver(caps);
DesiredCapabilities caps = DesiredCapabilities.chrome();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
caps.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
caps.setCapability("chrome.switches", switches);
//webdriver = new ChromeDriver(caps);
webdriver = new ChromeDriver(options);
Searched the same question.
The answer is: just add all your Capabilities into the "options" object and launch browser using options:
ChromeOptions options = new ChromeOptions();
LoggingPreferences logPrefs = new LoggingPreferences();
logPrefs.enable(LogType.BROWSER, Level.ALL);
options.setCapability(CapabilityType.LOGGING_PREFS, logPrefs);
webdriver = new ChromeDriver(options);
Updating to latest version of chrome driver fixed my issue:
ChromeDriver 2.28
if you observe ChromeDriver constructor in below link
http://seleniumhq.github.io/selenium/docs/api/java/org/openqa/selenium/chrome/ChromeDriver.html
It will accept capabilities or options, means any one. I hope it clears you.

How to disable web security using selenium code for IE and Firefox

Written below code for chrome to disable web security
System.setProperty("webdriver.chrome.driver", "D:\\Eclipse\\chromedriver_win32\\chromedriver.exe");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
/*options.addArguments("test-type");
options.addArgument("--start-maximized");*/
options.addArguments("--disable-web-security");
/* options.addArguments("--allow-running-insecure-content");*/
capabilities.setCapability("chrome.binary","D:/Eclipse/chromedriver_win32/chromedriver.exe");
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(options);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
driver = new ChromeDriver(options);
But I want to disable same for FF and IE.

RemoteWebDriver Chrome - start maximized

I need chrome to start maximized when running via the selenium grid.
This is how do I initialize it now:
Selenium selenium = new DefaultSelenium("localhost", 4444, "*googlechrome", "http://www.google.com");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
Chrome does come up, but not maximized. In usual ChromeDriver I did it like this
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
But I dont know how to pass it to RemoteWebDriver. Can anybody help?
ChromeOptions options = new ChromeOptions();
options.addArguments("--start-maximized");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY, options);
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
That's how I do it.
Ok, I found it, so lets answer my own question :)
Selenium selenium = new DefaultSelenium("localhost", 4444, "*googlechrome", "http://www.google.com");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability("chrome.switches", Arrays.asList("--start-maximized"));
WebDriver driver = new RemoteWebDriver(new URL("http://localhost:4444/wd/hub"), capabilities);
should work :}
The above solutions did not work for me but this did
ChromeOptions options = new ChromeOptions();
options.AddArguments("--start-maximized");
DesiredCapabilities capabilities = options.ToCapabilities() as DesiredCapabilities;
capabilities?.SetCapability(CapabilityType.BrowserName, "chrome");
Driver = new RemoteWebDriver(new Uri("http://localhost:4444/wd/hub"), capabilities);
Hope this helps someone.