|
| 1 | +package anhtester.com.testcases; |
| 2 | + |
| 3 | +import anhtester.com.common.BaseTest; |
| 4 | +import anhtester.com.drivers.DriverManager; |
| 5 | +import anhtester.com.helpers.SystemsHelper; |
| 6 | +import anhtester.com.keywords.WebUI; |
| 7 | +import org.openqa.selenium.By; |
| 8 | +import org.testng.Assert; |
| 9 | +import org.testng.annotations.Test; |
| 10 | + |
| 11 | +import java.awt.*; |
| 12 | +import java.awt.datatransfer.StringSelection; |
| 13 | +import java.awt.event.KeyEvent; |
| 14 | + |
| 15 | +public class HandleUploadFile extends BaseTest { |
| 16 | + |
| 17 | + @Test |
| 18 | + public void testUploadFileWithSendKeys() throws InterruptedException { |
| 19 | + DriverManager.getDriver().get("https://cgi-lib.berkeley.edu/ex/fup.html"); |
| 20 | + |
| 21 | + Thread.sleep(2000); |
| 22 | + |
| 23 | + By inputFileUpload = By.xpath("//input[@name='upfile']"); |
| 24 | + |
| 25 | + //DriverManager.getDriver().findElement(inputFileUpload).sendKeys("D:\\EmailFetch.txt"); |
| 26 | + //WebUI.setText(inputFileUpload, System.getProperty("user.dir") + "/src/test/resources/datatest/Sample_File_Word.docx"); |
| 27 | + WebUI.setText(inputFileUpload, SystemsHelper.getCurrentDir() + "src/test/resources/datatest/Sample_File_Word.docx"); |
| 28 | + |
| 29 | + Thread.sleep(4000); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void testUploadFile1() throws InterruptedException { |
| 34 | + DriverManager.getDriver().get("https://files.fm/"); |
| 35 | + |
| 36 | + Thread.sleep(2000); |
| 37 | + |
| 38 | + By inputFile = By.xpath("//input[@id='file_upload']"); |
| 39 | + //Không thể dùng sendKeys để uplaod trong trường hợp này (đặc biệt) |
| 40 | + WebUI.setText(inputFile, SystemsHelper.getCurrentDir() + "src/test/resources/datatest/Sample_File_Word.docx"); |
| 41 | + |
| 42 | + Thread.sleep(2000); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testUploadFile2() throws InterruptedException { |
| 47 | + DriverManager.getDriver().get("https://files.fm/"); |
| 48 | + |
| 49 | + Thread.sleep(2000); |
| 50 | + |
| 51 | + By textOnPage = By.xpath("//div[@id='file_select_dragndrop_text']"); |
| 52 | + By divFileUpload = By.xpath("//div[@id='uploadifive-file_upload']"); |
| 53 | + By inputFileUpload = By.xpath("//div[@id='file_select_button']//input[@id='file_upload']"); |
| 54 | + |
| 55 | + //Dùng dấu \\ để phân cách folder nếu link trực tiếp trong ổ đĩa máy tính Windows |
| 56 | + String filePath = SystemsHelper.getCurrentDir() + "src\\test\\resources\\datatest\\Sample_File_Word.docx"; |
| 57 | + |
| 58 | + //Click để mở form upload |
| 59 | + DriverManager.getDriver().findElement(divFileUpload).click(); |
| 60 | + Thread.sleep(3); |
| 61 | + |
| 62 | + // Khởi tạo Robot class |
| 63 | + Robot rb = null; |
| 64 | + try { |
| 65 | + rb = new Robot(); |
| 66 | + } catch (AWTException e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } |
| 69 | + |
| 70 | + // Copy File path vào Clipboard |
| 71 | + StringSelection str = new StringSelection(filePath); |
| 72 | + Toolkit.getDefaultToolkit().getSystemClipboard().setContents(str, null); |
| 73 | + |
| 74 | + Thread.sleep(1000); |
| 75 | + |
| 76 | + // Nhấn Control+V để dán |
| 77 | + rb.keyPress(KeyEvent.VK_CONTROL); |
| 78 | + rb.keyPress(KeyEvent.VK_V); |
| 79 | + |
| 80 | + // Xác nhận Control V trên |
| 81 | + rb.keyRelease(KeyEvent.VK_CONTROL); |
| 82 | + rb.keyRelease(KeyEvent.VK_V); |
| 83 | + |
| 84 | + Thread.sleep(1000); |
| 85 | + |
| 86 | + // Nhấn Enter |
| 87 | + rb.keyPress(KeyEvent.VK_ENTER); |
| 88 | + rb.keyRelease(KeyEvent.VK_ENTER); |
| 89 | + |
| 90 | + Thread.sleep(4000); |
| 91 | + String statusText = WebUI.getTextElement(By.xpath("//div[@id='upload_stats_text']")); |
| 92 | + Assert.assertTrue(statusText.contains("Selected files 1"), "Fail. Can not upload file."); |
| 93 | + |
| 94 | + } |
| 95 | + |
| 96 | +} |
0 commit comments