Configuration of mobile user agent with webdriver

I this posts you will learn how to execute webdriver test script on browser using mobile user agent.

Installation:
1. Install user agent in Firfox from link: User Agent
2. Install user agent in Google chrome from link: User Agent
User Agent String: 
Firefox:
  • Go to “Tools>>Default User Agent>>Edit User Agents..”
  • Select any user agent from list and click on Edit button like below screen I selected “iPhone 3.0”
  • Copy string from User Agent in opened popup window As “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”
Google Chrome:
  • Go to “chrome://extensions/”
  • Click on “option” link of user agent, as below screen.
  • You should get user agent string from opened window as ” Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5”
Script setup:
private WebDriver driver;
 
@Test
public void userAgentFirefox(){
 ProfilesIni profile = new ProfilesIni();
 FirefoxProfile profile = profile.getProfile("default");
   profile.setPreference("general.useragent.override", "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");  
 driver = new FirefoxDriver(profile);
 driver.get("http://www.google.com");
 driver.quit();
}
 
@Test
public void userAgentChrome(){
     System.setProperty("webdriver.chrome.driver","chromedriver.exe");
     ChromeOptions cOptions = new ChromeOptions();
     cOptions.addArguments("user-data-dir=C:/Users/{UserName}/AppData/Local/Google/Chrome/User Data");  
     cOptions.addArguments("–user-agent=Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_3_2 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8H7 Safari/6533.18.5");//iPhone 4
  
     driver = new ChromeDriver(cOptions);
     driver.get("http://www.google.com");
     driver.quit();
}

No comments:

Post a Comment

Leave your comments, queries, suggestion I will try to provide solution