Issues setting useragent with Chrome browser

530 views
Skip to first unread message

testingzeal

unread,
Jul 29, 2014, 7:17:17 PM7/29/14
to webd...@googlegroups.com
I tried this but it is not working.Please help me on this. Highlighted is the code to set user agent.


Using chrome 36
chrome driver 2.10
webdriver - 2.39

Here is what i tried - 

public static WebDriver getChromeBrowser(OperatingSystem os)
throws MalformedURLException {
DesiredCapabilities capability = null;

capability = DesiredCapabilities.chrome();
ChromeOptions options = new ChromeOptions();
options.addArguments(Arrays.asList("allow-running-insecure-content",
"ignore-certificate-errors"));
options.addArguments("test-type");
capability.setCapability("chromeOptions.args",Arrays.asList("--user-agent=Mozilla/5.0 (iPhone; U; CPU iPhone OS 3_0 like Mac OS X; en-us) AppleWebKit/528.18 (KHTML, like Gecko) Version/4.0 Mobile/7A341 Safari/528.16"));  

capability.setCapability(ChromeOptions.CAPABILITY, options);
capability.setBrowserName("chrome");
// capability.setPlatform(org.openqa.selenium.Platform.VISTA);
RunHost.runHost(capability);
Platform platform = setPlatform(os);
// OS setup
if (platform != null) {
capability.setPlatform(platform);
}
try {
RemoteWebDriver driver = new RemoteWebDriver(new URL(TestProperties
.getInstance().getProperty("hub.host", "")), capability);
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(45, TimeUnit.SECONDS);
return driver;

} catch (Exception e) {
e.printStackTrace();
}
return null;
}

testingzeal

unread,
Jul 30, 2014, 9:20:31 AM7/30/14
to webd...@googlegroups.com
To be clear ... i am not seeing any errors but the user agent is not set during script execution.
I read Browser Mob will help to solve the issue and haven't got any examples in my search

If anyone used Browser Mob, please help me.

Thanks

Krishnan Mahadevan

unread,
Jul 30, 2014, 9:25:02 AM7/30/14
to webdriver
The following has always worked for me

ChromeOptions options = new ChromeOptions();
options.addArguments("--user-agent=" + userAgent);
capabilities.setCapability(ChromeOptions.CAPABILITY, options);


Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/


--
You received this message because you are subscribed to the Google Groups "webdriver" group.
To unsubscribe from this group and stop receiving emails from it, send an email to webdriver+...@googlegroups.com.
To post to this group, send email to webd...@googlegroups.com.
Visit this group at http://groups.google.com/group/webdriver.
For more options, visit https://groups.google.com/d/optout.

testingzeal

unread,
Jul 30, 2014, 1:10:35 PM7/30/14
to webd...@googlegroups.com
Thanks Krishnan. That worked and you saved me again :)

As our company implements lots A/B testing i have this need to run my tests on a specific path when they perform this type of testing.To get the successful run i am running my tests as a bot on chrome/Firefox browsers and they report to a my sql db with test details along with browser name ,browser version,platform they ran.

As chrome and firefox browser updates automatically , i didn't set the version parameter to the capabilities and using version parameter differently.

Question is - After setting the bot user agent to chrome/firefox, Is there a way to strip browser version?Right now I am getting NULL POINTER EXCEPTION on browser version.(details below)

When there is no user agent i was getting browser name and browser version using below code -

without useragent 
+++++++++++++++
User Agent is :WINDOWS_7-CHROME
Browser is :CHROME
Version is :36.0.1985.125
+++++++++++++++++++++++++

with bot useragent
++++++++++++
User Agent is :UNKNOWN-BOT
Browser is :BOT
+++++++++++++++++++

//Code to strip browser name and browser version and this works when UA is not set to chrome/FF browsers.
public static String getBrowserVersion(WebDriver driver)
{
String userAgentString = (String) ((JavascriptExecutor) driver).executeScript("return navigator.userAgent", "");
UserAgent ua = UserAgent.parseUserAgentString(userAgentString);
System.out.println("User Agent is :"+ua);
String browser= ua.getBrowser().toString();
//String OS = ua.getOperatingSystem().toString();
if (browser.equals("IE9"))
{
browser="IE";
}
else if (browser.equals("IE8"))
{
browser="IE";
}
else if (browser.equals("IE7"))
{
browser="IE";
}
else if(browser.equals("FIREFOX2"))
{
browser="FIREFOX";
}
else
{
browser =ua.getBrowser().toString();
}
System.out.println("Browser is :"+browser);
System.out.println("Version is :"+ua.getBrowserVersion().toString());
//return ua.getOperatingSystem().toString() + "/"+ ua.getBrowser().toString()+ ua.getBrowserVersion().toString();
return browser+ " "+ua.getBrowserVersion().toString();
}
//Chrome browser setup code 

// Chrome browser setup
public static WebDriver getChromeBrowser(OperatingSystem os)
throws MalformedURLException {
DesiredCapabilities capability = null;
capability = DesiredCapabilities.chrome();
String userAgent ="Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)";
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-agent=" + userAgent);
options.addArguments(Arrays.asList("allow-running-insecure-content",
"ignore-certificate-errors"));
options.addArguments("test-type");

capability.setCapability(ChromeOptions.CAPABILITY, options);
capability.setBrowserName("Chrome");
RunHost.runHost(capability); // I am using version capability in this method and here is the code.String forceHost = System.getProperty("version");
Platform platform = setPlatform(os);
// OS setup
if (platform != null) {
capability.setPlatform(platform);
}
try {
RemoteWebDriver driver = new RemoteWebDriver(new URL(TestProperties
.getInstance().getProperty("hub.host", "")), capability);
driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(45, TimeUnit.SECONDS);
driver.manage().timeouts().setScriptTimeout(45, TimeUnit.SECONDS);
return driver;

} catch (Exception e) {
e.printStackTrace();
}
return null;
}


Thanks

Krishnan Mahadevan

unread,
Jul 30, 2014, 10:24:32 PM7/30/14
to webdriver
I am not sure as to what library you are using to parse user agents.

I use this :

<dependency>
    <groupId>nl.bitwalker</groupId>
    <artifactId>UserAgentUtils</artifactId>
    <version>1.6</version>
</dependency>

Since you already know what your user agents are going to be looking like, perhaps you might want to play around with this library to extract what values you need.





Thanks & Regards
Krishnan Mahadevan

"All the desirable things in life are either illegal, expensive, fattening or in love with someone else!"
My Scribblings @ http://wakened-cognition.blogspot.com/
My Technical Scribbings @ http://rationaleemotions.wordpress.com/


Reply all
Reply to author
Forward
0 new messages