Powered By Blogger

Search Here!

Friday, February 22, 2013

How to run same script on FF/IE/Chrome in Webdriver

Setting the Browser key in the app.config, we can execute the same script in the different browser(s).
        
         ///
        /// Returns an instance of the web driver based on test browser.
        ///

        /// The browser driver.

        public static IWebDriver GetInstance()
        {
            string browserName = ConfigurationManager.AppSettings["Browser"];
            IWebDriver webDriver = null;
            switch (browserName)
            {
                case Internet_Explorer: webDriver = IEWebDriver(); break;
                case FireFox: webDriver = FireFoxWebDriver(); break;
                case Safari: webDriver = SafariWebDriver(); break;
                case Chrome: webDriver = ChromeWebDriver(); break;
                default: throw new ArgumentException("The suggested browser was not found");
            }
            return webDriver;
        }

         ///
        /// Returns an instance of IE based driver.
        ///

        /// IE based driver.

        private static IWebDriver IEWebDriver()
        {
            IWebDriver webDriver = new InternetExplorerDriver(ieDriverPath, options, TimeSpan.FromMinutes(20));            webDriver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(timeOut));
            //Set The Cursor Position Center of the Screen
            Cursor.Position = new Point(Screen.PrimaryScreen.Bounds.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2);
            return webDriver;
        }

        ///
        /// Returns an instance of Firefox based driver.
        ///

        /// FireFox based driver.

        private static IWebDriver FireFoxWebDriver()
        {
            IWebDriver webDriver = new FirefoxDriver();            webDriver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(timeOut));
            //Set The Cursor Position Center of the Screen
            Cursor.Position = new Point(Screen.PrimaryScreen.Bounds.Width / 2, Screen.PrimaryScreen.Bounds.Height / 2);
            return webDriver;
        }

        ///
        /// Returns an instance of Safari based driver.
        ///

        /// Safari based driver

        private static IWebDriver SafariWebDriver()
        {
            IWebDriver webDriver = new SafariDriver();            webDriver.Manage().Timeouts().SetPageLoadTimeout(TimeSpan.FromSeconds(timeOut));
            return webDriver;
        }

        ///
        /// Returns an instance of Chrome based driver.
        ///

        /// Chrome based driver.

        private static IWebDriver ChromeWebDriver()
        {
            IWebDriver webDriver = new ChromeDriver(
                (Path.GetDirectoryName(
                System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)
                + "\\..\\..\\..\\..\\ExternalAssemblies").Replace("file:\\", ""));
            return webDriver;
        }
    }

Add browser key in the app.config -
<add key="Browser" value="Internet Explorer" />

No comments:

Post a Comment