Switching user agents

Earlier, cryptic commands and texts were used to retrieve data from the Internet, which would act as a user agent; now, web browsers are used as user agents. To put it simply, a user agent is a tool to browse the Internet by faking another browser. In general, websites are being rendered differently on different browsers with different platforms (for example, Chrome browser on Windows 7). However, the user agents cannot render a web page similar to the selected one. The user can track their own user agent string from http://whatsmyuseragent.com/.

For example, Mozilla Firefox 5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/36.0.1985.143 Safari/537.36.

Here, Mozilla/5.0 is an application name and Chrome/36.0.1985.143 is the browser type, with version. Whenever the user enters a URL in the web browser, a request will be sent to the web server to identify the user agent; it provides the relevant data as response. Selenium WebDriver has extended its features to drive tests over different user agents and execute tests from browser profiles.

Firefox user agent

Follow these steps to run tests on your favorite Firefox user agents:

  1. Launch Firefox and install User Agent Switcher, the Firefox add-on.
  2. From the Tools menu, navigate to Tools | Default User Agent | Edit User Agents and click on Edit User Agents.
  3. Select any one of the user agents from the list (for example, Googlebot 2.1) and click on the Edit button.
  4. Now, get the user agent string, as follows:
    Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)

Make use of the above user agent string to run tests through Googlebot 2.1. To run tests on a specific version of Firefox, refer to http://www.useragentstring.com/pages/Firefox/. The following code encapsulates the discussion in this paragraph:

ProfilesIni profile = new ProfilesIni();
FirefoxProfilemyprofile = 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);

Chrome user agent

Follow these steps to run tests on your favorite Chrome user agents:

  1. Launch Google Chrome and install the Chrome add-on User-Agent Switcher for Chrome.
  2. Go to chrome://extensions/ in the Chrome web browser and click on options link_text.
  3. Obtain the desired user agent string (for example, iPhone 4). The following is the stream for iPhone4:
    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

    The following screenshot shows us the Extensions page in which User-Agent Switcher for Chrome is added:

    Chrome user agent

Make use of the obtained user agent string to run the tests through the iPhone 4 user agent from Chrome, as follows:

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);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.117.11.247