Skip to content

Creating a Test From Scratch I

MatthewWalker42 edited this page Feb 24, 2014 · 6 revisions

Introduction

By now you may be very familiar with SharpSauce and all it has to offer. If you aren't, then feel free to stop on by the three part [tutorial](https://github.com/simnova/SharpSauce/wiki/Getting-Started-and-Running-Your-First-Test--%5BPart-1%5D). However, you may currently be stuck using the UnitTest.cs files, which you may wish to use as references, or just not use at all. If that is the case, then this is the page you have been looking for.

Creating a New CS file

So how can you create a new file and create a working test from there? Well, you will want to go to your Solution Explorer panel and right click on "ExampleTestProject" (We aren't going to go over creating a new project because that is a much more complicated task.) Select Add -> New Item. You will then have an "Add New Item" dialogue box open. Select the "Class" item and rename it to whatever you want. Then hit the "Add" button. ![FromScratch1](https://raw.github.com/simnova/SharpSauce/master/wiki%20Images/FromScratchImg1.png) ![FromScratch2](https://raw.github.com/simnova/SharpSauce/master/wiki%20Images/FromScratchImg2.png)

Filling in the Blank Spaces

![FromScratch3](https://raw.github.com/simnova/SharpSauce/master/wiki%20Images/FromScratchImg3.png)
Now the first thing we are going to want to do is add the proper libraries to this file. Otherwise things like Selenium and the SauceLabsDriver won't work. The best way to do this is copy all the "using ..." lines at the top of UnitTest3.cs and paste them over what was created in the new file. ![FromScratch4](https://raw.github.com/simnova/SharpSauce/master/wiki%20Images/FromScratchImg4.png) Now we will want to make our class capable of running unit tests. To do this simply change your code to look like the code below. The changes you will need to make are [TestClass] and public:
``` namespace ExampleTestProject { [TestClass] public class ExampleClass { } } ```
Now, return back to UnitTest3.cs and copy everything from the username string variable to "//Test Templates" line. Now paste that in your class as shown below:
namespace ExampleTestProject
{
    [TestClass]
    public class ExampleClass
    {
        //UserName and AccessKey must correspond to Sauce Labs Username and Acess Key
        private const string UserName = "username";
        private const string AccessKey = "accesskey";

        //Test setup values
        private const string baseURL = "https://www.google.com/"; //base url can be retrieved from selenium script
        private const string TextFile = "OS-Browser-Combo-San.txt"; //name of text file containing browser/os combinations to be tested

        //Test configuration values

        //_TestName: Sets session name of test ("on browser/os combo" will be added automatically). Should be text string.
        //_ScreenResolution: Sets screen resolution of test. Changing not recommended.
        //_Timeout: Sets command timeout. Should be integer
        //_BuildNumber: Sets build number. Should be text string
        //_Tags_seq: Sets tags for sequential test. Should be list of text strings. Can have as many tags as desired
        //_Tags_parall: Sets tags for parallel test. Should be list of text strings. Can have as many tags as desired

        private const string _TestName = "Test for Selenium in Google";
        private const SauceLabs.ScreenResolutions _ScreenResolution = SauceLabs.ScreenResolutions.screenDefault;
        private const int _Timeout = 30;
        private const string _BuildNumber = "11";
        private string[] _Tags_seq = new string[] { "Example", "Simple Navigation", "unit test" };
        private string[] _Tags_parall = new string[] { "parallel test", "Example", "extended timeout" };

        //timeoutInMinutes determines how many minutes must pass before timeout exception is thrown. For parallel testing only.
        private int timeoutInMinutes = 5;
    }
}

You may wish to remove any variables that are specific to sequential tests or parallel tests if you don't plan on running them in sequence or parallel. At this time you should reflect on how you wish to run your tests. Do you want to run them locally, in sequence, or in parallel? Depending on your answer, you will want to copy the corresponding template from UnitTest3.cs to your new file. If you wish, you can copy two or all the templates over. However, for the purposes of this example, I will only be copying the sequential template. Once you have pasted your template into the new class, you should add a test script procedure after your template. Please refer to the code below:
``` namespace ExampleTestProject { [TestClass] public class ExampleClass { //UserName and AccessKey must correspond to Sauce Labs Username and Acess Key private const string UserName = "username"; private const string AccessKey = "accesskey";
    //Test setup values
    private const string baseURL = "https://www.google.com/"; //base url can be retrieved from selenium script
    private const string TextFile = "OS-Browser-Combo-San.txt"; //name of text file containing browser/os combinations to be tested

    //Test configuration values

    //_TestName: Sets session name of test ("on browser/os combo" will be added automatically). Should be text string.
    //_ScreenResolution: Sets screen resolution of test. Changing not recommended.
    //_Timeout: Sets command timeout. Should be integer
    //_BuildNumber: Sets build number. Should be text string
    //_Tags_seq: Sets tags for sequential test. Should be list of text strings. Can have as many tags as desired
    //_Tags_parall: Sets tags for parallel test. Should be list of text strings. Can have as many tags as desired

    private const string _TestName = "Test for Selenium in Google";
    private const SauceLabs.ScreenResolutions _ScreenResolution = SauceLabs.ScreenResolutions.screenDefault;
    private const int _Timeout = 30;
    private const string _BuildNumber = "11";
    private string[] _Tags_seq = new string[] { "Example", "Simple Navigation", "unit test" };
    private string[] _Tags_parall = new string[] { "parallel test", "Example", "extended timeout" };

    //timeoutInMinutes determines how many minutes must pass before timeout exception is thrown. For parallel testing only.
    private int timeoutInMinutes = 5;

    //Remote Test Sequential Template
    [TestMethod]
    public void ExampleTestRemote()
    {
        string[] lines = System.IO.File.ReadAllLines(TextFile);
        List<string> failedCombos = new List<string>();
        timeoutInMinutes = 1;

        foreach (string line in lines)
        {
            var sauceLabs = new SauceLabs(UserName, AccessKey, timeoutInMinutes);
            var driver = sauceLabs.GetRemoteDriver(new SauceLabs.SauceLabsConfig
            {
                BrowserVersion = (SauceLabs.BrowserVersions)Enum.Parse(typeof(SauceLabs.BrowserVersions), line), //Do not change
                TestName = _TestName,
                ScreenResolution = _ScreenResolution,
                Timeout = _Timeout,
                BuildNumber = _BuildNumber,
                Tags = _Tags_seq,

            });
            var results = sauceLabs.RunRemoteTestCase(driver, TutorialDemoTestCase);

            try
            {
                Assert.IsTrue(results);
            }

            catch (AssertFailedException)
            {
                failedCombos.Add(line);
            }
        }

        if (failedCombos.Capacity > 0)
        {
            string allFailedCombos = string.Join(", ", failedCombos.ToArray());
            Assert.IsTrue(false, "Test failed these browser/OS combinations: " + allFailedCombos);
        }
    }

    //Test Script
    private bool TutorialDemoTestCase(SauceLabsDriver driver)
    {
        //Insert your test script here

    }
}

}

That is all for part I. In part II we look at building a test script from scratch. Click [here](https://github.com/simnova/SharpSauce/wiki/Creating-a-Test-From-Scratch-II) to check out part II.

Clone this wiki locally