Friday, 9 March 2018

Chrome browser maximize issue in Selenium C#

If you are using C# in selenium and chrome browser then you will face issue for maximize browser.

If you are using maximize() function then it will throw exception. If you use capability for chrome browser for maximize it then it will also not work.

You have to use AutoIT for maximize browser. It is very easy to use.

Configuration:

First of all you have to add Nuget Package 'AutoItX' into your project. Please see below screen shot.




Once you are installed AutoItX into your project then you have to add 'AutoItX3Lib.dll' into references.

You can find that .dll file from where AutoItX is installed.



One more thing and very important is you have to register that .dll first because without register that .dll you can not use.

You can register  .dll file using 'regsvr32' command from command prompt.

regsvr32 path of .dll file

Now you all set for used of AutoIt functions.You have to use AutoItX3 class for access all functionality.

I have created below function for maximize chrome browser.


public void MaximizeBrowserWindow(IWebDriver driver, String vWindowName)
        {
            try
            {
                AutoItX3 autoIT = new AutoItX3();  // Create object of AutoItX3 class

                autoIT.WinSetState(vWindowName, "", autoIT.SW_MAXIMIZE); // For Maximize browser
                
                Console.WriteLine("Browser is maximized");
            }
            catch (NoSuchWindowException E)
            {
                Console.WriteLine(E.Message);
                Console.WriteLine("Browser is not maximized");                
            }
            catch (Exception E)
            {
                Console.WriteLine(E.Message);
                Console.WriteLine("Browser is not maximized");                
            }
        }

Code Explanation:


autoIT.WinSetState(vWindowName, "", autoIT.SW_MAXIMIZE);

vWindowName : It is Browser window name.
autoIT.SW_MAXIMIZE : It is for maximize browser.

Please add comment if you have any question.

No comments:

Post a Comment

Popular