Geeks With Blogs

News Please visit me at my new blog!!

profile for Aligned at Stack Overflow, Q&A for professional and enthusiast programmers
"free in Christ Jesus from the law of sin and death." Romans 8:2 (ESV) Check out the Falling Plates video on YouTube.
more about the Gospel
And then listen to Francis Chan speaking at LifeLight in SD.



Programming and Learning from SD

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() 
     { 
         // send in properties to skip a size or browser type, or none to run all the tests in the csv.
         if(this.PrepForDataDrivenTest(BrowserSizeEnum.Small))
         {
            base.CreateTaskTest("New Task"); 
         }
     } 
}
private TestContext testContextInstance;
public TestContext TestContext
{
    get { return testContextInstance; }
    set { testContextInstance = value; }
}

/// <summary>
/// Read the csv file for desired test permutations, run a test for each row.
/// </summary>
/// <returns>True to run the test</returns>
protected bool PrepForDataDrivenTest()
{
    // to skip use the overloads below
    // to skip a size and a browser type, then call both overloads
    this.ReadTestContext();
    return true;
}

/// <summary>
/// Read the csv file for desired test permutations, skip the sizes given
/// </summary>
/// <param name="browserSizesToSkip">The browser widths to skip for this test</param>
/// <returns>True to run the test</returns>
protected bool PrepForDataDrivenTest(params BrowserSizeEnum[] browserSizesToSkip)
{
    this.ReadTestContext();
    if (browserSizesToSkip.ToList().Contains(this.BrowserSize))
    {
        Trace.WriteLine("Test Skipped for size " + this.BrowserSize.ToString());
        return false;
    }

    return true;
}

/// <summary>
/// Read the csv file for desired test permutations, skip the browser types given
/// </summary>
/// <param name="browserTypeToSkip">The browser types to skip</param>
/// <returns>True to run the test</returns>
protected bool PrepForDataDrivenTest(params BrowserType[] browserTypeToSkip)
{
    ReadTestContext();

    if (browserTypeToSkip.ToList().Contains(this.BrowserType))
    {
        Trace.WriteLine("Test Skipped for size " + this.BrowserType.ToString());
        return false;
    }

    return true;
}

private void ReadTestContext()
{
    // Edit: TestContext must be used instead of Context or the second test set in the class
    // will have null values for Context.DataRow
    // see: http://stackoverflow.com/questions/2993298/problems-with-data-driven-testing-in-mstest
    var browserType = this.ParseBrowserType(this.TestContext.DataRow["BrowserType"].ToString());
    var browserSize = this.ParseBrowserSize(this.TestContext.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!
Posted on Monday, November 4, 2013 12:02 PM UI Automated Testing , Selenium | Back to top


Comments on this post: Selenium – Use Data Driven tests to run in multiple browsers and sizes

# re: Selenium – Use Data Driven tests to run in multiple browsers and sizes
Requesting Gravatar...
Hi,

Can you please share the downloadable source code for he above solution

Thanks,
Raja
Left by Rajababu on Sep 08, 2015 6:18 AM

Your comment:
 (will show your gravatar)


Copyright © Aligned | Powered by: GeeksWithBlogs.net