In my project there was a requirement to automate few test cases that would validate functionality of the website on Safari browser on Mac machine. But automation framework supports only Windows machines.
To support the new requirement, I leveraged 'user-agent' feature of the browser to simulate safari browser on Windows client machine. The intention of testing here was not to validate the layout or look and feel of the website but rather validating features such as presence or absence of a button based on the OS on which we are browsing.
User-agent is basically a string that represents the browser engine, browser version, OS and OS version etc.
Browser will send user-agent header with every http request to server. Most of the websites depend on user-agent string to find out from which OS and Browser the request is coming and then change the business logic accordingly or even redirect to different website.
For example, when browsing on mobile you will observe facebook.com will automatically redirect to m.facebook.com. So to simulate any browser, user-agent should be altered accordingly before making request. There are many add-ons like this one for firefox and for chrome available to do that.
Switching user-agent can be achieved in Automation without using any add-ons. WebDriver allows setting user-agent preference before invoking the browser. Once it is set, the browser will send altered user-agent string to the website for every subsequent request and essentially mimicking the browser we wanted to test on.
Currently, I have tested it with Firefox and Chrome browsers and it is working fine. Here is the sample code to switch user-agent for different OS such as MAC, Linux, Android and iOS:
public void OpenFirefox()
{
string osType = "MAC";
FirefoxProfile _ffProfile = new FirefoxProfile();
_ffProfile.SetPreference("general.useragent.override", GetUserAgent(osType));
IWebDriver _driver = new FirefoxDriver(_ffProfile);
}
public void OpenChrome()
{
string osType = "MAC";
var options = new ChromeOptions();
options.AddArgument("--user-agent=" + GetUserAgent(osType));
IWebDriver _driver = new ChromeDriver(options);
}
public string GetUserAgent(string osType)
{
switch (osType.ToUpperInvariant())
{
case "MAC":
return "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25";
case "LINUX":
return "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:20.0) Gecko/20100101 Firefox/20.0";
case "ANDROID":
return "Mozilla/5.0 (Linux; Android 4.1.1; Galaxy Nexus Build/JRO03C) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19";
case "IOS":
return "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3";
default:
return "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31";
}
}
To support the new requirement, I leveraged 'user-agent' feature of the browser to simulate safari browser on Windows client machine. The intention of testing here was not to validate the layout or look and feel of the website but rather validating features such as presence or absence of a button based on the OS on which we are browsing.
User-agent is basically a string that represents the browser engine, browser version, OS and OS version etc.
Browser will send user-agent header with every http request to server. Most of the websites depend on user-agent string to find out from which OS and Browser the request is coming and then change the business logic accordingly or even redirect to different website.
For example, when browsing on mobile you will observe facebook.com will automatically redirect to m.facebook.com. So to simulate any browser, user-agent should be altered accordingly before making request. There are many add-ons like this one for firefox and for chrome available to do that.
Switching user-agent can be achieved in Automation without using any add-ons. WebDriver allows setting user-agent preference before invoking the browser. Once it is set, the browser will send altered user-agent string to the website for every subsequent request and essentially mimicking the browser we wanted to test on.
Currently, I have tested it with Firefox and Chrome browsers and it is working fine. Here is the sample code to switch user-agent for different OS such as MAC, Linux, Android and iOS:
public void OpenFirefox()
{
string osType = "MAC";
FirefoxProfile _ffProfile = new FirefoxProfile();
_ffProfile.SetPreference("general.useragent.override", GetUserAgent(osType));
IWebDriver _driver = new FirefoxDriver(_ffProfile);
}
public void OpenChrome()
{
string osType = "MAC";
var options = new ChromeOptions();
options.AddArgument("--user-agent=" + GetUserAgent(osType));
IWebDriver _driver = new ChromeDriver(options);
}
public string GetUserAgent(string osType)
{
switch (osType.ToUpperInvariant())
{
case "MAC":
return "Mozilla/5.0 (iPad; CPU OS 6_0 like Mac OS X) AppleWebKit/536.26 (KHTML, like Gecko) Version/6.0 Mobile/10A5355d Safari/8536.25";
case "LINUX":
return "Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:20.0) Gecko/20100101 Firefox/20.0";
case "ANDROID":
return "Mozilla/5.0 (Linux; Android 4.1.1; Galaxy Nexus Build/JRO03C) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19";
case "IOS":
return "Mozilla/5.0 (iPhone; CPU iPhone OS 5_0 like Mac OS X) AppleWebKit/534.46 (KHTML, like Gecko) Version/5.1 Mobile/9A334 Safari/7534.48.3";
default:
return "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.64 Safari/537.31";
}
}