javaseleniumselenium-webdriverautomated-teststakesscreenshot

screenshot is getting overwrite in next iteration of for loop or may be not taking screenshot in 2nd iteration


I have list size of 10 items and a for loop which has 2 iterations so in the first iteration selenium captures the screenshot of all 10 items and but in 2nd iteration i don't either it's overwriting the all 10 screenshots captured in the first iteration or it is not capturing any screenshot for the next 10 items remaining, It has 10 items in 1st iteration and then in the 2nd it has total 20 so there should be 20 screenshots but i'm getting only 10, below is the code snippet, if anybody can help or suggest something what i'm doing wrong -:

for(int i=1; i<=sheet.getLastRowNum(); i++) {   

if(SearchKey.equals(result.getText()))
     {        
        System.out.println("Search is pass!");
        Thread.sleep(3000);
        WebElement link = wait.until(ExpectedConditions.elementToBeClickable(By.className("search-result__result-link")));
        link.click();
        driver.manage().timeouts().implicitlyWait(90, TimeUnit.SECONDS);
        Thread.sleep(3000);
        WebElement next = driver.findElement(By.partialLinkText("employees on LinkedIn"));
        next.click();
        Thread.sleep(3000);
        List<WebElement> list= driver.findElements(By.cssSelector(".search-results__list > li"));
        System.out.println("Total number items :"+list.size());
        
        //Scroll and capture screenshot
        for(j= 1; j<=list.size(); j++)
        {
            WebElement elements= driver.findElement(By.cssSelector(".search-results__list > li"));    
            
            //Get entire page screenshot
            File screenshots = ((TakesScreenshot)driver).getScreenshotAs(OutputType.FILE);
            BufferedImage  fullImg = ImageIO.read(screenshots);
            
            // Get the location of element on the page
            Point point = Decision_Maker.getLocation();
            
            //Get width and height of the element
            int eleWidth =  Decision_Maker.getSize().getWidth();
            int eleHeight = Decision_Maker.getSize().getHeight();
            
            //Crop the entire page screenshot to get only element screenshot
            BufferedImage eleScreenshot= fullImg.getSubimage(point.getX(), point.getY(),
                eleWidth, eleHeight);
            String location = "E:\\Screenshots\\";              
            Thread.sleep(3000);
            
            //Scroll when element gets hide
            JavascriptExecutor js = (JavascriptExecutor) driver;
            js.executeScript("arguments[0].scrollIntoView();", elements);  
            Thread.sleep(3000);         
            
            FileUtils.copyFile(screenshots, new File (location + "img" + j+ ".jpg"));
        }
        int sum = sum + list.size();
        System.out.println("Total number of items available"+sum);
    }   
    
    else {
      System.out.println("Search is fail");
    }
    searchlist.clear();
}

Solution

  • Your outer loop declares i and your inner loop also uses i but it uses the same i that is used by outer loop. This is a really tricky logic that might lead to a lot of unexpected results..

    So unless you are sure you are using the right logic I suggest you to fix the loops in order to use different variables and build the screenshot name using both those variables so that they would be unique. Like:

    for(int i=...){
      for(int j=...){
         FileUtils.copyFile(screenshots, new File (location + "img" + i + "-" + j + ".jpg"));
      }
    }