Skip to content

Commit

Permalink
Update Upload File
Browse files Browse the repository at this point in the history
  • Loading branch information
vothaian committed Oct 22, 2022
1 parent 3c4ec27 commit db9651b
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 159 deletions.
207 changes: 51 additions & 156 deletions ExtentReports/ExtentReport.html

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions src/main/java/anhtester/com/helpers/Helpers.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
package anhtester.com.helpers;

import java.io.File;

public class Helpers {

public static String getCurrentDir() {
String current = System.getProperty("user.dir") + "/";
//File.separator = ký tự phân cách thư mục máy tính
String current = System.getProperty("user.dir") + File.separator;
return current;
}

}
87 changes: 87 additions & 0 deletions src/test/java/anhtester/com/pages/UploadFileCMSPage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package anhtester.com.pages;

import anhtester.com.helpers.Helpers;
import anhtester.com.utils.WebUI;
import org.openqa.selenium.By;

import java.awt.*;
import java.awt.datatransfer.StringSelection;
import java.awt.event.KeyEvent;

public class UploadFileCMSPage {

//Login
private By buttonCopy = By.xpath("//button[normalize-space()='Copy']");
private By buttonLogin = By.xpath("//button[normalize-space()='Login']");

//Category
private By menuProducts = By.xpath("//span[normalize-space()='Products']");
private By menuCategory = By.xpath("//span[normalize-space()='Category']");
private By butttonAddNewCategory = By.xpath("//span[normalize-space()='Add New category']");
private By chooseFile = By.xpath("//label[normalize-space()='Banner (200x200)']/following-sibling::div//div[normalize-space()='Choose file']");
private By buttonUploadNew = By.xpath("//a[normalize-space()='Upload New']");
private By buttonBrowse = By.xpath("//button[normalize-space()='Browse']");
private By buttonAddFiles = By.xpath("//button[normalize-space()='Add Files']");
private By messageComplete = By.xpath("//div[@class='uppy-StatusBar-statusPrimary']");

private By buttonSelectFile = By.xpath("//a[normalize-space()='Select File']");
private By inputSearchFile = By.xpath("//input[@placeholder='Search your files']");
private By itemFile = By.xpath("(//div[@class='card-body']//h6)[1]");

public void loginCMS() {
WebUI.openURL("https://demo.activeitzone.com/ecommerce/login");
WebUI.clickElement(buttonCopy);
WebUI.clickElement(buttonLogin);
}

public void uploadFileInCategory() {
WebUI.waitForPageLoaded();
WebUI.clickElement(menuProducts);
WebUI.clickElement(menuCategory);
WebUI.clickElement(butttonAddNewCategory);
WebUI.clickElement(chooseFile);
WebUI.clickElement(buttonUploadNew);
WebUI.clickElement(buttonBrowse); //Mở form select file từ local PC

//Dán đoạn code dùng Robot class để hành động với bàn phím
// Khởi tạo Robot class
Robot rb = null;
try {
rb = new Robot();
} catch (AWTException e) {
e.printStackTrace();
}

String filePath = Helpers.getCurrentDir() + "datatest\\Selenium4_Upload.png";

// Copy File path vào Clipboard
StringSelection str = new StringSelection(filePath);
Toolkit.getDefaultToolkit().getSystemClipboard().setContents(str, null);

WebUI.sleep(1);

// Nhấn Control+V để dán
rb.keyPress(KeyEvent.VK_CONTROL);
rb.keyPress(KeyEvent.VK_V);

// Xác nhận Control V trên
rb.keyRelease(KeyEvent.VK_CONTROL);
rb.keyRelease(KeyEvent.VK_V);

WebUI.sleep(2);

// Nhấn Enter
rb.keyPress(KeyEvent.VK_ENTER);
rb.keyRelease(KeyEvent.VK_ENTER);

WebUI.sleep(3);
WebUI.clickElement(buttonSelectFile);
WebUI.setText(inputSearchFile, "Selenium4_Upload");
WebUI.sleep(1);
WebUI.clickElement(itemFile);
WebUI.sleep(1);
WebUI.clickElement(buttonAddFiles);
WebUI.sleep(2);
}

}
30 changes: 30 additions & 0 deletions src/test/java/anhtester/com/testcases/UploadFileCMS.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package anhtester.com.testcases;

import anhtester.com.common.BaseTest;
import anhtester.com.helpers.Helpers;
import anhtester.com.pages.UploadFileCMSPage;
import anhtester.com.utils.WebUI;
import org.openqa.selenium.By;
import org.testng.annotations.Test;

public class UploadFileCMS extends BaseTest {

@Test
public void testUploadFileWithSendKeys() throws InterruptedException {
WebUI.openURL("https://cgi-lib.berkeley.edu/ex/fup.html");
WebUI.waitForPageLoaded();
By inputFileUpload = By.xpath("//input[@name='upfile']");

//DriverManager.getDriver().findElement(inputFileUpload).sendKeys(Helpers.getCurrentDir() + "datatest/Selenium4_Upload.png");
WebUI.setText(inputFileUpload, Helpers.getCurrentDir() + "datatest/Selenium4_Upload.png");
Thread.sleep(3000);
}

@Test
public void testUploadFileInCategoryCMS() {
UploadFileCMSPage uploadFileCMSPage = new UploadFileCMSPage();
uploadFileCMSPage.loginCMS();
uploadFileCMSPage.uploadFileInCategory();
}

}
2 changes: 1 addition & 1 deletion suites/ParallelLoginTest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<class name="anhtester.com.testcases.LoginTest">
<methods>
<include name="testLoginFromDataProvider"/>
<include name="testLoginWithUsernameInValid"/>
<!-- <include name="testLoginWithUsernameInValid"/>-->

</methods>
</class>
Expand Down

0 comments on commit db9651b

Please sign in to comment.