Switching user-agent can be done through browser custom profile.
Firefox
1| Install the Firefox Add-on, User Agent Switcher
2| Go to Tools > Default User Agent > Edit User Agents...
3| Select any User agent from list and click Edit button
4| Get the user agent string. e.g., Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)
ProfilesIni profile = new ProfilesIni();
FirefoxProfile myprofile = profile.getProfile("default");
myprofile.setPreference("general.useragent.override", "Mozilla/5.0 (compatible; Yahoo! Slurp; http://help.yahoo.com/help/us/ysearch/slurp)"); // here, the user-agent is 'Yahoo Slurp'
WebDriver driver = new FirefoxDriver(myprofile);
Google Chrome
1| Install the Chrome Add-on, User Agent Switcher
2| Go to chrome://extensions/
3| Click 'Options' under User-Agent Switcher for Chrome
4| Get the desired ser agent string. e.g., iPhone 4
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
System.setProperty("webdriver.chrome.driver","C:\\chromedriver.exe");
ChromeOptions options = new ChromeOptions();
options.addArguments("user-data-dir=C:/Users/user_name/AppData/Local/Google/Chrome/User Data");
options.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
options.addArguments("--start-maximized");
driver = new ChromeDriver(options);
Note:-
Phantomjs: phantomjs.page.settings.userAgent