Take the 2-minute tour ×
Software Quality Assurance & Testing Stack Exchange is a question and answer site for software quality control experts, automation engineers, and software testers. It's 100% free, no registration required.

How to break out of for loop when using this code? (utilizing webdriver)

public class MonthBooking_SkipStaff
{


    public static void main(String[] args)
    {

        driver = new FirefoxDriver();
        driver.manage().window().maximize();
        driver.get(url);
        currentdate();

    }


    public static void currentdate()

    {

        driver.findElement(By.xpath(xpath_service)).click();
        driver.findElement(By.cssSelector(css_currentdate)).click();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        String date = driver.findElement(By.cssSelector(css_currentdate)).getText();
        System.out.println("Date Slected :"+date);
        Integer currentdate = Integer.valueOf(date);
        days = driver.findElements(By.xpath(xpath_days)).size();
        newdate=currentdate+one;
        int slots=driver.findElements(By.xpath(xpath_slots)).size();
        int count=slots;
        if(count==0)

        {

            System.out.println(st);
            System.out.println("Total No.of days in the month " +days);
            System.out.println("Date Selected "+newdate);
            remainingdays();

        }

        WebElement element = driver.findElement(By.xpath(".//div[4]/ul/li[2]/a"));
        String strng = element.getText();
        System.out.println("Slot Selected: "+strng);
        //code to execute
        currentdate();

    }


    public static void remainingdays()

    {

        driver.navigate().refresh();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
        driver.findElement(By.xpath(xpath_service)).click();
        driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);

        for(int i=newdate;i<= month;i++)

        {

            driver.findElement(By.xpath("//div[@class='datepicker']/div/table/tbody/tr/td/table/tbody[2]/tr/td[@class='' or @class='datepickerSaturday' or @class='datepickerSunday']/a/span[contains(text(), \"" + newdate + "\")]")).click();
            int slots1=driver.findElements(By.xpath(xpath_slots)).size();
            driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
            int count1=slots1;

            if(count1!=0)

            {

                WebElement element = driver.findElement(By.xpath(".//div[4]/ul/li[2]/a"));
                String strng = element.getText();
                System.out.println("Slot Selected: "+strng);
                //code to execute
                remainingdays();

            }

            else

            {

                System.out.println(st);
                if(newdate<31)

                {

                    newdate=newdate+one;
                    System.out.println("Date Selected "+newdate);
                    remainingdays();

                }

                else

                {

                    System.out.println("No Slots available for "+newdate);
                    break;

                }


            }


        }


    }


}

When the last else is executed, I want to come out of for loop and to stop the testcase. But it again goes to for loop and searches for the this element

driver.findElement(By.xpath("//div[@class='datepicker']/div/table/tbody/tr/td/table/tbody[2]/tr/td[@class='' or @class='datepickerSaturday' or @class='datepickerSunday']/a/span[contains(text(), \"" + newdate + "\")]")).click();

How can this be done?

share|improve this question
    
Please, re-format your code, because it is not readable. –  SayusiAndo Mar 28 at 9:50
1  
Does it print the "No Slots available for "+newdate? If you want to not only exit the for loop, but exit the entire method, you can use return instead of break –  Sam Woods Mar 28 at 16:02
    
Thank you. It worked for me. –  sathiya Mar 28 at 16:13
    
It doesn't have anything in common with Selenium or WebDriver. This is a question about Java. Could you remove the tags and correct the title accordingly? –  dzieciou Apr 27 at 16:26

1 Answer 1

If could you try updating your for loop so it breaks out if the element you are iterating through is present.

for (int i = 1; IsElementPresent(By.ClassName("datepickerSunday")")) == true; i++)
                {
                    what your are looping

This might also help reduce the number of waits you are using

p.s. if you put your question outside the code-block it will make it easier to see what your issue is and for others to search it too

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.