Use this below class. It worked for me perfectly on Chrome.
public class ChromeDownloadsPage {
@FindBy(xpath = "//html/body/downloads-manager")
private WebElement downloadsManager;
protected WebDriver driver;
public ChromeDownloadsPage(WebDriver driver) {
PageFactory.initElements(driver, this);
this.driver =driver;
}
public List<ChromeObjects> getChromeDownloadDetails() {
List<ChromeObjects> chromeDetailsList = new ArrayList<>();
for(WebElement e : getDownloadItems()) {
WebElement downloadShadowItemElement = CommonFunctions.expandShadowRootElement(e, driver);
//Wait until download is completed
CommonFunctions.waitForElementToBeDisappear(driver, downloadShadowItemElement.findElements(By.className("show-progress")));
WebElement downloadContent = downloadShadowItemElement.findElement(By.id("content"));
WebElement downloadDetails = downloadContent.findElement(By.id("details"));
WebElement downloadArea = downloadDetails.findElement(By.id("title-area"));
//Download File
WebElement downloadFileElement = downloadArea.findElement(By.id("file-link"));
//File Download Status
WebElement fileDownloadStatusElement = downloadArea.findElement(By.id("tag"));
//Assigning the File name and Status
ChromeObjects obj = new ChromeObjects();
obj.setDownloadFileName(downloadFileElement.getText());
String tempStatus = CommonFunctions.waitForElement(driver, fileDownloadStatusElement).getText();
if(tempStatus.equalsIgnoreCase("")) {
obj.setDownloadStatus("Completed");
} else {
obj.setDownloadStatus(tempStatus);
}
chromeDetailsList.add(obj);
}
return chromeDetailsList;
}
//Get Shadow DOM WebElement of Download Item
private List<WebElement> getDownloadItems() {
WebElement downloadsManagerShadowElement = CommonFunctions.expandShadowRootElement(downloadsManager, driver);
WebElement downloadListManager = downloadsManagerShadowElement.findElement(By.id("downloads-list"));
return downloadListManager.findElements(By.tagName("downloads-item"));
}
public ChromeDownloadsPage openDownloadsPage() {
//Open New Tab
openNewTab();
//Switching to New Tab
CommonFunctions.switchToNextWindow(driver);
//Hitting Download URL
openUrl();
moveToDownloads();
return new ChromeDownloadsPage(driver);
}
private ChromeDownloadsPage openUrl() {
driver.navigate().to(GlobalThings.downloadUrl);
return new ChromeDownloadsPage(driver);
}
private void openNewTab() {
try {
Robot r = new Robot();
r.keyPress(KeyEvent.VK_CONTROL);
r.keyPress(KeyEvent.VK_T);
r.keyRelease(KeyEvent.VK_CONTROL);
r.keyRelease(KeyEvent.VK_T);
} catch (AWTException e) {
e.printStackTrace();
}
}
private void moveToDownloads() {
Robot r;
try {
for (int i = 0; i < 4; i++) {
r = new Robot();
r.keyPress(KeyEvent.VK_TAB);
r.keyRelease(KeyEvent.VK_TAB);
}
} catch (AWTException e) {
e.printStackTrace();
}
}
public class ChromeObjects {
private String downloadFileName;
private String downloadStatus;
public String getDownloadFileName() {
return downloadFileName;
}
private void setDownloadFileName(String downloadFileName) {
this.downloadFileName = downloadFileName;
}
public String getDownloadStatus() {
return downloadStatus;
}
private void setDownloadStatus(String downloadStatus) {
this.downloadStatus = downloadStatus;
}
}
}
------------------------------------------------------------------------------------------------------------------------------------------