NỘI DUNG BÀI HỌC
1. Login CRM System
Website CRM: https://crm.anhtester.com/admin/authentication
Email: admin@example.com
Password: 123456
2. Mở trang Customers list
3. Thêm mới một Customer (Client)
4. Search lại Customer và Verify
5. Open Projects để verify lại Customer vừa Add
Projects -> New project -> Dropdown Customer
🔆 Thao tác các chức năng còn lại
Add Contact for Customer, Edit, Delete, Import, Export,...
✳️ Code mẫu Add Customer
Hiện tại chúng ta có WebUI keyword và đang viết các hàm Waits vào. Giờ chúng ta bổ sung thêm vài hàm vào nhé.
An bổ sung hàm waitForPageLoaded()
giúp chờ đợi trang load xong mới thao tác.
🔅 WebUI class:
package anhtester.com.keywords;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.ExpectedCondition;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.Assert;
import java.time.Duration;
import java.util.List;
public class WebUI {
public static void sleep(double second) {
try {
Thread.sleep((long) (1000 * second));
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
public static void waitForElementVisible(WebDriver driver, By by, int second) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(second), Duration.ofMillis(500));
wait.until(ExpectedConditions.visibilityOfElementLocated(by));
}
public static void waitForElementPresent(WebDriver driver, By by, int second) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(second));
wait.until(ExpectedConditions.presenceOfElementLocated(by));
}
public static void waitForElementClickable(WebDriver driver, By by, int second) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(second));
wait.until(ExpectedConditions.elementToBeClickable(by));
}
public static Boolean checkElementExist(WebDriver driver, By by) {
List<WebElement> listElement = driver.findElements(by);
if (listElement.size() > 0) {
System.out.println("Element " + by + " existing.");
return true;
} else {
System.out.println("Element " + by + " NOT exist.");
return false;
}
}
public static Boolean checkElementExist(WebDriver driver, String xpath) {
List<WebElement> listElement = driver.findElements(By.xpath(xpath));
if (listElement.size() > 0) {
System.out.println("Element " + xpath + " existing.");
return true;
} else {
System.out.println("Element " + xpath + " NOT exist.");
return false;
}
}
/**
* Wait for Page loaded
* Chờ đợi trang tải xong (Javascript tải xong)
*/
public static void waitForPageLoaded(WebDriver driver) {
WebDriverWait wait = new WebDriverWait(driver, Duration.ofSeconds(30), Duration.ofMillis(500));
JavascriptExecutor js = (JavascriptExecutor) driver;
//Wait for Javascript to load
ExpectedCondition<Boolean> jsLoad = new ExpectedCondition<Boolean>() {
@Override
public Boolean apply(WebDriver driver) {
return js.executeScript("return document.readyState").toString().equals("complete");
}
};
//Check JS is Ready
boolean jsReady = js.executeScript("return document.readyState").toString().equals("complete");
//Wait Javascript until it is Ready!
if (!jsReady) {
System.out.println("Javascript is NOT Ready.");
//Wait for Javascript to load
try {
wait.until(jsLoad);
} catch (Throwable error) {
error.printStackTrace();
Assert.fail("FAILED. Timeout waiting for page load.");
}
}
}
}
🔅 BaseTest class:
package anhtester.com.common;
import anhtester.com.keywords.WebUI;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import java.time.Duration;
public class BaseTest {
public WebDriver driver;
@BeforeMethod
public void createBrowser() {
System.out.println("Start Chrome browser from BaseTest...");
driver = new ChromeDriver();
driver.manage().window().maximize();
//Chờ đợi trang load xong (trong 40s)
driver.manage().timeouts().pageLoadTimeout(Duration.ofSeconds(40));
}
@AfterMethod
public void closeBrowser() {
WebUI.sleep(2);
System.out.println("Close browser from BaseTest...");
driver.quit();
}
}
🔅 TestManageCustomers class:
package anhtester.com.Bai16_ThucHanhCRM;
import anhtester.com.common.BaseTest;
import anhtester.com.keywords.WebUI;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.testng.Assert;
import org.testng.annotations.Test;
public class TestManageCustomers extends BaseTest {
public void loginPerfexCRM() {
driver.get("https://crm.anhtester.com/admin/authentication");
driver.findElement(By.xpath("//input[@id='email']")).sendKeys("admin@example.com");
driver.findElement(By.xpath("//input[@id='password']")).sendKeys("123456");
driver.findElement(By.xpath("//button[normalize-space()='Login']")).click();
WebUI.waitForPageLoaded(driver);
}
private String CUSTOMER_NAME = "CMC Global";
private String WEBSITE = "https://cmcglobal.com.vn/vi/home-vi/";
@Test
public void addCustomer() {
loginPerfexCRM();
driver.findElement(By.xpath("//span[normalize-space()='Customers']")).click();
WebUI.waitForPageLoaded(driver);
driver.findElement(By.xpath("//a[normalize-space()='New Customer']")).click();
driver.findElement(By.id("company")).sendKeys(CUSTOMER_NAME);
driver.findElement(By.id("vat")).sendKeys("10");
driver.findElement(By.id("phonenumber")).sendKeys("0123456789");
driver.findElement(By.id("website")).sendKeys(WEBSITE);
WebUI.sleep(1);
driver.findElement(By.xpath("//label[@for='groups_in[]']/following-sibling::div")).click();
WebUI.sleep(1);
driver.findElement(By.xpath("//label[@for='groups_in[]']/following-sibling::div//input[@type='search']")).sendKeys("Gold", Keys.ENTER);
WebUI.sleep(1);
driver.findElement(By.xpath("//label[@for='groups_in[]']/following-sibling::div")).click();
WebUI.sleep(1);
driver.findElement(By.id("address")).sendKeys("Viet Nam");
driver.findElement(By.id("city")).sendKeys("Can Tho");
driver.findElement(By.id("state")).sendKeys("Can Tho");
driver.findElement(By.id("zip")).sendKeys("92000");
WebUI.sleep(1);
driver.findElement(By.xpath("//label[@for='country']/following-sibling::div")).click();
WebUI.sleep(1);
driver.findElement(By.xpath("//label[@for='country']/following-sibling::div//input[@type='search']")).sendKeys("Vietnam", Keys.ENTER);
WebUI.sleep(1);
driver.findElement(By.xpath("//div[@id='profile-save-section']//button[normalize-space()='Save']")).click();
WebUI.waitForPageLoaded(driver);
//Kiểm tra Save thành công chuyển hướng đến trang Customer Details
Assert.assertTrue(WebUI.checkElementExist(driver, By.xpath("//a[normalize-space()='Customer Details']")), "Can not navigate to Customer Details page.");
verifyCustomerAdded();
}
public void verifyCustomerAdded() {
//Click mở trang Customers lại
driver.findElement(By.xpath("//span[normalize-space()='Customers']")).click();
WebUI.waitForPageLoaded(driver);
//Search tên customer vừa add
driver.findElement(By.xpath("//input[@class='form-control input-sm']")).sendKeys(CUSTOMER_NAME);
WebUI.sleep(1);
WebUI.waitForPageLoaded(driver);
WebUI.waitForElementVisible(driver, By.xpath("//tbody/tr[1]/td[3]/a"), 10);
//Get Text cột customer name
String getCustomerName = driver.findElement(By.xpath("//tbody/tr[1]/td[3]/a")).getText();
System.out.println(getCustomerName);
//Verify equals với data input
Assert.assertEquals(getCustomerName, CUSTOMER_NAME, "FAILED. Customer Name not match.");
}
}
Yeah code trên An chỉ dừng lại tại check Customer Name ngoài Data Table. Chúng ta cần click vào Name và đi đến trang Customer Details để Assert thông tin chi tiết khác nhé.
Bước tiếp theo là mở Projects > New Project > Verify Dropdown Customer tồn tại