Produced by Araxis Merge on 5/10/2018 8:26:43 AM Central Daylight Time. See www.araxis.com for information about Merge. This report uses XHTML and CSS2, and is best viewed with a modern standards-compliant browser. For optimum results when printing this report, use landscape orientation and enable printing of background images and colours in your browser.
# | Location | File | Last Modified |
---|---|---|---|
1 | OSCIF MHED P3 Annie.zip\cms-staff-app-release-4.0.0@aff5ba84233.zip\automation_phase_3\src\main\java\com\agilex\common\selenium | DriverUtil.java | Thu Feb 22 10:28:51 2018 UTC |
2 | OSCIF MHED P3 Annie.zip\cms-staff-app-release-4.0.0@aff5ba84233.zip\automation_phase_3\src\main\java\com\agilex\common\selenium | DriverUtil.java | Wed May 9 13:08:45 2018 UTC |
Description | Between Files 1 and 2 |
|
---|---|---|
Text Blocks | Lines | |
Unchanged | 5 | 1612 |
Changed | 4 | 10 |
Inserted | 0 | 0 |
Removed | 0 | 0 |
Whitespace | |
---|---|
Character case | Differences in character case are significant |
Line endings | Differences in line endings (CR and LF characters) are ignored |
CR/LF characters | Not shown in the comparison detail |
No regular expressions were active.
1 | package co m.agilex.c ommon.sele nium; | |
2 | ||
3 | import jav a.io.File; | |
4 | import jav a.lang.ref lect.Array ; | |
5 | import jav a.util.*; | |
6 | import jav a.util.con current.Ti meUnit; | |
7 | ||
8 | import com .google.co mmon.colle ct.ArrayLi stMultimap ; | |
9 | import org .openqa.se lenium.*; | |
10 | import org .openqa.se lenium.NoS uchElement Exception; | |
11 | import org .openqa.se lenium.rem ote.Browse rType; | |
12 | import org .openqa.se lenium.rem ote.LocalF ileDetecto r; | |
13 | import org .openqa.se lenium.rem ote.intern al.WebElem entToJsonC onverter; | |
14 | import org .openqa.se lenium.sup port.pagef actory.Ele mentLocato r; | |
15 | import org .openqa.se lenium.sup port.ui.Ex pectedCond ition; | |
16 | import org .openqa.se lenium.sup port.ui.Se lect; | |
17 | import org .openqa.se lenium.sup port.ui.We bDriverWai t; | |
18 | import org .openqa.se lenium.int eractions. Actions; | |
19 | ||
20 | /** | |
21 | * <font c olor="red" ><b>NOTE:< /b> Intern et Explore r will NOT work loca lly unless you allow | |
22 | * active content to run local ly.</font> <br/> | |
23 | * 1. From Internet Explorer, select the Tools men u, then th e Options. ..<br/> | |
24 | * 2. In t he Interne t Options dialog, se lect the A dvanced ta b...<br/> | |
25 | * 3. Scro ll down un til you se e the Secu rity optio ns. Enabl e the chec kbox | |
26 | * "Allow active con tent to ru n in files on My Com puter".<br /> | |
27 | * 4. Clos e the dial og, and qu it Interne t Explorer . The cha nges will take | |
28 | * effect the next t ime you st art IE.<br /> | |
29 | * This in formation was obtain ed here: h ttp://www. genopro.co m/help/rep ort-genera tor/allow- blocked-co ntent/ | |
30 | * <br/><b r/> | |
31 | * | |
32 | * <hr> | |
33 | * Locator s are used to find t he Element s on the p age. | |
34 | * It is p ossible fo r the loca tor to hav e an optio nal prefix to the st ring that would star t | |
35 | * with on e of the f ollowing:< br/><br/> | |
36 | * <table> | |
37 | * <tr><td valign="t op">" <b>name=</ b>"</ td><td>Fin d the inpu t element with match ing " name" attribute .</td></tr > | |
38 | * | |
39 | * <tr><td valign="t op">" <b>xpath=< /b>"< /td><td><f ont color= "red">NOTE : Differen t behavior on differ ent browse rs</font>< br/> | |
40 | * At a hi gh level, WebDriver uses a | |
41 | * browser 's native XPath capa bilities w herever po ssible. On those bro wsers | |
42 | * that do n't have n ative XPat h support, WebDriver provides its own | |
43 | * impleme ntation. T his can le ad to some unexpecte d behaviou r unless y ou are | |
44 | * aware o f the diff erences in the vario us xpath e ngines. | |
45 | * <table border="1" ><thead> | |
46 | * <tr><th >Driver</t h><th>Tag and Attrib ute Name</ th><th>Att ribute Val ues</th><t h>Native X Path Suppo rt</th></t r> | |
47 | * </thead > | |
48 | * <tbody> | |
49 | * <tr><td >HtmlUnit Driver</td ><td>Lower -cased</td ><td>As th ey appear in the HTM L</td><td> Yes</td></ tr> | |
50 | * <tr><td >Internet Explorer D river</td> <td>Lower- cased</td> <td>As the y appear i n the HTML </td><td>N o</td></tr > | |
51 | * <tr><td >Firefox D river</td> <td>Case i nsensitive </td><td>A s they app ear in the HTML</td> <td>Yes</t d></tr> | |
52 | * </tbody ></table> | |
53 | * <p>This is a litt le abstrac t, so for the follow ing piece of HTML:</ p> | |
54 | * <code>& lt;input t ype=" text" name=&quo t;example& quot; /> ;</code><b r/> | |
55 | * <code>& lt;INPUT t ype=" text" name=&quo t;other&qu ot; />< /code><br/ ><br/> | |
56 | * And the following Java Code :<br/> | |
57 | * <code>L ist<Web Element> ; inputs = driver.fi ndElements (By.xpath( "//in put") );</code>< br/> | |
58 | * The fol lowing num ber of mat ches will be found | |
59 | * <table border="1" ><thead> | |
60 | * <tr><th >XPath exp ression</t h><th>Html Unit Drive r</th><th> Firefox Dr iver</th>< th>Interne t Explorer Driver</t h></tr> | |
61 | * <tbody> | |
62 | * <tr><td >//input</ td><td>1 ( "exam ple") </td><td>2 </td><td>2 </td></tr> | |
63 | * <tr><td >//INPUT</ td><td>0</ td><td>2</ td><td>0</ td></tr> | |
64 | * </tbody ></table> | |
65 | * Sometim es HTML el ements do not need a ttributes to be expl icitly dec lared | |
66 | * because they will default t o known va lues. For example, t he "i nput" | |
67 | * tag doe s not requ ire the &q uot;type&q uot; attri bute becau se it defa ults to | |
68 | * "t ext". The rule of thumb w hen using xpath in W ebDriver i s that | |
69 | * you sho uld not ex pect to be able to m atch again st these i mplicit at tributes. | |
70 | * </td></ tr> | |
71 | * | |
72 | * <tr><td valign="t op">" <b>/</b>&q uot;</td>< td><font c olor="red" >NOTE: Dif ferent beh avior on d ifferent b rowsers</f ont><br/> | |
73 | * A strin g starting with a sl ash is ass umed to be an xpath statement | |
74 | * </td></ tr> | |
75 | * | |
76 | * <tr><td valign="t op">" <b>domtagn ame=</b>&q uot;</td>< td>The DOM Tag Name of the ele ment.</td> </tr> | |
77 | * | |
78 | * <tr><td valign="t op">" <b>domclas s=</b>&quo t;</td><td >Class in this case refers to the attrib ute | |
79 | * on the DOM elemen t. <font c olor="red" >NOTE: Oft en in prac tical use there are many DOM e lements | |
80 | * with th e same cla ss name, t hus using this would find mult iple eleme nts | |
81 | * and onl y return t he first e lement (wh ich may no t be what you want). </font></t d></tr> | |
82 | * | |
83 | * <tr><td valign="t op">" <b>css=</b >"</t d><td>Like the name implies it is a loca tor strate gy by | |
84 | * css. <f ont color= "red">NOTE : Native b rowser sup port is us ed by defa ult, so pl ease refer to w3c | |
85 | * css sel ectors <ht tp://www.w 3.org/TR/C SS/#select ors> for a list of | |
86 | * general ly availab le css sel ectors. If a browser does not have nativ e | |
87 | * support for css q ueries, th en Sizzle is used. I E 6,7 and FF3.0 curr ently | |
88 | * use Siz zle as the css query engine.Be ware that not all br owsers wer e created equal, som e css that might | |
89 | * work in one versi on may not work in a nother.</f ont></td>< /tr> | |
90 | * | |
91 | * <tr><td valign="t op">" <b>link=</ b>"</ td><td>Fin d the link element w ith matchi ng visible text.</td ></tr> | |
92 | * | |
93 | * <tr><td valign="t op">" <b>partial link=</b>& quot;</td> <td>Find t he link el ement with partial m atching vi sible text .</td></tr > | |
94 | * | |
95 | * <tr><td valign="t op">" <b>id=</b> "</td ><td><i>(d efault)</i > This is the most e fficient a nd preferr ed | |
96 | * way to locate an element. If the str ing does n ot contain any of th ese | |
97 | * prefixe s, it is a ssumed to be the equ ivalent of id=.</td> </tr> | |
98 | * | |
99 | * <tr><td valign="t op">" <b>identif ier=</b>&q uot;</td>< td>A synon ym for &qu ot;id=&quo t;</td></t r> | |
100 | * </table > | |
101 | * <br/> | |
102 | * <font c olor="red" >NOTE: dom class, dom tagname, a nd partial link do no t exist in Selenium RC - | |
103 | * they ar e specific to WebDri ver. So yo u risk los ing backwa rds compat ibility by | |
104 | * using t hem.</font ><br/><br/ > | |
105 | * | |
106 | * Example s using ea ch of the locators a bove: | |
107 | * <table border="1" ><thead> | |
108 | * <tr><th >locator</ th><th>Htm l source i t would fi nd</th></t r> | |
109 | * <tbody> | |
110 | * <tr><td >name=One< /td><td>&l t;input ty pe="c heckbox&qu ot; id=&qu ot;chbx1&q uot; name= "One& quot; /> ;</td></tr > | |
111 | * <tr><td >xpath=//* [@id=\&quo t;chbx4\&q uot;]</td> <td><in put type=& quot;check box" id="c hbx4" name=&quo t;Four&quo t; /></ td></tr> | |
112 | * <tr><td >//*[@id=\ "chbx 3\"]< /td><td>&l t;input ty pe="c heckbox&qu ot; id=&qu ot;chbx3&q uot; name= "Thre e" /& gt;</td></ tr> | |
113 | * <tr><td >domtagnam e=a</td><t d><a hr ef="j a DNS ipt:toggle _visibilit y('toggleT ext');&quo t;>Toggle Me</a&g t;</td></t r> | |
114 | * <tr><td >domclass= gsfi</td>< td><inp ut type=&q uot;text&q uot; class ="gsf i" /& gt;</td></ tr> | |
115 | * <tr><td >css=input #chbx6.chk </td><td>& lt;input t ype=" checkbox&q uot; id=&q uot;chbx6& quot; name ="Six " cla ss="c hk" / ></td>< /tr> | |
116 | * <tr><td >link=Togg le Me</td> <td><a href=" ;ja DNS ipt:toggle _visibilit y('toggleT ext');&quo t;>Toggle Me</a&g t;</td></t r> | |
117 | * <tr><td >partialli nk=Toggle< /td><td>&l t;a href=& quot;ja DNS ipt:toggle _visibilit y('toggleT ext');&quo t;>Toggle Me</a&g t;</td></t r> | |
118 | * <tr><td >identifie r=male<br/ >id=male<b r/>male</t d><td>< input type ="rad io" n ame=" sex" id="m ale" /></td> </tr> | |
119 | * </tbody ></table> | |
120 | */ | |
121 | public cla ss DriverU til | |
122 | { | |
123 | pr ivate WebD river driv er = null; | |
124 | privat e final st atic int e xplicitWai tSeconds = 10; | |
125 | ||
126 | ||
127 | //-------- ---------- ----Constr uctors---- ---------- ---------- ---------- ---------- | |
128 | ||
129 | pu blic Drive rUtil(WebD river myDr iver) | |
130 | { | |
131 | driv er = myDri ver; | |
132 | driv er.manage( ).timeouts ().implici tlyWait(10 , TimeUnit .SECONDS); | |
133 | } | |
134 | ||
135 | //-------- ---------- ----Utilit y Methods- ---------- ---------- ---------- ---------- | |
136 | ||
137 | /* * | |
138 | * See the d escription of {@link DriverUti l} for how we are us ing this p arseLocato r method. | |
139 | * / | |
140 | pr otected By parseLoca tor(String locator) | |
141 | { | |
142 | if ( locator.to LowerCase( ).startsWi th("name=" )) | |
143 | return By.name(l ocator.sub string(5)) ; | |
144 | else if (locat or.toLower Case().sta rtsWith("x path=")) | |
145 | return By.xpath( locator.su bstring(6) ); // By. xpath("//* [contains( @id, 'inco ming[')]") | |
146 | else if (locat or.toLower Case().sta rtsWith("/ ")) | |
147 | return By.xpath( locator); | |
148 | else if (locat or.toLower Case().sta rtsWith("d omtagname= ")) | |
149 | return By.tagNam e(locator. substring( 11)); | |
150 | else if (locat or.toLower Case().sta rtsWith("d omclass=") ) | |
151 | return By.classN ame(locato r.substrin g(9)); | |
152 | else if (locat or.toLower Case().sta rtsWith("c ss=")) | |
153 | return By.cssSel ector(loca tor.substr ing(4)); | |
154 | else if (locat or.toLower Case().sta rtsWith("l ink=")) | |
155 | return By.linkTe xt(locator .substring (5)); | |
156 | else if (locat or.toLower Case().sta rtsWith("p artiallink =")) | |
157 | return By.partia lLinkText( locator.su bstring(12 )); | |
158 | else if (locat or.toLower Case().sta rtsWith("c lass=")) | |
159 | return By.classN ame(locato r.substrin g(6)); | |
160 | else if (locat or.toLower Case().sta rtsWith("i d=")) | |
161 | return By.id(loc ator.subst ring(3)); | |
162 | else if (locat or.toLower Case().sta rtsWith("i dentifier= ")) | |
163 | return By.id(loc ator.subst ring(11)); | |
164 | ||
165 | retu rn By.id(l ocator); | |
166 | } | |
167 | ||
168 | pr otected By parseLoca tor(String Builder lo cator) { | |
169 | retu rn(parseLo cator(loca tor.toStri ng())); | |
170 | } | |
171 | ||
172 | /* * | |
173 | * Returns t he attribu te's curre nt value o r null if the value is not set .<br/> | |
174 | * Get the v alue of a the given attribute of the ele ment. Will return th e | |
175 | * current v alue, even if this h as been mo dified aft er the pag e has been | |
176 | * loaded. M ore exactl y, this me thod will return the value of the given | |
177 | * attribute , unless t hat attrib ute is not present, in which c ase the va lue | |
178 | * of the pr operty wit h the same name is r eturned. I f neither value is s et, | |
179 | * null is r eturned. T he "s tyle" attribute is conver ted as bes t can be t o a | |
180 | * text repr esentation with a tr ailing sem i-colon. T he followi ng are dee med | |
181 | * to be &qu ot;boolean " att ributes, a nd will re turn eithe r "tr ue" o r "fa lse": | |
182 | * async, au tofocus, a utoplay, c hecked, co mpact, com plete, con trols, dec lare, | |
183 | * defaultch ecked, def aultselect ed, defer, disabled, draggable , ended, | |
184 | * formnoval idate, hid den, indet erminate, iscontente ditable, i smap, | |
185 | * itemscope , loop, mu ltiple, mu ted, nohre f, noresiz e, noshade , novalida te, | |
186 | * nowrap, o pen, pause d, pubdate , readonly , required , reversed , scoped, | |
187 | * seamless, seeking, selected, spellcheck , truespee d, willval idate<br/> | |
188 | * Finally, the follow ing common ly mis-cap italized a ttribute/p roperty na mes | |
189 | * are evalu ated as ex pected:<br />"cl ass"< br/>" readonly&q uot; | |
190 | * | |
191 | * @param lo cator For a complete descripti on of loca tors and h ow to use them, | |
192 | * see the J avadoc for the class {@link Se leniumWebD river}. | |
193 | * @param at tribute Th e name of the attrib ute. | |
194 | * / | |
195 | pu blic Strin g getAttri bute(Strin g locator, String at tribute) { | |
196 | try { | |
197 | WebEle ment eleme nt = drive r.findElem ent(parseL ocator(loc ator)); | |
198 | String retValue = element. getAttribu te(attribu te); | |
199 | ||
200 | if (re tValue == null) | |
201 | throw ne w WebTesti ngExceptio n("Unable to find th e attribut e \"" + at tribute + "\" for " + locator + ", it wa s null."); | |
202 | return retValue; | |
203 | } | |
204 | catc h (org.ope nqa.seleni um.NoSuchE lementExce ption t) { | |
205 | throw new WebTes tingExcept ion("Unabl e to find the attrib ute \"" + attribute + "\" for " + locato r + ": " + t.getMess age(), t); | |
206 | } | |
207 | catc h (Throwab le t) { | |
208 | throw new WebTes tingExcept ion("Probl em with at tribute \" " + attrib ute + "\" for " + lo cator + ": " + t.get Message(), t); | |
209 | } | |
210 | } | |
211 | ||
212 | pu blic Strin g getAttri bute(Strin gBuilder l ocator, St ring attri bute) { | |
213 | retu rn(getAttr ibute(loca tor.toStri ng(), attr ibute)); | |
214 | } | |
215 | ||
216 | //-------- ---------- ---------- --Methods- ---------- ---------- ---------- ---------- | |
217 | ||
218 | /* * | |
219 | * Used to q uit browsi ng the app lication. | |
220 | * / | |
221 | pu blic void quit() { | |
222 | driv er.quit(); | |
223 | } | |
224 | ||
225 | /* * | |
226 | * Clicks a link, butt on, check box, radio button, e tc.<br/> | |
227 | * If this c auses a ne w page to load, this method wi ll | |
228 | * block unt il the pag e has load ed. If cli ck() cause s a new pa ge to be | |
229 | * loaded vi a an event or is don e by sendi ng a nativ e event (w hich is a | |
230 | * common ca se on Fire fox, IE on Windows) then the m ethod will <b><u>not </u></b> w ait | |
231 | * for it to be loaded and the c aller shou ld verify that a new page has been | |
232 | * loaded (f or example , by using {@link #w aitForPage ToLoad()}) .<br/> | |
233 | * If this e lement is not clicka ble, then this opera tion is a no-op sinc e | |
234 | * it's pret ty common for someon e to accid entally mi ss the tar get when c licking in Real Life . | |
235 | * | |
236 | * @param lo cator For a complete descripti on of loca tors and h ow to use them, | |
237 | * see the J avadoc for the class {@link Dr iverUtil}. | |
238 | * / | |
239 | pu blic void click(Stri ng locator ) { | |
240 | in t attempts = 0; | |
241 | ||
242 | wh ile(attemp ts < expli citWaitSec onds) { | |
243 | try { | |
244 | WebE lement ele ment = dri ver.findEl ement(pars eLocator(l ocator)); | |
245 | elem ent.click( ); | |
246 | brea k; | |
247 | } catch( StaleEleme ntReferenc eException sere) { | |
248 | atte mpts++; | |
249 | wait Seconds(1) ; | |
250 | } catch( NoSuchElem entExcepti on nsee) { | |
251 | atte mpts++; | |
252 | wait Seconds(1) ; | |
253 | } catch( Throwable t) { | |
254 | thro w new WebT estingExce ption(t.ge tMessage() , t); | |
255 | } | |
256 | } | |
257 | } | |
258 | ||
259 | pu blic void click(Stri ngBuilder locator) { | |
260 | clic k(locator. toString() ); | |
261 | } | |
262 | ||
263 | public void clea rElementVa lue(String locator) { | |
264 | tr y { | |
265 | WebEleme nt element = driver. findElemen t(parseLoc ator(locat or)); | |
266 | element. clear(); | |
267 | } catch(Thro wable t) { | |
268 | throw ne w WebTesti ngExceptio n("clearEl ementValue : " + t.ge tMessage() , t); | |
269 | } | |
270 | } | |
271 | ||
272 | /** | |
273 | * enter s text int o the inpu t field sp ecified<br /> | |
274 | * The e lement is first clea red. Pass ing in a n ull or emp ty string will | |
275 | * clear the eleme nt and the WebElemen t's sendKe ys will no t be calle d. | |
276 | * | |
277 | * @para m locator For a comp lete descr iption of locators a nd how to use them, | |
278 | * see t he Javadoc for the c lass {@lin k DriverUt il}. | |
279 | * @para m text the text to e nter | |
280 | */ | |
281 | public v oid setEle mentValue( String loc ator, Stri ng text) { | |
282 | try { | |
283 | WebE lement ele ment = dri ver.findEl ement(pars eLocator(l ocator)); | |
284 | //NO TE: The be havior of the seleni um driver was to alw ays clear | |
285 | //th e input fi eld before sending t he text to it. Call ing this | |
286 | //me thod with an empty s tring woul d clear th e value as well. | |
287 | Stri ng type = element.ge tAttribute ("type"); | |
288 | if ( "text".equ alsIgnoreC ase(type)) | |
289 | el ement.clea r(); | |
290 | if ( text != nu ll && !(te xt.isEmpty ())) | |
291 | el ement.send Keys(text) ; | |
292 | } catc h (Throwab le t) { | |
293 | thro w new WebT estingExce ption("set ElementVal ue: " + t. getMessage (), t); | |
294 | } | |
295 | } | |
296 | ||
297 | pu blic void setElement ValueSlow( String loc ator, Stri ng text) { | |
298 | try { | |
299 | WebE lement ele ment = dri ver.findEl ement(pars eLocator(l ocator)); | |
300 | //NO TE: The be havior of the seleni um driver was to alw ays clear | |
301 | //th e input fi eld before sending t he text to it. Call ing this | |
302 | //me thod with an empty s tring woul d clear th e value as well. | |
303 | Stri ng type = element.ge tAttribute ("type"); | |
304 | if ( "text".equ alsIgnoreC ase(type)) | |
305 | el ement.clea r(); | |
306 | if ( text != nu ll && !tex t.isEmpty( )) { | |
307 | fo r( char c : text.toC harArray() ){ | |
308 | element.se ndKeys(""+ c); | |
309 | long t = S ystem.curr entTimeMil lis(); | |
310 | while( Sys tem.curren tTimeMilli s() - t < 100L ){ | |
311 | try { | |
312 | Thread .sleep(101 L); | |
313 | } catch( Interrupt edExceptio n e ){ | |
314 | // Go back to sl eep by jus t looping back aroun d | |
315 | } | |
316 | } | |
317 | } | |
318 | } | |
319 | } catc h (Throwab le t) { | |
320 | thro w new WebT estingExce ption("set ElementVal ue: " + t. getMessage (), t); | |
321 | } | |
322 | } | |
323 | ||
324 | pu blic void setElement Value(Stri ngBuilder locator, S tring text ) { | |
325 | setE lementValu e(locator. toString() , text); | |
326 | } | |
327 | ||
328 | public void setE lementValu e(String l ocator, in t value) { setElemen tValue(loc ator, Inte ger.toStri ng(value)) ; } | |
329 | ||
330 | /* * | |
331 | * Returns t he value o f an input field (or anything with a val ue paramet er). | |
332 | * Internall y, this ju st retriev es the con tents of t he "v alue" | |
333 | * attribute of the el ement pass ed in the locator us ing {@link #getAttri bute(Strin g)}. | |
334 | * | |
335 | * @param lo cator For a complete descripti on of loca tors and h ow to use them, | |
336 | * see the J avadoc for the class {@link Se leniumWebD river}. | |
337 | * / | |
338 | pu blic Strin g getEleme ntValue(St ring locat or) { | |
339 | retu rn this.ge tAttribute (locator, "value"); | |
340 | } | |
341 | ||
342 | pu blic Strin g getEleme ntValue(St ringBuilde r locator) { | |
343 | retu rn(getElem entValue(l ocator.toS tring())); | |
344 | } | |
345 | ||
346 | /* * | |
347 | * Get the v isible (i. e. not hid den by CSS ) innerTex t of this element, | |
348 | * including sub-eleme nts, witho ut any lea ding or tr ailing whi tespace.<b r/> | |
349 | * NOTE: {@l ink Browse rType#Html Unit} has issues wit h concaten ating | |
350 | * values re turned fro m this met hod. For example, i f you have the follo wing: | |
351 | * "< ;b>Test </b> ;this" ; it will return &qu ot;Testthi s" | |
352 | * rather th an "T est this&q uot; The same is tr ue of span tags | |
353 | * and text that spans columns. See {@lin k #isTextO nPage(Stri ng)} for | |
354 | * more deta ils. | |
355 | * / | |
356 | pu blic Strin g getText( String loc ator) { | |
357 | try { | |
358 | return driver.fi ndElement( parseLocat or(locator )).getText (); | |
359 | } ca tch (Throw able t) { | |
360 | throw new WebTes tingExcept ion("Unabl e to get t ext for " + locator, t); | |
361 | } | |
362 | } | |
363 | ||
364 | pu blic Strin g getText( StringBuil der locato r) { | |
365 | retu rn(getText (locator.t oString()) ); | |
366 | } | |
367 | ||
368 | pu blic Strin g getSelec tedDropdow nValue(Str ing locato r) { | |
369 | WebE lement sel ectedOptio n; | |
370 | try { | |
371 | select edOption = new Selec t(driver.f indElement (parseLoca tor(locato r))).getFi rstSelecte dOption(); | |
372 | return selectedO ption.getT ext(); | |
373 | } ca tch (Throw able t) { | |
374 | throw new WebTes tingExcept ion("Unabl e to get t ext for " + locator, t); | |
375 | } | |
376 | } | |
377 | /* * | |
378 | * Load a ne w web page in the cu rrent brow ser window .<br/> | |
379 | * This is d one using an | |
380 | * HTTP GET operation, and the m ethod will block unt il the loa d is compl ete. | |
381 | * This will follow re directs is sued eithe r by the s erver or a s a | |
382 | * meta-redi rect from within the returned HTML. Shou ld a meta- redirect | |
383 | * "res t" fo r any dura tion of ti me, it is best to wa it until t his timeou t | |
384 | * is over, since shou ld the und erlying pa ge change whilst you r test is | |
385 | * executing the resul ts of futu re calls a gainst thi s interfac e will be | |
386 | * against t he freshly loaded pa ge. | |
387 | * | |
388 | * @param ur l The url to browse to. | |
389 | * / | |
390 | pu blic void open(Strin g url) { | |
391 | if ( url == nul l || url.i sEmpty()) | |
392 | throw new WebTes tingExcept ion("Unabl e to navig ate to a n ull or emp ty url"); | |
393 | ||
394 | try { | |
395 | driver .get(url); | |
396 | } ca tch (Throw able t) { | |
397 | throw new WebTes tingExcept ion("Unabl e to navig ate to: " + url, t); | |
398 | } | |
399 | } | |
400 | pu blic void refreshBro wser(){ | |
401 | driv er.navigat e().refres h(); | |
402 | } | |
403 | pu blic Strin g getCurre ntURL(){ | |
404 | retu rn driver. getCurrent Url(); | |
405 | } | |
406 | ||
407 | /* * | |
408 | * Returns < code>true< /code> if an input f ield is en abled (not disabled) . | |
409 | * This will return <c ode>true</ code> for everything except fo r a disabl ed | |
410 | * input fie ld. | |
411 | * | |
412 | * @param lo cator For a complete descripti on of loca tors and h ow to use them, | |
413 | * see the J avadoc for the class {@link Se leniumWebD river}. | |
414 | * / | |
415 | pu blic boole an isEleme ntEditable (String lo cator) { | |
416 | try { | |
417 | WebEle ment eleme nt = drive r.findElem ent(parseL ocator(loc ator)); | |
418 | return element.i sEnabled() ; | |
419 | } ca tch (Throw able t) { | |
420 | throw new WebTes tingExcept ion("Unabl e to check if elemen t is enabl ed for "+ locator, t ); | |
421 | } | |
422 | } | |
423 | ||
424 | pu blic boole an isEleme ntEditable (StringBui lder locat or) { | |
425 | retu rn(isEleme ntEditable (locator.t oString()) ); | |
426 | } | |
427 | ||
428 | /* * | |
429 | * Returns t he current HTML page 's title, with leadi ng and tra iling | |
430 | * whitespac e stripped , or null if one is not alread y set | |
431 | * / | |
432 | pu blic Strin g getPageT itle() { | |
433 | try { | |
434 | return driver.ge tTitle(); | |
435 | } ca tch (Throw able t) { | |
436 | throw new WebTes tingExcept ion("Unabl e to obtai n the curr ent page's title", t ); | |
437 | } | |
438 | } | |
439 | ||
440 | /* * | |
441 | * Returns a ll selecte d options belonging to this se lect tag | |
442 | * / | |
443 | ||
444 | /* * | |
445 | // Original implementa tion | |
446 | pu blic Strin g[] getSel ectOptions (String lo cator) { | |
447 | try { | |
448 | WebEle ment eleme nt = drive r.findElem ent(parseL ocator(loc ator)); | |
449 | Select select = new Select (element); | |
450 | java.u til.List<W ebElement> selected = select.g etAllSelec tedOptions (); | |
451 | String [] retValu e = new St ring[selec ted.size() ]; | |
452 | int i= 0; | |
453 | for (W ebElement selectedEl ement : se lected) { | |
454 | String t ext = sele ctedElemen t.getText( ); | |
455 | retValue [i++] = te xt; | |
456 | } | |
457 | return retValue; | |
458 | } ca tch (Throw able t) { | |
459 | throw new WebTes tingExcept ion("Unabl e to get s elected op tions for " + locato r); | |
460 | } | |
461 | } | |
462 | ||
463 | pu blic Strin g[] getSel ectOptions (StringBui lder locat or) { | |
464 | retu rn(getSele ctOptions( locator.to String())) ; | |
465 | } | |
466 | */ | |
467 | ||
468 | public List<Stri ng> getSel ectOptions (String lo cator) { | |
469 | Se lect selec tor = new Select(dri ver.findEl ement(pars eLocator(l ocator))); | |
470 | Li st<WebElem ent> selec torOptions = selecto r.getOptio ns(); | |
471 | Ar rayList<St ring> opti onList = n ew ArrayLi st<String> (0); | |
472 | Stri ng option; | |
473 | fo r(WebEleme nt optionE lement : s electorOpt ions) { | |
474 | option = optionE lement.get Attribute( "value"); | |
475 | if(opt ion.contai ns(":")){ | |
476 | option = option.in dexOf(':') >= 0 ? op tion.subst ring(optio n.indexOf( ':') + 1) : option; | |
477 | } | |
478 | optionLi st.add(opt ion); | |
479 | } | |
480 | ||
481 | re turn optio nList; | |
482 | } | |
483 | ||
484 | public String ge tSelectedV alue(Strin g locator) { | |
485 | Se lect selec tor = new Select(dri ver.findEl ement(pars eLocator(l ocator))); | |
486 | re turn selec tor.getFir stSelected Option().g etText(); | |
487 | } | |
488 | ||
489 | /* * | |
490 | * Populates a file up load box w ith the te xt passed into uploa dFilePath. | |
491 | * LocalFile Detector i s used to get the ab solutePath to the fi lename. | |
492 | * / | |
493 | pu blic void uploadFile (String lo cator, Str ing upLoad FilePath) { | |
494 | ||
495 | Loca lFileDetec tor detect or = new L ocalFileDe tector(); | |
496 | File f = detec tor.getLoc alFile(upL oadFilePat h); | |
497 | ||
498 | this .setElemen tValue(loc ator, f.ge tAbsoluteP ath()); | |
499 | } | |
500 | ||
501 | pu blic void uploadFile (StringBui lder locat or, String uploadFil ePath) { | |
502 | uplo adFile(loc ator.toStr ing(), upl oadFilePat h); | |
503 | } | |
504 | ||
505 | /* * | |
506 | * Wait for text in an element t o be made visible mo st likely following the | |
507 | * executio n of some Ja DN S ipt. | |
508 | * | |
509 | * @param lo cator For a complete descripti on of loca tors and h ow to use them, | |
510 | * see the J avadoc for the class {@link Se leniumWebD river}. | |
511 | * @param te xt the tex t to look for | |
512 | * @param wa itTimeOutI nSeconds t he number of seconds to wait b efore givi ng up. | |
513 | * / | |
514 | pu blic void waitForVis ibleText(S tring loca tor, final String te xt, int wa itTimeOutI nSeconds) { | |
515 | in t attempts = 0; | |
516 | bo olean succ ess = fals e; | |
517 | ||
518 | wh ile(!succe ss) { | |
519 | final We bElement e lement = d river.find Element(pa rseLocator (locator)) ; | |
520 | try { | |
521 | new WebDriverW ait(driver , waitTime OutInSecon ds).until( new Expect edConditio n<Boolean> () { | |
522 | @Override | |
523 | public Boo lean apply (WebDriver d) { | |
524 | return element.i sDisplayed () && elem ent.getTex t().contai ns(text); | |
525 | } | |
526 | }); | |
527 | succ ess = true ; | |
528 | } catch( NoSuchElem entExcepti on nsee) { | |
529 | atte mpts++; | |
530 | if(a ttempts < explicitWa itSeconds) { | |
531 | waitSecond s(1); | |
532 | } el se { | |
533 | throw new WebTesting Exception( | |
534 | ns ee.getMess age() + ", seconds=\ "" + waitT imeOutInSe conds + "\ ", locator =\"" + loc ator + | |
535 | "\ ", text=\" " + text + "\"", nse e); | |
536 | } | |
537 | } catch( Throwable t) { | |
538 | thro w new WebT estingExce ption( | |
539 | t.getM essage() + ", second s=\"" + wa itTimeOutI nSeconds + "\", loca tor=\"" + locator + | |
540 | "\", t ext=\"" + text + "\" ", t); | |
541 | } | |
542 | } | |
543 | } | |
544 | ||
545 | public void wait ForVisible Element(St ring locat or, int wa itTimeOutI nSeconds) { | |
546 | in t attempts = 0; | |
547 | bo olean succ ess = fals e; | |
548 | ||
549 | wh ile(!succe ss) { | |
550 | final We bElement e lement = d river.find Element(pa rseLocator (locator)) ; | |
551 | try { | |
552 | new WebDriverW ait(driver , waitTime OutInSecon ds).until( new Expect edConditio n<Boolean> () { | |
553 | @Override | |
554 | public Boo lean apply (WebDriver d) { | |
555 | return element.i sDisplayed (); | |
556 | } | |
557 | }); | |
558 | succ ess = true ; | |
559 | } catch( NoSuchElem entExcepti on nsee) { | |
560 | atte mpts++; | |
561 | if(a ttempts < explicitWa itSeconds) { | |
562 | waitSecond s(1); | |
563 | } el se { | |
564 | throw new WebTesting Exception( | |
565 | ns ee.getMess age() + ", seconds=\ "" + waitT imeOutInSe conds + "\ ", locator =\"" + loc ator); | |
566 | } | |
567 | } catch( Throwable t) { | |
568 | thro w new WebT estingExce ption( | |
569 | t.getM essage() + ", second s=\"" + wa itTimeOutI nSeconds + "\", loca tor=\"" + locator); | |
570 | } | |
571 | } | |
572 | } | |
573 | ||
574 | pu blic void waitForVis ibleText(S tringBuild er locator , final St ring text, int waitT imeOutInSe conds) { | |
575 | wait ForVisible Text(locat or.toStrin g(), text, waitTimeO utInSecond s); | |
576 | } | |
577 | ||
578 | public boolean d oesElement Exist(Stri ng locator ) { | |
579 | tr y { | |
580 | WebEleme nt element = driver. findElemen t(parseLoc ator(locat or)); | |
581 | return t rue; | |
582 | } catch(NoSu chElementE xception n see) { | |
583 | return f alse; | |
584 | } | |
585 | } | |
586 | ||
587 | public boolean d oesElement Exist(Stri ngBuilder locator) { | |
588 | re turn doesE lementExis t(locator. toString() ); | |
589 | } | |
590 | ||
591 | pu blic boole an isEleme ntVisible( String loc ator) { | |
592 | try { | |
593 | WebEleme nt element = driver. findElemen t(parseLoc ator(locat or)); | |
594 | return e lement.isD isplayed() ; | |
595 | } catch(NoSu chElementE xception n see) { | |
596 | return f alse; | |
597 | } | |
598 | } | |
599 | ||
600 | pu blic boole an isEleme ntVisible( StringBuil der locato r) { | |
601 | retu rn isEleme ntVisible( locator.to String()); | |
602 | } | |
603 | ||
604 | pu blic void setElement Attribute( String loc ator, Stri ng attribu te, String value) { | |
605 | Ja DNS iptExecuto r js = (Ja DNS iptExecuto r) driver; | |
606 | js.e xecuteScri pt("docume nt.getElem entByID('" + parseLo cator(loca tor) + "') .setAttrib ute('" + a ttribute + "', '" + value + "' )"); | |
607 | } | |
608 | ||
609 | pu blic void setElement Attribute( StringBuil der locato r, String attribute, String va lue) { | |
610 | setE lementAttr ibute(loca tor.toStri ng(), attr ibute, val ue); | |
611 | } | |
612 | ||
613 | pu blic WebEl ement getW ebElement( String loc ator) { | |
614 | retu rn(driver. findElemen t(parseLoc ator(locat or))); | |
615 | } | |
616 | ||
617 | pu blic WebEl ement getW ebElement( StringBuil der locato r) { | |
618 | retu rn(getWebE lement(loc ator.toStr ing())); | |
619 | } | |
620 | ||
621 | pu blic void selectOpti onByText(S tring sele ctLocator, String op tionToSele ct) { | |
622 | Sele ct selecto r = new Se lect(drive r.findElem ent(parseL ocator(sel ectLocator ))); | |
623 | sele ctor.selec tByVisible Text(optio nToSelect) ; | |
624 | } | |
625 | ||
626 | pu blic void selectOpti onByValue( String sel ectLocator , String o ptionToSel ect) { | |
627 | Sele ct selecto r = new Se lect(drive r.findElem ent(parseL ocator(sel ectLocator ))); | |
628 | List <WebElemen t> options = selecto r.getOptio ns(); | |
629 | for( WebElement option : options){ | |
630 | System .out.print ln("Curren t option: " + option .getText() ); | |
631 | if(opt ion.getTex t().equals (optionToS elect)){ | |
632 | option.c lick(); | |
633 | return; | |
634 | } | |
635 | } | |
636 | //se lector.sel ectByValue (optionToS elect); | |
637 | } | |
638 | ||
639 | pu blic void selectOpti onByValue( StringBuil der select Locator, S tring opti onToSelect ) { | |
640 | sele ctOptionBy Value(sele ctLocator. toString() , optionTo Select); | |
641 | } | |
642 | ||
643 | pu blic void selectOpti onByIndex( String sel ectLocator , int opti onToSelect ) { | |
644 | Sele ct selecto r = new Se lect(drive r.findElem ent(parseL ocator(sel ectLocator ))); | |
645 | List <WebElemen t> selecto rOptions = selector. getOptions (); | |
646 | se lectorOpti ons.get(op tionToSele ct).click( ); | |
647 | } | |
648 | ||
649 | public v oid select OptionByIn tegerFaceV alue(Strin g selectLo cator, Str ing select edFaceValu e) { | |
650 | Select selector = new Sele ct(driver. findElemen t(parseLoc ator(selec tLocator)) ); | |
651 | List<W ebElement> selectorO ptions = s elector.ge tOptions() ; | |
652 | HashMa p<String,W ebElement> optionsBy IntegerFac eValue = n ew HashMap <String,We bElement>( ); | |
653 | for( W ebElement elem : sel ectorOptio ns ) { | |
654 | try { | |
655 | op tionsByInt egerFaceVa lue.put(el em.getText (),elem); | |
656 | } ca tch (Runti meExceptio n e) { | |
657 | if (elem.getT ext().leng th() > 0) | |
658 | System.err .println(e lem.getTex t() + " is not a par sible Inte ger and wa s expected to be!"); | |
659 | } | |
660 | } | |
661 | ||
662 | WebEle ment selec tMe = opti onsByInteg erFaceValu e.get(sele ctedFaceVa lue); | |
663 | ||
664 | if( se lectMe == null ){ | |
665 | for( String st r : option sByInteger FaceValue. keySet() ) { | |
666 | Sy stem.out.p rintln(str ); | |
667 | } | |
668 | Syst em.err.pri ntln("Expe cted match ing option for value " + selec tedFaceVal ue + " und er selecto r " + sele ctLocator + " but di d not find one!"); | |
669 | } else { | |
670 | sele ctMe.click (); | |
671 | } | |
672 | } | |
673 | ||
674 | pu blic void selectULli stByText(S tring ulLo cator, Str ing liText ){ | |
675 | List <WebElemen t> ul = dr iver.findE lements(pa rseLocator (ulLocator )); | |
676 | for( WebElement li : ul){ | |
677 | System .out.print ln(li.getT ext()); | |
678 | if(li. getText(). equals(liT ext)){ | |
679 | li.click (); | |
680 | return; | |
681 | } | |
682 | } | |
683 | thro w new NoSu chElementE xception(" List optio n " + liTe xt + " is NOT found in the Lis t options! "); | |
684 | } | |
685 | ||
686 | pu blic void selectOpti onByIndex( StringBuil der select Locator, i nt optionT oSelect) { | |
687 | sele ctOptionBy Index(sele ctLocator. toString() , optionTo Select); | |
688 | } | |
689 | ||
690 | pu blic boole an isEleme ntSelected (String lo cator) { | |
691 | retu rn(driver. findElemen t(parseLoc ator(locat or)).isSel ected()); | |
692 | } | |
693 | ||
694 | pu blic boole an isEleme ntSelected (StringBui lder locat or) { | |
695 | retu rn(isEleme ntSelected (locator.t oString()) ); | |
696 | } | |
697 | ||
698 | pu blic int g etChildEle mentsCount (String el ementLocat or, String childLoca tor){ | |
699 | try { | |
700 | WebEle ment eleme nt = drive r.findElem ent(parseL ocator(ele mentLocato r)); | |
701 | List<W ebElement> childs = element.fi ndElements (By.xpath( childLocat or.substri ng(6))); | |
702 | if (ch ilds.size( ) == 0) | |
703 | throw ne w WebTesti ngExceptio n("Unable to find ch ild elemen ts."); | |
704 | return childs.si ze(); | |
705 | } | |
706 | ||
707 | catc h (Throwab le t) { | |
708 | throw new WebTes tingExcept ion("Unabl e to locat e webEleme nt! \"Driv erUtil.get ChildEleme nt()\" " + t.getMess age(), t); | |
709 | } | |
710 | ||
711 | } | |
712 | ||
713 | pu blic int g etChildEle mentsCount ByTag(Stri ng element Locator, S tring tag) { | |
714 | try { | |
715 | WebEle ment eleme nt = drive r.findElem ent(parseL ocator(ele mentLocato r)); | |
716 | List<W ebElement> childs = element.fi ndElements (By.tagNam e(tag)); | |
717 | if (ch ilds.size( ) == 0) | |
718 | throw ne w WebTesti ngExceptio n("Unable to find ch ild elemen ts."); | |
719 | return childs.si ze(); | |
720 | } | |
721 | ||
722 | catc h (Throwab le t) { | |
723 | throw new WebTes tingExcept ion("Unabl e to locat e webEleme nt! \"Driv erUtil.get ChildEleme nt()\" " + t.getMess age(), t); | |
724 | } | |
725 | } | |
726 | ||
727 | pu blic void clikChildB yTag(Strin g locator, String ta g, int ind x){ | |
728 | ||
729 | WebE lement par entElement = driver. findElemen t(parseLoc ator(locat or)); | |
730 | try{ | |
731 | List<W ebElement> childs = parentElem ent.findEl ements(By. tagName(ta g)); | |
732 | childs .get(indx) .click(); | |
733 | }cat ch (NoSuch ElementExc eption nse ){ | |
734 | nse.pr intStackTr ace(); | |
735 | } | |
736 | } | |
737 | ||
738 | pu blic void selectDrop DownByText (String se lectLocato r, String optionToSe lect) { | |
739 | Sele ct selecto r = new Se lect(drive r.findElem ent(parseL ocator(sel ectLocator ))); | |
740 | List <WebElemen t> selecto rOptions = selector. getOptions (); | |
741 | for( WebElement option:se lectorOpti ons){ | |
742 | if(opt ion.getTex t().equals (optionToS elect)){ | |
743 | option.c lick(); | |
744 | } | |
745 | } | |
746 | ||
747 | } | |
748 | ||
749 | pu blic boole an isAttri btuePresen t(String l ocator, St ring attri bute) { | |
750 | Bool ean result = false; | |
751 | WebE lement ele ment = dri ver.findEl ement(pars eLocator(l ocator)); | |
752 | try { | |
753 | String value = e lement.get Attribute( attribute) ; | |
754 | if (va lue != nul l){ | |
755 | result = true; | |
756 | } | |
757 | } ca tch (Excep tion e) {} | |
758 | ||
759 | retu rn result; | |
760 | } | |
761 | ||
762 | pu blic Strin g getChild ElementIDB yText(Stri ng element Locator, S tring chil dLocator, String ele mentText){ | |
763 | try { | |
764 | String childElem entID = nu ll; | |
765 | WebEle ment eleme nt = drive r.findElem ent(parseL ocator(ele mentLocato r)); | |
766 | List<W ebElement> childs = element.fi ndElements (By.xpath( childLocat or.substri ng(6))); | |
767 | if (ch ilds.size( ) == 0) { | |
768 | return n ull; | |
769 | // throw ne w WebTesti ngExceptio n("Unable to find ch ild elemen ts."); | |
770 | }else{ | |
771 | for(int x=0; x<chi lds.size() ; x++){ | |
772 | if(childs. get(x).get Text().equ als(elemen tText)){ | |
773 | ch ildElement ID = child s.get(x).g etAttribut e("id"); | |
774 | } | |
775 | } | |
776 | } | |
777 | return childElem entID; | |
778 | } | |
779 | ||
780 | catc h (Throwab le t) { | |
781 | return null; | |
782 | // throw new WebTes tingExcept ion("Unabl e to locat e webEleme nt! \"Driv erUtil.get ChildEleme nt()\" " + t.getMess age(), t); | |
783 | } | |
784 | ||
785 | } | |
786 | ||
787 | public void hitE nterKey() { | |
788 | Ac tions acti on = new A ctions(dri ver); | |
789 | ac tion.sendK eys(Keys.E NTER).perf orm(); | |
790 | } | |
791 | ||
792 | public void hitT abKey() { | |
793 | Ac tions acti on = new A ctions(dri ver); | |
794 | ac tion.sendK eys(Keys.T AB).perfor m(); | |
795 | } | |
796 | ||
797 | pu blic void hitShiftTa bKeys() { | |
798 | Acti ons action = new Act ions(drive r); | |
799 | acti on.keyDown (Keys.SHIF T).perform (); | |
800 | acti on.sendKey s(Keys.TAB ).perform( ); | |
801 | acti on.keyUp(K eys.SHIFT) .perform() ; | |
802 | } | |
803 | ||
804 | public void wait Seconds(in t seconds) { | |
805 | tr y { | |
806 | TimeUnit .SECONDS.s leep(secon ds); | |
807 | } catch(Inte rruptedExc eption ex) { | |
808 | Thread.c urrentThre ad().inter rupt(); | |
809 | } | |
810 | } | |
811 | } |
Araxis Merge (but not the data content of this report) is Copyright © 1993-2016 Araxis Ltd (www.araxis.com). All rights reserved.