我正在编写负面测试,以检查显示给用户的错误消息的内容。应用程序中有两种语言:英语和德语。该测试有效,但是用于检查每种语言的代码如下:
//Check if modal dialog with error message is shown
string currentLanguage = loginPage.currentLanguage.Text;
string modalMessage = loginPage.errorMsgModalDialogTitle.Text;
try
{
Assert.True(!string.IsNullOrEmpty(modalMessage));
test.Log(Status.Pass, "Office365 login has failed and modal dialog was shown to user!");
test.Log(Status.Info, "Checking modal dialog error message...");
switch (currentLanguage)
{
//Current language is english
case "German":
try
{
Assert.AreEqual(modalMessage, "User does not exist!");
test.Log(Status.Pass, "Modal dialog message title verified! Message title: '" + modalMessage + "'");
}
catch(AssertionException)
{
test.Log(Status.Fail, "Modal dialog did not contain message title: '" + modalMessage + "'");
}
break;
//Current language is german
case "English":
try
{
Assert.AreEqual(modalMessage, "Benutzer existiert nicht!");
test.Log(Status.Pass, "Modal dialog message title and text verified! Message title: '" + modalMessage + "'");
}
catch (AssertionException)
{
test.Log(Status.Fail, "Modal dialog did not contain message title: '" + modalMessage + "'");
}
break;
}
}
如您所见,用于检查两种语言的模态对话框标题的代码有点太大,并且将存在负面测试,甚至有更多错误消息可用于两种语言的检查。
是否可以通过某种方式折射该代码并使它更简单或更简洁?我可以创建一些仅将当前语言作为参数,消息和预期消息并返回true或false的辅助方法吗?
答案 0 :(得分:0)
在这里,您应该遵循以下方法
使用上述方法,您将不需要任何切换用例,并且无需更改任何代码即可在所有语言中使用。