Open website with selenium as an Android device - c#

I want to make a program with opens a website "in mobile view", I don't really know how to call it, but I want that the website thinks I'm using an android phone.
I have tried it using ChromeOptions and changing the user-agent, but somehow it doesn't work.
ChromeOptions options = new ChromeOptions();
options.AddArgument("user-agent=Mozilla/5.0 (Linux; Android 8.1.0; Phone) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.90 Mobile Safari/537.36");
driver = new ChromeDriver("./", options);

This site has detailed documentation of using Mobile Emulation.
Moreover, after ChromeDriver v2.11 has mobileEmulation option.
For C#
Use something like this,
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.EnableMobileEmulation("Pixel 2");
chromeOptions.AddUserProfilePreference("safebrowsing.enabled", true);
chromeOptions.AddUserProfilePreference("credentials_enable_service", false);
chromeOptions.AddUserProfilePreference("profile.password_manager_enabled", false);
ChromeDriverService service = ChromeDriverService.CreateDefaultService(#"C:\chromedriver");
IWebDriver driver = new ChromeDriver(service, chromeOptions);
You can enter the device required like iPhone X, iPad Pro, Samsung s7, etc..
Also remember that,
EnableMobileEmulation("deviceName");
deviceName:
The name of the device to emulate. The device name must be a valid device name
from the Chrome DevTools Emulation panel.
Note: specifying an invalid device name will not throw an exception, but will generate
an error in Chrome when the driver starts. To unset mobile emulation, call this
method with null as the argument.

Related

Is there a way to open HTTP web in Edge using selenium and iedriver?

So i'm using this code but if the web is HTTP it opens on IE instead of Edge.
var ieOptions = new InternetExplorerOptions();
ieOptions.EdgeExecutablePath = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe";
IWebDriver driver = new InternetExplorerDriver(ieOptions);
driver.Url = "some http web";
is there a way to force it on edge?
You need to download Microsoft Edge Driver and use that. The name of the class your code currently uses should give you a hint about why Internet Explorer is being opened:
IWebDriver driver = new InternetExplorerDriver(ieOptions);
^^^^^^^^^^^^^^^^
It is right in the name: InternetExplorerDriver. You are using the web driver for Internet Explorer. If you want to automate Edge, you need to use EdgeDriver.
I think the curious thing is that Edge is launched when loading an HTTPS URL when using InternetExplorerDriver. I suspect there are Windows Policies installed that override Internet Explorer causing Edge to be launched instead.
Create InternetExplorerDriver and pass the InternetExplorerOptions:
var options = new InternetExplorerOptions
{
AttachToEdgeChrome = true,
EdgeExecutablePath = "C:/Program Files (x86)/Microsoft/Edge/Application/msedge.exe"
};
var driver = new InternetExplorerDriver(options);
driver.Url = "https://example.com";
This will open Edge in IE mode...
If you don't need IE mode you need to use EdgeDriver instead of InternetExplorerDriver and EdgeOption instead of InternetExplorerOptions

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

Why won't chrome option arguments work in remote web driver? (Selenium 3.11.0)

I have recently ugpraded to Chrome 65, ChromeDriver 2.37, and Selenium 3.11.0. I am trying to pass in chrome options into our remote web driver for our grid runs so that chrome starts maximized, but I can see from screenshots that the window is NOT getting maximized. Code looks like this:
var options = new ChromeOptions();
options.AddArguments("--start-maximized");
DriverThread.Value = new RemoteWebDriver(new Uri(remoteAddress), options);
We used to have to convert the chrome options to capabilities first but now the remote web driver takes in driver options as a parameter. Does anyone know why this isn't working? Am I doing something wrong?
Try it like
options.addArguments("start-maximized");

C# Selenium Mobile Emulation in landscape

I'm doing some mobile UI testing using Selenium in a .Net environment using c#.
I'm able to do testing quite successfully using the chrome mobile emulation in portrait mode, but I can not found how to put the emulation in Landscape mode.
I would like to be able to programmatically rotate during testing but through research it appears this is not possible yet.
Working in Portrait mode.
ChromeOptions chromeCapabilities = new ChromeOptions();
chromeCapabilities.EnableMobileEmulation("Apple iPhone 6");
ChromeDriverService service = ChromeDriverService.CreateDefaultService(#"C:\chromedriver");
IWebDriver driver = new ChromeDriver(service, chromeCapabilities);
driver.Navigate().GoToUrl("www.google.com");
However how do I put the iPhone emulation in to a landscape orientation?
I have tried this but it does't work and the browser opens without the size limitations
ChromeMobileEmulationDeviceSettings CMEDS = new ChromeMobileEmulationDeviceSettings();
CMEDS.Width = 66;
CMEDS.Height = 37;
CMEDS.PixelRatio = 1.0;
CMEDS.UserAgent = "Mozilla/5.0 (iPhone; CPU iPhone OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5376e Safari/8536.25";
ChromeOptions chromeCapabilities = new ChromeOptions();
chromeCapabilities.EnableMobileEmulation(CMEDS);
ChromeDriverService service = ChromeDriverService.CreateDefaultService(#"C:\chromedriver");
IWebDriver driver = new ChromeDriver(service, chromeCapabilities);
driver.Navigate().GoToUrl("www.google.com");
Any help or advice greatly received!!
Thanks in advance
From what I understand, it is not possible to change the screen orientation at the moment.
Here is a related open issue:
Support setting screen orientation on mobile device
There are some and few hints in the source code that got me thinking that it might be possible to have a "landscape" oriented emulated device by adding a custom brand new device (how to add a device programmatically is an open question though).
I think it is possible using
emulation = {"width": 384, "height": 700, "deviceScaleFactor": 10,
        "screenOrientation": {"type": "landscapePrimary", "angle": 0}},
driver.execute_cdp_cmd('Emulation.setDeviceMetricsOverride', emulation)
There's actually a driver for Mobile Emulation for Selenium using Python called: Selenium-Profiles
Documentation ChromeDeveloper-Protocoll (cdp_cmd)
possible screenOrientation values are "portraitPrimary", "portraitSecondary", "landscapePrimary", "landscapeSecondary"

How to use chromedriver to test Chrome on Android

in my infrastructure, I have Selenium Hub and Selenium nodes connected to this Hub. I have nodes for each desktop browser I need to test. To run a test in my grid on let's say Chrome, I start the chromedriver with the following parameters:
java -Dwebdriver.chrome.driver=C:\chromedriver.exe -jar selenium-server-standalone-2.52.0.jar -role webdriver -hub http://myseleniumhubip:4444/grid/register -browser browserName=chrome,platform=WINDOWS -port 5557
And I create my driver in the test like this:
DesiredCapabilities capability = DesiredCapabilities.Chrome();
driver = new RemoteWebDriver(new Uri("http://myseleniumhubip:4444/wd/hub"), capability);
And everything works as expected. Browser is launched on remote machine and test performs.
However, I would also like to test in Chrome on my real Android device. Problem is, I have no idea how to start chromedriver (what parameters to use), nor how to create RemoteWebDriver to accomplish this.
Could anyone please help me?
I have Android SDK installed on the machine with chromedriver
Phone is set into debugging mode
I'm using C# for my tests
Thank you!
If anyone's still struggling with this, the following approach works fine for me:
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddAdditionalCapability("androidPackage", "com.android.chrome");
driver = new RemoteWebDriver(new Uri("http://myseleniumhubip:4444/wd/hub"), chromeOptions.ToCapabilities());

Categories

Resources