Wednesday, September 23, 2015

Running your WebDriver tests on different browsers with Jenkins

Introduction

In this article I'll show you how you can run your tests on different browsers by using an app.config in your project and Jenkins. I use C# for my webdriver project.

App.config

In your project that contains the webdriver tests (nunit, mstest etc.) add an app.config file.



This is an .xml file in which you can add a key value for the browser type you want to run your tests. Below, you have an example:
<configuration>
<appSettings>
<add key = "BrowserType"  value = "chrome" />
<add key = "Email"  value = "youremail@com" />
<add key = "Password"  value = "yourpassword />
<add key = "Site"  value = "http://yoursite.com />
</appSettings>

</configuration>

In a basic class you can initialize your Driver like this:

public static void Initialize()
        {
            var browserType = GetConfigProperty("BrowserType");
            switch (browserType)
            {
                case "chromemac":
                    ChromeDriverService service = ChromeDriverService.CreateDefaultService("path to your driver", "chromedriver");
                    Instance = new ChromeDriver(service, new ChromeOptions());
                    Instance.Manage().Window.Position = new Point(0, 0);
                    Instance.Manage().Window.Size = new Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
                    break;
                case "chrome":
                    Instance = new ChromeDriver(@"path to your driver");
                    Instance.Manage().Window.Maximize();
                    break;
                case "firefox":
                    Instance = new FirefoxDriver();
                    Instance.Manage().Window.Maximize();
                    break;
                case "ie":
                    Instance = new InternetExplorerDriver(@"path to your driver");
                    Instance.Manage().Window.Maximize();
                    break;
                case "safari":
                    Instance = new SafariDriver();
                    Instance.Manage().Window.Maximize();
                    break;
            }
        }

where var browserType = GetConfigProperty("BrowserType"); will take your value from the app.config.
In our case, the tests will be executed on Chrome for Windows.
Note that this example shows you how you can run your tests on Chrome for Mac, and gives you a solution on how you can maximize your Chrome browser on Mac OS.

Jenkins

For each configuration where you want to run your tests, store a different app.config file with the desired key values.



For example, for firefox, 
- BrowserType = "firefox"
- Email = "email@firefoc.com", etc



For chrome, BrowserType will be "chrome."

How can you use these new configurations in the Jenkins job? Create your job for running tests on, lets' say Firefox. After the step that is taking the latest sources of the application we have to add a new step that is copying our app.config in the project location. You can do this like in the example below
- for a Windows OS: xcopy /s/y "C:\TFSOnline\PIN\config\firefox\App.config" "C:\Users\Raluca\.jenkins\jobs\xamarin - pin - jenkins - firefox\workspace\aut\Pin_Selenium_Testing-Xamarin\PinTests\App.config"
- for a Mac OS: cp /Users/raluca/Desktop/Raluca/PINHelpers/config/firefox/App.config /Users/raluca/.jenkins/jobs/macPinFirefox/workspace/aut/Pin_Selenium_Testing-Xamarin/PinTests/app.config

In this way you tell your project to run your tests on firefox/chrome/ie/safari driver.
For each configuration you'll need a different job, that will be executed, during night at a specific hour.
That's it.

Happy testing and... make it green, becomes a dream :).
For every post I'll recommend something to read. The link for today is:  Agile Testing: A Practical Guide for Testers and Agile Teams


No comments:

Post a Comment

Popular Posts