[Java – Webdriver 05] Download và kiểm tra/ xóa file đã tải về

Yêu cầu:
Thông thường khi chúng ta viết kịch bản cho chức năng download này sẽ có 2 trường hợp xảy ra:

  • Click vào 1 element bất kì -> tải về file có tên file cố định
  • Click vào 1 element bất kì -> tải về file có tên file ngẫu nhiên (hệ thống tự động random)

Vậy làm thế nào để:

  • Kiểm tra file đã được tải về thành công?
  • Chỉ có duy nhất 1 file được tải về trong thư mục?
  • Xóa file đã tải về thành công?

Giải pháp:

Hầu hết các file tải về đều có một định dạng/ đuôi mở rộng nhất định. Ví dụ như các dạng file như: Word (.doc/ .docx), Excel (.xls/ .xlsx/ .csv), Pdf (.pdf), nén (.rar/ .zip),… Với trường hợp tải về tên file cố định thì chỉ cần verify fullname của file đó có tồn tại trong thư mục tải về và xóa đi (cái này thì cơ bản), còn trường hợp hệ thống tự random chúng ta có thể theo 1 số step sau:

  • Trước khi tải về -> xóa toàn bộ file trong thư mục
  • Sau khi tải về -> kiểm tra số lượng file trong thư mục = 1
  • Verify đuôi mở rộng của tên file tải về
  • Xóa đuôi mở rộng của tên file tải về
  • Kiểm tra số lượng file trong thư mục = 0 (có hoặc không)

Source demo:

Mình sẽ viết demo trên site: http://spreadsheetpage.com/index.php/file/C35/P10/

package seleniumWebDriver;

import java.io.File;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.Assert;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class WebDriver05_DownloadDeleteFileExists {
WebDriver driver;

@BeforeClass
public void setUp() {
System.setProperty(“webdriver.chrome.driver”,
“\\11-AutomationTestingDemo\\lib\\chromedriver.exe”);
driver = new ChromeDriver();
}

@Test
public void test01_DownloadAndDeleteFileFullName() throws Exception {
String file = “smilechart.xls”;
driver.navigate().to(“http://spreadsheetpage.com/index.php/file/C35/P10/”);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

//Xóa toàn bộ file trong thư mục
deleteAllFileInFolder();

//Click vào title chứa file tải về
driver.findElement(By.xpath(“//a[contains(text(),’smilechart.xls’)]”)).click();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

//Verify có 1 file được tải về
waitForDownloadFileFullnameCompleted(file);

//Đếm số lượng file trong thư mục sau khi tải về
int countFileBeforeDelete = countFilesInDirectory();
System.out.println(“SAU KHI TAI VE: ” + countFileBeforeDelete);

//Verify số lượng file tải về bằng 1
Assert.assertEquals(countFileBeforeDelete, 1);

//Xóa file đã tải về
deleteFileFullName(file);

//Đếm số lượng file trong thư mục sau khi xóa
int countFileAfterDelete = countFilesInDirectory();
System.out.println(“SAU KHI XOA: ” + countFileAfterDelete);

//Verify số lượng file tải về bằng 0
Assert.assertEquals(countFileAfterDelete, 0);
}

@Test
public void test02_DownloadAndDeleteFileContainName() throws Exception {
String file = “.xls”;
driver.navigate().to(“http://spreadsheetpage.com/index.php/file/C35/P10/”);
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
driver.manage().window().maximize();

//Xóa toàn bộ file trong thư mục
deleteAllFileInFolder();

//Click vào title chứa file tải về
driver.findElement(By.xpath(“//a[contains(.,’lister.xls’)]”)).click();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

//Verify có 1 file được tải về chứa đuôi file mở rộng là .xls
waitForDownloadFileContainsNameCompleted(file);

//Đếm số lượng file trong thư mục sau khi tải về
int countFileBeforeDelete = countFilesInDirectory();
System.out.println(“SAU KHI TAI VE: ” + countFileBeforeDelete);

//Verify số lượng file tải về bằng 1
Assert.assertEquals(countFileBeforeDelete, 1);
deleteFileContainName(file);

//Đếm số lượng file trong thư mục sau khi xóa
int countFileAfterDelete = countFilesInDirectory();
System.out.println(“SAU KHI XOA: ” + countFileAfterDelete);

//Verify số lượng file tải về bằng 0
Assert.assertEquals(countFileAfterDelete, 0);
}

public void deleteAllFileInFolder() {
try {
String pathFolderDownload = getPathContainDownload();
File file = new File(pathFolderDownload);
File[] listOfFiles = file.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
new File(listOfFiles[i].toString()).delete();
}
}
} catch (Exception e) {
System.out.print(e.getMessage());
}
}

public String getPathContainDownload() {
String path = “”;
String machine_name;
machine_name = System.getProperty(“user.home”);
path = String.format(“%s\\Downloads\\”, machine_name);
return path;
}

public void waitForDownloadFileFullnameCompleted(String fileName)
throws Exception {
int i = 0;
while (i < 30) {
boolean exist = isFileExists(fileName);
if (exist == true) {
i = 30;
}
Thread.sleep(500);
i = i + 1;
}
}

public boolean isFileExists(String file) {
try {
String pathFolderDownload = getPathContainDownload();
File files = new File(pathFolderDownload + file);
boolean exists = files.exists();
return exists;
} catch (Exception e) {
System.out.print(e.getMessage());
return false;
}
}

public int countFilesInDirectory() {
String pathFolderDownload = getPathContainDownload();
File file = new File(pathFolderDownload);
int i = 0;
for (File listOfFiles : file.listFiles()) {
if (listOfFiles.isFile()) {
i++;
}
}
return i;
}

public void deleteFileFullName(String fileName) {
if (isFileExists(fileName)) {
deleteFullName(fileName);
}
}

public void deleteFullName(String fileName) {
try {
if (isFileExists(fileName)) {
String pathFolderDownload = getPathContainDownload();
File files = new File(pathFolderDownload + fileName);
files.delete();
}
} catch (Exception e) {
System.out.print(e.getMessage());
}
}

public void waitForDownloadFileContainsNameCompleted(String fileName)
throws Exception {
int i = 0;
while (i < 30) {
boolean exist = isFileContain(fileName);
if (exist == true) {
i = 30;
}
Thread.sleep(500);
i = i + 1;
}
}

public boolean isFileContain(String fileName) {
try {
boolean flag = false;
String pathFolderDownload = getPathContainDownload();
File dir = new File(pathFolderDownload);
File[] files = dir.listFiles();
if (files == null || files.length == 0) {
flag = false;
}
for (int i = 1; i < files.length; i++) {
if (files[i].getName().contains(fileName)) {
flag = true;
}
}
return flag;
} catch (Exception e) {
System.out.print(e.getMessage());
return false;
}
}

public void deleteFileContainName(String fileName) {
deleteContainName(fileName);
}

public void deleteContainName(String fileName) {
try {
String files;
String pathFolderDownload = getPathContainDownload();
File file = new File(pathFolderDownload);
File[] listOfFiles = file.listFiles();
for (int i = 0; i < listOfFiles.length; i++) {
if (listOfFiles[i].isFile()) {
files = listOfFiles[i].getName();
if (files.contains(fileName)) {
new File(listOfFiles[i].toString()).delete();
}
}
}
} catch (Exception e) {
System.out.print(e.getMessage());
}
}

@AfterClass
public void tearDown() {
driver.quit();
}
}

Kết quả khi run demo:

Video demo: