Forum Discussion

suwingben's avatar
suwingben
Contributor
13 years ago

User Agent - Switchable?

Is it possible to trigger a user agent change in TestComplete through a command in the keyword test or through a script? I know firefox could do it from within the web browser through an extentions and I'm assuming that test complete can capture me clicking buttons within the browser window as well. For internet explorer, how would it be done?







  • Julia_K's avatar
    Julia_K
    SmartBear Alumni (Retired)

    Hello,


    If you need to switch the user agent in your load tests, you can do this either manually in the Load Test editor, or by calling the HTTPTask.SetBrowser method in your tests. For additional information, please see the Emulating Various Browsers Help topic.



    If you need to switch the user agent outside of the load tests, you can act in any of the following ways:



    1. Both Firefox and Internet Explorer support plug-ins and extensions allow setting the needed user agent. You can use TestComplete tests to automate the actions you perform to set the user agent with those plug-ins.



    2. For Internet Explorer, you can set the needed user agent via the Windows registry. A user agent is specified by the values of the HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings\5.0\User Agent registry key.

    You can use the Section object to manage this key.

    For example, the following script sets the "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)" user agent string for Internet Explorer:



    function SetIEUserAgent()

    {

    var key = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\5.0\\User Agent";

    var rootkey = HKEY_LOCAL_MACHINE;



    var UserAgentSection = Storages.Registry(key, rootkey);



    UserAgentSection.SetOptionByIndex(0, "Mozilla/5.0");

    UserAgentSection.SetOption("Compatible", "compatible");

    UserAgentSection.SetOption("Platform", "Windows NT 6.1");

    UserAgentSection.SetOption("Version", "MSIE 9.0");



    key = "Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings\\5.0\\User Agent\\Post Platform";



    var PlatformSection = Storages.Registry(key, rootkey);

    PlatformSection.SetOption("Trident/5.0", "");



    }



    You can find more information on the registry key and user agents strings in this MSDN article.



    3. To switch user agents in Firefox, you can use the Preferences object. This object provides access to the settings available for Firefox. You can view these settings by entering about:config in the Firefox address bar and pressing Enter.

    To switch the user agent in Firefox, you need to specify the needed value of the general.useragent.override setting.

    For example, the following script sets the "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)" user agent string for Firefox:



    Sys.Process("firefox").Preferences.general.useragent.override = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1)"



    In order for this script to work properly, you may need to manually add the "general.useragent.override" entry to the list of available settings on the about:config page first.


    Please let us know whether any of these ways help.

    Thanks in advance.