Skip to content

Commit bcfd603

Browse files
committed
Bài 31 - TestListener
1 parent c81372e commit bcfd603

File tree

15 files changed

+461
-11
lines changed

15 files changed

+461
-11
lines changed
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.anhtester.Bai31_TestListener;
2+
3+
import com.anhtester.drivers.DriverManager;
4+
import com.anhtester.listeners.TestListener;
5+
import org.openqa.selenium.WebDriver;
6+
import org.openqa.selenium.chrome.ChromeDriver;
7+
import org.testng.Assert;
8+
import org.testng.SkipException;
9+
import org.testng.annotations.AfterMethod;
10+
import org.testng.annotations.BeforeMethod;
11+
import org.testng.annotations.Listeners;
12+
import org.testng.annotations.Test;
13+
14+
@Listeners(TestListener.class)
15+
public class DemoListener {
16+
WebDriver driver;
17+
18+
@BeforeMethod
19+
public void setupDriver() {
20+
System.out.println("BeforeClass");
21+
driver = new ChromeDriver();
22+
DriverManager.setDriver(driver);
23+
}
24+
25+
@BeforeMethod
26+
public void beforeMethod() {
27+
System.out.println("Đây là BeforeMethod");
28+
}
29+
30+
@Test(priority = 1) //Success Test
31+
public void gotoPage() {
32+
driver.get("https://anhtester.com");
33+
}
34+
35+
@Test(priority = 2) //Failed Test
36+
public void checkTitle() {
37+
driver.get("https://anhtester.com");
38+
String expectedTitle = "Anh Tester";
39+
String originalTitle = driver.getTitle();
40+
Assert.assertEquals(originalTitle, expectedTitle, "Title of the website do not match");
41+
}
42+
43+
@Test(priority = 3) //Skip Test
44+
public void skipTest() {
45+
throw new SkipException("Skipping The Test Method ");
46+
}
47+
48+
@Test(priority = 4) //Failed Test
49+
public void checkURL() {
50+
driver.get("https://anhtester.com");
51+
String expectedTitle = "Anh Tester";
52+
String originalTitle = driver.getCurrentUrl();
53+
Assert.assertEquals(originalTitle, expectedTitle, "Title of the website do not match");
54+
}
55+
56+
@AfterMethod
57+
public void closeDriver() {
58+
driver.quit();
59+
System.out.println("Đây là AfterClass");
60+
}
61+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.anhtester.Bai31_TestListener.pages;
2+
3+
import com.anhtester.drivers.DriverManager;
4+
import com.anhtester.keywords.WebUI;
5+
import org.openqa.selenium.By;
6+
import org.openqa.selenium.Keys;
7+
import org.testng.Assert;
8+
9+
public class CustomerPage {
10+
11+
private By buttonAddNewCustomer = By.xpath("//a[normalize-space()='New Customer']");
12+
private By headerCustomerPage = By.xpath("//span[normalize-space()='Customers Summary']");
13+
private By inputSearch = By.xpath("//div[@id='DataTables_Table_0_filter']//input[@type='search']");
14+
15+
//Trang Customers List
16+
private By buttonImportCustomer = By.xpath("//a[normalize-space()='Import Customers']");
17+
private By buttonContacts = By.xpath("//a[contains(@href, 'clients/all_contacts')]");
18+
private By inputSearchCustomers = By.xpath("//div[@id='DataTables_Table_0_filter']//input");
19+
private By firstItemCustomerOnTable = By.xpath("//td[@class='sorting_1']/a");
20+
21+
//Add New Customer
22+
public By inputCompany = By.xpath("//input[@id='company']");
23+
public By inputVatNumber = By.xpath("//input[@id='vat']");
24+
public By inputPhone = By.xpath("//input[@id='phonenumber']");
25+
public By inputWebsite = By.xpath("//input[@id='website']");
26+
private By dropdownGroups = By.xpath("//button[@data-id='groups_in[]']");
27+
private By inputSearchGroups = By.xpath("//div[@app-field-wrapper='groups_in[]']//input[@type='search']");
28+
private By dropdownCurrency = By.xpath("//button[@data-id='default_currency']");
29+
private By inputSearchCurrency = By.xpath("//div[@app-field-wrapper='default_currency']//input");
30+
private By inputAddress = By.xpath("//textarea[@id='address']");
31+
private By inputCity = By.xpath("//input[@id='city']");
32+
private By inputState = By.xpath("//input[@id='state']");
33+
private By inputZipCode = By.xpath("//input[@id='zip']");
34+
private By dropdownCountry = By.xpath("//button[@data-id='country']");
35+
private By inputSearchCountry = By.xpath("//div[@app-field-wrapper='country']//input[@aria-label='Search']");
36+
private By buttonSaveAndCreateContact = By.xpath("//button[normalize-space()='Save and create contact']");
37+
private By buttonSaveCustomer = By.xpath("//div[@id='profile-save-section']//button[normalize-space()='Save']");
38+
39+
40+
public void clickButtonAddNew() {
41+
WebUI.clickElement(buttonAddNewCustomer);
42+
}
43+
44+
public void verifyRedirectCustomerPageSuccess() {
45+
Assert.assertTrue(DriverManager.getDriver().findElement(headerCustomerPage).isDisplayed(), "Không tìm thấy Customer header page");
46+
Assert.assertEquals(WebUI.getElementText(headerCustomerPage), "Customers Summary", "Giá trị header của Customer page không đúng");
47+
WebUI.assertContains(WebUI.getElementText(headerCustomerPage), "Customers Summary", "");
48+
}
49+
50+
public void inputDataCustomer(String companyName) {
51+
WebUI.setText(inputCompany, companyName);
52+
WebUI.setText(inputVatNumber, "10");
53+
WebUI.setText(inputPhone, "123456");
54+
WebUI.setText(inputWebsite, "https://viettel.com.vn");
55+
WebUI.clickElement(dropdownGroups);
56+
WebUI.setText(inputSearchGroups, "Gold");
57+
WebUI.sleep(1);
58+
//driver.findElement(inputSearchGroups).sendKeys(Keys.ENTER);
59+
WebUI.setKey(inputSearchGroups, Keys.ENTER);
60+
WebUI.clickElement(dropdownGroups);
61+
WebUI.setText(inputAddress, "Hà Nội");
62+
WebUI.setText(inputCity, "Hà Nội");
63+
WebUI.setText(inputState, "Thăng Long");
64+
WebUI.setText(inputZipCode, "1234");
65+
WebUI.clickElement(dropdownCountry);
66+
WebUI.setText(inputSearchCountry, "Vietnam");
67+
WebUI.sleep(1);
68+
//driver.findElement(inputSearchCountry).sendKeys(Keys.ENTER);
69+
WebUI.setKey(inputSearchCountry, Keys.ENTER);
70+
WebUI.clickElement(buttonSaveCustomer);
71+
}
72+
73+
public ProfilePage searchCustomer(String companyName) {
74+
WebUI.setText(inputSearchCustomers, companyName);
75+
WebUI.sleep(2);
76+
WebUI.clickElement(firstItemCustomerOnTable);
77+
78+
79+
return new ProfilePage();
80+
}
81+
82+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.anhtester.Bai31_TestListener.pages;
2+
3+
import com.anhtester.keywords.WebUI;
4+
import org.openqa.selenium.By;
5+
6+
public class DashboardPage {
7+
8+
private By menuDashboard = By.xpath("//span[normalize-space()='Dashboard']");
9+
private By menuCustomers = By.xpath("//span[normalize-space()='Customers']");
10+
private By menuProjects = By.xpath("//span[normalize-space()='Projects']");
11+
private By dropdownProfile = By.xpath("//li[@class='icon header-user-profile']");
12+
private By optionLogout = By.xpath("(//a[.='Logout'])[1]");
13+
private By optionProfile = By.xpath("(//a[normalize-space()='My Profile'])[2]");
14+
15+
public CustomerPage clickMenuCustomers() {
16+
WebUI.waitForPageLoaded();
17+
WebUI.clickElement(menuCustomers);
18+
System.out.println("Click on menu Customers");
19+
20+
return new CustomerPage();
21+
}
22+
23+
public void clickMenuProjects() {
24+
WebUI.clickElement(menuProjects);
25+
System.out.println("Click on menu Projects");
26+
}
27+
28+
public LoginPage logOut() {
29+
WebUI.clickElement(dropdownProfile);
30+
System.out.println("Click on dropdown Profile");
31+
WebUI.clickElement(optionLogout);
32+
System.out.println("Click on option Logout");
33+
34+
return new LoginPage();
35+
}
36+
37+
}
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package com.anhtester.Bai31_TestListener.pages;
2+
3+
import com.anhtester.drivers.DriverManager;
4+
import com.anhtester.keywords.WebUI;
5+
import org.openqa.selenium.By;
6+
import org.testng.Assert;
7+
8+
public class LoginPage {
9+
10+
private String url = "https://crm.anhtester.com/admin/authentication";
11+
12+
//Khai báo tất cả các object (element) trên page này
13+
//Thuộc kiểu đối tượng By (thuộc của Selenium)
14+
By headerLoginPage = By.xpath("//h1[normalize-space()='Login']");
15+
By inputEmail = By.xpath("//input[@id='email']");
16+
By inputPassword = By.xpath("//input[@id='password']");
17+
By buttonLogin = By.xpath("//button[normalize-space()='Login']");
18+
By errorMessage = By.xpath("//div[@class='text-center alert alert-danger']");
19+
By menuDashboard = By.xpath("//ul[@id='side-menu']//span[normalize-space()='Dashboard']");
20+
21+
//Xây dựng các hàm xử lý trên chính page này
22+
private void setInputEmail(String email) {
23+
WebUI.setText(inputEmail, email);
24+
}
25+
26+
private void setInputPassword(String password) {
27+
WebUI.setText(inputPassword, password);
28+
}
29+
30+
private void clickButtonLogin() {
31+
WebUI.waitForElementVisible(buttonLogin);
32+
WebUI.clickElement(buttonLogin);
33+
}
34+
35+
public DashboardPage loginCRM(String email, String password) {
36+
WebUI.openURL(url);
37+
WebUI.waitForPageLoaded();
38+
setInputEmail(email);
39+
setInputPassword(password);
40+
clickButtonLogin();
41+
WebUI.waitForPageLoaded();
42+
return new DashboardPage();
43+
}
44+
45+
public void verifyLoginSuccess() {
46+
System.out.println("Verify login success");
47+
Assert.assertTrue(DriverManager.getDriver().findElement(menuDashboard).isDisplayed(), "Menu Dashboard not displayed.");
48+
}
49+
50+
public void verifyLoginFail() {
51+
System.out.println("Verify login fail");
52+
Assert.assertTrue(DriverManager.getDriver().findElement(errorMessage).isDisplayed(), "Error Message not displayed.");
53+
Assert.assertEquals(WebUI.getElementText(errorMessage), "Invalid email or password", "Content of Error Message not match.");
54+
}
55+
56+
public void verifyRedirectLoginPage() {
57+
System.out.println("Verify redirect to Login page");
58+
Assert.assertTrue(DriverManager.getDriver().findElement(headerLoginPage).isDisplayed(), "Header of Login page not displayed.");
59+
Assert.assertEquals(WebUI.getElementText(headerLoginPage), "Login", "Content of header for Login page not match.");
60+
}
61+
62+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.anhtester.Bai31_TestListener.pages;
2+
3+
import com.anhtester.drivers.DriverManager;
4+
import org.testng.Assert;
5+
6+
public class ProfilePage extends CustomerPage {
7+
8+
public void verifyCustomerDetail(String companyName) {
9+
Assert.assertEquals(DriverManager.getDriver().findElement(inputCompany).getAttribute("value"), companyName, "Message");
10+
//Viết thêm cho các fields khác
11+
}
12+
13+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.anhtester.Bai31_TestListener.testcases;
2+
3+
import com.anhtester.Bai31_TestListener.pages.CustomerPage;
4+
import com.anhtester.Bai31_TestListener.pages.DashboardPage;
5+
import com.anhtester.Bai31_TestListener.pages.LoginPage;
6+
import com.anhtester.Bai31_TestListener.pages.ProfilePage;
7+
import com.anhtester.common.BaseTest;
8+
import com.anhtester.constants.ConfigData;
9+
import org.testng.annotations.Test;
10+
11+
public class CustomerTest extends BaseTest {
12+
13+
LoginPage loginPage;
14+
DashboardPage dashboardPage;
15+
CustomerPage customerPage;
16+
ProfilePage profilePage;
17+
18+
private String CUSTOMER_NAME = "Viettel A7";
19+
20+
@Test
21+
public void testAddNewCustomer() {
22+
loginPage = new LoginPage();
23+
dashboardPage = loginPage.loginCRM(ConfigData.EMAIL, ConfigData.PASSWORD);
24+
loginPage.verifyLoginSuccess();
25+
26+
customerPage = dashboardPage.clickMenuCustomers();
27+
28+
customerPage.verifyRedirectCustomerPageSuccess();
29+
customerPage.clickButtonAddNew();
30+
customerPage.inputDataCustomer(CUSTOMER_NAME);
31+
dashboardPage.clickMenuCustomers();
32+
33+
profilePage = customerPage.searchCustomer(CUSTOMER_NAME);
34+
profilePage.verifyCustomerDetail(CUSTOMER_NAME);
35+
36+
}
37+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package com.anhtester.Bai31_TestListener.testcases;
2+
3+
import com.anhtester.Bai31_TestListener.pages.CustomerPage;
4+
import com.anhtester.Bai31_TestListener.pages.DashboardPage;
5+
import com.anhtester.Bai31_TestListener.pages.LoginPage;
6+
import com.anhtester.common.BaseTest;
7+
import org.testng.annotations.Test;
8+
9+
public class DashboardTest extends BaseTest {
10+
11+
LoginPage loginPage;
12+
DashboardPage dashboardPage;
13+
CustomerPage customerPage;
14+
15+
@Test
16+
public void testOpenCustomerPage() {
17+
//loginPage = new LoginPage(driver);
18+
//loginPage.loginCRM("admin@example.com", "123456");
19+
//loginPage.verifyLoginSuccess();
20+
21+
//dashboardPage = new DashboardPage(driver);
22+
//dashboardPage.clickMenuCustomers();
23+
24+
//customerPage = new CustomerPage(driver);
25+
//customerPage.verifyRedirectCustomerPageSuccess();
26+
27+
loginPage = new LoginPage();
28+
dashboardPage = loginPage.loginCRM("admin@example.com", "123456");
29+
loginPage.verifyLoginSuccess();
30+
customerPage = dashboardPage.clickMenuCustomers();
31+
customerPage.verifyRedirectCustomerPageSuccess();
32+
33+
}
34+
35+
@Test
36+
public void testLogout() {
37+
loginPage = new LoginPage();
38+
dashboardPage = loginPage.loginCRM("admin@example.com", "123456");
39+
loginPage.verifyLoginSuccess();
40+
41+
//dashboardPage = new DashboardPage(driver);
42+
loginPage = dashboardPage.logOut();
43+
loginPage.verifyRedirectLoginPage();
44+
45+
}
46+
47+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.anhtester.Bai31_TestListener.testcases;
2+
3+
import com.anhtester.Bai31_TestListener.pages.LoginPage;
4+
import com.anhtester.common.BaseTest;
5+
import com.anhtester.helpers.ExcelHelper;
6+
import org.testng.annotations.Test;
7+
8+
public class LoginTest extends BaseTest {
9+
10+
LoginPage loginPage;
11+
12+
@Test
13+
public void testLoginCRM_Success() {
14+
System.out.println("=====testLoginCRM_Success=====");
15+
loginPage = new LoginPage();
16+
17+
ExcelHelper excelHelper = new ExcelHelper();
18+
excelHelper.setExcelFile("src/test/resources/testdata/LoginData.xlsx", "Sheet1");
19+
20+
loginPage.loginCRM(
21+
excelHelper.getCellData("EMAIL", 1),
22+
excelHelper.getCellData("PASSWORD", 1)
23+
);
24+
loginPage.verifyLoginSuccess();
25+
}
26+
27+
@Test
28+
public void testLoginCRM_EmailInvalid() {
29+
System.out.println("=====testLoginCRM_EmailInvalid=====");
30+
loginPage = new LoginPage();
31+
32+
ExcelHelper excelHelper = new ExcelHelper();
33+
excelHelper.setExcelFile("src/test/resources/testdata/LoginData.xlsx", "Sheet1");
34+
35+
loginPage.loginCRM(
36+
excelHelper.getCellData("EMAIL", 2),
37+
excelHelper.getCellData("PASSWORD", 2)
38+
);
39+
loginPage.verifyLoginFail();
40+
}
41+
42+
@Test
43+
public void testLoginCRM_PasswordInvalid() {
44+
System.out.println("=====testLoginCRM_PasswordInvalid=====");
45+
loginPage = new LoginPage();
46+
loginPage.loginCRM("admin@example.com", "123");
47+
loginPage.verifyLoginFail();
48+
}
49+
50+
}

0 commit comments

Comments
 (0)