我有这段代码:
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());
}
我的问题是我想这样做,以便我可以在其余的代码中多次使用'try'周围的代码。我认为它与包装器有关,但我无法从网上得到一个简单的答案。
所以简单来说,这段代码中唯一改变的是try {}之间的位,其余的是标准代码,我目前使用了100多次,结果是难以维护。
希望这很清楚,非常感谢。
答案 0 :(得分:8)
两个明显且有些相同的建议:
Action<ISelenium>
)更改main方法以接受委托(或接口)类型的参数并在try块中执行:
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());
}
无论哪种方式,你都要改变那些没有改变的位。我可能会使用委托表单,特别是如果您使用的是C#3。然后您可以将代码放在方法中并使用方法组转换,或者对小块使用lambda表达式。
答案 1 :(得分:2)
您可以使用委托来实现它:
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());
}
电话看起来像是:
var result = Contacts( /* your arguments */, ACallback );
和
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"));
}
注意:您可以传递多于ISelenium
的参数,当然也可以返回结果(使用Func<>
代替Action
)。
答案 2 :(得分:0)
为什么不简单地提取它?
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;
}