Selenium – Use Data Driven tests to run in multiple browsers and sizes

Posted by Aligned on Geeks with Blogs See other posts from Geeks with Blogs or by Aligned
Published on Mon, 04 Nov 2013 12:02:34 GMT Indexed on 2013/11/04 21:55 UTC
Read the original article Hit count: 171

Filed under:

Originally posted on: http://geekswithblogs.net/Aligned/archive/2013/11/04/selenium-ndash-use-data-driven-tests-to-run-in-multiple.aspx

Selenium uses WebDriver (or is it the same? I’m still learning how it is connected) to run Automated UI tests in many different browsers. For example, you can run the same test in Chrome and Firefox and in a smaller sized Chrome browser. The permutations can grow quickly. One way to get them to run in MStest is to create  a test method for each test (ie ChromeDeleteItem_Small, ChromeDeleteItem_Large, FFDeleteItem_Small, FFDeleteItem_Large) that each call  the same base method, passing in the browser and size you’d like. This approach was causing a lot of duplicate code, so I decided to use the data driven approach, common to Coded UI or Unit test methods.

1. Create a class with a test method.

2. Create a csv with two columns: BrowserType, BrowserSize

3. Add rows for each permutation: Chrome, Large | Chrome, Small | Firefox, Large | Firefox, Small | IE, Large | IE, Small | ***

4. Add the csv to the Visual Studio Project.

5. Set the Copy to output directory to Copy always

6. Add the attribute: [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\TestMatrix.csv", "TestMatrix#csv", DataAccessMethod.Sequential), DeploymentItem("TestMatrix.csv")]

7. Run the test in the test explorer

Example:

[CodedUITest] 
public class AllTasksTests : TasksTestBase 
{ 
     [TestMethod] 
     [TestCategory("Tasks")] 
     [DataSource("Microsoft.VisualStudio.TestTools.DataSource.CSV", "|DataDirectory|\\TestMatrix.csv", "TestMatrix#csv", DataAccessMethod.Sequential), DeploymentItem("TestMatrix.csv")] 
     public void CreateTask() 
     { 
         this.PrepForDataDrivenTest(); 
         base.CreateTaskTest("New Task"); 
     } 
}
protected void PrepForDataDrivenTest()
{
    var browserType = this.ParseBrowserType(Context.DataRow["BrowserType"].ToString());
    var browserSize = this.ParseBrowserSize(Context.DataRow["BrowserSize"].ToString());
    this.BrowserType = browserType;
    this.BrowserSize = browserSize;
    Trace.WriteLine("browser: " + browserType.ToString());
    Trace.WriteLine("browser size: " + browserSize.ToString());
}
/// <summary>
/// Get the enum value from the string
/// </summary>
/// <param name="browserType">Chrome, Firefox, or IE</param>
/// <returns>The browser type.</returns>
private BrowserType ParseBrowserType(string browserType)
{
    return (UITestFramework.BrowserType)Enum.Parse(typeof(UITestFramework.BrowserType), browserType, true);
}

/// <summary>
/// Get the browser size enum value from the string
/// </summary>
/// <param name="browserSize">Small, Medium, Large</param>
/// <returns>the browser size</returns>
private BrowserSizeEnum ParseBrowserSize(string browserSize)
{
    return (BrowserSizeEnum)Enum.Parse(typeof(BrowserSizeEnum), browserSize, true);
}
/// <summary>
/// Change the browser to the size based on the enum.
/// </summary>
/// <param name="browserSize">The BrowserSizeEnum value to resize the window to.</param>
private void ResizeBrowser(BrowserSizeEnum browserSize)
{
    switch (browserSize)
    {
        case BrowserSizeEnum.Large:
            this.driver.Manage().Window.Maximize();
            break;
        case BrowserSizeEnum.Medium:
            this.driver.Manage().Window.Size = new Size(800, this.driver.Manage().Window.Size.Height);
            break;
        case BrowserSizeEnum.Small:
            this.driver.Manage().Window.Size = new Size(500, this.driver.Manage().Window.Size.Height);
            break;
        default:
            break;
    }            
}
/// <summary>
/// Browser sizes for automation testing
/// </summary>
public enum BrowserSizeEnum
{
    /// <summary>
    /// Large size, Maximized to the desktop
    /// </summary>
    Large,

    /// <summary>
    /// Similar to tablets
    /// </summary>
    Medium,

    /// <summary>
    /// Phone sizes... 610px and smaller
    /// </summary>
    Small
}
 
Hope it helps!

© Geeks with Blogs or respective owner