Search code examples
c#seleniumwebdrivernunitcompatibility-mode

selenium - internetexplorerdriver compatibility mode


Is there any way how to force webdriver/internetexplorerdriver to open a site in compatibility mode. Every time I run my tests by Nunit all the history and compatibility mode list (where was my site listed before) are cleaned.

I can't change the code of the site. Can I add item to compatibility mode list or open site in specifi version of IE (I have 11 and I need open it in 7 with doc type 5).


Solution

  • This is better description of my issue: I need to test a site that I can't edit. The site works only in compatibility mode in my IE 11 (it is made for ie 7 doc type 5). I want to run tests and cookies should be cleaned before that. But if I set "EnsureCleanSession = true" it cleans compatibilty list in IE besides cookies. Because of it, it's not possible to test the site.

    I have found possible solution, but I have to test it... I've found out that compatibility list is in registry and I can load its value before it is cleaned and set the value again:

            const string keyName = @"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\BrowserEmulation\ClearableListData";     
            var a = Registry.GetValue(keyName, "UserFilter" , "Return this default if NoSuchName does not exist.");
            // value of registry is removed
            Registry.SetValue(keyName, "UserFilter", a);
            Console.ReadLine();
    

    But as I said, I don't know if it will do the trick...

    [UPDATE]

    Ok, it works with small work-around (because IE must be restarted after change in registry)

        [SetUp]
        public void SetUp()
        {
            //read the compatibility mode list from registry
            const string path = @"HKEY_CURRENT_USER\Software\Microsoft\Internet Explorer\BrowserEmulation\ClearableListData";
            const string key = "UserFilter";
            var regValue = Registry.GetValue(path, key, "Return this default if NoSuchName does not exist.");
    
            //run IE driver with cleaning of cookies and history
            var options = new InternetExplorerOptions
            {
                IntroduceInstabilityByIgnoringProtectedModeSettings = true,
                EnsureCleanSession = true
            };
            _driver = new InternetExplorerDriver(IeDriversPath, options);
    
            //cloase IE
            _driver.Quit();
            _driver.Dispose();
    
            //put the compatibility mode list back into registry 
            Registry.SetValue(path, key, regValue);
    
            //run IE driver without cleaning of cookies and history
            options.EnsureCleanSession = false;
            _driver = new InternetExplorerDriver(IeDriversPath, options);
        }