Ben
Ben

Reputation: 43

C# Do/While loop Selenium - Execute particular statement only after initial execution

OK, I may be overthinking this but I'm at a loss. I'm automating a web page and validating some entries which I've added to a list actually exist in the list once the add function is completed. My issue is I don't know the contents of the list prior to adding my own entries (and don't really need to) so, after adding my entries they may or may not appear on the 1st page of the list. I was planning on looking for the existence of the scroll arrow to determine if I needed to continue looking for items but I'm having trouble with my loop and when to check it.

Right now I have this:

List<string> addedTestNames = new List<string>();
do
{
    IList<IWebElement> displayedTests = cycle.ReturnListOfTestsWithinCycle();
    for (int i = 0; i < cycle.ReturnListOfTestsWithinCycle().Count; i++)
    {
        addedTestNames.Add(displayedTests[i].Text);
    }
} while (helper.DoesElementExist(driver, cycle.getScrollForwardArrowInResults()));

My while condition is looking for the scroll arrow, if it's there I will do the actions again. If not, I'll know to stop and continue with the rest of my test. My issue is, if the arrow is there I need to click it to get to the next page of results. I don't know where to put that action. I cannot put it as the first condition because if it's there I don't want to click it prior to adding the 1st page of results and I cannot really put it after adding the first page of the results because that may change the answer to my while condition (the arrow goes away once you're on the "last" page). What I really want to do is complete all the actions once, look for the existence of the arrow, if it's there click it, then continue with the do actions again.

And yes, I'm sort of new to all this.

Upvotes: 0

Views: 493

Answers (1)

JeffC
JeffC

Reputation: 25542

You had most of the guts, you just needed to move one thing around and add a boolean flag to know when you are done.

List<string> addedTestNames = new List<string>();
bool done = false;
do
{
    // MORE OPTIMIZED - OPTION 1
    addedTestNames.AddRange(cycle.ReturnListOfTestsWithinCycle().Select(e => e.Text).ToList());

    // LESS OPTIMIZED - OPTION 2
    //IList<IWebElement> displayedTests = cycle.ReturnListOfTestsWithinCycle();
    //for (int i = 0; i < displayedTests.Count; i++)
    //{
    //    addedTestNames.Add(displayedTests[i].Text);
    //}

    if (helper.DoesElementExist(driver, cycle.getScrollForwardArrowInResults()))
    {
        // click Next
    }
    else
    {
        done = true;
    }
} while (!done);

Upvotes: 1

Related Questions