What is the best method to track the total duration of a while loop nested within a for loop?

I am working on a project where I need to track the number of attempts made by a conditional while loop nested inside a for loop. This is to ensure the stability of the while-looping action and test its reliability through repeated testing. (using Selenium framework)

Despite trying various methods like changing the loop structure or setting different variables, I have not found any similar cases shared in the community.

public static void main(String[] args) throws Exception {
  for (int loop = 1; loop <= 10; loop++) {
    System.out.println("Loop count: " + loop);
    boolean retry = true;
    int i = 1;
    while (retry == true) {
      int t = i++;
      //...
      System.out.println("Test case 1 - Start Run " + t);
      try {
         // my testing script of actions
        System.out.println("Test case 1 - Run Success, finished by " 
            + t + " execution(s)");
        retry = false;
        break;
      } catch (Exception e) {
        System.out.println("Test case 1 - Run " 
            + t + " Failed, retry...");
      } 
      driver.quit();
    }
  }
}

This code snippet aims to evaluate the stability of an action script (Selenium webdriver framework) by automatically retrying when encountering errors, instead of stopping the script completely. The goal is to iterate through the process and record the total number of attempts needed to achieve 10 successful runs at the end of each for-loop iteration. In simpler terms, it counts the cumulative attempts within the retry loop required to reach 10 successful executions during the for loop.

Answer №1

Is there a benefit to maintaining the total count outside of both loops?

int totalCount = 0;

for (int i = 0; i < 10; i ++) {

  while (retry) {
    totalCount ++;
    // perform necessary tasks
  }
}

System.out.println("After " + totalCount + " tries, successfully completed 10 actions");

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

``There seems to be an issue with clicking an element in selenium

Having trouble with selenium while testing an angular site. Need to click on the pub name field on this screen: https://i.stack.imgur.com/i2NEO.png The side menu is open and here is the HTML: https://i.stack.imgur.com/0AxR9.png Tried waiting for the ele ...

Unable to convert JSON data into a datatable

My code looks like this: $.ajax({ type : 'POST', url : 'classfication_of_productjson.html', dataType : 'json', data : { "search_bunrui_code ...

The titles and view counts of YouTube videos are not displayed

I am currently working on a project to extract and print the title and view count of each video from different YouTube channels listed in my urls. I encountered an issue where it only displayed results for one channel (https://www.youtube.com/c/TuckerBud ...

Selenium is encountering difficulties in recognizing a radio button

driver.get("http://www.testingwebsite.com/html/form/radio_btn.html"); List<WebElement> radioButtons = driver.findElements(By.name("favorite_food")); System.out.println(radioButtons.get(0).getAttribute("value")); System.out.println(radioButtons.get ...

Ways to shut down an iframe using selenium

Currently tackling a Selenium webdriver Project where I find myself entering an iframe to input keys. However, now I am seeking guidance on how to exit that iframe in order to access a button outside of it. Here is a snippet of my code: WebDriverWait(bot,2 ...

Can someone show me how to find and select the start button with selenium in python coding?

<div id="start" class="btn btn-huge btn-success"><i class="fas fa-power-off"></i> Start</div> This snippet of HTML code is found within the source of a website, representing a 'Start' button. ...

Locate the XPath for a recurring AngularJS element

Is it possible to determine the xpath for an angularJs element? I noticed that all links on my page share the same xpath due to repeated items in angularJS. .//*[@id='div_1_1_1_2']/div/div[1]/div[2]/div[2]/div/div[2]/div[1]/div/div[2]/a I have ...

Problem encountered when integrating Jenkins with Maven

Encountering a PluginResolutionException while running code through Jenkins. The code works fine in Eclipse, but throws an exception when executed in Jenkins. public class LoginPageTest extends TestBase { // Code snippet here } This is the content of t ...

Is there a way to extract and send all console data using Python through email?

I've been working on a code that schedules jobs, takes screenshots on the chromedriver, and emails them. I'm currently exploring ways to capture the console output to a file for later emailing. Below is a snippet of the code for the scheduler and ...

What factors should be considered when selecting a suitable web scraper according to the type of content?

When selecting a scraper for a specific URL, I often encounter issues with BeautifulSoup not being able to scrape JavaScript protected pages. In these cases, I resort to using Selenium instead of BeautifulSoup. What I am interested in is how to determine ...

Choosing Only the Visible Element with CSS

Within my program, there exists a text element that appears in two distinct sections: section A and section B (in the form of a popup). My intention was to create one object using CSS that could be utilized in both areas. By doing so, I would be able to em ...

Can we enhance the filtering capabilities of the Elements Collection?

When using the following code, I am able to retrieve one table row from a page: ElementsCollection letters = $$("#myIdName tr").filterBy(Condition.text(email)) Now, my goal is to select and click on a td href element within that row. How can I accomplish ...

Discovering changing text on a website using Python's Selenium WebDriver

Website myList = ['Artik'] How do I verify if the content of myList is visible on the mentioned website? <span class="ipo-TeamStack_TeamWrapper">Artik<span> This specific webelement showcases 'Artik' in the given websi ...

Using Selenium for Python-based authentication

Is it possible to automate the launch of admin area URIs on a website using Selenium in a specific browser without prior authentication? If not, what is the best way to handle authentication using Selenium? ...

Webdriver Selenium 2

What is the process for creating a batch file to run Selenium Webdriver projects in Java? I have successfully created executable jar files by using File > Export > Java > Runnable Jar, but I am interested in creating a BATCH File. Any assistance would be ...

FileDetector feature in Selenium for Java RemoteWebDriver

I'm encountering an issue with file uploads using Selenium's RemoteWebDriver (specifically the Chrome driver, set up with Selenium Grid). A problem similar to the one discussed in this thread: Sending Keys to Input WebElement fails as it is not ...

"Unable to retrieve element from the cache" with Selenium WebDriver

Can Selenium WebDriver be used to clear browser cache efficiently? I am hoping to find a solution that can help with larger test cases, as sometimes I encounter the error message: "Element not found in the cache - perhaps the page has changed since it was ...

Python Selenium for accessing and retrieving all attributes

I'm facing an issue where I need to extract all elements from a page that has infinite scrolling. I've tried scrolling down the page and collecting attributes, but it seems like I'm only getting about half of them. Is there a reason for this ...

Concentrate on the current open tab with the help of Selenium WebDriver

I have encountered an issue on my website where clicking a button opens a new tab with a link in the same browser. Now, I am seeking a way to instruct selenium to focus on that newly opened tab. I have experimented with multiple methods but so far none h ...

Element Not Found Error

Can someone help me find the Xpath for the element below? - <ion-button class="ta-btn btn-red ios button button-block button-round button-outline ion-activatable ion-focusable hydrated"expand="block"fill="outline"mode=" ...