Creating a Simple C # Wrapper to Clean Up Code

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 that I would like to keep the code surrounding the try many times in the code. I think this has something to do with wrappers, but I can't get a simple answer for this from the Internet.

Thus, with a simple expression, the only part of this code that changes is the bit between try {}, the rest is the standard code, which I have used more than 100 times now and it turns out to be sick to maintain.

Hope this is understandable, thank you very much.

+4
source share
3 answers

Two obvious and somewhat equivalent sentences:

  • Use a delegate to represent the code in a try block (e.g. Action<ISelenium> )
  • Use the interface to represent code in a try block.

Modify the main method to accept a delegate (or interface) type parameter and execute it in a try block:

 public void Contacts(string domainToBeTested, string[] browserList, string timeOut, int numberOfBrowsers, Action<ISelenium> test) { verificationErrors = new StringBuilder(); for (int i = 0; i < numberOfBrowsers; i++) { ISelenium selenium = new DefaultSelenium ("LMTS10", 4444, browserList[i], domainToBeTested); try { test(selenium); } catch (AssertionException e) { verificationErrors.AppendLine(browserList[i] + " :: " + e.Message); } finally { selenium.Stop(); } } Assert.AreEqual("", verificationErrors.ToString(), verificationErrors.ToString()); } 

In any case, you separate a bit that changes from a bit that does not. I would probably go with a delegate form, especially if you use C # 3. Then you can put the code in the method and use the transformation of the group of methods or use the lambda expression for small blocks.

+8
source

You can use delegates to achieve it:

 public void Contacts(string domainToBeTested, string[] browserList, string timeOut, int numberOfBrowsers, Action<ISelenium> callback) { verificationErrors = new StringBuilder(); for (int i = 0; i < numberOfBrowsers; i++) { ISelenium selenium = new DefaultSelenium("LMTS10", 4444, browserList[i], domainToBeTested); try { // Here the delegate is called callback( selenium ); } catch (AssertionException e) { verificationErrors.AppendLine(browserList[i] + " :: " + e.Message); } finally { selenium.Stop(); } } Assert.AreEqual("", verificationErrors.ToString(), verificationErrors.ToString()); } 

The call looks like this:

 var result = Contacts( /* your arguments */, ACallback ); 

and

 private void ACallback( ISelenium selenium ) { 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")); } 

Note: you can pass more arguments than just ISelenium , and, of course, return the result too (using Func<> instead of Action ).

+2
source

Why not just extract it?

  public string TestSelenium(ISelenium selenium) { 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) { return e.Message; } finally { selenium.Stop(); } return String.Empty; } 
0
source

Source: https://habr.com/ru/post/1312061/


All Articles