Problem
1.How to Configure download path of browser
2.Upload File
3.Download File
4.To Verify File Exists
5.Finally to Delete File
Step1
To Integrate
String downloadPath = System.getProperty("user.dir");
WebDriverManager.chromedriver().setup();
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
//property in chrome
chromePrefs.put("download.default_directory", downloadPath);
//chrome options
ChromeOptions options = new ChromeOptions();
//prefs is a string/object
options.setExperimentalOption("prefs", chromePrefs);
WebDriver driver = new ChromeDriver(options);
1.How to Configure download path of browser
2.Upload File
3.Download File
4.To Verify File Exists
5.Finally to Delete File
Step1
To Integrate
String downloadPath = System.getProperty("user.dir");
WebDriverManager.chromedriver().setup();
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
//property in chrome
chromePrefs.put("download.default_directory", downloadPath);
//chrome options
ChromeOptions options = new ChromeOptions();
//prefs is a string/object
options.setExperimentalOption("prefs", chromePrefs);
WebDriver driver = new ChromeDriver(options);
Step2:
To Upload file Follow this
driver.get("https://altoconvertpdftojpg.com/");
driver.manage().window().maximize();
Thread.sleep(3000);
driver.findElement(By.xpath("//span[contains(text(),'Choose File')]")).click();
Thread.sleep(2000);
Runtime.getRuntime().exec("D:\\UPC\\Selenium\\AutoIt\\fileUpload.exe");
WebDriverWait wait = new WebDriverWait(driver, 20);
Step 3:
To Download File
JavascriptExecutor js = (JavascriptExecutor) driver;
wait.until(ExpectedConditions
.visibilityOfElementLocated(By.xpath("//button[@class='g-btn g-btn--primary g-btn--medium']")));
js.executeScript("arguments[0].click();",
driver.findElement(By.xpath("//button[@class='g-btn g-btn--primary g-btn--medium']")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Download Now")));
js.executeScript("arguments[0].click();", driver.findElement(By.linkText("Download Now")));
Thread.sleep(5000);
File f = new File(downloadPath + "/converted.zip");
Step 4 and 5
To Verify and Delete
if (f.exists()) {
Assert.assertTrue(f.exists());
System.out.println("file found");
if (f.delete()) {
System.out.println("file deleted");
}
}
Complete Code
package automationTest;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import io.github.bonigarcia.wdm.WebDriverManager;
public class DownloadFile {
public static void main(String[] args) throws InterruptedException, IOException {
// System.setProperty("webdriver.chrome.driver","D:\\UPC\\Selenium\\Repository\\Project1\\drivers\\chromedriver1.exe");
String downloadPath = System.getProperty("user.dir");
WebDriverManager.chromedriver().setup();
HashMap<String, Object> chromePrefs = new HashMap<String, Object>();
chromePrefs.put("profile.default_content_settings.popups", 0);
chromePrefs.put("download.default_directory", downloadPath);
ChromeOptions options = new ChromeOptions();
options.setExperimentalOption("prefs", chromePrefs);
WebDriver driver = new ChromeDriver(options);
driver.get("https://altoconvertpdftojpg.com/");
driver.manage().window().maximize();
Thread.sleep(3000);
driver.findElement(By.xpath("//span[contains(text(),'Choose File')]")).click();
Thread.sleep(2000);
Runtime.getRuntime().exec("D:\\UPC\\Selenium\\AutoIt\\fileUpload.exe");
WebDriverWait wait = new WebDriverWait(driver, 20);
JavascriptExecutor js = (JavascriptExecutor) driver;
wait.until(ExpectedConditions
.visibilityOfElementLocated(By.xpath("//button[@class='g-btn g-btn--primary g-btn--medium']")));
js.executeScript("arguments[0].click();",
driver.findElement(By.xpath("//button[@class='g-btn g-btn--primary g-btn--medium']")));
wait.until(ExpectedConditions.visibilityOfElementLocated(By.linkText("Download Now")));
js.executeScript("arguments[0].click();", driver.findElement(By.linkText("Download Now")));
Thread.sleep(5000);
File f = new File(downloadPath + "/converted.zip");
if (f.exists()) {
Assert.assertTrue(f.exists());
System.out.println("file found");
if (f.delete()) {
System.out.println("file deleted");
}
}
}
}
No comments:
Post a Comment