0

I have a list that contain information that I want to assert. I want to assert in a table on a webpage

My Code looks:

for (var X = 0; X < test.Count; X++)
{
    try
    {
        for (var i = 0; i <= 4; i++)
        {
            if (GetElement(i).Text == test[X].Type.ToString())
            {
                switch (test[X].Type)
                {
                    case Enum.Type.X:
                        [Asserts]
                         break;
                    case Enum.Type.Y:
                        [Asserts]
                        break;
                    case Enum.Type.Z:                                                                                
                    [Asserts]
                        break;
                    default:
                        break;
                }
                break;
            }
        }
        break;

    }
    catch (NoSuchElementException)
    {
        throw new NoSuchElementException($"text");
    }
}

What I want:

  • I want that for each line in the list it goes to do if statement. If the statement is correct Assert Are Done.
  • When Asserts are correct is should go back and start with the second record in the list (named test).
  • If the if statement is never true a catch (NoSuchElementException) should appear.

The list looks has 4 fields, most of the time it has 3 records, sometimes 4 records depending on the situation.

I want each record in the list be checked.

Currently it only checks the first record in the list and then it stops. While I want each record in the list to be checked.

If more information is needed, let me know.

juharr
  • 31,741
  • 4
  • 58
  • 93
xxx2017
  • 207
  • 1
  • 5
  • 15
  • wow, the level of nesting makes it hard to read. Can you track the problem using a debugger? – Peter Bons Feb 23 '18 at 14:26
  • I tried but was not able to fix it with debugger thats why I asked it here. – xxx2017 Feb 23 '18 at 14:28
  • 1
    You have a `break` at the end of the `try` which will cause the outter loop to never get past `X` = 0 – juharr Feb 23 '18 at 14:31
  • The `break` would have been easily found with the debugger... – maccettura Feb 23 '18 at 14:32
  • I want it to stop when assertion fails. – xxx2017 Feb 23 '18 at 14:33
  • @xxx2017 If X=0 and the if is false for all the values of i then you hit that break and it never tries any values of X larger than 0. Basically as it is now you could remove the outter loop and just replace X with 0 and get the same results. – juharr Feb 23 '18 at 14:35
  • Have you considered comparing the two lists? If you use NUnit, there are methods that do this for you, e.g. https://stackoverflow.com/questions/19861619/nunit-comparing-two-lists. The code would be much simpler. – JeffC Feb 23 '18 at 18:39

1 Answers1

1

If you want a break for an inner loop to break the outer loop you need to do that using some type of flag. You can use that flag in the condition of both loops and instead of the break from the inner loop you just set the flag accordingly. Also you need to remove the break from the end of the try as that stops the outer loop from doing more than one iteration. If you only want it to break on the [Asserts] then you'd move the code that sets the flag to each case accordingly.

bool keepGoing = true;
for (var X = 0; X < test.Count && keepGoing; X++)
{
    try
    {
        for (var i = 0; i <= 4 && keepGoing; i++)
        {
            if (GetElement(i).Text == test[X].Type.ToString())
            {
                switch (test[X].Type)
                {
                    case Enum.Type.X:
                        [Asserts]
                         break;
                    case Enum.Type.Y:
                        [Asserts]
                        break;
                    case Enum.Type.Z:                                                                                
                        [Asserts]
                        break;
                    default:
                        break;
                }

                keepGoing = false;
            }
        }
    }
    catch (NoSuchElementException)
    {
        throw new NoSuchElementException($"text");
    }
}
juharr
  • 31,741
  • 4
  • 58
  • 93
  • The first in the list goes fine (0) when it finishes the asserts correctly . It then goes back to for (var X = 0; X < test.Count && keepGoing; X++) Now X=1. When I continue it is not going into: try { for (var i = 0; i <= 4 && keepGoing; i++) But skips that part and goes out of the method as if it finished everything. – xxx2017 Feb 26 '18 at 10:13
  • Was `GetElement(i).Text == test[X].Type.ToString()` true for `X=0` and `0<=i<=4`? If so then `keepGoing` would be set to `false` and both loops with break. Also make sure `test.Count` is greater than 1. – juharr Feb 26 '18 at 17:04
  • I removed keepgoing from the first for and lowered bool keepGoing = true; a bit. Then it worked the way I wanted. Thanks. – xxx2017 Feb 27 '18 at 10:28
  • @xxx2017 Then that's just the same as removing the second `break` from your original code. I thought you wanted to stop the outter loop as well when the `if` in the inner loop was true. If not, then I'd just go back to one `break` in the inner loop and get rid of `keepGoing`. – juharr Feb 27 '18 at 12:21