Creating a Simple C# Wrapper to clean up code

Posted by Tangopop on Stack Overflow See other posts from Stack Overflow or by Tangopop
Published on 2010-06-07T10:33:38Z Indexed on 2010/06/07 10:42 UTC
Read the original article Hit count: 166

Filed under:

I have this code:

public void Contacts(string domainToBeTested, string[] browserList, string timeOut, int numberOfBrowsers)
    {
        verificationErrors = new StringBuilder();

        for (int i = 0; i < numberOfBrowsers; i++)
        {
            ISelenium selenium = new DefaultSelenium("LMTS10", 4444, browserList[i], domainToBeTested);

            try
            {
                selenium.Start();
                selenium.Open(domainToBeTested);

                selenium.Click("link=Email");
                Assert.IsTrue(selenium.IsElementPresent("//div[@id='tabs-2']/p/a/strong"));

                selenium.Click("link=Address");
                Assert.IsTrue(selenium.IsElementPresent("//div[@id='tabs-3']/p/strong"));

                selenium.Click("link=Telephone");
                Assert.IsTrue(selenium.IsElementPresent("//div[@id='tabs-1']/ul/li/strong"));
            }
            catch (AssertionException e)
            {
                verificationErrors.AppendLine(browserList[i] + " :: " + e.Message);
            }
            finally
            {
                selenium.Stop();
            }
        }
        Assert.AreEqual("", verificationErrors.ToString(), verificationErrors.ToString());
    }

My problem is i would like to make it so that i can use the code surrounding the 'try' many many times in the rest of the code. I think it has something to do with wrappers, but i can't get a simple answer for this from the web.

So in simple terms the only piece of this code which changes is the bit between the try {} the rest is standard code that i have currently used over 100 times and is turning out to be a pain to maintain.

Hope this is clear, many thanks.

© Stack Overflow or respective owner

Related posts about c#