Nested if statements in C#
I need to write a conditional statement that would check if L tab is present.
- if L tab is not present it would print "L is NOT present".
- If L is present, it would print "L is present" , and checks to see if S, C and K tabs are present:
- If S is present, it would print "S is present" else "S is not present.
- If C is present it would print "C is present", else "C is not present",
- ...and same for K.
I have the following code written, but for some reason, the code doesn't look nice to me. Any ideas would be highly appreciated. Thanks!
if (Sel.IsElementPresent(arrObj1[4]))
{
Lib.WriteDebugLogs("Test 1: General", "NORMAL", "L tab present in home page");
Sel.Click(arrObj1[4]);
if (Sel.IsElementPresent(arrObj1[5]))
{
Lib.WriteDebugLogs("Test 1: General", "NORMAL", "S tab present under L tab.");
}
if (Sel.IsElementPresent(arrObj1[6]))
{
Lib.WriteDebugLogs("Test 1: General", "NORMAL"开发者_如何学Go, "C tab present under L tab.");
}
if (Sel.IsElementPresent(arrObj1[7]))
{
Lib.WriteDebugLogs("Test 1: General", "NORMAL", "K tab present under L tab.");
}
}
else
{
s = "'L' tab is not displayed in the home page.";
arrTestResults[6] = arrTestResults[7] = s;
Lib.WriteDebugLogs("Test 1: General", "****ERROR****", "Could noT find the L tab");
}
You should pretty much never be referencing values within an array by "magic constants" that you happen to know refer to specific elements. So I'm going to assume that somewhere before this method gets called, someone has done something like this:
var tabs = new[] {
new Tab{Name = "L", ParentName= "home page", Identifier = arrObj1[4],
// and so on for the tabs you care about.
};
That way, the first part of your method can be replaced with a method that is only involved in displaying information about which tabs are displayed:
public void LogDisplayedTabs(IEnumerable<Tab> tabs)
{
for(var tab in tabs)
{
if (Sel.IsElementPresent(tab.Identifier))
{
Lib.WriteDebugLogs("Test 1: General", "NORMAL",
string.Format("{0} tab present under {1} tab.", tab.Name, tab.ParentName));
}
}
}
I don't have time now to address the rest of the method, but this pattern should help you figure out how to fix the rest.
精彩评论