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"
Related
I am using Selenium Firefox Options Preferences to avoid pop ups for permissions such as microphone, video. But I want to avoid the pop up for screen sharing too. This is the code where i am setting the Firefox Options:
FirefoxOptions firefoxOptions = new FirefoxOptions();
firefoxOptions.SetPreference("permissions.default.microphone", 1);
firefoxOptions.SetPreference("permissions.default.camera", 1);
So, as i mentioned before, the question is, what would be the preference to allow screen sharing of entire screen in Firefox Web Driver?
i think you need this:
firefoxOptions.SetPreference("media.navigator.permission.disabled", true);
I'm using TFS 2015 to trigger Selenium automated test suite. Few of test cases are failing due to some HTML elements get overlapped and this happens due to the screen resolution of the session which TFS service account opens is smaller (Programmatically I took screenshots while running the test methods in TFS). All the test cases are passing when I run from Visual Studio or through CMD locally or Remote Desktop session in TFS server.
I tried increasing the screen resolution using bellow techniques , but still screenshots saved in TFS server shows smaller screen size.
Increase the window size :
driver.Manage().Window.Size = new Size(x, y);
Chrome capabilities - Resolution
DesiredCapabilities caps = new DesiredCapabilities();
caps.SetCapability("resolution", "1920x1080");
Chrome capabilities - Window Size
ChromeOptions chromeOptions = new ChromeOptions();
chromeOptions.AddArgument("--window-size=1300,1000");
Maximize the window
driver.manage.window.maximize();
None of these approaches works since the screen resolution of the session which TFS service account opens is smaller.
When I run the test cases with Headless option, browser windows opens with Maximum window size but test fails since selenium can't identify the elements (NoSuchElementException) :
var chromeOptions = new ChromeOptions();
chromeOptions.AddArguments("headless");
(Project which I'm automating is a ReactJS project)
I would like to know a way to increase the screen size when test are being executed form TFS agent.
Thank You
chromeOptions.addArgument("--headless");
chromeOptions.addArgument("--disable-gpu");
chromeOptions.addArgument("--window-size=1920,1080")
WebDriver driver = new ChromeDriver(chromeOptions);
don not use browser.maximize after this it will reset the windows size to system default.
Try also:
chromeOptions.addArguments("--headless","--disable-gpu","--window-size=1920,1080")
WebDriver driver = new ChromeDriver(chromeOptions);
Debugging tips
See if the test are passing with same window size but non headless mode
If it does pass then it is because of faster execution speed , headless browser is faster than non headless browser so you need to add more explicit waits for those elements
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.
Host machine: Windows 10 with VS 2017, Selenium (.NET) & SpecFlow+
I've got a Mojave MacOS with Safari v12 on the network that I need to run my test scripts on.
I'm running Selenium C# scripts on it using RemoteWebDriver but they are failing because v12 uses the latest W3C protocols.
SafariDriver can be started using the "--legacy" switch.
SafariDriverService has a "UseLegacyProtocol" but can't be passed in RemoteWebDriver (example below).
Is there a way to activate the switch by:
a) Passing it through RemoteWebDriver?
b) Merging it as a capability with the options and passing it through RemoteWebDriver?
c) Configuring the switch in a json file for use with Selenium Grid v3?
This is to work with C# code.
Code examples I already have:
var sOptions = new SafariOptions();
sOptions.Proxy = null;
var sService = SafariDriverService.CreateDefaultService();
sService.Port = xxxx;
sService.UseLegacyProtocol = true;
Browser = new SafariDriver(sService, sOptions,
TimeSpan.FromSeconds(PageTimeout));
var rOptions = new SafariOptions();
Browser = new RemoteWebDriver(new Uri("http://xx.xxx.xx.xx:xxxx/wd/hub"), rOptions);
Thanks
Legacy protocol is no longer supported in shipping versions of Safari/safaridriver, so I think this question can be closed.
You can use the following code to merge the capabilities into the SafariOptions:
SafariOptions options = new SafariOptions();
options.merge(capabilities);
But there is still a bug, Safaridriver can't handle Proxy, it is still an open ticket in there backlog.
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());