Set UserAgent when running Selenium mobile tests with SafariDriver - selenium

I want to be able to set UserAgent when running Selenium mobile tests on Safari but I can't figure out how I do that. When I run the same tests on Chrome I can define the UserAgent in my desiredCapabilities like this:
var options = {
desiredCapabilities: {
browserName: 'chrome',
chromeOptions: {
args: [
'use-mobile-user-agent',
'user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OSX) ...'
]
}
}
}
Guess there must be a similar way to do it when running it on Safari. I'm using webdriverio to setup my Selenium project but don't think that should matter.
Thanks.

This is not supported by SafariDriver unfortunately. Hope we will see this in the future.

Related

protractor/selenium started ignoring the chrome options (chromeOptions) in protractor.conf.js

I just rebuilt our development environment. This forced the latest version of chrome and suddenly the protractor based tests stopped working. After some research, I tracked this down to the fact that our chrome options in the protractor configuration file are now ignored.
Here is the configuration that used to work fine before:
exports.config = {
...
multiCapabilities : [
{
browserName: "chrome",
chromeOptions: {
args: [
"--disable-gpu",
"--headless",
'--no-sandbox',
"--incognito"
]
}
},
},
],
...
};
Before After
protractor 6.0.0 6.0.0
selenium 4.0.0-alpha-1 4.0.0-alpha-1
chrome 74.0.3729.157 76.0.3809.100
chromedriver 74.0.3729.6 76.0.3809.68
In recent versions of selenium you have to specify the chrome options as goog:chromeOptions. So just change the chromeOptions line above to the following:
...
"goog:chromeOptions": {
...
and you are back in business.
"chromeOptions": {}
its still usable in most cases, but does not support new arguments provided by chrome
suggested to use
"goog:chromeOptions": {}
Ref to doc: http://chromedriver.chromium.org/capabilities
Same rule for firefox
"moz:firefoxOptions": {}
Ref to doc: https://developer.mozilla.org/en-US/docs/Web/WebDriver/Capabilities/firefoxOptions

Appium/Protractor - Cordova app - When I try to run simple test I get following error - Failed to get sockets matching: #webview_devtools_remote_

I'm trying to run a simple test on my hybrid app with Appium + Protractor and I am unable to since I am getting following error:
Failed to get sockets matching: #webview_devtools_remote_.*15239
I am using Ubuntu, and on it I have set up Appium and Protractor, tried literally every solution I have found on the internet, could not resolve the issue.
Only thing that would "remove" the error is adding following code into capabilities:
chromeOptions: {
androidPackage: "com.android.chrome"
},
But then I only get in the app, and Appium server just gets stuck at:
[debug] [JSONWP Proxy] Proxying [POST /session] to [POST http://127.0.0.1:8001/wd/hub/session] with body: {"desiredCapabilities":{"chromeOption {"androidPackage":"com.android.chrome","androidUseRunningApp":true,"androidDeviceSerial":"1cdc4ed10c027ece"}}}
It won't start the spec file at all.
var SpecReporter = require('jasmine-spec-reporter').SpecReporter;
exports.config = {
seleniumAddress: 'http://localhost:4723/wd/hub',
allScriptsTimeout: 50976,
specs: [
'test.js'
],
capabilities: {
platformName: 'Android',
platformVersion: '8.0.0',
deviceName: 'Galaxy S9',
app: 'path_to_app',
autoWebview: true,
browserName: '',
appPackage: 'app_package_name',
newCommandTimeout: '140',
chromeOptions: {
androidPackage: "com.android.chrome"
}
},
onPrepare: function () {
jasmine.getEnv().addReporter(new SpecReporter({displayStacktrace: 'all'}));
},
framework: 'jasmine',
jasmineNodeOpts: {
print: function () {}, //remove protractor dot reporter
defaultTimeoutInterval: 100000
}
}
It seems to be a common appium problem.
Issue from github: https://github.com/appium/python-client/issues/255
Fix:
See here: https://github.com/blutter/appium-crosswalk-fix
This also happens if you want to switch to a web browser on the mobile phone, while you have already instanciated an appium on a mobile app.
To avoid having this exception, such as described in
https://github.com/appium/appium/issues/11189, [Chromedriver] Error: Failed to start Chromedriver session: An unknown server-side error occurred while processing the command. (Original error: unknown error: Failed to get sockets matching: #webview_devtools_remote_.*24811 ,
before trying to switch to your web browser on the mobile phone just, reinstantiate the appium forcefully to web like below. Appium will pilot the appp then be forced and
switch cleanly to the web browser without complaining anymore :
DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
desiredCapabilities.setCapability(MobileCapabilityType.BROWSER_NAME, "Chrome"); // or other browsers
desiredCapabilities.setCapability("noReset", true );
try
{
URL url = new URL("http://127.0.0.1:4723/wd/hub");
AppiumDriver driver = new AppiumDriver(url, desiredCapabilities);
}
catch (Exception e )
{
}
I got rid of a similar error after changing the default browser of the mobie device to Chrome from Samsung. Also updated the chromedriver version on laptop running appium server to match the version of Chrome version running on mobile. And appium was started with appium --chromedriver-executable=/usr/local/bin/chromedriver.
Also had setWebContentsDebuggingEnabled=true in desired_capabilities.

wdio chrome headless is not running headlessly

Headless chrome doesn't seem to be headless for me. I'm using wdio and have this as my configuration:
capabilities: [
{
// maxInstances can get overwritten per capability. So if you have an in-house Selenium
// grid with only 5 firefox instances available you can make sure that not more than
// 5 instances get started at a time.
maxInstances: 5,
//
browserName: 'chrome',
args: ['--headless', '--disable-gpu', '--window-size=1280,800'],
binary: '/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome'
}
]
I'm also outputting what the capabilities are prior to the browser starting:
{
"maxInstances": 5,
"browserName": "chrome",
"args": [
"--headless",
"--disable-gpu",
"--window-size=1280,800"
],
"binary": "/Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome"
}
My chrome browser is launching and I can see webdriver driving the test. Everything I have many posts doing it in this manner and it's supposed to just work. What am I missing?
UPDATE
I've modified capabilities to be read in from an environment variable. If I use BROWSER=chrome, I see the proper capabilities go through and the browser starts in chrome. If I use BROWSER=firefox, firefox opens up and I see the proper capabilities. If I don't use anything I see the proper capabilities, chrome opens up, but it's not headless.
const CHROME = {
browserName: 'chrome',
};
const FIREFOX = {
browserName: 'firefox',
};
const CHROME_HEADLESS = {
browserName: 'chrome',
args: ['headless', 'disable-gpu']
};
function getCapabilities() {
let browser;
switch(process.env.BROWSER && process.env.BROWSER.toLowerCase()) {
case 'chrome':
browser = CHROME;
break;
case 'firefox':
browser = FIREFOX;
break;
default:
browser = CHROME_HEADLESS;
break;
}
return [Object.assign({maxInstances: 5}, browser)];
}
To go along with the accepted answer, in newer version of Selenium (3.8 and higher) you may have to specify chromeOptions as "goog:chromeOptions"
https://gist.github.com/disintegrator/ff6e9341860e9b121099c71bc9381bd6
Have the capabilities inside your chrome options.
It works fine for me.
capabilities: [
{
      browserName: 'chrome',
      chromeOptions: {
        args: ['headless', 'disable-gpu'],
      },
    },
  ],

Protractor (5.2.2) - element.all(by.css('')).click() stopped working suddenly from Dec 17th

We are using Protractor (5.2.2), chrome based functional testing. Please see the configuration information.
seleniumAddress: 'http://127.0.0.1:4444/wd/hub',
getPageTimeout: 1000,
allScriptsTimeout: 3000,
framework: 'custom',
verbose:'true',
frameworkPath: require.resolve('../../node_modules/protractor-cucumber-framework'),
ignoreUncaughtExceptions: true,
capabilities: {
'browserName': 'chrome',
'chromeOptions': {
'args': ['--disable-web-security'],
'mobileEmulation': {
'deviceName': 'Nexus 6'
}
}
}
Our test scripts where working from past 4 months without any issue, suddenly from Dec 17th all the element.all(by.css('')).click() stopped working. When it tries to execute this method after some time I could see browser right-click menu instead of element click.
Please help me in resolving this issue.
I did a work around by pointing the chromeOptions to user-agent instead of mobileEmulation, the test script worked fine.
capabilities: {
'browserName': 'chrome',
'chromeOptions': {
'args': ['user-agent=Mozilla/5.0 (iPhone; CPU iPhone OS 8_0_2 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A405 Safari/600.1.4']
}
},
and set the browser size - browser.manage().window().setSize(375, 667);

How to change browser language of webdriverio

I would like to change browser language. but it is not working. default browser language is displayed..
capabilities: [{
browserName: 'chrome',
chromeOptions: {
args: ['--lang=ja']
}
}],
If anyone is still interested in making this work, the WebdriverIO implementation would be:
capabilities: [{
browserName: 'chrome',
'goog:chromeOptions': {
args: [ '--your-args-go-here',
'--like-so',
'--and-so-and-so'
// e.g: '--headless', '--disable-gpu', '--start-fullscreen'
],
prefs: {
'intl.accept_languages': 'ru,RU'
}
}
}]
For the full list of Chromium switches (args array values), click here.
For the full list of Chromium preferences (prefs object properties), click here.
Note: another useful resource (which is always up to date) for Chromium switches is Peter Beverloo's Chromium CLI Switches portal.
Using the above Chrome config in the wdio.conf.js & running an Instagram login test will successfully convert the locale of the page to Russian, as seen below:
could you try this instead?
options = webdriver.ChromeOptions()
options.add_experimental_option('prefs', {'intl.accept_languages': 'en,en_US'})
driver = webdriver.Chrome(chrome_options=options)
check on webdriver io how to use add_experimental_option