Reputation: 73
I am trying to launch chrome in mobile emulation mode and also wanted to set user agent before launch happens. I have found ways to do this one at a time but unable to do it both for same instance of chrome. Following is my code:
String useragent="Mozilla/5.0 (Windows NT 6.1\\; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36 WFBTesting";
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Google Nexus 5");
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("mobileEmulation", mobileEmulation);
ChromeOptions options = new ChromeOptions();
options.addArguments("--user-agent="+useragent);
options.addArguments("--test-type");
options.addArguments("--allow-running-insecure-content");
DesiredCapabilities capabilities = DesiredCapabilities.chrome();
//capabilities.setCapability(ChromeOptions.CAPABILITY,(chromeOptions));
capabilities.setCapability(ChromeOptions.CAPABILITY,options);
WebDriver driver = new ChromeDriver(capabilities);
In above code I can either pass options or chromeOptions in "capabilities.setCapability(ChromeOptions.CAPABILITY,options);" but if write two setCapability methods one with options and other with chromeOptions, only one of them is in effect for the launched chrome session and not both. Kindly suggest the best way to set both the capabilities and launch the chrome session.
EDIT As suggested I tried following: Thanks @Cyildirim for your response again. I used following code for my script this time:
Map<String, String> mobileEmulation = new HashMap<String, String>();
mobileEmulation.put("deviceName", "Google Nexus 5");
Map<String, Object> chromeOptions = new HashMap<String, Object>();
chromeOptions.put("args", Arrays.asList("user-agent=Mozilla/5.0 (Windows NT 6.1\\; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.59 Safari/537.36 WFBTesting","test-type","allow-running-insecure-content"));
chromeOptions.put("mobileEmulation", mobileEmulation);DesiredCapabilities capabilities = DesiredCapabilities.chrome();
capabilities.setCapability(ChromeOptions.CAPABILITY,(chromeOptions));
But even using above I cannot get the user profile to set correctly. Only mobile emulation capability is getting set. Can you correct me if I am writing useragent incorrectly in Array? Or any other suggestion to make it work.
Upvotes: 1
Views: 1920
Reputation: 641
Did you try set both of them ?
capabilities.setCapability(ChromeOptions.CAPABILITY,chromeOptions);
capabilities.setCapability(ChromeOptions.CAPABILITY,options);
Upvotes: 1