-1

When I Clicked download button using it opens a download pop up in firefox. Its running correctly and saving the files but when i iterate in loop its not saving instead its opening the file.Any solution for below mentioned code it ?

for (int j = 0; j < StoreSelectedYear_size; j++) {
            System.out.println(StoreSelectedYear.get(j));
            YearSelection(StoreSelectedYear.get(j));
            Thread.sleep(5000);

            filedownload(i);

        }
        StoreSelectedYear.clear();
    }

}

public void YearSelection(String StoreSelectedYearStr) throws InterruptedException, AWTException {
    Select yearselction = new Select(driver.findElement(By.cssSelector("#u14_input")));
    yearselction.selectByVisibleText(StoreSelectedYearStr);
    Thread.sleep(5000);

}

public void filedownload(int i) throws AWTException, InterruptedException {
    driver.findElement(By.xpath("//button[@id='export']")).click();
    Thread.sleep(6000);
    Robot robot = new Robot();
    robot.delay(5000);
    // Thread.sleep throws InterruptedException

    if (i == 0) {
        robot.keyPress(KeyEvent.VK_DOWN);
        robot.delay(2000);
        robot.keyPress(KeyEvent.VK_TAB);

        robot.keyPress(KeyEvent.VK_TAB);

        robot.keyPress(KeyEvent.VK_TAB);

        robot.keyPress(KeyEvent.VK_ENTER);

    }

Firefox save image:

Firefox save image

kishor
  • 3
  • 1
  • 5
  • Your problem statement doesn't tells us about the problem that you're having. please edit the question with the proper problem statement and what error you're facing for better debugging. – demouser123 Jun 16 '17 at 15:24

1 Answers1

0

You could try an alternate solution by getting the src attribute of the download element, and then using an http library such as HttpUnit to make a direct request to download the file.

This has the added benefit that it will do the work of giving you the file as an object easier if you need to validate or manipulate it within your tests if that matches your use case.

I suggest this because if you're doing this for a job, then utilizing a solution that doesn't require manipulating screen coordinates and window position is often always a better option. And there is likely little value in testing the download prompt, since it doesn't exist in the sandbox with your app.

You can retrieve the cookies in use by your current selenium test session with this code just in case this is compelling to you.

Set<Cookie> seleniumCookies = driver.manage().getCookies();
    org.apache.http.client.CookieStore cookieStore = new org.apache.http.client.BasicCookieStore();

    for (Cookie seleniumCookie : seleniumCookies) {
        org.apache.http.impl.cookie.BasicClientCookie basicClientCookie =
                new BasicClientCookie(seleniumCookie.getName(), seleniumCookie.getValue());
        basicClientCookie.setDomain(seleniumCookie.getDomain());
        basicClientCookie.setExpiryDate(seleniumCookie.getExpiry());
        basicClientCookie.setPath(seleniumCookie.getPath());
        cookieStore.addCookie(basicClientCookie);
    }

    return cookieStore;

This will essentially convert your cookies into a form usable by the apache http library, which you can use to make requests to your app without the app realizing you stepped out of selenium. And if your requests make changes to the cookies in this example, then you can re-set the cookies in selenium afterwards with the new versions.

Julian
  • 1,665
  • 2
  • 15
  • 33