66package anhtester .com .utils ;
77
88import anhtester .com .helpers .PropertiesHelpers ;
9+ import org .apache .commons .lang3 .reflect .FieldUtils ;
910import 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
1118public 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 }
0 commit comments