Skip to content

Commit e550c2b

Browse files
committed
Update version 1.7.0
1 parent 4e12929 commit e550c2b

11 files changed

Lines changed: 168 additions & 50 deletions

File tree

pom.xml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@
66

77
<groupId>anhtester.com</groupId>
88
<artifactId>AutomationFrameworkSelenium</artifactId>
9-
<version>1.7.0-preview</version>
9+
<version>1.7.0</version>
1010
<name>AutomationFrameworkSelenium</name>
1111
<url>https://github.com/anhtester/AutomationFrameworkSelenium</url>
1212

1313
<properties>
1414
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
1515
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
1616
<java-compiler.version>8</java-compiler.version>
17-
<maven-surefire-plugin.version>3.0.0-M5</maven-surefire-plugin.version>
17+
<maven-surefire-plugin.version>3.0.0-M7</maven-surefire-plugin.version>
1818
<maven-compiler-plugin.version>3.10.1</maven-compiler-plugin.version>
19-
<selenium.version>4.3.0</selenium.version>
19+
<selenium.version>4.4.0</selenium.version>
2020
<testng.version>7.4.0</testng.version>
21-
<webdrivermanager.version>5.2.1</webdrivermanager.version>
21+
<webdrivermanager.version>5.2.3</webdrivermanager.version>
2222
<aspectjweaver.version>1.9.9.1</aspectjweaver.version>
2323
<ashot.version>1.5.4</ashot.version>
2424
<log4j.version>2.18.0</log4j.version>

src/main/java/anhtester/com/constants/FrameworkConstants.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ private FrameworkConstants() {
4141
public static final String screenshot_skipped_steps = PropertiesHelpers.getValue("screenshot_skipped_steps");
4242
public static final String screenshot_all_steps = PropertiesHelpers.getValue("screenshot_all_steps");
4343
public static final String ZIP_FOLDER = PropertiesHelpers.getValue("ZIP_FOLDER");
44+
public static final String ZIP_FOLDER_PATH = PropertiesHelpers.getValue("ZIP_FOLDER_PATH");
45+
public static final String ZIP_FOLDER_NAME = PropertiesHelpers.getValue("ZIP_FOLDER_NAME");
4446
public static final String VIDEO_RECORD = PropertiesHelpers.getValue("VIDEO_RECORD");
4547

4648
public static final int WAIT_DEFAULT = Integer.parseInt(PropertiesHelpers.getValue("WAIT_DEFAULT"));
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package anhtester.com.enums;
22

3-
/**A Java Enum is a special Java type used to define collections of constants.*/
3+
/**
4+
* A Java Enum is a special Java type used to define collections of constants.
5+
*/
46
public enum AuthorType {
5-
ANHTESTER, AUTOMATION, VOTHAIAN
7+
AnhTester, James, Robert
68
}

src/main/java/anhtester/com/utils/ObjectUtils.java

Lines changed: 113 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -6,24 +6,124 @@
66
package anhtester.com.utils;
77

88
import anhtester.com.helpers.PropertiesHelpers;
9+
import org.apache.commons.lang3.reflect.FieldUtils;
910
import org.openqa.selenium.By;
11+
import org.openqa.selenium.WebElement;
12+
import org.openqa.selenium.support.pagefactory.DefaultElementLocator;
13+
14+
import java.lang.reflect.Proxy;
15+
import java.util.regex.Matcher;
16+
import java.util.regex.Pattern;
1017

1118
public class ObjectUtils {
1219

13-
public static By getObject(String elementName) {
20+
private static final String BY = "by";
21+
private static final String H = "h";
22+
private static final String LOCATOR = "locator";
23+
private static final String FOUND_BY = "foundBy";
24+
25+
public static By getByFromWebElement(WebElement element) {
26+
if (element instanceof DefaultElementLocator) {
27+
return getByLocator(element);
28+
} else if (element instanceof Proxy) {
29+
Object proxyOrigin = getField(element, H);
30+
Object locator = getField(proxyOrigin, LOCATOR);
31+
32+
return getByLocator(locator);
33+
34+
} else /* if WebElement is RemoteWebElement */ {
35+
String foundByString = getFoundBy(element);
36+
String foundByPattern = "(?<=\\-> ).*";
37+
38+
Pattern pattern = Pattern.compile(foundByPattern);
39+
Matcher matcher = pattern.matcher(foundByString);
40+
41+
if (matcher.find()) {
42+
int locatorDefinitionIndex = 0;
43+
String locatorDefinition = matcher.group(locatorDefinitionIndex);
44+
45+
return getByLocatorFromString(locatorDefinition);
46+
47+
} else {
48+
throw new IllegalStateException("Failed to get locator from RemoteWebElement. Please, check if the Regex pattern is valid.");
49+
}
50+
51+
}
52+
}
53+
54+
private static Object getField(Object element, String fieldName) {
55+
try {
56+
return FieldUtils.readField(element, fieldName, true);
57+
} catch (IllegalAccessException e) {
58+
throw new RuntimeException(e);
59+
}
60+
}
61+
62+
private static String getFoundBy(Object element) {
63+
try {
64+
return (String) FieldUtils.readField(element, FOUND_BY, true);
65+
} catch (IllegalAccessException e) {
66+
throw new RuntimeException(e);
67+
}
68+
}
69+
70+
private static By getByLocator(Object element) {
71+
try {
72+
return (By) FieldUtils.readField(element, BY, true);
73+
} catch (IllegalAccessException e) {
74+
throw new RuntimeException(e);
75+
}
76+
}
77+
78+
public static By getByLocatorFromString(String locatorToString) {
79+
String[] locatorSplit = locatorToString.split(": ");
80+
81+
if (locatorSplit.length != 2)
82+
throw new IllegalStateException(String.format("Locator definition does not had 2 elements for %s locator", locatorToString));
83+
84+
String locatorType = locatorSplit[0];
85+
String locatorValue = locatorSplit[1];
86+
87+
switch (locatorType) {
88+
case "css selector":
89+
return By.cssSelector(locatorValue);
90+
case "id":
91+
return By.id(locatorValue);
92+
case "link text":
93+
return By.linkText(locatorValue);
94+
case "partial link text":
95+
return By.partialLinkText(locatorValue);
96+
case "tag name":
97+
return By.tagName(locatorValue);
98+
case "name":
99+
return By.name(locatorValue);
100+
case "class":
101+
return By.className(locatorValue);
102+
case "xpath":
103+
return By.xpath(locatorValue);
104+
default:
105+
throw new IllegalStateException("Cannot define locator for WebElement definition: " + locatorToString);
106+
}
107+
}
108+
109+
public static By getByLocatorFromConfig(String elementName) {
14110

15111
// retrieve the specified object from the object list in properties file
16112
String locator = PropertiesHelpers.getValue(elementName);
17113

18114
if (locator.equals("") || locator.isEmpty()) {
19-
Log.info("The Locator " + elementName + " does not exist !!");
115+
Log.info("The Locator string " + elementName + " does not exist !!");
20116
try {
21117
throw new Exception("The Locator " + elementName + " does not exist !!");
22118
} catch (Exception e) {
23119
e.printStackTrace();
24120
}
25121
}
26122

123+
if (elementName.split("&&").length != 2) {
124+
throw new IllegalStateException(String.format("Locator definition does not had 2 elements for %s locator", elementName));
125+
}
126+
27127
// extract the locator type and value from the object
28128
String locatorType = locator.split("&&")[0];
29129
String locatorValue = locator.split("&&")[1];
@@ -32,24 +132,20 @@ public static By getObject(String elementName) {
32132

33133
// Trả về một thể hiện của lớp By dựa trên loại định vị (id, name, xpath, css,...)
34134
// Đối tượng By có thể được sử dụng bởi driver.findElement (WebElement)
35-
if (locatorType.toLowerCase().equals("id"))
36-
return By.id(locatorValue);
37-
else if (locatorType.toLowerCase().equals("name"))
38-
return By.name(locatorValue);
39-
else if (locatorType.toLowerCase().equals("xpath"))
40-
return By.xpath(locatorValue);
41-
else if ((locatorType.toLowerCase().equals("cssselector")) || (locatorType.toLowerCase().equals("css")))
135+
if (locatorType.toLowerCase().trim().equals("id")) return By.id(locatorValue);
136+
else if (locatorType.toLowerCase().trim().equals("name")) return By.name(locatorValue);
137+
else if (locatorType.toLowerCase().trim().equals("xpath")) return By.xpath(locatorValue);
138+
else if ((locatorType.toLowerCase().trim().equals("cssselector")) || (locatorType.toLowerCase().trim().equals("css")))
42139
return By.cssSelector(locatorValue);
43-
else if ((locatorType.toLowerCase().equals("classname")) || (locatorType.toLowerCase().equals("class")))
140+
else if ((locatorType.toLowerCase().trim().equals("classname")) || (locatorType.toLowerCase().trim().equals("class")))
44141
return By.className(locatorValue);
45-
else if ((locatorType.toLowerCase().equals("tagname")) || (locatorType.toLowerCase().equals("tag")))
142+
else if ((locatorType.toLowerCase().trim().equals("tagname")) || (locatorType.toLowerCase().trim().equals("tag")))
46143
return By.tagName(locatorValue);
47-
else if ((locatorType.toLowerCase().equals("linktext")) || (locatorType.toLowerCase().equals("link")))
144+
else if ((locatorType.toLowerCase().trim().equals("linktext")) || (locatorType.toLowerCase().trim().equals("link")))
48145
return By.linkText(locatorValue);
49-
else if ((locatorType.toLowerCase().equals("partiallinktext")) || (locatorType.toLowerCase().equals("partial")))
146+
else if ((locatorType.toLowerCase().trim().equals("partiallinktext")) || (locatorType.toLowerCase().trim().equals("partial")))
50147
return By.partialLinkText(locatorValue);
51-
else
52-
try {
148+
else try {
53149
throw new Exception("Unknown locator type '" + locatorType + "'");
54150
} catch (Exception e) {
55151
e.printStackTrace();
@@ -76,9 +172,8 @@ public static String getXpathValue(String elementName) {
76172

77173
if (!locatorType.toLowerCase().trim().equals("xpath")) {
78174
try {
79-
Log.info(locatorType.toLowerCase());
80-
Log.info("The Locator Type of " + elementName + " does not XPATH !!");
81-
throw new Exception("The Locator Type of " + elementName + " does not XPATH !!");
175+
Log.info("The Locator Type of " + elementName + " does not XPATH !! => " + locatorType);
176+
throw new Exception("The Locator Type of " + elementName + " does not XPATH !! => " + locatorType);
82177
} catch (Exception e) {
83178
e.printStackTrace();
84179
}

src/main/java/anhtester/com/utils/ZipUtils.java

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,21 @@
99

1010
import java.io.File;
1111

12-
import static anhtester.com.constants.FrameworkConstants.EXTENT_REPORT_FOLDER_PATH;
13-
import static anhtester.com.constants.FrameworkConstants.Zipped_ExtentReports_Folder_Name;
12+
import static anhtester.com.constants.FrameworkConstants.*;
1413

1514
public class ZipUtils {
1615

1716
/* Make Zip file of Extent Reports in Project Root folder */
1817
public static void zip() {
19-
20-
ZipUtil.pack(new File(EXTENT_REPORT_FOLDER_PATH), new File(Zipped_ExtentReports_Folder_Name));
21-
22-
Log.info("Zipped " + Zipped_ExtentReports_Folder_Name + " folder successfully !!");
18+
if (ZIP_FOLDER.toLowerCase().trim().equals(YES)) {
19+
if ((ZIP_FOLDER_PATH != null || !ZIP_FOLDER_PATH.isEmpty()) && (ZIP_FOLDER_NAME != null || !ZIP_FOLDER_NAME.isEmpty())) {
20+
ZipUtil.pack(new File(ZIP_FOLDER_PATH), new File(ZIP_FOLDER_NAME));
21+
Log.info("Zipped " + Zipped_ExtentReports_Folder_Name + " folder successfully !!");
22+
} else {
23+
ZipUtil.pack(new File(EXTENT_REPORT_FOLDER_PATH), new File(Zipped_ExtentReports_Folder_Name));
24+
Log.info("Zipped " + Zipped_ExtentReports_Folder_Name + " folder successfully !!");
25+
}
26+
}
2327
}
2428

2529
}

src/test/java/anhtester/com/listeners/TestListener.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public void onStart(ISuite iSuite) {
6868
Log.info("Start suite: " + iSuite.getName());
6969
iSuite.setAttribute("WebDriver", DriverManager.getDriver());
7070
// //Gọi hàm startRecord video trong CaptureHelpers class
71-
// if (VIDEO_RECORD.trim().toLowerCase().equals(YES)) {
71+
// if (VIDEO_RECORD.toLowerCase().trim().equals(YES)) {
7272
// CaptureHelpers.startRecord(iSuite.getName());
7373
// }
7474
}
@@ -77,15 +77,13 @@ public void onStart(ISuite iSuite) {
7777
public void onFinish(ISuite iSuite) {
7878
Log.info("End suite testing " + iSuite.getName());
7979
WebUI.stopSoftAssertAll();
80-
//Kết thúc và thực thi Extents Report
80+
//Kết thúc Suite và thực thi Extents Report, đóng gói Folder report và send mail
8181
ExtentReportManager.flushReports();
82-
if (ZIP_FOLDER.trim().toLowerCase().equals(YES)) {
83-
ZipUtils.zip();
84-
}
82+
ZipUtils.zip();
8583
EmailSendUtils.sendEmail(count_totalTCs, count_passedTCs, count_failedTCs, count_skippedTCs);
8684

8785
//Gọi hàm stopRecord video trong CaptureHelpers class
88-
// if (VIDEO_RECORD.trim().toLowerCase().equals(YES)) {
86+
// if (VIDEO_RECORD.toLowerCase().trim().equals(YES)) {
8987
// CaptureHelpers.stopRecord();
9088
// }
9189
}
@@ -123,7 +121,7 @@ public void onTestStart(ITestResult iTestResult) {
123121
ExtentReportManager.info(BOLD_START + IconUtils.getOSIcon() + " "
124122
+ BrowserInfoUtils.getOSInfo() + BOLD_END);
125123

126-
if (VIDEO_RECORD.trim().toLowerCase().equals(YES)) {
124+
if (VIDEO_RECORD.toLowerCase().trim().equals(YES)) {
127125
screenRecorder.startRecording(getTestName(iTestResult));
128126
}
129127

@@ -167,7 +165,7 @@ public void onTestFailure(ITestResult iTestResult) {
167165
ExtentReportManager.logMessage(Status.FAIL, "Test case: " + getTestName(iTestResult) + " is failed.");
168166
ExtentReportManager.logMessage(Status.FAIL, iTestResult.getThrowable());
169167

170-
if (VIDEO_RECORD.trim().toLowerCase().equals(YES)) {
168+
if (VIDEO_RECORD.toLowerCase().trim().equals(YES)) {
171169
screenRecorder.stopRecording(true);
172170
}
173171

@@ -184,7 +182,7 @@ public void onTestSkipped(ITestResult iTestResult) {
184182

185183
ExtentReportManager.logMessage(Status.SKIP, "Test case: " + getTestName(iTestResult) + " is skipped.");
186184

187-
if (VIDEO_RECORD.trim().toLowerCase().equals(YES)) {
185+
if (VIDEO_RECORD.toLowerCase().trim().equals(YES)) {
188186
screenRecorder.stopRecording(true);
189187
}
190188

src/test/java/anhtester/com/projects/website/crm/testcases/ClientTest.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,7 @@ public ClientTest() {
2727
signInPage = new SignInPage();
2828
}
2929

30-
@FrameworkAnnotation(author = {AuthorType.ANHTESTER, AuthorType.VOTHAIAN},
31-
category = {CategoryType.SANITY, CategoryType.REGRESSION})
30+
@FrameworkAnnotation(author = {AuthorType.AnhTester, AuthorType.Robert}, category = {CategoryType.REGRESSION})
3231
@Test(priority = 1, dataProvider = "getClientDataHashTable", dataProviderClass = DataProviderManager.class)
3332
@Step("Add new Client")
3433
public void testAddClient(Hashtable<String, String> data) {
@@ -38,8 +37,7 @@ public void testAddClient(Hashtable<String, String> data) {
3837
clientPage.addClient(data);
3938
}
4039

41-
@FrameworkAnnotation(author = {AuthorType.ANHTESTER, AuthorType.AUTOMATION},
42-
category = {CategoryType.SANITY, CategoryType.REGRESSION})
40+
@FrameworkAnnotation(author = {AuthorType.James}, category = {CategoryType.SANITY, CategoryType.REGRESSION})
4341
@Test(priority = 2)
4442
@Step("Search Client")
4543
public void testSearchClient() {

src/test/java/anhtester/com/projects/website/crm/testcases/TestHandle.java

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@
3131
import java.io.File;
3232
import java.text.DecimalFormat;
3333
import java.util.ArrayList;
34-
import java.util.List;
3534
import java.util.Set;
3635

3736
public class TestHandle {
@@ -48,6 +47,24 @@ public void Setup() {
4847
// driver = DriverManager.getDriver(); //Get WebDriver from global in ThreadLocal
4948
}
5049

50+
@Test
51+
public void testConvertWebElementToBy() {
52+
WebUI.getToUrl(FrameworkConstants.URL_CRM);
53+
SignInPage signInPage = new SignInPage();
54+
55+
//WebElement
56+
WebElement emailElement = WebUI.getWebElement(signInPage.inputEmail);
57+
58+
//Convert WebElement to By
59+
By emailBy = ObjectUtils.getByFromWebElement(emailElement);
60+
61+
WebUI.setText(emailBy, "admin@mailinator.com");
62+
63+
WebUI.setText(signInPage.inputPassword, "123456");
64+
WebUI.clickElement(signInPage.buttonSignIn);
65+
WebUI.waitForElementVisible(new DashboardPage().menuDashboard);
66+
}
67+
5168
@Test
5269
public void testLocalStorage() {
5370
WebUI.getToUrl(FrameworkConstants.URL_CRM);
@@ -57,18 +74,18 @@ public void testLocalStorage() {
5774
LocalStorageUtils.setItem("email", "admin02@mailinator.com");
5875
LocalStorageUtils.setItem("password", "123456");
5976

60-
WebUI.setText(ObjectUtils.getObject("inputEmail"), LocalStorageUtils.getItem("email"));
61-
WebUI.setText(ObjectUtils.getObject("inputPassword"), LocalStorageUtils.getItem("password"));
62-
WebUI.clickElement(ObjectUtils.getObject("buttonSignIn"));
77+
WebUI.setText(ObjectUtils.getByLocatorFromConfig("inputEmail"), LocalStorageUtils.getItem("email"));
78+
WebUI.setText(ObjectUtils.getByLocatorFromConfig("inputPassword"), LocalStorageUtils.getItem("password"));
79+
WebUI.clickElement(ObjectUtils.getByLocatorFromConfig("buttonSignIn"));
6380
WebUI.waitForPageLoaded();
6481

6582
//Get value in Project page
66-
WebUI.clickElement(ObjectUtils.getObject("menuProjects"));
83+
WebUI.clickElement(ObjectUtils.getByLocatorFromConfig("menuProjects"));
6784
WebUI.logConsole(LocalStorageUtils.getItem("email"));
6885
WebUI.waitForPageLoaded();
6986
WebUI.sleep(1);
7087
//Get value in ClientModel page
71-
WebUI.clickElement(ObjectUtils.getObject("menuClients"));
88+
WebUI.clickElement(ObjectUtils.getByLocatorFromConfig("menuClients"));
7289
WebUI.logConsole(LocalStorageUtils.getItem("password"));
7390

7491
//=> You can get value by key everywhere before closing the browser

src/test/java/anhtester/com/projects/website/crm/testcases/TestSimpleCode.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import java.sql.Statement;
1717

1818
public class TestSimpleCode {
19-
19+
2020
@Test
2121
public void testGetXpathDynamic() {
2222
String xpath1 = ObjectUtils.getXpathDynamic("//button[normalize-space()='%s']", "Login");

src/test/resources/config/config.properties

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ EXPORT_VIDEO_PATH=ExportData/Videos
3232
EXPORT_CAPTURE_PATH=ExportData/Images
3333
# Zip folder report after run test --> yes or no
3434
ZIP_FOLDER=no
35+
ZIP_FOLDER_PATH=
36+
ZIP_FOLDER_NAME=
3537
# Video Record
3638
VIDEO_RECORD=no
3739
#override_reports --> yes or no

0 commit comments

Comments
 (0)