+ All Categories
Home > Documents > Selenium Webdriver Tutorials

Selenium Webdriver Tutorials

Date post: 07-Jul-2018
Category:
Upload: sudheer-reddy
View: 231 times
Download: 0 times
Share this document with a friend
292
Day 1 - Basic Java Tutorials For Selenium WebDriver 1. Datatypes In Java 2. String Class In Java . i!" i! else an# neste# i! else In Java $. !or loop In Java %. &'ile" #o &'ile loops In Java (. )ne an# t&o #imensional array In Java *. +et'o#s In Java ,. ccess +o#iers In Java /. 0eturn Type )! +et'o# In Java 1. Static" on Static +et'o#s" 3 ariables In Java 11. )b4ect In Java 12. 3 ariabl e T ypes In Java 1. Constructor In Java 1$. In'eritance In Java 1%. Inter!ace In Java 1(. rray5ist In Java 1*. 6as'table In Java 1,. 0ea#-Write Te7t File In Java 1/. 87ception 'an#ling" try-catc'-nally" t'ro& an# t'ro&s In Java Day 2 - WebDriver Installation n# Conguration 1. Intro#uctio n o! Selenium WebDr iver 2. Do&nloa#ing an# installation )! Selenium Web#r iver &it' 8clipse . Creating n# 0unning First Web#river Script --.1 0unning WebDriver In 9oogle C'rome Day - Congure J:nit Wit' WebDriver $. Do&nloa#ing an# installation )! J:nit &it' 8clipse --%.1 Creating an# running &eb#river test &it' 4unit --%.2 Creating an# running 4unit test suite &it' &eb#river --(.1 :sing J:nit nnotations in &eb#river  --(.2 ;Be!ore<;!ter 3S ;Be!oreClass<;!terClass Di=erence --(. Ignoring J:nit Test !rom e7ecution --(.$ Junit Timeout n# 87pecte# 87 ception T est Day $ - 9enerate Test 0eport :sing J:nit *. Selenium WebDriver Test report 9eneration using J:nit - Steps  Day % - 8lement 5ocators In WebDriver ,. Di=erent Ways )! 5ocating 8lements In WebDriver  Day % - WebDriver Basic ction Comman#s Wit' 87ample
Transcript
Page 1: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 1/292

Day 1 - Basic Java Tutorials For Selenium WebDriver

1. Datatypes In Java2. String Class In Java

. i!" i! else an# neste# i! else In Java$. !or loop In Java%. &'ile" #o &'ile loops In Java(. )ne an# t&o #imensional array In Java*. +et'o#s In Java,. ccess +o#i ers In Java/. 0eturn Type )! +et'o# In Java1 . Static" on Static +et'o#s" 3ariables In Java11. )b4ect In Java12. 3ariable Types In Java1 . Constructor In Java1$. In'eritance In Java1%. Inter!ace In Java1(. rray5ist In Java1*. 6as'table In Java1,. 0ea#-Write Te7t File In Java1/. 87ception 'an#ling" try-catc'- nally" t'ro& an# t'ro&s In JavaDay 2 - WebDriver Installation n# Con guration

1. Intro#uction o! Selenium WebDriver2. Do&nloa#ing an# installation )! Selenium Web#river &it' 8clipse

. Creating n# 0unning First Web#river Script-- .1 0unning WebDriver In 9oogle C'rome

Day - Con gure J:nit Wit' WebDriver

$. Do&nloa#ing an# installation )! J:nit &it' 8clipse --%.1 Creating an# running &eb#river test &it' 4unit--%.2 Creating an# running 4unit test suite &it' &eb#river--(.1 :sing J:nit nnotations in &eb#river --(.2 ;Be!ore<; !ter 3S ;Be!oreClass<; !terClass Di=erence--(. Ignoring J:nit Test !rom e7ecution--(.$ Junit Timeout n# 87pecte# 87ception Test

Day $ - 9enerate Test 0eport :sing J:nit

*. Selenium WebDriver Test report 9eneration using J:nit - Steps

Day % - 8lement 5ocators In WebDriver

,. Di=erent Ways )! 5ocating 8lements In WebDriver

Day % - WebDriver Basic ction Comman#s Wit' 87ample

Page 2: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 2/292

/. WebDriver Basic ction Comman#s n# )perations Wit' 87amples > )pen Fire!o7 Bro&ser > +a7imi?ing Bro&ser Win#o& > )pen :05 In Bro&ser > Clic@ing )n Button > Submitting Form :sing .submitA +et'o# > Store Te7t )! 8lement > Typing Te7t In To Te7t bo7 > 9et age Title > 9et Current age :05 > 9et Domain ame > 9enerating lert +anually > Selecting 3alue From Drop#o&n )r 5istbo7 > Deselecting 3alue From Drop#o&n )r 5istbo7 > avigating Bac@ n# For&ar# > 3eri!y 8lement resent > Capturing 8ntire age Screens'ot > 9enerating +ouse 6over 8vent > 6an#ling +ultiple Win#o&s > 3eri!y 8lement Is 8nable# )r ot > 8nable<Disable 8lement > 6an#ling lert" Con rmation an# rompt popup > 6an#le :ne7pecte# lert

Day ( - WebDriver Wait For 87amples :sing J:nit n# 8clipse

1 . 6o& to apply implicit &ait in selenium WebDriver script 11. WebDriver 87amples -> Wait For 8lement

> Wait For Te7t > Wait For lert > Wait For Title

> Wait For 8lement 3isible > Wait For 8lement Invisible

Day * - WebDriver )t'er 87amples

12. WebDriver )t'er 87amples -> n#8lementA n# n#8lementsA Di=erence > 9enerating 5og In WebDriver > Creating )b4ect 0epository :sing roperties File > 87tracting ll 5in@s From age > 87tracting<0ea#ing Table Data > 6an#le Dynamic Web Table > Create n# :se Custom Fire!o7 ro le In Selenium WebDriver Test

> Do&nloa#ing Files :sing Selenium WebDriver > 6an#ling 4a7 uto Suggest Drop 5ist

Page 3: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 3/292

1 . arameteri?ation<Data Driven Testing 1$. Selenium WebDriver Test

Day , - Test 9 Frame&or@ Tutorials

1%. Intro#uction )! Test 9 1(. Test 9 Installation Steps1*. Similarities an# Di=erence Bet&een Test 9 an# J:nit1,. Create n# 0un First Test 9-WebDriver Test 1/. Test g annotations &it' e7amples 2 . Creating n# 0unning WebDriver Test Suit :sing testng.7ml File--2 .1 Creating Single )r +ultiple Tests For +ultiple Classes--2 .2 Creating Test Suite :sing Class From Di=erent ac@ages--2 . Creating Test Suite :sing Selecte# )r ll ac@ages--2 .$ Inclu#ing )nly Selecte# Test +et'o#s In Selenium WebDriver-Test g

Test--2 .% testng.7ml - Inclu#e<87clu#e Selenium WebDriver Test ac@age--2 .( testng.7ml - :sing 0egular 87pression To Inclu#e<87clu#e Test+et'o#--2 .* testng.7ml - S@ip Test Intentionally :sing S@ip87ceptionA--2 ., Data #riven Testing using ;Data rovi#er nnotation )! Test 9--2 ./ arallel test e7ecution In multiple bro&sers using ; arametersannotation

Day / - WebDriver ssertions Wit' Test 9

6ar# ssertions2 .1 assert8 uals ssertion Wit' 87ample2 .2 assert ot8 uals ssertion Wit' 87ample2 . assertTrue ssertion Wit' 87ample2 .$ assertFalse ssertion Wit' 87ample2 .$ assert ull ssertion Wit' 87ample2 .$ assert ot ull ssertion Wit' 87ample

So!t ssertion2 .$ pplying Test 9 So!t ssertion Wit' 87ample

Day 1 Common Functions To :se In WebDriver Test2 .1 Common Function To Compare T&o Strings2 .2 Common Function To Compare T&o Integers2 . Common Function To Compare T&o Doubles

Page 4: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 4/292

1. Datatypes In Java

1.Data types - Basic Java Tutorials For Selenium WebDriver

I 'ave receive# many re uests !rom my blog rea#ers !or posting somebasic 4ava tutorials &'ic' are really re uire# in selenium &eb#river. Sono& I 'ave planne# to post some basic 4ava tutorial posts &'ic' are reallyre uire# in selenium &eb#river learning an# implementation in yourpro4ect. T'ese tutorials &ill 'elpyou !or selenium &eb#river intervie& preparation too. 5et &e start !rom#i=erent #ata types In 4ava &'ic' &e can use in our selenium &eb#rivertest preparation.

In 4ava or any ot'er programming languages" #ata types represents t'at&'ic' type o! values can be store# an# &'at &ill be t'e si?e o! t'at valueor 'o& muc' memory &ill be allocate#. T'ere are nearest eig't #i=erent#ata types in 4ava li@e byte" s'ort" int" long" Eoat" #ouble" c'ar" Boolean.byte an# s'ort #atatypes are not muc' more use!ul in selenium &eb#riverso I am s@ipping t'em 'ere to #ecrease your con!usions.

int #atatype

Page 5: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 5/292

int #ata type is use!ul to store 2 bit integer A87ample $%2 values only.We can not store #ecimal A87ample $%2.2 values in int #ata type.87ample

int i G $%2 H

long #atatypelong #atatype is use!ul to store ($ bit integerA87ample (%2 $% values.

ou can use it &'en your value is more larger an# can not 'ol# it in int.Same as int #atatype" &e can not store #ecimal A87ample $%2.2 valuesin long #atatype87ample

long l G (%2 $%H

#ouble #atatype

#ouble #atatype is use!ul to store ($ bit #ecimalA87ample %(.2 %$values. We can store integer A87ample 12$%( values too in #ouble#atatype.87ample

#ouble #1 G %(.2 %$H#ouble #2 G 12$%(H

c'ar #atatype

c'ar #atatype is use!ul to store single c'aracterA87ample # . It can notstore more t'an one A87ample ## c'aracter in it.87ample c'ar c G # H

boolean #atatypeboolean #atatype is use!ul to store only booleanA87ample true values.87ample

boolean b G trueH

String ClassString is not a #ata type but it is a class &'ic' is use!ul to store string invariable.87ample

String str G K6ello Worl#KH

3I8W DIFF808 T F: CTI) S )F ST0I 9 C5 SS

Create# bello& given e7ample !or all t'ese #atatypes. 0un it in your

Page 6: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 6/292

eclipse an# veri!y results.

public class #atatypes L

public static voi# mainAStringMN args L int i G $%2 H <<Can store 2 bit integer values only. long l G (%2 $%H <<Can store ($ bit integer values only. #ouble #1 G %(.2 %$H <<Can store ($ bit #ecimal values. #ouble #2 G 12$%(H <<We can use it !or integer values too. c'ar c G # H <<Can store single c'aracter only. boolean t G trueH <<Can store only boolean values li@e true or !alse. String str G K6ello Worl#KH <<Can store any string values.

System.out.printlnAKInteger 3ar Is --O KPi H System.out.printlnAK5ong 3ar Is --O KPl H System.out.printlnAK#ouble 3ar #1 Is --O KP#1 H System.out.printlnAK#ouble 3ar #2 Is --O KP#2 H System.out.printlnAKc'ar 3ar c Is --O KPc H System.out.printlnAKboolean 3ar b Is --O KPt H System.out.printlnAKboolean 3ar str Is --O KPstr H QQ

Bello& given result &ill #isplay in your console at t'e en# o! e7ecution.

Integer 3ar Is --O $%2

5ong 3ar Is --O (%2 $%#ouble 3ar #1 Is --O %(.2 %$#ouble 3ar #2 Is --O 12$%(.c'ar 3ar c Is --O #boolean 3ar b Is --O trueString 3ar str Is --O 6ello Worl#

2. String Class In Java

2.String In Java - Tutorials For WebDriver

We 'ave learnt about DIFF808 T D T T 8S I J 3 In my past post.o&" +any peoples are un#erstan#ing t'at String Is also a #ata type. 5et

me correct t'em -O String Is not #ata type. I! string Is not #ata types t'en&'at Is StringR T'is uestion can be as@e# by Intervie&er too. String Is anInbuilt class o!

4ava. String class 'as many Inbuilt !unctions &'ic' &e can use to per!orm#i=erent actions on string.

I! you #on t @no&" 5et me tell you t'at &e 'ave to &or@ &it' strings very!re uently In selenium WebDriver tests. So you must 'ave @no&le#ge o! use!ul !unctions o! String class to use t'em In your selenium &eb#river

Page 7: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 7/292

test #evelopment. 5et us ta@e simple e7ample o! String to un#erstan##i=erent met'o#s o! String class.

public class Strng 87ample L

public static voi# mainAStringMN args L

String st1 G KT'is Worl# is 3ery iceKH String st2 G K n# Beauti!ul.KH

<<Comparing t&o strings. 0eturn true I! bot' matc' else return !alse. System.out.printlnAKst1 e uals to st2R -O KPst1.e ualsAst2 H

<<Concatenates st2 &it' st1. System.out.printlnAKConcatenation o! st1 an# st2 Is -OKPst1.concatAst2 H

<<0etrieve t'e /t' In#e7e# c'aracter !rom string. System.out.printlnAKC'aracter at In#e7 / Is -O KPst1.c'ar tA/ H

<<Fin# t'e lengt' o! string. System.out.printlnAK5engt' )! St1 -O KPst1.lengt'A H

<<Converting &'ole string In lo&er case. System.out.printlnAKString In 5o&ercase -O KPst1.to5o&erCaseA H

<<Converting &'ole string In upper case. System.out.printlnAKString In uppercase -O KPst1.to:pperCaseA H

<<0etrieve t'e In#e7 o! rst i c'aracter. System.out.printlnAKIn#e7 o! 1st c'arater i Is -O KPst1.in#e7)!A i H

<<0etrieve t'e in#e7 o! 2n# most i c'aracter. System.out.printlnAKIn#e7 o! 2n# c'arater i Is -O KPst1.in#e7)!A i " H

<<0etrieve t'e In#e7 o! &or# 3ery !rom string. System.out.printlnAKIn#e7 o! &or# 3ery Is -O KPst1.in#e7)!AK3eryK H

<<Converting value From int to string. int 4 G *%H String val2 G String.value)!A4 H System.out.printlnAK3alue )! string val2 Is -O KPval2 H

<<Converting string to integer. String val1GK% KH int i G Integer.parseIntAval1 H

System.out.printlnAK3alue )! int i Is -O KPi H

Page 8: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 8/292

<< rint t'e String starting !rom %t' In#e7 to 12t' In#e7. System.out.printlnAK0etrieving sub string !rom string -OKPst1.substringA%" 1 H

<<Split string.String spltMN G st1.splitAK3eryK H

System.out.printlnAKString art 1 Is -O KPspltM N H System.out.printlnAKString art 2 Is -O KPspltM1N H

<<Trim String. System.out.printlnAKTrimme# st2 -O KPst2.trimA HQ

Q

I! you &ill loo@ at above e7ample" I 'ave prepare# #i=erent e7amples o! string met'o# to ta@e some action or &e can say operations on string.Sort e7planation o! all above string met'o#s are as bello&.

T&o String Comparison To compare t&o strings" &e can use synta7 li@e st1.e ualsAst2 . It &illreturn True I! bot' strings are same else It &ill return False.

T&o String ConcatenationSynta7 st1.concatAst2 &ill concatenate st1 &it' st2.

0etrieving C'aracter !rom In#e7

Synta7 st1.c'ar tA/ &ill retrieve t'e c'aracter !rom string st1 locate# at/t' In#e7. In#e7 Starts !rom .

5engt' )! Stringst1.lengt'A &ill return leng't o! string st1.

Convert String In 5o&er Case 5ettersst1.to5o&erCaseA &ill convert &'ole string letters In lo&er case.

Convert String In :pperCase 5ettersst1.to:pperCaseA &ill convert &'ole string letters In upper case.

0etrieving In#e7 )! 1st most c'aracterst1.in#e7)!A i &ill retrieve In#e7 o! rst most c'aracter i !rom string.

0etrieving In#e7 )! 2n# most c'aracterst1.in#e7)!A i " &ill retrieve In#e7 o! secon# most c'aracter i !romstring. #escribe# t'e !rom In#e7 means It &ill start n#ing c'aracter i!rom In#e7 . First i 'as In#e7 2 so &e nee# to use to n# 2n# mostc'aracter.

0etrieving In#e7 )! speci c &or# !rom stringst1.in#e7)!AK3eryK &ill retrieve in#e7 o! &or# 3ery !rom string.

Page 9: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 9/292

Convert !rom Integer To StringString.value)!A4 &ill convert value o! 4 !rom int to string.

Convert !rom String To IntegerInteger.parseIntAval1 &ill conver value o! val1 !rom string to int.

0etrieving sub string !rom stringSynta7 st1.substringA%" 1 &ill retrieve string !rom In#e7 % To In#e7 1 .

Split StringString spltMN G st1.splitAK3eryK &ill split string st1 !rom &or# 3ery an#store bot' strings In array.

Trim StringI! string 'as &'ite space at beginning or en# o! t'e string t'en you canuse trim !unction li@e st2.trimA to remove t'at &'ite space.

. i!" i! else an# neste# i! else In Java

I! 8lse Statement - Basic Java Tutorials For Selenium WebDriver

In my 083I):S )ST " &e 'ave seen #i=erent #ata types in 4ava. It is very

important to learn basic 4ava tutorials I! you &ants to learn selenium&eb#river &it' Java. Because I! you #o not 'ave @no&le#ge o! basic 4avat'en you can not create single logical &eb#river test case. n# also youcan not pass anycompany intervie&. So @eep your eyes on t'ese tutorials an# I &ill ta@eyou !rom basic to a#vance# 4ava tutorials step by step. In t'is post" 5et metell you t'e usage o! I! else con#itional statements in 4ava.

i!" i! else an# neste# i! else statements are use!ul to ta@e t'e #ecisionbase# on con#itional matc'.W'en you &ants to e7ecute some part o! co#ei! con#ition returns true t'en you nee# to use t'is @in# o! con#itionalstatements.

Simple I! Statementart o! co#e &ill be e7ecute# only i! speci e# con#ition returns true. I!

con#ition &ill return !alse t'en t'at co#e &ill be not e7ecute#.87ample

i! Ai 4System.out.printlnAK3alue )! iAKPiPK Is Smaller T'an 3alue )! 4AKP4PK .K HIn above given e7ample" message &ill be printe# only an# only i! value o!

variable i is less t'an value o! variable 4.

Page 10: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 10/292

I! else StatementI! con#ition returns true t'en part o! i! bloc@ &ill be e7ecute#. I! con#itionreturns !alse t'en part o! else bloc@ &ill be e7ecute#.87ample

i! AiOG4L System.out.printlnAK3alue )! iAKPiPK Is 9reater T'an )r 8 uals To 3alue)! 4AKP4PK .K HQelseL System.out.printlnAK3alue )! iAKPiPK Is Smaller T'an 3alue )! 4AKP4PK .K HQIn above given e7ample" i! bloc@ s message &ill be printe# i! value o! variable i is greater t'an or e uals to value o! variable 4. else bloc@ &ill bee7ecute# i! value o! variable i is less t'an value o! variable 4.

este# I! 8lse Statements ou can use neste# i! else statement &'en you &ants to c'ec@ multiplecon#itions an# ta@e #ecision base# on it.87ample

i! A@ iL System.out.printlnAK3alue )! @AKP@PK Is 5ess T'an 3alue )! iAKPiPK K H

Qelse i! A@OGi UU @ G4L System.out.printlnAK3alue )! @AKP@PK Is In Bet&een 3alue )! iAKPiPK n#3alue )! 3alue )! 4AKP4PK K HQelseL System.out.printlnAK3alue )! @AKP@PK Is 9reater T'an 3alue )!

4AKP4PK K HQ

In above given e7ample" rst Ai! bloc@ &ill be e7ecute# i! value o! variable@ is less t'an t'e value o! variable i. Secon# Aelse i! bloc@ &ill be e7ecute#i! value o! variable @ is greater t'an or e uals to value o! variable i an#less t'an or e uals to value o! variable 4. T'ir# Aelse bloc@ &ill bee7ecute# i! value o! variable @ is greater t'an value o! value o! variable 4.

ou can ma@e a c'ain o! i! else statement i! you &ants to c'ec@ morecon#itions.

T'is &ay" ou can use any o! above con#itional statement base# on yourre uirement. 0un bello& given e7ample in your eclipse by c'anging t'evalues o! variables.

Page 11: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 11/292

public class I!Statements L

public static voi# mainAStringMN args L

int i G 2%H int 4 G % H int @ G 2$H <<Simple I! statement System.out.printlnAK>>>Simple I! Statement 87ample>>>K H i! Ai 4 <<Bello& given message &ill be printe# only i! value o! variable i isless t'an value o! variable 4. System.out.printlnAK3alue )! iAKPiPK Is Smaller T'an 3alue )!

4AKP4PK .K H

<<I! 8lse Statement System.out.printlnAKK H System.out.printlnAK>>>I! 8lse Statement 87ample>>>K H i! AiOG4 <<Bello& given message &ill be printe# i! value o! variable i isgreater t'an or e uals to value o! variable 4. L System.out.printlnAK3alue )! iAKPiPK Is 9reater T'an )r 8 uals To 3alue)! 4AKP4PK .K H Qelse <<Bello& given message &ill be printe# i! value o! variable i is lesst'an value o! variable 4.

L System.out.printlnAK3alue )! iAKPiPK Is Smaller T'an 3alue )! 4AKP4PK .K H Q

<< este# I! 8lse Statement System.out.printlnAKK H System.out.printlnAK>>> este# I! 8lse Statement art>>>K H i! A@ i<<Bello& given message &ill be printe# i! value o! variable @ is lesst'an value o! variable i.

L System.out.printlnAK3alue )! @AKP@PK Is 5ess T'an 3alue )! iAKPiPK K H Qelse i! A@OGi UU @ G4 <<Bello& given message &ill be printe# i! value o!variable @ is greater t'an or e uals to value o! variable i an# less t'an ore uals to value o! variable 4. L System.out.printlnAK3alue )! @AKP@PK Is In Bet&een 3alue )! iAKPiPK

n# 3alue )! 3alue )! 4AKP4PK K H Qelse <<Bello& given message &ill be printe# i! value o! variable @ isgreater t'an value o! variable 4.

L

Page 12: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 12/292

System.out.printlnAK3alue )! @AKP@PK Is 9reater T'an 3alue )! 4AKP4PK KH

Q QQ

Bello& given output &ill be printe# in your eclipse console &'en you &illrun above e7ample.

>>>Simple I! Statement 87ample>>>3alue )! iA2% Is Smaller T'an 3alue )! 4A% .

>>>I! 8lse Statement 87ample>>>3alue )! iA2% Is Smaller T'an 3alue )! 4A% .

>>> este# I! 8lse Statement art>>>3alue )! @A2$ Is 5ess T'an 3alue )! iA2%

$. !or loop In Java

!or 5oop - Basic Java Tutorials For Selenium WebDriver

We 'ave learnt #i=erent i! else con#ition statements in my 083I):S)ST . o& let &e move to loops in 4ava. 5oopsA!or loop" &'ile loop" #o&'ile loop 'ave very important role in selenium &eb#river test case#evelopment &it' 4ava or any ot'er languages. s you @no&" sometimesyou nee# to per!orm same actionmultiple timesA87ample 1 or more times on your &eb page. o& i!you &ill &rite multiple lines o! co#e to per!orm same action multiple timest'en it &ill increase your co#e si?e. For t'e best practice" you nee# to useloops in t'is @in# o! situations.

5et me #escribe you !or loop in t'is post an# you can vie& my 8VT )ST!or rea#ing about &'ile loop.

!or 5oop T'ere are t'ree parts insi#e !or loop. 1. 3ariable Initiali?ation" 2. Con#ition To Terminate an# . Increment<Decrements variable. !or loop &ill beterminate# &'en con#ition to terminate &ill becomes !alse.87ample !orAint iG H i G H iPP L System.out.printlnAK3alue )! 3ariable i is K Pi HQ

Bello& given e7ample is simple e7ample o! !or loop. run it in your eclipse

Page 13: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 13/292

an# veri!y t'e result.

public class !orloop L

public static voi# mainAStringMN args L !orAint iG H i G H iPP L <<T'is loop &ill be e7ecute# $ times System.out.printlnAK3alue )! 3ariable i is K Pi H Q

System.out.printlnAKK H int iG H int @ G 2 H !orAint 4G H 4OGiH 4-- L<<T'is loop &ill be e7ecute# $ times System.out.printlnAK3alue )! 3ariable 4 is K P4 H @ G @-1 H Q System.out.printlnAKK H System.out.printlnAK3alue )! 3ariable @ is K P@ H

Q

Q

W'en you &ill run above given e7ample" you &ill get bello& given output.

3alue )! 3ariable i is

3alue )! 3ariable i is 13alue )! 3ariable i is 23alue )! 3ariable i is

3alue )! 3ariable 4 is 3alue )! 3ariable 4 is 23alue )! 3ariable 4 is 13alue )! 3ariable 4 is

3alue )! 3ariable @ is 1(%. &'ile" #o &'ile loops In Java

&'ile" #o &'ile 5oops - Basic Java Tutorials For Selenium WebDriver

s &e 'ave learnt in my 083I):S )ST " loopsA!or loop" &'ile loop in 4ava or any ot'er programming languages are use!ul to e7ecute bloc@ o! co#e multiple times. ou &ill 'ave to use loops in your selenium &eb#rivertest very !re uently. We 'ave alrea#y learnt !or loop &it' #i=erente7amples in my previous post.

o& let me #escribe you &'ile loop an# #o &'ile loop &it' practicale7amples.

&'ile 5oop

Page 14: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 14/292

Bloc@ o! co#e &'ic' is &ritten insi#e &'ile loop &ill be e7ecute# till t'econ#ition o! &'ile loop remains true.87ample int i G H&'ileAi G L

System.out.printlnAK3alue )! 3ariable i Is KPi H iPPH QIn above given e7ample" &'ile loop &ill be e7ecute# !our times.

#o &'ile 5oopSame as &'ile loop" #o &'ile loop &ill be e7ecute# till t'e con#itionreturns true.87ample

int 4G H #oL System.out.printlnAK3alue )! 3ariable 4 Is KP4 H 4G4-1H Q&'ileA4O HIn above given e7ample" &'ile loop &ill be e7ecute# only one time.

Di=erence bet&een &'ile an# #o &'ile loop T'ere is one #i=erence bet&een &'ile an# #o &'ile loop.

• &'ile loop &ill c'ec@ con#ition at t'e beginning o! co#e bloc@ so It

&ill be e7ecute# only i! con#ition A&'ileAi G returns true.

• #o &'ile loop &ill c'ec@ con#ition at t'e en# o! co#e bloc@ so It &illbe e7ecute# minimum one time. !ter 1st time e7ecution" it &illc'ec@ t'e con#ition an# i! it returns true t'en co#e o! bloc@ &ill bee7ecute# once more or multiple time.

Disa#vantage o! &'ile or #o &'ile loopI! you &ill !orget to Increment or #ecrements variable value insi#e &'ileloop bloc@ t'en bloc@ o! co#e &ill be e7ecute# in nite time.87ample

int i G H&'ileAi G L

System.out.printlnAK3alue )! 3ariable i Is KPi HQbove given &'ile loop &ill be e7ecute# in nite time because variable is

not incremente# insi#e &'ile loop bloc@.

Bello& given !ull e7ample o! &'ile an# #o &'ile loops &ill clear out your all#oubts. Simple run it in your eclipse an# veri!y result.

public class W'ileloop L

Page 15: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 15/292

public static voi# mainAStringMN args L

<<&'ile loop - &ill be e7ecute# till con#ition returns true. System.out.printlnAK>>>&'ile loop e7ample>>>K H int i G H <<3ariable initiali?ation &'ileAi G L System.out.printlnAK3alue )! 3ariable i Is KPi H iPPH <<Incrementing value o! i by 1. Q

<<#o &'ile loop - &ill be e7ecute# minimum one time &it'out consi#eringcon#ition. System.out.printlnAKK H System.out.printlnAK>>>#o &'ile loop e7ample>>>K H int 4G H <<3ariable initiali?ation #oL System.out.printlnAK3alue )! 3ariable 4 Is KP4 H 4G4-1H<<Decrementing value o! 4 by 1H Q&'ileA4OG H QQ

)utput o! above given e7ample &ill be as bello&.

>>>&'ile loop e7ample>>>3alue )! 3ariable i Is

3alue )! 3ariable i Is 13alue )! 3ariable i Is 23alue )! 3ariable i Is

>>>#o &'ile loop e7ample>>>3alue )! 3ariable 4 Is 3alue )! 3ariable 4 Is 23alue )! 3ariable 4 Is 13alue )! 3ariable 4 Is

(. )ne an# t&o #imensional array In Java

rrays - Basic Java Tutorials For Selenium WebDriver

W'at is rrayRs &e 'ave learnt in my post about DIFF808 T D T T 8S " We can store

values in variables base# on type o! #ata li@e int iG%H #ouble # G 12.2%$Hetc. o& i! you &ants to store multiple values A1 or 1% or more #i=erentvalues in #i=erent variables t'en it &ill be over'ea# !or you to create an#initiali?e #i=erentvariables !or eac' value. In t'is situation" you can use array to storemultiple #i=erent values in array. n array can store multiple value o!

Page 16: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 16/292

same #ata typeAint" c'ar" String at t'e same time an# eac' store# #atalocation 'as uni ue In#e7. T'ere are t&o types o! array as bello&.

)ne Dimensional rray

bove given Image #escribes one #imensional array 'aving one ro& o! #ata. )ne #imensional array is 4ust li@e sprea#s'eet &it' #ata in one ro&.

s s'o&n in above Image" In#e7 o! array Is starting !rom . +eans value1 'as In#e7 " 12 'as In#e7 1 an# so on an# eac' value can be I#enti e#by Its In#e7. We can create an# Initiali?e values In array as s'o&n Inbello& given e7ample.public class rray 87ample L

public static voi# mainAStringMN args L int aMN G ne& intM(NH<< rray #eclaration an# Creation. ( is lengt' o! array. aM N G 1 H<<initiali?e 1st array element aM1N G 12H<<initiali?e 2n# array element aM2N G $,H <<initiali?e r# array element aM N G 1*H<<initiali?e $t' array element aM$N G %H<<initiali?e %t' array element aM%N G $/H<<initiali?e (t' array element

!orAint iG H i a.lengt'H iPP L

System.out.printlnAaMiN H Q Q

Q

In above e7ample" 5engt' o! array aMN is ( an# #ata type o! array is int.+eans &e can store ma7 ( integer values in t'is array. !or loop 'elps toprint value o! eac' array cell. a.lengt' &ill return lengt' o! array. W'enyou &ill run above e7ample in eclipse" ou &ill get bello& given result inconsole.aM N 6ol#s 1aM1N 6ol#s 12aM2N 6ol#s $,aM N 6ol#s 1*aM$N 6ol#s %aM%N 6ol#s $/

not'er &ay o! creating an# initiali?ing same one #imensional array is ass'o&n bello&.

int aMN G L1 "12"$,"1*"%"$/QH

Page 17: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 17/292

T&o Dimensional rray

To& #imensional array is 4ust li@e sprea#s'eet &it' multiple ro&s an#columns 'aving #i=erent #ata in eac' cell. 8ac' cell &ill be I#enti e# byit s uni ue ro& an# column In#e7 combinationA87ample strM%NM N . We canuse t&o #imensional array to store user i# an# pass&or# o! #i=erent usersas s'o&n in above Image. For above given e7ample Image" We can createan# initiali?e values in t&o #imensional array as s'o&n in bello& givene7ample.public class T&o#imarray L

public static voi# mainAStringMN args L String strMNMN G ne& StringM NM2NH<< ro&s" 2 columns strM NM NGK:ser1KH strM1NM NGK:ser2KH strM2NM NGK:ser KH strM NM1NGK ass&or#1KH strM1NM1NGK ass&or#2KH strM2NM1NGK ass&or# KH

!orAint iG H i str.lengt'H iPP L <<T'is !or loop &ill be total e7ecute# times. !orAint 4G H 4 strMiN.lengt'H 4PP L <<T'is !or loop &ill be e7ecute# !or 2time on every iteration. System.out.printlnAstrMiNM4N H Q Q QQ

W'en you &ill run t'is e7ample in eclipse" It &ill return bello& given resultin console.

:ser1ass&or#1

:ser2ass&or#2

:serass&or#

)t'er &ay o! creating an# initiali?ing same t&o #imensional array is ass'o&n bello&.

Page 18: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 18/292

String strMNMN G LLK:ser1K"K ass&or#1KQ"LK:ser2K"K ass&or#2KQ"LK:ser K"K ass&or# KQQH

*. +et'o#s In Java

+et'o#s In Java - Tutorials For Selenium WebDriver

W'at Is +et'o#RIn Selenium &eb#river test suite" ou nee# to per!orm some actionsmultiple time or in multiple test cases li@e 5ogin in to ccount" lace )r#eror any ot'er action. I! you &ill &rite t'is @in# o! Asame repeate# actions Inall t'e test cases t'en It &ill becomes over'ea# !or you an# &ill Increasesi?e o! your co#e.Instea# o! t'at" I! you create met'o#s !or t'is @in# o! operations an# t'en

call t'at met'o# &'enever re uire# t'en It &ill be very easy to maintainyour test cases.

+et'o# Is group o! statements &'ic' is create# to per!orm some actionsor operation &'en your 4ava co#e call it !rom main met'o#. Co#e &rittenInsi#e met'o#s can not be e7ecute# by It sel!. To e7ecute t'at met'o#co#e bloc@" ou nee# to call t'at met'o# insi#e main met'o# bloc@. 5etme give you simple e7ample o! 'o& to create a met'o# an# 'o& to call It!rom main met'o# bloc@.

Simple +et'o# 87ample

public class +et'o#e7ample L

public static voi# mainAStringMN args L Test1A H<<Test1A met'o# calle# insi#e main met'o#. Q

public static voi# Test1A L <<Simple +et'o# - Calle# !rom main met'o#.

System.out.printlnAKTest1 +et'o# 87ecute#.K H Q public static voi# Test2A L <<Simple +et'o# - ot calle# !rom main

met'o#. System.out.printlnAKTest2 +et'o# 87ecute#.K H Q

Q

I! you &ill run above e7ample t'en console output &ill loo@s li@e bello&. Test1 +et'o# 87ecute#.

s per t'e console output" We can say t'at Test1A met'o# is e7ecute# but Test2A met'o# is not e7ecute# because It Is not calle# !rom main met'o#.

Page 19: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 19/292

+et'o#s &ill 'ave bello& given #i=erent components.

1. 3isibility )! +et'o# ou can set your met'o# s visibility by provi#ingaccess mo#i er @ey&or#s li@e public" private" protecte# at beginning o! met'o#. 3I8W D8T I58D 8V + 58 .87ample

public static voi# Testing omo#A L <<public met'o# <<Bloc@ o! co#eQ

2. 0eturn Type )! +et'o# ny met'o# can 'ave return types li@e int"String" #ouble etc.. base# on returning value s #ata type. voi# meansmet'o# is not returning any value.3I8W 8V + 58 )F 08T:0 T 887ample

public int 0eturn TypeA L <<T'is met'o# 'as int return type. int iG1 H return iH Q

. Static or non static met'o# +et'o# can be static or non static. I! you&ants to @eep your met'o# as static t'en you nee# to use static @ey&or#

&it' met'o#. I! you &ants to create non static met'o# t'en #o not &ritestatic @ey&or# &it' met'o#. 3I8W F:55 D8SC0I TI) ) ST TIC D) ST TIC .

87ample

public voi# +y +et'o#1A L << on static +et'o# <<Bloc@ o! co#e Q public static voi# +y +et'o#2A L <<Static +et'o# <<Bloc@ o! co#e

Q

$. +et'o# ame ou can give any name to your met'o#. l&ays usemet'o# name relative to its !unctional &[email protected]

public static voi# 5oginAString user" String ass L <<5ogin is t'e met'o#name. <<Bloc@ o! co#e Q

%. Input arameters To +et'o# We can pass Input parameters to any

Page 20: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 20/292

met'o#. ee# to &rite parameter &it' its #ata type.3I8W 8V + 58 )F I :T 0 +8T80S87ample

public static voi# Stu#ent DetailsAint 0ollno" String ame L <<0ollno an#ame are input parameters prece#e# by t'eir #ata types

<<Bloc@ o! co#e Q

,. ccess +o#i ers In Java

ccess +o#i ers In Java - Java Tutorials For Selenium WebDriver

ccess mo#i ers are t'e @ey&or#s in 4ava by &'ic' &e can set t'e level o!

access !or class" met'o#s" variables an# constructors. T'ere are $#i=erent access mo#i ers available in 4ava. o& let me try to #escribe you'o& can &e use t'em to set t'e access !or t'e met'o#s an# variables.First o! all let me tell youmeaning o! eac' access mo#i er @ey&or# an# t'en &ill give you e7amples!or t'em.

• public We can access public met'o#s or variables !rom all class o! same pac@age or ot'er pac@age.

• private private met'o#s an# variables can be accesse# only !romsame class. We can not access It !rom ot'er classes or sub classeso! even same pac@age.

• protecte# protecte# met'o#s can be accesse# !rom classes o! same pac@age or sub classes o! t'at class.

• No Access Modifer I! met'o# 'ave not any access mo#i er t'en&e can access It Insi#e all class o! same pac@age only.

o& let me s'o& you practical #i=erence bet&een all t'ese $ access

mo#i ers. I 'ave create# classes as bello&. T&oA ccessmo#i!" ccess o! t'em are in same pac@age Test ac@age1 an# one class A ccessing is inot'er pac@age name# Test ac@age2 o! same pro4ect. Class ccessing isc'il# class o! ccessmo#i! class.

pac@age Test ac@age1 H

public class ccessmo#i! L

public static int iG1 H private static String strGK6elloKH protecte# static #ouble #G .2 %H

Page 21: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 21/292

static c'ar cG g H public static voi# mainAStringMN args L

Testing pubA H Testing priA H Testing proA H Testing omo#A H Q <<+et'o# &it' public access mo#i er. Can be accessible by any class.

public static voi# Testing pubA L System.out.printlnAKTesting pubA 87ecute#K H System.out.printlnAK3alue )! i Is KPi H System.out.printlnAK3alue )! str Is KPstr H System.out.printlnAK3alue )! # Is KP# H System.out.printlnAK3alue )! c Is KPc H Q <<+et'o# &it' private access mo#i er. Can be accessible !rom same class

only. private static voi# Testing priA L System.out.printlnAKTesting priA 87ecute#K HQ

<<+et'o# &it' protecte# access mo#i er. Can be accessible !rom any

class o! same pac@age or sub class o! t'is class.

protecte# static voi# Testing proA L System.out.printlnAKTesting proA 87ecute#K H Q <<+et'o# &it' no access mo#i er. Can be accessible by all classes o!

same pac@age )nly. static voi# Testing omo#A L System.out.printlnAKTesting omo#A 87ecute#K H QQ

pac@age Test ac@age1 H

public class ccess L

public static voi# mainAStringMN args L ccessmo#i!.Testing pubA H <<Can access public met'o#s outsi#e t'e classor pac@age. << ccessmo#i!.Testing priA H - Can not access private met'o#s outsi#e t'eclass ccessmo#i!.Testing proA H <<Can access protecte# met'o#s insi#e samepac@age class.

Page 22: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 22/292

ccessmo#i!.Testing omo#A H <<Can access no mo#i er met'o#s insi#esame pac@age class.

System.out.printlnA H System.out.printlnAK3alue )! i Is KP ccessmo#i!.i H <<Can access publicvariables outsi#e t'e class or pac@age. <<System.out.printlnAK3alue )! str Is KP ccessmo#i!.str H - Can not accessprivate variables outsi#e t'e class System.out.printlnAK3alue )! # Is KP ccessmo#i!.# H <<Can accessprotecte# variables insi#e same pac@age class. System.out.printlnAK3alue )! c Is KP ccessmo#i!.c H <<Can access nomo#i er variables insi#e same pac@age class. QQ

pac@age Test ac@age2 H

import Test ac@age1. ccessmo#i!H

public class ccessing e7ten#s ccessmo#i! L

public static voi# mainAStringMN args L Testing pubA H <<Can access public met'o#s outsi#e t'e pac@age. <<Testing priA H - Can not access private met'o#s outsi#e t'e class. Testing proA H <<Can access protecte# met'o#s insi#e c'il# class.

<<Testing omo#A H - Can not access no access mo#i er met'o#s outsi#et'e pac@age.

System.out.printlnA H System.out.printlnAK3alue )! i Is KPi H <<Can access public variablesoutsi#e t'e pac@age. <<System.out.printlnAK3alue )! str Is KPstr H - Can not access privatevariables outsi#e t'e class. System.out.printlnAK3alue )! # Is KP# H <<Can access protecte# variablesinsi#e c'il# class. <<System.out.printlnAK3alue )! c Is KPc H - Can not access no accessmo#i er variables outsi#e t'e pac@age. QQ

s per above e7ample" o& its clear t'at base# on access mo#i ers" &ecan access variables" met'o#s" etc.. in any ot'er pac@age or class. I! you&ill un comment t'e commente# met'o#s or variables !rom above givenclasses" It &ill s'o& you error relate# to access level.

/. 0eturn Type )! +et'o# In Java

0eturn Type )! +et'o# In Java - Tutorials For Selenium WebDriver

Page 23: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 23/292

We 'ave learnt +8T6)D I J 3 an# #i=erent +8T6)D CC8SS+)DIFI80S in my previous posts. o& let me intro#uce you &it' #i=erentreturn types o! met'o#s in 4ava. ou can use t&o @in# o! return types &it'met'o#s. 1. 3oi# an# 2. ny #ata types. 3oi# means returning not'ing an#&'en yourmet'o# is returning some values li@e Integer" String" #ouble" etc.." ounee# to provi#e return type &it' met'o# accor#ing to returning value s#ata types.

• int" String" #ouble etc..Data types - I! met'o# is returning any valuet'en you nee# to provi#e return type &it' met'o# li@e int" String"Double etc..

• voi# - I! met'o# is not returning any value t'en you can give voi# asreturn type. voi# means met'o# returning not'ing.

5et me give you simple e7ample o! int" #ouble an# voi# return type.pac@age Test ac@age1H

public class 0eturn Types L

static int cH static #ouble #Hpublic static voi# mainAStringMN args L

+ulA2" H DivA*" H System.out.printlnAK3alue o! c Is KPc H System.out.printlnAK3alue o! # Is KP# H +essageA H Q <<T'is met'o# is returning integer value. It s return type is int. public static int +ulAint a" int b L cGa>bH return cHQ<<T'is met'o# is returning #ouble value. It s return type is #ouble.

public static #ouble DivA#ouble a" #ouble b L #Ga<bH return #HQ

<<T'is met'o# is returning not'ing so t'ere is use# voi# return type. public static voi# +essageA L System.out.printlnAKTest +essageK H QQI! you see in above e7ample" I 'ave use# t'ree met'o#s A+ul" Div an#+essage an# calle# all t'ree met'o#s in main met'o# to e7ecute t'em.

Page 24: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 24/292

• +et'o# +ul is returning multiplie# integer value in variable c so I'ave use# int #ata type as return type &it' +ul met'o#.

• +et'o# Div is returning #ivision o! t&o values in variable # so I 'aveuse# #ouble #ata type as return type &it' Div met'o#.

• +et'o# +essage is returning not'ing so I nee# to use voi# returntype &it' It.

1 . Static" on Static +et'o#s" 3ariables In Java

Java Tutorials For Selenium WebDriver - Static n# on Static +et'o#s

0ig't no& &e are learning #i=erent components o! met'o#s in 4ava as#escribe# in T6IS )ST . ou can rea# about use o! #i=erent CC8SS

+)DIFI80S an# 08T:0 T 8S o! met'o# I! you are not a&are about it.)ne anot'er component o! met'o# Is It can be static or non static. Static@ey&or# &it'met'o# Is #escribes t'at t'is met'o# Is static an# I! met'o# #o not 'avestatic @ey&or# t'en t'at met'o# Is non static. Same rule Is applie# !orvariables too. Static means stable an# non static means unstable incommon &or#s. T'ere are several #i=erence bet&een static an# not staticmet'o#s In 4ava as #escribe# bello&.

+ain Di=erence Bet&een Static n# on Static +et'o#s In Java

• We can call static met'o#s #irectly &'ile &e can not call non staticmet'o#s #irectly. ou nee# to create an# instantiate an ob4ect o! class !or calling non static met'o#s. 3I8W T6IS )ST to learn aboutob4ect In Java.

• on static stu= Amet'o#s" variables can not be accessible Insi#estatic met'o#s +eans &e can access only static stu= Insi#e staticmet'o#s. )pposite to It" on static met'o# #o not 'ave any suc'restrictions. We can access static an# non static bot' @in# o! stu=sInsi#e non static met'o#s

• Static met'o# Is associate# &it' t'e class &'ile non static met'o# Isassociate# &it' an ob4ect.

o& 5et us loo@ at one simple e7ample o! static an# non static met'o#san# variables. Bello& given e7ample #escribes you 'o& to access statican# non static stu= Insi#e static an# non static met'o#s o! same class or#i=erent class.

Create# T&o #i=erent class as bello&.

pac@age Test ac@age1H

Page 25: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 25/292

public class static nonstatic L static int &'eel G 2H

int price G 2% H public static voi# mainAStringMN args L <<static met'o#. <<Can access static met'o#s #irectly Insi#e static met'o#. by@e1A H

<<Can access static variables #irectly Insi#e static met'o#. System.out.printlnAK+ain static met'o# &'eels G KP&'eel H

<<Can not access non static variables #irectly insi#e static met'o#. <<System.out.printlnAK+ain static met'o# &'eels G KPprice H

<<Can not access non static met'o#s #irectly insi#e static met'o#. <<by@e2A H

<<Create# ob4ect o! class to access non static stu= Insi#e static met'o#. static nonstatic sn G ne& static nonstaticA H

<< o& We can access non static met'o#s o! class Insi#e static met'o#susing ob4ect re!erence. sn.by@e2A H

<< o& We can access non static variables o! class Insi#e static met'o#susing ob4ect re!erence. System.out.printlnAK+ain static met'o# price G KPsn.price H

Q public static voi# by@e1A L <<static met'o#.

<<Can access static variables #irectly Insi#e static met'o#. System.out.printlnAKby@e1 static met'o# &'eels G KP&'eel H

<<Can not access non static variables #irectly insi#e static met'o#. <<System.out.printlnAprice H Q

public voi# by@e2A L <<non static met'o#.

<<Can access static variable #irectly insi#e non static met'o#. System.out.printlnAKby@e2 on static met'o# &'eels G KP&'eel H

<<Can access non static variable #irectly insi#e non static met'o#. System.out.printlnAKby@e2 on static met'o# price G KPprice H

<<Can access static met'o#s #irectly Insi#e non static met'o#. by@e1A H

QQ

Page 26: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 26/292

pac@age Test ac@age1H

public class static ousi#eclass L

public static voi# mainAStringMN args L <<static met'o#.

<<Can call static !unction !rom ot'er class #irectly using class name. static nonstatic.by@e1A H

<<Can call static variables !rom ot'er class #irectly using class name. System.out.printlnAK:sing static variable o! ot'erclassKPstatic nonstatic.&'eel H

<<Create# ob4ect o! class static nonstatic to access non static stu= !romt'at class. static nonstatic oc G ne& static nonstaticA H

<< o& We can access non static variables o! ot'er class Insi#e staticmet'o#s using ob4ect re!erence. System.out.printlnAK ccessing non static variable outsi#e t'e class KPoc.price H

<< o& We can access non static met'o#s o! ot'er class Insi#e staticmet'o#s using ob4ect re!erence. oc.by@e2A H

Q

Q

s you can see in above e7amples" &e can access only static stu= Insi#eany static met'o#s #irectly. I! you &ants to access static met'o# orvariable Insi#e #i=erent class t'en you can access It using simply classname as s'o&n In above e7ample. ou must 'ave to create ob4ect o! class

to access non static met'o# or variable Insi#e static met'o# Aby@e1 o! same class or #i=erent classAmain met'o# o! 2n# class .

)n ot'er si#e" &e can access static an# non static met'o#s an# variables#irectly insi#e non static met'o# Aby@e2 . T'ere is not any suc' accessrestrictions.

11. )b4ect In Java

Selenium WebDriver Java Tutorials - )b4ect In Java

W'at Is n )b4ect In JavaR

Page 27: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 27/292

I! Intervie&er as@ you t'is uestion t'en your ans&er s'oul# be li@e t'is )b4ect Is an Instance o! class. In ot'er &or#s &e can say ob4ect Is bun#leo! relate# met'o#s an# variables. 8very ob4ect 'as Its o&n states an#be'avior. )b4ects are generally use# &it' constructors In 4ava.:n#erstan#ing o! ob4ect Is veryImportant !or Selenium WebDriver learner because &e 'ave to create an#use ob4ects In our test case #evelopment. We can create multiple ob4ects!or t'e same class 'aving Its o&n property.

5et us ta@e one real &orl# scenario to un#erstan# ob4ect clearly. Simpleste7ample Is bicycle Is an ob4ect o! ve'icle class 'aving Its o&n states an#be'avior. Same &ay" motorcycle Is anot'er ob4ect o! ve'icle class. Fe&properties !or bot' t'e ob4ects are same li@e &'eelsG2" 'an#leG1. Fe&properties !or bot' t'e ob4ects are #i=erent li@e price" color" spee# etc..

6o& to create ob4ectR ou can create ob4ect o! class ve'icle using ne& @ey&or# as bello&.

public class ve'icle Lpublic static voi# mainAStringMN args L

<<Create# ob4ect !or ve'icle class using ne& @ey&or#. <<bicycle is t'e re!erence variable o! t'is ob4ect. ve'icle bicycle G ne& ve'icleAKBlac@K H Q<<Constructor &it' color parameter passe#. It &ill retrieve value !rom

ob4ect ve'icle.public ve'icleAString color L <<0etrieve# value &ill be printe#. System.out.printlnAKColor )! ve'icle Is KPcolor HQ

QConsole )utput &ill loo@s li@e bello& &'en you &ill run above e7ample.

Color )! ve'icle Is Blac@

In above e7ample" :se# one constructor to pass t'e value o! ob4ect. We&ill loo@ about constructor In my upcoming post. lease remember 'ereone t'ing - bicycle Is not an ob4ect. It Is re!erence variable o! ob4ectve'icle. Base# on t'is e7ample" o& &e can say t'at ob4ect 'as t'reeparts as bello&.

• Declaration 3ariable #eclaration !or ob4ect. In t'is e7ample" bicycleis t'e re!erence variable !or ob4ect.

• Instantiation )b4ect creation using ne& @ey&or# Is calle#as Instantiation.

• Initiali?ation Call to a constructor Is @no&n as ob4ect initiali?ation.

Page 28: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 28/292

s #escribe# In 083I):S )ST " ou can use ob4ect o! class to access nonstatic variables or met'o#s o! class in #i=erent class too. T'is &ay you canuse ob4ect o! class an# also you can create multiple ob4ects o! any class.

Bello& given e7ample &ill s'o& you 'o& to create multiple ob4ect o! classto pass #i=erent @in# o! multiple values In constructor.public class ve'icle L

public static voi# mainAStringMN args L <<Create 2 ob4ects o! class. Bot' 'ave #i=erent re!erencevariables. ve'icle bicycle G ne& ve'icleAKblac@K" 2" $% " .* H ve'icle motorcycle G ne& ve'icleAKBlueK" 2" (* " *$.( H

Q public ve'icleAString color" int &'eels" int price" #ouble spee# L

System.out.printlnAKColor G KPcolorPK" W'eels G KP&'eelsPK" rice GKPpricePK" Spee# G KPspee# H QQ

)utput o! above program &ill loo@s li@e bello&.

Color G blac@" W'eels G 2" rice G $% " Spee# G .*Color G Blue" W'eels G 2" rice G (* " Spee# G *$.(

12. 3ariable Types In Java

3ariable Types In Java - WebDriver Tutorials Wit' Java

Be!ore learning about variable types In 4ava" I recommen# you to rea# myposts relate# to variable s D T T 8S " CC8SS +)DIFI80S an# ST TIC

D ) ST TIC 3 0I B58S . s you @no&" 3ariable provi#e us a memoryspace Abase# on type an# si?e o! #eclare# variable to store a value. T'ereare #i=erent t'ree types o! variables available In 4ava as bello&.

1. 5ocal 3ariables

• 3ariables &'ic' are #eclare# Insi#e met'o#s or constructor arecalle# as local variables !or t'at met'o# or constructor.

• 3ariables #eclare# Insi#e met'o# or constructor are limite# !or t'atmet'o# or constructor only. ou can not access to It outsi#e t'atmet'o# or constructor.

• ou can not use access mo#i ers &it' local variables because t'ey

are limite# to t'at met'o# or constructor bloc@ only.

Page 29: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 29/292

• ee# to initiali?e t'e value o! local variables be!ore using It becauset'ere Is not any #e!ault value assigne# to local variables.

2. Instance 3ariables A on Static

• Instance variables are #eclare# on class level &'ic' is parallel tomet'o#s or constructors Aoutsi#e t'e met'o# or constructor bloc@ .

• Instance variables are generally use# &it' ob4ects. So t'ey arecreate# an# #estroye# &it' ob4ect creation an# #estruction.

• InstanceAnon static variables are accessible #irectly by all t'e nonstatic met'o#s an# constructors o! t'at class.

• I! you &ants to access InstanceAnon static variables Insi#e static

met'o#" ou nee#s to create ob4ect o! t'at class.

• Instance variables are al&ays Initiali?e# &it' Its #e!ault valuesbase# on Its #ata types.

• ou can access Instance variable #irectly Aby Its name Insi#e sameclass. I! you &ants to access It outsi#e t'e class t'en you 'ave toprovi#e ob4ect re!erence &it' variable name.

• 3ast usage o! Instance variables in 4ava programs an# selenium

&eb#river tests.

. Class 3ariables AStatic

• Same as Instance variables" Class variables are #eclare# on classlevel Aoutsi#e t'e met'o# or constructor bloc@ . )nly #i=erenceIs class variables are #eclare# using static @ey&or#.

• Class variables are use# In rare case li@e &'en It Is pre#e ne# t'atvalue o! variable &ill never c'ange. In t'at case &e can use classvariables.

• Class variables are create# on program start an# #estroye# onprogram en#.

• Class variables are visible to all met'o#s an# constructors o! class.Class variables are visible to ot'er class base# on Its accessmo#i ers. 9enerally t'ey are public.

• We can access class variables #irectly using Its name Insi#e sameclass. I! you &ants to access It outsi#e t'e class t'en you nee# touse class name &it' variable.

Page 30: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 30/292

Bello& given e7ample &ill give you some I#ea about all t'ree types o! variables. Create# t&o class to s'o& you 'o& to access class variableinsi#e ot'er class. Bot' t'ese class &ill s'o& you t'e access levels o! allt'ree types o! variables.

public class Collage1 L

<<Class 3ariables - Collage name &ill be same !or bot' #epartments so#eclare# as classAstatic variable. public static String Collage ame G K 1 CollageKH

<<Instance 3ariables private String Department G KComputer 8ngineeringKH private String nameHprivate #ouble percentileH

public static voi# mainAStringMN args L <<Static +et'o# <<Can access class variable #irectly I! nee#e#. i.e. Collage ame Collage1 stu#ent1 G ne& Collage1AK0obertK H stu#ent1.set ercentageA(*. 2 H stu#ent1.print #etailsA H <<Can access Instance variable using ob4ect re!erence I! nee#e#.

<<87ample stu#ent1.name G K0obertKH

Collage1 stu#ent2 G ne& Collage1AK le7K H stu#ent2.set ercentageA*2./% H

stu#ent2.print #etailsA H Qpublic Collage1AString stu#ent name L <<Constructor

<<Can access Instance variable #irectly Insi#e constructor. name G stu#ent nameHQpublic voi# set ercentageA#ouble perc L

<<Can access Instance variable #irectly Insi#e non static met'o#. percentile G percHQ

public voi# print #etailsA L int ear G 2 1$H <<5ocal 3ariable - Can not access It outsi#e t'is met'o#. System.out.printlnAK0esultg )! ear G KP ear H System.out.printlnAKStu#ent s Collage ame G KPCollage ame H System.out.printlnAKStu#ent s Department G KPDepartment H System.out.printlnAKStu#ent s ame G KPname H

System.out.printlnAKStu#ent s percentile G KPpercentilePK K H System.out.printlnAK>>>>>>>>>>>>>>>>>>>>>>K HQ

Q

Page 31: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 31/292

Console output &ill loo@s li@e bello&.

0esultg )! ear G 2 1$Stu#ent s Collage ame G 1 CollageStu#ent s Department G Computer 8ngineeringStu#ent s ame G 0obertStu#ent s percentile G (*. 2>>>>>>>>>>>>>>>>>>>>>>0esultg )! ear G 2 1$Stu#ent s Collage ame G 1 CollageStu#ent s Department G Computer 8ngineeringStu#ent s ame G le7Stu#ent s percentile G *2./%>>>>>>>>>>>>>>>>>>>>>>

public class Collage2 L private String Department G K+ec'anical 8ngineeringKH

private String nameHprivate #ouble percentileH

public static voi# mainAStringMN args L Collage2 stu#ent1 G ne& Collage2AKSmit'K H stu#ent1.set ercentageA%*. % H stu#ent1.print #etailsA H

Q public Collage2AString stu#ent name L name G stu#ent nameHQ

public voi# set ercentageA#ouble perc L

percentile G percH Q public voi# print #etailsA Lint ear G 2 1$H

System.out.printlnAK0esultg )! ear G KP ear H <<Can access ot'er class s class variable using t'at class name. System.out.printlnAKStu#ent s Collage ame GKPCollage1.Collage ame H System.out.printlnAKStu#ent s Department G KPDepartment H System.out.printlnAKStu#ent s ame G KPname H

System.out.printlnAKStu#ent s percentile G KPpercentilePK K H System.out.printlnAK>>>>>>>>>>>>>>>>>>>>>>K HQ

Q

Console output &ill loo@s li@e bello&.

Page 32: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 32/292

0esultg )! ear G 2 1$Stu#ent s Collage ame G 1 CollageStu#ent s Department G +ec'anical 8ngineeringStu#ent s ame G Smit'Stu#ent s percentile G %*. %>>>>>>>>>>>>>>>>>>>>>>

1 . Constructor In Java

Selenium WebDriver Java Tutorials Constructors

W'at Is Constructor In JavaRI! intervie&er as@s you a #e nition o! constructor t'en you can give 'im

ans&er li@e Constructor Is a co#e bloc@ &'ic' Is Calle# an# e7ecute# att'e time o! ob4ect creation an# constructs t'e values Ai.e. #ata !or ob4ectan# t'at s &'y It Is @no&n as constructor. 8ac' class 'ave #e!ault noargument constructor In Java I!you 'ave not speci e# any e7plicit constructor !or t'at class.In 4ava"Constructor loo@s li@e met'o#s but bello& given properties o! constructorare #i=erentiate t'em !rom t'e met'o#s. ou can 3I8W T:T)0I 5S )F+8T6)DS I J 3 .

Constructor Creation 0ules

• ame o! t'e constructor must be same as class name. +eans I! yourclass name Is Stu#ent t'en constructor name must be Stu#ent.

• Constructor must 'ave not any return types.

Same as met'o#s" We can pass parameters to constructors. 5et me s'o&you simple e7ample o! parameteri?e# cunstructor In 4ava. ou can alsovie& my 083I):S )ST to see t'e e7ample o! constructor.

public class Stu#ent L

public static voi# mainAStringMN args L <<T&o #i=erent ob4ects create# &it' value. Stu#ent st#n1 G ne& Stu#entAK+ic'aelK H

Stu#ent st#n2 G ne& Stu#entAK0obertK H Q

<<Constructor &it' parameter to pass values o! ob4ect << ame o! constructor Is same as class name. public Stu#entAString name L

String st#nname G nameH System.out.printlnAKStu#ent s ame Is KPst#nname H

Q

Page 33: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 33/292

Q

Console output &ill loo@s li@e bello&.

Stu#ent s ame Is +ic'aelStu#ent s ame Is 0obert

In above e7ample" Constructor &ill be calle# at t'e time o! ob4ectscreation. Constructor &ill pass t'e values o! ob4ects one by to print t'em.

Constructor )verloa#ings &e @no&" Constructors name must be same as class name" W'en you

create more t'an one constructors &it' same name but #i=erentparameters In same class Is calle# constructor overloa#ing. So uestion Is&'y constructor overloa#ing Is re uire#R Constructor overloa#ing Is use!ul&'en you &ants to construct your ob4ect In #i=erent &ay or &e can sayusing #i=erent number o! parameters. ou &ill un#erstan# It better &'en&e &ill loo@ at simple e7ample but be!ore t'at let me tell some basic ruleso! constructor overloa#ing.

• T&o constructors &it' same arguments are not allo&e# !orconstructor overloa#ing.

• ou nee# to use t'isA @ey&or# to call overloa#e# constructor.

• I! you are calling constructor !rom overloa#e# constructorusing t'isA @ey&or#" It must be rst statement.

• It is best practice to call constructor !rom overloa#e# constructor toma@e It easy to maintain.

Bello& given e7ample s'o&s best e7ample o! constructor overloa#ing.public class Stu#ent L String nameH <<Instance variable String minameH <<Instance 3ariable public static voi# mainAStringMN args L

Stu#ent st#n1 G ne& Stu#entAKJimK H Stu#ent st#n2 G ne& Stu#entAK+aryK" K8li?abet'K H Q <<Constructor &it' one argument.

public Stu#entAString !name Lname G !nameH <<5ocal 3ariable

System.out.printlnAK1. First ame Is KP name H Q <<)verloa#e# Constructor &it' t&o arguments.

public Stu#entAString !name" String mname L name G !nameH

Page 34: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 34/292

miname G mnameH System.out.printlnAK2. First ame Is KP name H System.out.printlnAK2. +i##le ame Is KPminame HQ

Q

Console output &ill loo@s li@e bello&.

1. First ame Is Jim2. First ame Is +ary2. +i##le ame Is 8li?abet'

We can #o same t'ing using single ob4ect creation too using t'isA@ey&or# as bello&.

public class Stu#ent L String nameH String minameHpublic static voi# mainAStringMN args L

Stu#ent st#n2 G ne& Stu#entAK+aryK" K8li?abet'K H Q <<Constructor &it' one argument.

public Stu#entAString !name Lname G !nameH

System.out.printlnAK1. First ame Is KP name H

Q <<)verloa#e# Constructor &it' t&o arguments.

public Stu#entAString !name" String mname L t'isAKJimK H <<1st constructor Is calle# using t'is @ey&or#. name G !nameH miname G mnameH System.out.printlnAK2. First ame Is KP name H System.out.printlnAK2. +i##le ame Is KPminame HQ

QI! you &ill run above e7ample t'en console s output &ill be same but youcan see &e 'ave create# only one ob4ect Insi#e main met'o#. :se# t'is@ey&or# In overloa#e# constructor to call anot'er constructor.

1$. In'eritance In Java

In'eritance In Java Tutorials For Selenium WebDriver

Till no& &e 'ave learnt many tutorials on 4ava li@e +et'o#s " ccess+o#i ers " Static an# on Static " )b4ect " Constructor " an# +any +ore

Tutorials )n Java. .. ll t'ese 4ava ob4ect oriente# concept tutorials are very

Page 35: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 35/292

muc' use!ul in selenium &eb#river test case #evelopment. T'at s &'y Irecommen# you allto learn t'em very care!ully. ll t'ese tutorials &ill also 'elps you to passselenium &eb#river Intervie&. o& let me ta@e you one step a'ea# toIn'eritance In 4ava.

W'at Is In'eritance In JavaRIn'eritanceAparent-c'il# Is very use!ul concept o! 4ava ob4ect oriente#programming language by &'ic' &e can reuse t'e co#e o! parent class.In'eritance Is provi#ing us a mec'anism by &'ic' &e can in'erit t'eproperties o! parent class In to c'il# class. 87ample u#i class Is c'il#class o! Car class t'en u#i class can access<use all t'e nonprivate properties Amet'o#s" variables..etc o! Car class. :singIn'eritance" &e can reuse t'e co#e o! parent class In to c'il# class so t'atsi?e o! co#e &ill #ecrease. +aintenance o! co#e &ill be also very easy.

3I8W 8V + 58 )F :SI 9 I 680IT C8 I S858 I:+ W8BD0I380

extends @ey&or# Is use# to In'erit c'il# class !rom parent class. 5et mes'o& you very simple e7ample o! In'eritance In 4ava. In t'is e7ample" Carclass Is parent class o! u#i class so all t'e non private properties o! Carclass are In'erite# an# able to use In u#i class.

arent Class

public class Car L<<Car Class Is arent Class )! u#i Class

private String typeGK3e'icleKH public static int &'eels G $H public String color G KblueKH String !uel G K etrolKH public String get!uelA Lreturn !uelH

Q protecte# voi# SeatsA L

int seat G $H

System.out.printlnAKCar Seats G KPseat HQQ

C'il# Class or Sub Class

public class u#i e7ten#s Car L<< u#i Is c'il# Class )! Car Class. public int spee#G1% H public static voi# mainAStringMN args L

u#i G ne& u#iA H .print#etailsA H

Page 36: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 36/292

<<Can access instance variable o! parent class using ob4ect re!erence o!c'il# class Insi#e static met'o#s. System.out.printlnAKColor )! u#i G KP .color H <<Can access non static met'o# o! parent class using ob4ect re!erence o!c'il# class Insi#e static met'o#s. System.out.printlnAKFuel )! u#i G KP .get!uelA H Q public voi# print#etailsA L

<<Can access class variables o! parent class #irectly Insi#e c'il# class nonstatic met'o#s. System.out.printlnAKW'eels )! u#i G KP&'eels H System.out.printlnAKSpee# )! u#i G KPspee# H <<Can access non static met'o#s o! parent class #irectly Insi#e c'il# classnon static met'o#s. SeatsA H <<Can not access private variable o! parent class In c'il# class. <<System.out.printlnAKW'eels )! u#i G KPtype H QQ

)utput o! above e7ample &ill loo@s li@e bello&.

W'eels )! u#i G $Spee# )! u#i G 1%Car Seats G $

Color )! u#i G blueFuel )! u#i G etrol

I! you &ill stu#y above e7ample care!ully" ou can un#erstan# It easily t'atIn c'il# class" &e can access only non private members o! parent class.

T'is Is 4ust basic e7ample o! In'eritance In Java.

)verri#ingIn sub class" W'en you create a met'o# &it' same signature" return typesan# arguments o! parent class s met'o# t'en t'at sub class s met'o# Is@no&n as overri##en met'o# an# It Is calle# as overri#ing In 4ava.)verri#ing Is use!ul to c'ange to t'e be'avior o! parent class met'o#s.5et me s'o& you simple e7ample o! met'o# overri#ing In 4ava.

Consi#er same above given e7ample. 9enerally all cars 'ave $ seats so&e 'ave create# SeatsA met'o# &it' $ seat variable. But supposing !or#car 'as ( seats an# I &ants to print ( seats !or For# car. In t'is case &ecan use met'o# overri#ing In For# class as bello&. For t'is e7ample"Consi#er Car class o! above e7ample as parent class o! For# class. 6eremet'o# SeatsA Is overri##en In sub class.public class For# e7ten#s Car L<<For# Is c'il# Class )! Car Class.

public static voi# mainAStringMN args L

Page 37: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 37/292

Car C G ne& For#A H <<Create# For# Class ob4ect &it' Car Class re!erence. C.SeatsA H Q << arent class met'o# Seats Is overri##en In c'il# class.

protecte# voi# SeatsA L int seat G (H System.out.printlnAK u#i Seats G KPseat HQ

Q

)utput o! above e7ample &ill loo@s li@e bello&.

u#i Seats G (

6ere" met'o# o! sub class Is calle# at place o! parent class. not'er maint'ing to notice 'ere Is &e 'ave create# ob4ect o! c'il# class but re!erenceIs given o! parent class.

1%. Inter!ace In Java

Inter!ace In Java Tutorials For Selenium Web#river

I #escribe# about In'eritance an# met'o# overri#ing In my 083I):S

)ST so I am suggesting you to rea# t'at post" Specially met'o#overri#ing concept be!ore learning about Inter!ace In Java. I! you &ill go!or selenium &eb#river Intervie& t'en Intervie&er &ill as@ you rst

uestion W'at Is WebDriverR eoples&ill give #i=erent #i=erent ans&ers but rig't ans&er Is WebDriver Is anInter!ace. So It Is most Important t'ing !or all selenium &eb#river learnerto un#erstan# an Inter!ace properly.

W'at Is n Inter!ace n# W'y To :se ItR:sing Inter!ace" We can create a contract or &e can say set o! rules !orbe'avior o! application. Inter!ace Is loo@s li@e class but It Is not class.W'en you implements t'at Inter!ace In any class t'en all t'ose Inter!acerules must be applie# on t'at class. In sort" I! you Implement an Inter!aceon class t'en you must 'ave to overri#e all t'e met'o#s o! Inter!ace Inyour class. Inter!ace &ill create 0ules To Follo& structure !or class &'ere ItIs Implemente#. T'is &ay" I! you loo@ at t'e co#e o! Inter!ace" ou can getI#ea about your program business logic. W'en you are #esigning bigarc'itecture applications li@e selenium &eb#river" ou can use Inter!ace to#e ne business logic at Initial level.

Inter!ace can be Implemente# &it' any class using implements @ey&or#.

T'ere are set o! rules to be !ollo&e# !or creating an Inter!ace. 5et me tellyou all t'ese rules rst an# t'en give you an e7ample o! an Inter!ace.

Page 38: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 38/292

• Inter!ace can not 'ol# constructor.

• Inter!ace can not 'ol# instance el#s<variables.

• Inter!ace can not 'ol# static met'o#s.

• ou can not instantiate<create ob4ect o! an Inter!ace.

• 3ariables Insi#e Inter!ace must be static an# man#atory to initiali?et'e variable.

• ny class can Implement Inter!ace but can not e7ten# Inter!ace.

• Can &rite bo#y less met'o#s Insi#e Inter!ace.

• By #e!ault all t'e met'o#s an# variables o! Inter!ace are public sono nee# to provi#e access mo#i ers.

6o& To ## Inter!ace In our ro4ect To a## Inter!ace In your pro4ect" 0ig't clic@ on pac@age an# go to -O e&-O Inter!ace. It &ill open e& 4ava Inter!ace popup. 9ive any name toInter!aceA87ample College an# clic@ on save button. It &ill a##College.4ava Inter!ace le un#er your pac@age.

o& let us loo@ at simple e7ample o! Inter!ace.

Page 39: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 39/292

• Create )ne Inter!ace le &it' name G College.4ava as s'o&n Inbello& e7ample.

• Create class le &it' name G Computer.4ava" +ec'anical.4avaan# TestCollege.4ava as s'o&n In bello& e7ample.

College.4ava

public inter!ace College L <<Inter!ace le <<Initiali?e# static variable. << o nee# to &rite static because It Is by #e!ault static.

String Collegename G KV XKH <<Create# non static met'o#s &it'out bo#y.

voi# Stu#entDetailsA H

voi# Stu#ent0esultA HQ

Computer.4ava

<<Class le Implemente# &it' Inter!ace le using implements @ey&or#.public class Computer implements College L

<<;)verri#e annotation #escribes t'at met'o#s are overri##en oninter!ace met'o#. <<+et'o#s name" return type are same as met'o#s o! an Inter!ace.

;)verri#e public voi# Stu#entDetailsA L System.out.printlnAKComputer Dept. Stu#ent Detail artK H Q

;)verri#e public voi# Stu#ent0esultA L System.out.printlnAKComputer Dept. Stu#ent 0esult artK HQ

Q

+ec'anical.4ava

<<Class le Implemente# &it' Inter!ace le using implements @ey&or#.public class +ec'anical implements CollegeL

;)verri#e public voi# Stu#entDetailsA L System.out.printlnAK+ec'anical Dept. Stu#ent Detail artK HQ

;)verri#e public voi# Stu#ent0esultA L

Page 40: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 40/292

System.out.printlnAK+ec'anical Dept. Stu#ent 0esult artK HQ

Q

TestCollege.4ava

public class TestCollege L <<Class le. o nee# to implement Inter!ace.

public static voi# mainAStringMN args L <<Can access Inter!ace variable #irectly using Inter!ace name. System.out.printlnACollege.CollegenamePK Collage stu#ent #etails.K H

<<Create# Computer class ob4ect &it' re!erence o! inter!ace College. College comp#ept G ne& ComputerA H <<+et'o#s &ill be calle# !rom Computer class. comp#ept.Stu#entDetailsA H comp#ept.Stu#ent0esultA H

<<Create# +ec'anical class ob4ect &it' re!erence o! inter!ace College. College mec#ept G ne& +ec'anicalA H <<+et'o#s &ill be calle# !rom +ec'anical class. mec#ept.Stu#entDetailsA H mec#ept.Stu#ent0esultA HQ

Q

o& I! you &ill run TestCollege.4ava le" )utput &ill loo@s li@e bello&.

V X Collage stu#ent #etails.Computer Dept. Stu#ent Detail artComputer Dept. Stu#ent 0esult art+ec'anical Dept. Stu#ent Detail art+ec'anical Dept. Stu#ent 0esult art

Selenium WebDriver n# Inter!aceSimple e7ample o! Inter!ace In selenium WebDriver Is WebDriverInter!ace. W'en you are Initiali?ing any bro&ser using seleniumWebDriver" ou are &riting statements li@e bello&.

WebDriver #river G ne& Fire!o7DriverA H)r

WebDriver #river G ne& C'romeDriverA H

ou can vie& more e7amples o! selenium WebDriver on T6IS 98 .6ere" WebDriver Is Inter!ace an# Fire!o7Driver an# C'romeDriver are t'eclass les &'ere WebDriver Inter!ace Is Implemente#.

1(. rray5ist In Java

Page 41: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 41/292

WebDriver Tutorial rray5ist Class In Java

In T6IS )ST " I 'ave #escribe# about one an# t&o #imensional array In Java. T'en uestion Is &'at Is arraylist In 4ava an# &'y &e nee# ItRIntervie&er can as@ you t'is uestion t'en you can give 'im ans&er li@e arraylist Is a class t'at Implements list Inter!ace In 4ava &'ic' supports#ynamic array means si?e o!array can gro& as nee#e#. s you @no&" We can create only 7e# si?earray to store values Insi#e It an# t'at Is t'e limitation<#ra&bac@ o! arraybecause &'en you #on t @no& 'o& many values you nee#s to store Inarray t'en 'o& can you provi#e<#e ne si?e o! arrayR rray5ist 'elps us toovercome t'is limitation o! array because It can re-si?e automatically byItsel!.

So nally &e can say t'at &'en you #on t @no& 'o& many values you'ave to store In array" ou can use rray5ist at place o! simple array. We'ave to use arraylist many times In our selenium &eb#river test case#evelopment. 87ample Store all lin@s !rom page" store all buttons I# !rompage etc.. In all suc' case" ou &ill not a&are about 'o& many values you'ave to store.

3I8W 8VT )ST to learn about 'as'table In Java !or selenium &eb#river

Bello& given e7ample &ill s'o& you 'o& to create an# &or@ &it' arraylistclass In 4ava. Same t'ing &e &ill use In selenium WebDriver test case#evelopment to store values.

<<Import rray5ist class 'ea#er leimport 4ava.util. rray5istH

public class rray5ist 87ample L

public static voi# mainAStringMN args L <<Create ob4ect o! rray5ist class. It &ill store only string values. rray5ist StringO Sample G ne& rray5ist StringOA H << o& you can store any number o! values In t'is arraylist as bello&. Si?econstrain &ill comes never. Sample.a##AKbutton1K H << utting an Item In arraylist at In#e7 G . Sample.a##AKbutton2K H << utting an Item In arraylist at In#e7 G 1. Sample.a##AKbutton K H << utting an Item In arraylist at In#e7 G 2. Sample.a##AKbutton$K H << utting an Item In arraylist at In#e7 G .

!orAint iG H i Sample.si?eA HiPP L <<loop &ill e7ecute till si?e o! arraylist. System.out.printlnASample.getAi H <<print arraylist values one by one. Q

System.out.printlnAK>>>>>>>>>>>>>>>>>>>>>>>>>K H <<To get t'e In#e7 o! an Item !rom arraylist. int ItemIn#e7 G Sample.in#e7)!AKbutton K H

Page 42: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 42/292

System.out.printlnAKIn#e7 )! button Item G KPItemIn#e7 HSystem.out.printlnAK>>>>>>>>>>>>>>>>>>>>>>>>>K H

Sample.removeA1 H <<To remove an Item !rom arraylist. !orAint iG H i Sample.si?eA HiPP L

System.out.printlnASample.getAi HQ

System.out.printlnAK>>>>>>>>>>>>>>>>>>>>>>>>>K H

Sample.setA2" KButton,K H <<To reset value o! an arraylist item. !orAint iG H i Sample.si?eA HiPP L

System.out.printlnASample.getAi HQ

QQ

In above e7ample" I 'ave #escribe# 'o& to create# arraylist ob4ect" 'o& toa## values In arraylist" 6o& to get In#e7 o! arraylist Item" 6o& to removearraylist Item" 6o& to reset arraylist Item. ou can #o many more actionson arraylist ob4ect.

1*. 6as'table In Java

WebDriver Java Tutorials 6as'table

s &e 'ave learnt In my 083I):S )ST " :sing rray5ist Is very goo#alternative o! array &'en you #on t @no& 'o& many values you 'ave tostore In array because rray5ist Is automatic #ynamic gro&ing array byIn#e7. )ne more t'ing &e nee#s to use In selenium &eb#river Is6as'table. We &ill learn onlybasic t'ing about 6as'table to use It In our selenium WebDriver Test case#evelopment.

W'at Is 6as'tableR6as'table Is a class In 4ava an# provi#es us a structure to store @ey an# itsvalue as a pair In table !ormat. +eans &e can store value &it' Its @ey an#can access t'at value using Its @ey. T'ere Is not any In#e7 o! value In6as'table. Yey s 6as'co#e &ill be use# to map t'e value &it' @ey In6es'table. 5et me s'o& you very simple e7ample o! 6as'table.

<<Import 6as'table 'ea#er le.import 4ava.util.6as'tableH

public class 6as' L public static voi# mainAStringMN args L <<Create# 'as'table class ob4ect to use Its #i=erent properties. 6as'table String" IntegerO t1 G ne& 6as'table String" IntegerOA H

Page 43: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 43/292

t1.putAK5egsK" $ H <<Store value $ In @ey G 5egs t1.putAK8yesK"2 H <<Store value 2 In @ey G 8yes t1.putAK+out'K"1 H <<Store value 1 In @ey G +out'

<< ccessing 'as' table values using @eys. System.out.printlnAK nimal 5egs G K Pt1.getAK5egsK H System.out.printlnAK nimal 8yes G K Pt1.getAK8yesK H System.out.printlnAK nimal +out' G K Pt1.getAK+out'K H QQ

ee# +ore Tutorials )n Java n# Selenium WebDriverR 3ISIT T6IS 98

s you see In above e7ample" I 'ave create# ob4ect o! Java 6as'tableclass an# t'en store values &it' Its uni ue @eys. T'en I 'ave use# @eys toaccess an# print Its values. T'is &ay &e can store an# access values !rom'as' table &'enever re uire#.

1,. 0ea#-Write Te7t File In Java

0ea#-Write Te7t File In Java - Tutorials For WebDriver

We 'ave learnt about String class an# Its #i=erent !unctions In my083I):S )ST . o& our ne7t topic Is 6o& to &rite In to te7t le or 6o&

to rea# te7t le In 4ava. +any times you &ill nee# rea#ing or &riting te7tle In your selenium &eb#river test case #evelopment. For 87ample" ou

are rea#ing some large #ata!rom &eb page an# &ants to store It In te7t le to use It some&'ere elseIn !uture. Same &ay" ou 'ave to rea# #ata !rom le !or some purpose.

It Is very easy to Create" Write an# rea# te7t le In 4ava. We can use 4avabuilt in class File to create ne& le" FileWriter an# Bu=ere#Writer class to&rite In to le" File0ea#er an# Bu=ere#0ea#er class to rea# te7t le.

Bello& given e7ample &ill rst o! all create temp.t7t le In D #rive an#t'en &rite t&o line In to It. T'en It &ill rea# bot' lines one by one !romte7t le using &'ile loop an# print In console. ou can use bello& givene7ample co#e !or rea#ing or &riting te7t le In your selenium &eb#rivertest case &'enever nee#e#.

import 4ava.io.Bu=ere#0ea#erHimport 4ava.io.Bu=ere#WriterHimport 4ava.io.FileHimport 4ava.io.File0ea#erHimport 4ava.io.FileWriterHimport 4ava.io.I)87ceptionH

public class 0W File L

Page 44: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 44/292

public static voi# mainAStringMN args t'ro&s I)87ception L <<Create File In D Driver.

String TestFile G KD ZZtemp.t7tKH File FC G ne& FileATestFile H <<Create# ob4ect o! 4ava File class. FC.create e&FileA H <<Create le.

<<Writing In to le. <<Create )b4ect o! 4ava FileWriter an# Bu=ere#Writer class. FileWriter FW G ne& FileWriterATestFile H Bu=ere#Writer BW G ne& Bu=ere#WriterAFW H BW.&riteAKT'is Is First 5ine.K H <<Writing In To File. BW.ne&5ineA H<<To &rite ne7t string on ne& line. BW.&riteAKT'is Is Secon# 5ine.K H <<Writing In To File. BW.closeA H

<<0ea#ing !rom le. <<Create )b4ect o! 4ava File0ea#er an# Bu=ere#0ea#er class. File0ea#er F0 G ne& File0ea#erATestFile H Bu=ere#0ea#er B0 G ne& Bu=ere#0ea#erAF0 H String Content G KKH

<<5oop to rea# all lines one by one !rom le an# print It. &'ileAAContent G B0.rea#5ineA [G null L System.out.printlnAContent H Q Q

Q T'is &ay &e can rea# an# &rite te7t le In 4ava !or your selenium&eb#river test case #evelopment.

1/. 87ception 'an#ling" try-catc'- nally" t'ro& an# t'ro&s In Java

6o& To 6an#le 87ception In Java Tutorials For Selenium WebDriver

nyone #on t li@e e7ceptions an# at t'e same time" anyone can not 'i#e'imsel! !rom e7ceptions &'en running &eb#river tests because AW'at Is

n 87ceptionR -O e7ception Is an error event generate# #uring t'ee7ecution o! &eb#river test case or 4ava program &'ic' #isrupts teste7ecution In bet&een. 87ceptioncan arise #ue to t'e many reasons li@e" net&or@ connection or 'ar#&are!ailure" Invali# #ata entere# by user" DB server #o&n etc.. So all t'eset'ings can 'appens any time &'en you run your selenium &eb#river testcase an# &e can not stop It. So Instea# o! t'in@ing about stoppinge7ceptionAW'ic' Is not possible #uring run time" T'in@ about 'an#linge7ceptions. Java provi#es very goo# e7ception 'an#ling mec'anism torecover !rom t'is @in# o! errors. 5et us learn #i=erent &ays o! 'an#linge7ception In 4ava.

T'ere are t&o types o! e7ceptions In 4ava as bello&.

Page 45: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 45/292

1. C'ec@e# 87ception C'ec@e# e7ceptions are t'ose e7ceptions &'ic' are c'ec@e# #uringcompile time an# nee#s catc' bloc@ to caug't t'at e7ception #uringcompilation. I! compiler &ill not n# catc' bloc@ t'en It &ill t'ro&compilation error. 3ery simple e7ample o! c'ec@e# e7ception Is using

T'rea#.sleepA% H statement In your co#e. I! you &ill not put t'isstatement Insi#e try catc' bloc@ t'en It &ill not allo& you to compile yourprogram.

2. :nc'ec@e# 87ceptions :nc'ec@e# e7ception are t'ose e7ception &'ic' are not c'ec@e# #uringcompile time. 9enerally c'ec@e# e7ceptions occurre# #ue to t'e error Inco#e #uring run time. Simplest e7ample o! unc'ec@e# e7ception Is int i G$< H. T'is statement &ill t'ro&s < by ?ero e7ception #uring run time.

6an#ling e7ceptions using try-catc' bloc@We nee# to place try catc' bloc@ aroun# t'at co#e &'ic' mig't generatee7ception. In bello& given e7ample" System.out.printlnAaM/N H Is &rittenIntentionally to generate an e7ception. I! you see" t'at statement Is&ritten Insi#e try bloc@ so I! t'at statement t'ro& an e7ception - catc'bloc@ can caug't an# 'an#le It.

public class 6an#le e7ce L

public static voi# mainAStringMN args L

int aMN G L "1"(QH try L <<I! any e7ception arise Insi#e t'is try bloc@" Control &ill goes tocatc' bloc@. System.out.printlnAKBe!ore 87ceptionK H

<<unc'ec@e# e7ceptionSystem.out.printlnAaM/N H <<87ception &ill arise 'ere

because &e 'ave only values In array. System.out.printlnAK !ter 87ceptionK H Qcatc'A87ception e L System.out.printlnAK87ception Is KPe H

Q System.out.printlnAK)utsi#e T'e try catc'.K H QQ

I! you &ill run above e7ample In your eclipse" try bloc@ &ill bee7ecute#. KBe!ore 87ceptionK statement &ill be printe# In console an#

e7t statement &ill generate an e7ception so K !ter 87ceptionK statement&ill be not printe# In console an# control &ill goes to catc' bloc@ to 'an#let'at e7ception.

l&ays use try catc' bloc@ to log your e7ception In selenium &eb#riverreports.

Page 46: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 46/292

6an#ling e7ceptions using t'ro&s @ey&or#not'er &ay o! 'an#ling e7ception Is using t'ro&s @ey&or# &it' met'o#

as s'o&n In bello& given e7ample. Supposing you 'ave a t'ro&e7cmet'o# &'ic' Is t'ro&ing some e7ception an# t'is met'o# Is calle# !romsome ot'er met'o# catc'e7c. o& you &ants to 'an#le e7ception o! t'ro&e7c met'o# In to catc'e7c met'o# t'en you nee# to use t'ro&s@ey&or# &it' t'ro&e7c met'o#.

public class 6an#le e7ce L

public static voi# mainAStringMN args L catc'e7cA H Q private static voi# catc'e7cA L try L <<t'ro&e7cA +et'o# calle#. t'ro&e7cA H Q catc' A rit'metic87ception e L <<87ception o! t'ro&e7cA &ill becaug't 'ere an# ta@e re uire# action. System.out.printlnAKDevi#e by error.K H Q Q<<T'is met'o# &ill t'ro& rit'metic87ception #ivi#e by .

private static voi# t'ro&e7cA t'ro&s rit'metic87ception L

int iG1%< HQQ

In above given e7ample" 87ception o! t'ro&e7cA Is 'an#le# Insi#ecatc'e7cA met'o#.

:sing t'ro& Yey&or# To T'ro& n 87ception 87plicitlyDi=erence bet&een t'ro& an# t'ro&s In 4ava

s &e learnt" t'ro&s @ey&or# Is use!ul to t'ro& t'ose e7ceptions to callingmet'o#s &'ic' are not 'an#le# Insi#e calle# met'o#s. T'ro& @ey&or# 'asa #i=erent &or@ - t'ro& @ey&or# Is use!ul to t'ro& an e7ception e7plicitly&'enever re uire#. T'is Is t'e #i=erence bet&een t'ro& an# t'ro&s In

4ava. Intervie&er can as@ t'is uestion. 5et us loo@ at practical e7ample.

public class 6an#le e7ce L

public static voi# mainAStringMN args L catc'e7cA H Q private static voi# catc'e7cA L try L <<t'ro&e7cA +et'o# calle#.

Page 47: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 47/292

t'ro&e7cA H Q catc' A rrayIn#e7)ut)!Boun#s87ception e L

System.out.printlnAK rray In#e7 out o! boun# e7ception.K H Q Q

private static voi# t'ro&e7cA L <<T'is statement &ill t'ro& rrayIn#e7)ut)!Boun#s87ception e7ception. t'ro& ne& rrayIn#e7)ut)!Boun#s87ceptionA HQ

Q

In above e7ample" rrayIn#e7)ut)!Boun#s87ception e7ception &ill bet'ro&n by t'ro& @ey&or# o! t'ro&e7c met'o#.

nally @ey&or# an# Its usenally @ey&or# Is use# &it' try catc' bloc@ at t'e en# o! try catc' bloc@.

Co#e &ritten Insi#e nally bloc@ &ill be e7ecute# al&ays regar#less o! e7ception Is occurre# or not. +ain intention o! using nally &it' try catc'bloc@ Is I! you &ants to per!orm some action &it'out consi#eringe7ception occurre# or not. nally bloc@ &ill be e7ecute# In bot' t'e cases.5et us see simple e7ample o! nally.

public class 6an#le e7ce L

public static voi# mainAStringMN args L tryL int iG%< H<<87ception &ill be t'ro&n. System.out.printlnAK3alue )! i Is KPi H <<T'is statement &ill be note7ecute#. Qcatc' A87ception e <<87ception &ill be caug't.

L System.out.printlnAKInsi#e catc'.KPe H <<print t'e e7ception. Q nally << nally bloc@ &ill be e7ecute#. L System.out.printlnAKInsi#e nally. lease ta@e appropriate actionK H Q

tryL int 4G%<2H<<87ception &ill be not t'ro&n. System.out.printlnAK3alue )! 4 Is KP4 H <<T'is statement &ill be e7ecute#. Qcatc' A87ception e << o e7ception so catc' bloc@ co#e &ill not e7ecute. L System.out.printlnAKInsi#e catc'.KPe H Q nally << nally bloc@ co#e &ill be e7ecute#. L System.out.printlnAKInsi#e nally. lease ta@e appropriate actionK H Q Q

Page 48: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 48/292

Q

In above e7ample" 2 try-catc'- nally bloc@s use#. 0un above e7ample an#observe result.

1st try bloc@ &ill t'ro& an error an# catc' &ill 'an#le an# t'en nallybloc@ &ill be e7ecute#. 2n# try bloc@ &ill not t'ro& any error so catc' &illbe not e7ecute# but nally bloc@ &ill be e7ecute#. So In bot' t'e cases

nally bloc@ &ill be e7ecute#.

So all t'ese t'ings about e7ception an# &ays o! 'an#ling t'em &ill 'elpsyou In your &eb#river test e7ception 'an#ling.

oste# by 8mail T'is BlogT'is[ S'are to T&itter S'are to Faceboo@ S'are to interest

1. Intro#uction o! Selenium WebDriver

W'at is selenium &eb#river

W'at is selenium &eb#river an# 'o& it 'elp us in so!t&are testingprocessR

Web#river in selenium A 'ttp <<#ocs.selenium' .org<pro4ects<&eb#river< isan Inter!ace an# it is #esigne# to overcome some limitations o! selenium

0C so!t&are testing tool. 6o&ever selenium WebDriver s #evelopment is inprocess so still t'ere are !e& limitations an# @no&n issues available in

Page 49: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 49/292

selenium WebDriver. Selenium WebDriver is also @no&n as Selenium 2 an#use# !or &eb as &ell mobileapplications testing. It is !ree&are so!t&are testing tool an# mostly use#as a regression testing tool !or &eb an# mobile applications.

Selenium 2 supports most o! all bro&sers to run your test cases an# manyprogramming languages li@e C\" Java" yt'on" 0uby" . et" erl" 6 " etc..to create an# mo#i!y your test scripts. Selenium 2AWebDriver controlsbro&ser #irectly !rom operating system level so it is interacting very !astan# &it' more realistic &ay &it' bro&sers. +a4or people in &orl# usingSelenium &eb#river &it' 4ava.

Di=erence bet&een WebDriver an# selenium 0C

WebDriver

• Selenium WebDriver #o not re uire selenium server !or running test.

• WebDriver is using native automation !rom eac' an# everysupporte# language !or running automation scripts on bro&sers.

• WebDriver supports &eb as &ell mobile application testing so youcan test mobile applications Ai 'one or n#roi# .

• Supporting latest versions o! almost all bro&sers.

• WebDriver controls t'e bro&ser itsel!.

Selenium 0C

• 0e uire# selenium server !or running test.

• Selenium 0C is using JavaScripts to #rive automation &it' bro&ser.

• Selenium 0C supports only &eb application testing.

• Supporting all bro&sers but not supporting latest versions.

• Selenium 0C is using 4avascript to interact an# operate on &eb page

ou can s'are i! you @no& any limitations or a#vantages o! SeleniumWebDriver by posting comments bello&.

Clic@ 'ere to rea# more about WebDriver. ou can rea# my ne7t postabout selenium &eb#river #o&nloa# install.

selenium &eb #river" selenium &eb#river #ocumentation" selenium

Page 50: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 50/292

testing" &eb#river selenium" selenium testing tool" selenium #river"selenium tool" automate# testing tools" so!t&are testing tools" seleniumautomation" selenium &eb#river vs remote control" selenium onlinetraining" selenium training" selenium test" selenium test tool" &eb#river"automate# testing tools" &eb testing tools" test automationtools" selenium re!o7 &eb#river

2. Do&nloa#ing an# installation )! Selenium Web#river &it' 8clipse

5ist o! Selenium Comman#s Wit' 87amples art - 1

Selenium ID8 comman#s &it' e7amples T'ere are many comman#s available in selenium ID8. I 'ave prepare# oneselenium comman#s list an# lin@e# some selenium i#e comman# &it' itse7amples. So you can clic@ on comman# lin@AFrom bello& given seleniumi#e comman#s list table to vie& 'o& to an# &'ere to use t'at comman#&it' e7ample . T'is !ull selenium comman# list &ill 'elp you to learnselenium ID8 on beginning level.

ADo you &ants to be master o! selenium ID8 in 4ust * #aysR I! yes t'en0ea# t'is Selenium ID8 tutorials

en#ing comman# sAW'ic' are not lin@e# e7ample creation is in process.you can subscribe via email !orne& post up#ate or you can boo@mar@ t'is page in your bro&ser to visit it

again.

S:BSC0IB8 3I 8+ I5 T) 98T 55 C)++ D 8V + 58S 3I 8+ I5

Complete 5ist o! Selenium ID8 Comman#sAClic@ 'ere to vie& part 2

a##5ocationStrategya##5ocationStrategy n#Waita##Scripta##Script n#Waita##Selectiona##Selection n#Waitallo& ativeVpat'allo& ativeVpat' n#WaitaltYeyDo&naltYeyDo&n n#WaitaltYey:paltYey:p n#Waitans&er)n e7t romptassert lert

assert lert ot resentassert lert resent

Page 51: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 51/292

assert llButtonsassert llFiel#sassert ll5in@sassert llWin#o&I#sassert llWin#o& amesassert llWin#o&Titlesassert ttributeassert ttributeFrom llWin#o&sassertBo#yTe7tassertC'ec@e#assertCon rmationassertCon rmation ot resentassertCon rmation resentassertCoo@ieassertCoo@ieBy ame

assertCoo@ie ot resentassertCoo@ie resentassertCursor ositionassert8#itableassert8lement6eig'tassert8lementIn#e7assert8lement ot resentassert8lement osition5e!tassert8lement ositionTopassert8lement resent

assert8lementWi#t'assert8valassert87pressionassert6tmlSourceassert5ocationassert+ouseSpee#assert ot lertassert ot llButtonsassert ot llFiel#sassert ot ll5in@sassert ot llWin#o&I#sassert ot llWin#o& amesassert ot llWin#o&Titlesassert ot ttributeassert ot ttributeFrom llWin#o&sassert otBo#yTe7tassert otC'ec@e#assert otCon rmationassert otCoo@ieassert otCoo@ieBy ameassert otCursor ositionassert ot8#itableassert ot8lement6eig't

Page 52: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 52/292

assert ot8lementIn#e7assert ot8lement osition5e!tassert ot8lement ositionTopassert ot8lementWi#t'assert ot8valassert ot87pressionassert ot6tmlSourceassert ot5ocationassert ot+ouseSpee#assert ot)r#ere#assert ot romptassert otSelect)ptionsassert otSelecte#I#assert otSelecte#I#sassert otSelecte#In#e7

assert otSelecte#In#e7esassert otSelecte#5abelassert otSelecte#5abelsassert otSelecte#3alueassert otSelecte#3aluesassert otSomet'ingSelecte#assert otSpee#assert otTableassert otTe7tassert otTitle

assert ot3alueassert ot3isibleassert otW'et'erT'isFrame+atc'Frame87pressionassert otW'et'erT'isWin#o&+atc'Win#o&87pressionassert otVpat'Countassert)r#ere#assert romptassert rompt ot resentassert rompt resentassertSelect)ptionsassertSelecte#I#

assertSelecte#I#sassertSelecte#In#e7assertSelecte#In#e7esassertSelecte#5abelassertSelecte#5abelsassertSelecte#3alueassertSelecte#3aluesassertSomet'ingSelecte#assertSpee#assertTableassertTe7t

Page 53: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 53/292

assertTe7t ot resentassertTe7t resentassertTitleassert3alueassert3isibleassertW'et'erT'isFrame+atc'Frame87pressionassertW'et'erT'isWin#o&+atc'Win#o&87pressionassertVpat'CountassignI#assignI# n#Waitbrea@capture8ntire ageScreens'otcapture8ntire ageScreens'ot n#Waitc'ec@c'ec@ n#Wait

c'ooseCancel)n e7tCon rmationc'oose)@)n e7tCon rmationc'oose)@)n e7tCon rmation n#Waitclic@clic@ n#Waitclic@ tclic@ t n#Waitcloseconte7t+enuconte7t+enu n#Wait

conte7t+enu tconte7t+enu t n#WaitcontrolYeyDo&ncontrolYeyDo&n n#WaitcontrolYey:pcontrolYey:p n#WaitcreateCoo@iecreateCoo@ie n#Wait#elete ll3isibleCoo@ies#elete ll3isibleCoo@ies n#Wait#eleteCoo@ie#eleteCoo@ie n#Wait#eselect op:p#eselect op:p n#Wait#oubleClic@#oubleClic@ n#Wait#oubleClic@ t#oubleClic@ t n#Wait#rag n#Drop#rag n#Drop n#Wait#rag n#DropTo)b4ect#rag n#DropTo)b4ect n#Wait#rag#rop

Page 54: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 54/292

#rag#rop n#Waitec'o

re8ventre8vent n#Wait

!ocus!ocus n#WaitgoBac@goBac@ n#Wait'ig'lig't'ig'lig't n#Waitignore ttributesWit'out3alueignore ttributesWit'out3alue n#Wait@eyDo&n@eyDo&n n#Wait@ey ress

@ey ress n#Wait@ey:p@ey:p n#WaitmetaYeyDo&nmetaYeyDo&n n#WaitmetaYey:pmetaYey:p n#WaitmouseDo&nmouseDo&n n#WaitmouseDo&n t

mouseDo&n t n#WaitmouseDo&n0ig'tmouseDo&n0ig't n#WaitmouseDo&n0ig't tmouseDo&n0ig't t n#Waitmouse+ovemouse+ove n#Waitmouse+ove tmouse+ove t n#Waitmouse)utmouse)ut n#Waitmouse)vermouse)ver n#Waitmouse:pmouse:p n#Waitmouse:p tmouse:p t n#Wait

mouse:p0ig'tmouse:p0ig't n#Waitmouse:p0ig't tmouse:p0ig't t n#Waitopen

Page 55: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 55/292

openWin#o&openWin#o& n#Waitpausere!res're!res' n#Waitremove llSelectionsremove llSelections n#WaitremoveScriptremoveScript n#WaitremoveSelectionremoveSelection n#Waitrolluprollup n#WaitrunScriptrunScript n#Wait

selectselect n#WaitselectFrameselect op:pselect op:p n#WaitselectWin#o&sen#YeyssetBro&ser5og5evelsetBro&ser5og5evel n#WaitsetCursor osition

setCursor osition n#Waitset+ouseSpee#set+ouseSpee# n#WaitsetSpee#setSpee# n#WaitsetTimeouts'i!tYeyDo&ns'i!tYeyDo&n n#Waits'i!tYey:ps'i!tYey:p n#Waitstorestore lertstore lert resentstore llButtonsstore llFiel#sstore ll5in@sstore llWin#o&I#sstore llWin#o& amesstore llWin#o&Titlesstore ttributestore ttributeFrom llWin#o&sstoreBo#yTe7tstoreC'ec@e#

Page 56: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 56/292

storeCon rmationstoreCon rmation resentstoreCoo@iestoreCoo@ieBy amestoreCoo@ie resentstoreCursor ositionstore8#itablestore8lement6eig'tstore8lementIn#e7store8lement osition5e!tstore8lement ositionTopstore8lement resentstore8lementWi#t'store8valstore87pression

store6tmlSourcestore5ocationstore+ouseSpee#store)r#ere#store romptstore rompt resentstoreSelect)ptionsstoreSelecte#I#storeSelecte#I#sstoreSelecte#In#e7

storeSelecte#In#e7esstoreSelecte#5abelstoreSelecte#5abelsstoreSelecte#3aluestoreSelecte#3aluesstoreSomet'ingSelecte#storeSpee#storeTablestoreTe7tstoreTe7t resentstoreTitlestore3aluestore3isiblestoreW'et'erT'isFrame+atc'Frame87pressionstoreW'et'erT'isWin#o&+atc'Win#o&87pressionstoreVpat'Countsubmitsubmit n#Waittypetype n#WaittypeYeystypeYeys n#Waitunc'ec@

Page 57: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 57/292

6o& to #o&nloa# an# install Selenium Web#river &it' 8clipse an# JavaStep By Step

Do&nloa# selenium &eb#river an# install selenium &eb#river is not muc'more 'ar#. ctually t'ere is not'ing to install e7cept JDY. 5et me #escribeyou step by step process o! #o&nloa#" installation an# con guration o!&eb #river an# ot'er re uire# components. ou can vie& my post aboutKW'at is selenium &eb#river K i! you &ants to @no& #i=erencebet&een WebDriver an# selenium 0C so!t&are tool.

A ote I am suggesting you to ta@e a tour o! Basic selenium comman#stutorials &it' e7amples be!ore going a'ea# !or &eb#river. It &ill improveyour basic @no&le#ge an# 'elps you to create &eb#river scripts veryeasily.

Steps To Setup an# con gure Selenium Web#river Wit' 8clipse an# Java

A ote ou can 3ie& +ore rticles )n WebDriver to learn it step by stepStep 1 Do&nloa# an# install Java in your systemFirst o! all you nee# to install JDY AJava #evelopment @it in your system.

So your ne7t uestion &ill be K'o& can i #o&nloa# 4avaK Clic@ 'ere to#o&nloa# Java an# install it in your system as per given installation gui#eover t'ere.

Step 2 Do&nloa# an# install 8clipseDo&nloa# 8clipse !or Java Developers an# e7tract save it in any #rive. It istotally !ree. ou can run eclipse.e7e #irectly so you #o not nee# to install8clipse in your system.

Step Do&nloa# WebDriver Java client #river.Selenium &eb#river supports many languages an# eac' language 'as itso&n client #river. 6ere &e are con guring selenium 2 &it' 4ava so &enee# &eb#river Java client #river . Clic@ 'ere to go onWebDriver Java client #river #o&nloa# page !or &eb#river #o&nloa# le.)n t'at page clic@ on Do&nloa# lin@ o! 4ava client #river as s'o&n inbello& image.

Alanguage-speci c client #river s version is c'anging time to time so it

Page 58: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 58/292

may be #i=erent version &'en you &ill visit #o&nloa# page.

Do&nloa#e# &ebDriver Java client #river &ill be in ?ip !ormat. 87tract an#save it in your system at pat' D Zselenium-2. . . T'ere &ill be libs!ol#er" 2 4ar les an# c'ange log in un?ippe# !ol#er as s'o&n in bello&

gure. We &ill use all t'ese les !or con guring &eb#river in eclipse.

Step $ Start 8clipse an# con gure it &it' selenium 2 A&eb#river

• Select Wor@Space on eclipse start up

Double clic@ on eclipse.e7e to start eclipse. First time &'en you starteclipse" it &ill as@ you to select your &or@space &'ere your &or@ &ill bestore# as s'o&n in bello& image. Create ne& !ol#er in D #rive &it' name

Web#river&or@ an# select it as your &or@space. ou can c'ange it lateron !rom S&itc' Wor@space un#er le menu o! eclipse.

!ter selecting &or@space !ol#er" 8clipse &ill be open.

• Create ne& pro4ect

Create ne& 4ava pro4ect !rom File O e& O ro4ect O Java ro4ect an# giveyour pro4ect name testpro4ect as s'o&n in bello& given gures. Clic@ on

nis' button.

Page 59: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 59/292

o& your ne& create# pro4ect testpro4ect &ill #isplay in eclipse pro4ecte7plorer as bello&.

Page 60: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 60/292

• Create ne& pac@age

0ig't clic@ on pro4ect name testpro4ect an# select e& O ac@age. 9iveyour pac@age name G mytestpac@ an# clic@ on nis' button. It &ill a##ne& pac@age &it' name mytestpac@ un#er pro4ect name testpro4ect .

• Create e& Class

0ig't clic@ on pac@age mytestpac@ an# select e& O Class an# set classname G mytestclass an# clic@ on Finis' button. It &ill a## ne& class

mytestclass un#er pac@age mytestpac@ .

Page 61: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 61/292

o& your 8clipse &in#o& &ill loo@s li@e bello&.

• ## e7ternal 4ar le to 4ava buil# pat'

o& you nee# to a## selenium &eb#river s 4ar les in to 4ava buil# pat'.

• 0ig't clic@ on pro4ect testpro4ect O Select roperties O Select Javabuil# pat' O avigate to 5ibraries tab

• Clic@ on a## e7ternal J 0s button O select bot' .4ar les!rom D Zselenium-2. . .

Page 62: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 62/292

• Clic@ on a## e7ternal J 0s button O select all .4ar les!rom D Zselenium-2. . Zlibs

o& your testpro4ect s properties #ialogue &ill loo@s li@e bello&.

T'at s all about con guration o! WebDriver &it' eclipse. o& you arerea#y to &rite your test in eclipse an# run it in WebDriver.

ou can 0ea# +y ost about 'o& to &rite an# run your rst testin WebDriver .

#o&nloa# selenium &eb#river" install &eb#river" #o&nloa# &eb#riverselenium" selenium testing" selenium testing tool" 'o& to #o&nloa#selenium &eb#river" &'at is selenium &eb#river" &eb#river #o&nloa#"selenium &eb#river #o&nloa#" selenium automation" selenium #o&nloa#"

selenium install" install selenium &eb#river" install selenium &eb#river ineclipse" eclipse an# selenium" 4ava an# selenium" 'o& to install a server"selenium #river" 'o& to setup selenium &eb#river" #o&nloa# &eb#riverselenium" selenium &eb#river tutorial 4ava

. Creating n# 0unning First Web#river Script

Create n# 0un First Web#river Script Wit' 8clipse

Page 63: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 63/292

0un First Web#river Script ou nee# to install &eb#river &it' eclipse to run your script in &eb#river.

!ter installation o! &eb#river" ou nee# to &rite a 4ava co#e in eclipse !oryour test case o! so!t&are &eb application. 5et me give you one simplee7ample o! creating simple &eb#river script. First o! all you nee# to &riteco#e as bello& in your

mytestclass.4ava le. ou can re uestAi! you &is' t'is sample co#e leby submitting comment bello& &it' your email i#.

o& you are rea#y to run your script !rom 0un menu as s'o&n bello&.

Script 87planationbove script &ill

• open Fire!o7 bro&ser.

• T'en #river.get synta7 &ill open 'ttp <<only-testing-blog.blogspot.in< in re!o7 bro&ser.

• T'en #river.getCurrent:rlA &ill get t'e current page :05 an# it &illbe store# in variable i . ou can #o same t'ing using Kstore5ocationKcomman# in selenium ID8

• n# at last" it &ill print value o! variable in console as s'o&n bello&.

Page 64: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 64/292

So t'is is t'e simple &eb#river script e7ample. ou can create it !or youro&n so!t&are &eb application too by replacing :05 in above script. We &illlearn more scripts in #etail in my upcoming posts.-- .1 0unning WebDriver In 9oogle C'rome

0unning Selenium Web#river Test In 9oogle C'rome

ou can rea# my T6IS )ST to @no& 'o& to run selenium &eb#river test o! so!t&are application in +o?illa Fire!o7 bro&ser. s you @no&" &eb#riversupport 9oogle C'rome bro&ser too. ou nee# to #o somet'ing e7tra !orlaunc'ing &eb#river test in in 9oogle c'rome bro&ser. 5et me #escribeyou steps to launc'&eb#river test in 9oogle C'rome.

Do&nloa# C'romeDriver serverFirst o! all" #o&nloa# latest version o! C'romeDriver server !or &eb#river. ou can #o&nloa# it #irectly !rom'ttp <<co#e.google.com<p<c'rome#river<#o&nloa#s<list . Current latestversion !or &in 2 is c'rome#river &in 2 2. as s'o&n bello&.

Clic@ on t'at lin@. It &ill ta@e you to #o&nloa# page.

Page 65: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 65/292

Clic@ on lin@ s'o&n above to #o&nloa# c'rome #river ?ip le. )ncompletion o! #o&nloa#" e7tract ?ip le to D < #rive.

o& in eclipse" create ne& pro4ect &it' name G c'romepro4ect an#create ne& class &it' name G c'romebro&ser . Don t !orget to select

public static voi# mainAStringMN args met'o# #uring ne& 4ava classcreation. Write co#e as s'o&n bello& in class le. ou can re uest t'iscopy o! t'is co#e by submitting comment bello& t'is post.

5oo@ in to above image. I set pat' !or 9oogle c'rome #river asSystem.set ropertyAK&eb#river.c'rome.#riverK" KD ZZc'rome#river &in 2 2. ZZc'rome#river.e7eK H

So no&" your test &ill run in 9oogle c'rome bro&ser.

Day - Con gure J:nit Wit' WebDriver

$. Do&nloa#ing an# installation )! J:nit &it' 8clipse

6o& to #o&nloa# an# install 4unit &it' eclipse step by step

Page 66: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 66/292

We 'ave alrea#y learn 'o& to con gure eclipse !or &eb#river" Creatingne& pro4ect" ne& pac@age an# t'en a##ing ne& class in my one o! t'eprevious post . In t'at post you can see 'o& to a## e7ternal 4ars o! &eb#river in eclipse. lso you can vie& 'o& to run your rst &eb#rivertest in eclipse !or betterun#erstan#ing.

9enerally you #o not nee# to install 4unit &it' eclipse i! you 'ave alrea#ya##e# e7ternal 4ar les o! &eb#river &it' eclipse. Because re uire# 4ar le!or 4unit &ill be alrea#y t'ere &it' &eb#river 4ar les.

6o& to veri!y t'at re uire# 4ar le !or 4unit is in my buil# pat' or notFor 4unit 4ar le veri cation" 0ig't clic@ on your pro4ect -O Buil# at' -OCon gure buil# pat'It &ill open 4ava buil# pat' &in#o& as s'o&n in bello& image. In t'at&in#o&" go to 5ibraries tab. 6ere you &ill see all your 4ar les. In 4ar letree vie&" loo@ !or t'e 4ar le name &'ic' is starting &it' 4unit.

Java buil# pat' &in#o&

I! t'is @in# o! 4unit 4ar le is alrea#y t'ere t'en you #o not nee# to install 4unit 4ar &it' eclipse an# you are rea#y to use 4unit &it' eclipse. But i! 4unit 4ar le is not t'ere t'en you nee# to a## it.

Do&nloa#ing 4unit 4ar le9o to 'ttp <<4unit.org< &ebsite -O Clic@ on #o&nloa# an# install gui#e lin@&ill re#irect t'e page at 4unit 4ar le #o&nloa# an# install page. 6ere you&ill n# t'e lin@ li@e K4unit.4arK. Clic@ on it &ill re#irect to 4unit 4ar le#o&nloa#ing page. From t'is page #o&nloa# t'e latest version o! 4unit 4ar

le.

Installing 4unit 4ar le &it' eclipse9o to 4ava buil# pat' &in#o& as s'o&n in above gure. o& clic@ on ##

87ternal J 0s button to a## your #o&nloa#e# 4unit 4ar le &it' eclipse.

Page 67: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 67/292

!ter a##ing 4unit 4ar le" clic@ on )Y button to close 4ava buil# pat'&in#o&.

o& you are rea#y to use 4unit &it' eclipse !or your &eb#river test o! so!t&are application. Clic@ 'ere to vie& all articles on &eb#river.

--%.1 Creating an# running &eb#river test &it' 4unit

Creating an# running &eb#river test &it' 4unit an# eclipse step by step

We 'ave alrea#y #iscusse# about 'o& to #o&nloa# an# install 4unit ineclipse to run &eb#river test in my previous post . lease note t'at younee# to a## 4unit 4ar le only i! it is not available in &eb#river 4ar le!ol#er. I! it is alrea#y t'ere &it' &eb#river 4ar le !ol#er t'en you not nee#to #o anyt'ing to create an# run

4unit test. o& let me #escribe you 'o& to create &eb#river test caseusing 4unit.

Web#river test case creation using 4unit an# eclipse ou are alrea#y a&are about 'o& to create ne& pro4ect an# pac@age ineclipse an# i! you are not a&are t'en please visit t'is post page . Tocreate J:nit Test Case" you nee# to a## J:nit Test Case at place o! Classinsi#e your pac@age. 5oo@ at bello& given image to @no& 'o& to a## J:nit

Test Case.

s s'o&n in above image" Select J:nit Test Case !rom Select a &i?ar##ialog an! t'en clic@ on e7t button. )n e7t page" 8nter your test casename an# clic@ on Finis' button.

Page 68: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 68/292

W'en you a## J:nit Test Case" your eclipse pro4ect e7plorer &in#o& &illloo@s li@e bello& given image.

o& replace bello& given co#e &it' your original co#e.

pac@age Test4unitH

import org.4unit.TestHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverH

public class +ytest L WebDriver #river G ne& Fire!o7DriverA H

;Test public voi# testA L

Page 69: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 69/292

#river.manageA .&in#o&A .ma7imi?eA H System.out.printAKWin#o& ma7imiseK H #river.getAK'ttp <<only-testing-blog.blogspot.in<K H System.out.printAKSite )penK H #river. uitA H System.out.printAK8n# o! TestK H Q

Q

0unning &eb#river test case using 4unit in eclipse!ter replacing above co#e &it' your original co#e" clic@ on 0un button in

eclipse. It &ill s'o& you 0un s #ialog as s'o&n bello&.

Select J:nit Test !rom 0un s #ialog an# clic@ on )@ button. 8clipse &ill runyour test case using J:nit an# on completion o! e7ecution" you can seeyour result in J:nit pane as bello&.

3I8W T6IS )ST to @no& 'o& to create an# run 4unit test suite in eclipse.

--%.2 Creating an# running 4unit test suite &it' &eb#river

Page 70: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 70/292

6o& To Create n# 0un J:nit Test Suit For WebDriver Test - Step By Step

I! you are planning to per!orm regression testing o! any application using&eb#river t'en obviously t'ere &ill be multiple test cases or test classesun#er your &eb#river pro4ect. 87ample - T'ere are 2 4unit test cases un#eryour pro4ect s pac@age. o& i! you &ants to run bot' o! t'em t'en 'o&&ill you #o itR Simple an#easy solution is creating J:nit test suite. I! your pro4ect 'as more t'an 2test cases t'en you can create test suite !or all t'ose test cases to run alltest cases !rom one place.

Be!ore creating 4unit test suite" you must 'ave to rea# my post aboutK6o& to #o&nloa# an# install 4unit in eclipse K an# K6o& to create an# run

4unit test in eclipse K. o& let me #escribe you 'o& to create 4unit testsuite in eclipse !or your 4unit test case.

Step 1 - Create ne& pro4ect an# pac@ageCreate ne& pro4ect in eclipse &it' name G 4unitpro4ect an# t'en a## ne&pac@age G 4unitpac@ un#er your pro4ect. 3I8W T6IS )ST to @no& 'o& tocreate ne& pro4ect an# a## pac@age in eclipse. ## re uire# e7ternal 4ar

les !or selenium &eb#river.

Step 2 - Create 1st Test Caseo& create J:nit test case un#er 4unitpac@ pac@age &it' class name G

4unittest1 as bello&. 3I8W T6IS post to @no& 'o& to create 4unit test casein eclipse.

pac@age 4unitpac@H

import org.4unit.TestHimport org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverH

public class 4unittest1 L WebDriver #river G ne& Fire!o7DriverA H

;Test public voi# testA t'ro&s Interrupte#87ception L #river.manageA .&in#o&A .ma7imi?eA H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1 <11<ne&-test.'tmlK H #river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK4unittest1 e7ecute#K H T'rea#.sleepA2 H System.out.printAK4unittest1 class is e7ecute#K H #river. uitA H QQ

Page 71: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 71/292

Step - Create 2n# test caseSame &ay" Create 2n# test class &it' name G 4unittest2 un#er pac@age G

4unitpac@ as bello&.

pac@age 4unitpac@H

import org.4unit. !terHimport org.4unit.Be!oreHimport org.4unit.TestHimport org.4unit.IgnoreHimport org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverH

public class 4unittest2 L WebDriver #river G ne& Fire!o7DriverA H

;Be!orepublic voi# setup A L #river.manageA .&in#o&A .ma7imi?eA H#river.getAK'ttp <<only-testing-blog.blogspot.in<2 1 <11<ne&-test.'tmlK H

Q

; !ter

public voi# a!tertestA L #river. uitA HQ

;Test public voi# test1A t'ro&s Interrupte#87ceptionL

#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK4unittest2 class-test1K H System.out.printAKZn4unittest2 class-test1 met'o# is e7ecute#K H T'rea#.sleepA2 H

Q ;Test

public voi# test2A t'ro&s Interrupte#87ception L #river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK4unittest2 class-test2K H T'rea#.sleepA2 H System.out.printAKZn4unittest2 class-test2 met'o# is e7ecute#K H QQ

Step $ - Create test suite !or bot' test cases

Page 72: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 72/292

o& &e 'ave 2 test casesA4unittest1.4ava an# 4unittest2.4ava un#erpac@age G 4unitpac@.

To create test suite" 0ig't clic@ on 4unitpac@ pac@age !ol#er an# 9o to -Oe& -O )t'er -O Java -O Junit -O Select J:nit Test Suite as s'o&n in

bello& image.

o& clic@ on e7t button.)n ne7t screen" a## 4unit test suite nameG 4unittestsuite an# select bot' test cases as s'o&n bello& image an#t'en clic@ on Finis' button.

It &ill a## ne& test suite class G 4unittestsuite.4ava un#er your pac@age asbello&.pac@age 4unitpac@H

import org.4unit.runner.0unWit'Himport org.4unit.runners.SuiteH

Page 73: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 73/292

import org.4unit.runners.Suite.SuiteClassesH

;0unWit'ASuite.class;SuiteClassesAL 4unittest1.class" 4unittest2.class Qpublic class 4unittestsuite L

Q

I! you see in above suite class" t'ere are 2 4unit annotations a##e# &it'name G ;0unWit' an# ;SuiteClasses. ;0unWit' annotation &illre!erences 4unit to run test in class an# ;SuiteClasses #escribes classesinclu#e# in t'at test suite.

o& your 4unit test suite is create# an# pro4ect structure &ill loo@s li@ebello&.

Step $ - 0unning test suiteSelect 4unit test suite class G 4unittestsuite an# run it in eclipse. W'en yourun 4unit test suite" eclipse &ill run bot' test cases A4unittest1 an#

4unittest2 one by one. W'en e7ecution complete#" you &ill see bello&given output lines in console.

4unittest1 class is e7ecute# 4unittest2 class-test1 met'o# is e7ecute# 4unittest2 class-test2 met'o# is e7ecute#

4unit test e7ecution report &ill loo@s li@e bello&.

Page 74: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 74/292

T'is &ay &e can create 4unit test suite to run multiple test cases !rom oneplace. ou can vie& all Junit tutorial posts !or &eb#river on T6IS 5I Y .

3I8W + 8VT )ST to @no& 'o& to ignore test !rom e7ecution.

--(.1 :sing J:nit nnotations in &eb#river

6o& to :se J:nit nnotations in &eb#river test case &it' e7ample

:nit testing !rame&or@ J:nit 'as many annotations to control t'e Eo& an#activity o! co#e e7ecution. ou must nee# to insert J:nit annotation insi#et'e 4ava co#e to e7ecute your test case as 4unit test case. ou can loo@ inmy previous post &'ere i 'ave use# J:nit ;Test annotation be!ore testmet'o#. 5et me #escribeyou mostly use# J:nit nnotations &it' e7ample. ou can vie& more#etails on J:nit at 'ttp <<4unit.org< .

1. ;Be!ore2. ;Test

. ; !ter

2 ot'er !re uently use# annotations are ;Be!oreClass an# ; !terClass.

Depen#ing on annotation names" J:nit !rame&or@ &ill #eci#e t'e co#ee7ecution Eo&. i.e. First J:nit !rame&or@ &ill e7ecute ;Be!ore met'o#"Secon# it &ill e7ecute ;Test met'o# an# at last it &ill e7ecute ; !termet'o#.

1. ;Be!ore nnotations name suggest. met'o# &ritten un#er ;Be!ore annotation &ill be

e7ecute# be!ore t'e met'o# &ritten un#er ;Test annotation. Base# on;Be!ore annotation" J:nit !rame&or@ &ill e7ecute t'at be!ore met'o# rst.9enerally met'o# un#er ;Be!ore annotation is use# to initiali?ing &ebsitean# ot'er environment relate# setup. ;Be!ore annotation met'o# &ill bee7ecute# be!ore eac' ;Test annotation met'o# means i! t'ere are t&o;Test met'o#s in your class t'en ;Be!ore met'o# &ill be e7ecute# t&otimes.

2. ; !ter

Page 75: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 75/292

+et'o# un#er ; !ter annotation is @no&n as a!ter met'o# an# e7ecutiono! ; !ter met'o# &ill start as soon as completion o! ;Test met'o#e7ecution completion. Same as ;Be!ore " ; !ter annotation met'o# &illbe e7ecute# t&o times i! t'ere are t&o ;Test met'o#s in your class.

. ;Test+et'o# un#er ;Test annotation is @no&n as test met'o#. 87ecution o! ;Test met'o# &ill start as soon as ;Be!ore met'o# e7ecution complete#.9enerally &e are &riting all testing relate# activity un#er ;Test. We canuse multiple ;Test met'o#s in single class too.

$. ;Be!oreClass+et'o#s un#er ;Be!oreClass annotation &ill be e7ecute# be!ore t'e anytest met'o# starts e7ecution. It &ill be e7ecute# only once even i! t'ereare multiple ;Test met'o#s in your class.

%. ; !terClass; !terClass met'o# &ill be run on completion o! all t'e test met'o# se7ecution !rom t'at class. Same as ;Be!oreClass" ; !terClass met'o#&ill be e7ecute# once only.

3I8W 8V + 58 )F DIFF808 C8 B8TW88 ;Be!ore vs ;Be!oreClassD ; !ter 3S ; !terClass )T TI) S

87ecute bello& given e7ample in your eclipse as a #ebugging mo#e toveri!y its e7ecution.

pac@age Test4unitH

import 4ava.util.concurrent.Time:nitH

import org.4unit. !terHimport org.4unit.Be!oreHimport org.4unit.TestHimport org.open a.selenium.>Himport org.open a.selenium. re!o7.Fire!o7DriverH

public class +ytest L WebDriver #river G ne& Fire!o7DriverA H ;Be!ore

public voi# be!oretestA L #river.manageA .&in#o&A .ma7imi?eA H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1 <11<ne&-test.'tmlK H Q ; !ter

public voi# a!tertestA L #river. uitA H

Page 76: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 76/292

Q

;Test

public voi# testA L

#river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H #river. n#8lementABy.7pat'AK<<inputM;valueG Bi@e NK .clic@A H boolean str1 G#river. n#8lementABy.7pat'AK<<inputM;valueG Bi@e NK .isSelecte#A H i!Astr1 G true L System.out.printAKC'ec@bo7 is c'ec@e#K H Q else L System.out.printAKC'ec@bo7 is not c'ec@e#K H Q Q Q

6ere ; !ter met'o# is &ritten be!ore ;Test met'o# but J:nit Frame&or@&ill tells eclipse to e7ecute ;Test met'o# rst an# t'en ; !ter met'o#

--(.2 ;Be!ore<; !ter 3S ;Be!oreClass<; !terClass Di=erence

87ample )! Di=erence Bet&een ;Be!ore<; !ter 3S;Be!oreClass<; !terClass In J:nit Wit' WebDriver

+any rea#ers are as@ing me t'e #i=erence bet&een J:nitannotations ;Be!ore 3S ;Be!oreClass an# ; !ter 3S ; !terClass in&eb#river test. I! you 'ave rea# my J: IT )T TI) S )ST " I 'aveclearly #escribe# #i=erence bet&een ;Be!ore an# ;Be!oreClassannotations an# ; !ter an#; !terClass annotations in bol# te7t. o& let me #escribe once more an#t'en &e &ill loo@ at practical e7ample !or bot' o! t'em.

Di=erence bet&een ;Be!ore an# ;Be!oreClass annotations

• Test met'o# mar@e# &it' ;Be!ore annotation &ill be e7ecute#be!ore t'e eac' ;Test met'o#. +eans i! t'ere are % ;Test met'o#sin your class t'en ;Be!ore test met'o# &ill be e7ecute# % times.

• Test met'o# mar@e# &it' ;Be!oreClass annotation &ill be e7ecute# 4ust be!ore t'e class. +eans ;Be!oreClass met'o# &ill be e7ecute#only once be!ore t'e class even i! t'ere are % ;Test met'o#s in your

class.

Page 77: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 77/292

Di=erence bet&een ; !ter an# ; !terClass annotations

• Same as ;Be!ore annotation" Test met'o# mar@e# &it'; !ter annotation &ill be e7ecute# a!ter t'e eac' ;Test met'o#.

• ; !terClass annotation &ill be e7ecute# only once a!ter last ;Testmet'o# e7ecute#.

87ecute bello& given ;Be!ore an# ; !ter annotations e7ample in youreclipse an# observe result in console.pac@age 4unitpac@H

import org.4unit. !terHimport org.4unit. !terClassHimport org.4unit.Be!oreHimport org.4unit.Be!oreClassHimport org.4unit.TestHimport org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverH

public class 4unittest2 L private static WebDriver #riverH ;Be!orepublic voi# openbro&serA L System.out.printAKZnBro&ser openK H #river G ne& Fire!o7DriverA H#river.manageA .&in#o&A .ma7imi?eA H#river.getAK'ttp <<only-testing-blog.blogspot.in<2 1 <11<ne&-test.'tmlK H

Q

; !terpublic voi# closebro&serA L System.out.printAKZnBro&ser closeK H #river. uitA HQ ;Test

public voi# test1A t'ro&s Interrupte#87ceptionL

#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK4unittest2 class-test1K H System.out.printAKZn4unittest2 class-test1 met'o# is e7ecute#K H

T'rea#.sleepA2 H Q

Page 78: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 78/292

;Test public voi# test2A t'ro&s Interrupte#87ception L #river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .clearA H #river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK4unittest2 class-test2K H T'rea#.sleepA2 H System.out.printAKZn4unittest2 class-test2 met'o# is e7ecute#K H QQW'en you e7ecute above e7ample in eclipse" Bello& given result &ill be#isplaye# in console.Console )utput Bro&ser open

4unittest2 class-test1 met'o# is e7ecute#Bro&ser closeBro&ser open

4unittest2 class-test2 met'o# is e7ecute#Bro&ser close

Base# on above given console output" &e can say eac' ;Be!ore met'o#is e7ecute# be!ore eac' ;Test met'o# an# eac' ; !ter met'o# ise7ecute# a!ter eac' ;Test met'o#. o& let &e replace ;Be!oreannotation &it' ;Be!oreClass an# ; !ter annotation &it' ; !terClass insame e7ample an# t'en observe result. 0eplace bello& given

;Be!oreClass an# ; !terClass part &it' ;Be!ore an# ; !ter part inabove e7ample as bello&.

;Be!oreClasspublic static voi# openbro&serA L System.out.printAKZnBro&ser openK H #river G ne& Fire!o7DriverA H#river.manageA .&in#o&A .ma7imi?eA H#river.getAK'ttp <<only-testing-blog.blogspot.in<2 1 <11<ne&-test.'tmlK H

Q

; !terClasspublic static voi# closebro&serA L System.out.printAKZnBro&ser closeK H #river. uitA HQ

o& run above e7ample an# loo@ at console result. Console result &ill beas bello&.Console )utput

Bro&ser open 4unittest2 class-test1 met'o# is e7ecute#

Page 79: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 79/292

4unittest2 class-test2 met'o# is e7ecute#Bro&ser close

s per console result" &e can say ;Be!oreClass met'o# is e7ecute# onceonly. Same &ay ; !terClass annotation mar@e# met'o# is also e7ecute#once only.

--(. Ignoring J:nit Test !rom e7ecution

6o& To Ignore Test In WebDriver Wit' J:nit

s #escribe# in my 083I):S )ST " We can create 4unit test suite toe7ecute multiple test cases !rom one place. See e7ample o! my previouspost. 4unittest2 class 'ave t&o ;Test met'o#s. o& I! I &ants toe7clu#e<Ignore 1st ;Test met'o# !rom e7ecution an# &ants to e7ecuteonly 2n# ;Test met'o# t'en 'o&can I #o ItR Ignoring speci c test in 4unit test is very easy.

;Ignore annotationWe can use J:nit s 'as inbuilt ;Ignore annotation be!ore ;Test met'o# toIgnore t'at speci c &eb#river test !rom e7ecution. 5et &e apply ;Ignoreannotation practically in our test an# t'en observe its e7ecution result.

3I8W J: IT 8V + 58 )F TI+8):T D 8V 8CT8D 8VC8 TI) T8ST

+y scenario is to ignore 1st ;Test met'o# !rom e7ecution so i nee# to put;Ignore annotation in my test be!ore 1st ;Test met'o# as bello&.

;Ignore ;Test public voi# test1A t'ro&s Interrupte#87ceptionLQ

6ere" ;Ignore annotation &ill e7clu#e test1A met'o# !rom e7ecution.

Copy bello& given Ignoring ;Test met'o#Atest1A part &it' ;Ignoreannotation an# replace it &ill ;Test met'o# part given on Step ACreate2n# test case o! T6IS 8V + 58 A ote ;Test met'o# part &'ic' nee#s toreplace is mar@e# &it' pin@ color in t'at e7ample post .

<<To ignore t'is ;Test met'o# !rom e7ecution ;Ignore ;Test public voi# test1A t'ro&s Interrupte#87ceptionL

#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK4unittest2 class-test1K H System.out.printAKZn4unittest2 class-test1 met'o# is e7ecute#K H

Page 80: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 80/292

T'rea#.sleepA2 H Q

o& &'en you run !ull test suiteA4unittestsuite.4ava t'en on completion o! 4unit test suite e7ecution" you &ill get bello& given result in console.

4unittest1 class is e7ecute# 4unittest2 class-test2 met'o# is e7ecute#

s per console result" ignore# testAtest1A is not e7ecute#.

J:nit test e7ecution report &ill loo@s li@e bello&.

T'is &ay" Junit s ;Ignore annotation &ill 'elp us to ignore speci c test!rom e7ecution.

--(.$ Junit Timeout n# 87pecte# 87ception Test

87ample )! Junit Timeout n# 87pecte# 87ception Test For WebDriver

Sometimes you nee# to set time out !or your &eb#river test or you nee#to set e7pecte# e7ception con#ition !or your test. Supposing you 'ave&ritten test !or one mo#ule an# you &ants to set timeout !or your test.6ere timeout means allo&e# ma7imum time to complete !ull test. Same&ay" ou are e7pecting somee7ception #uring your &eb#river test e7ecution an# t'at e7ception is

acceptable.I! you are using 4unit !rame&or@ !or your &eb#river test t'enyou can #o it very easily. We 'ave seen e7ample o! - 'o& to Ignorespeci c &eb#river test using 4unit s ;Ignore annotation in T6IS )ST . o&let me #escribe 'o& to &rite timeout test an# e7ception test in 4unit &it'e7amples.

Timeout Test In J:nit For WebDriverWe can speci!y timeout time &it' 4unit s ;Test annotation as s'o&nbello&. It &ill allo& ma7imum 2 millisecon#s to complete t'e e7ecutiono! test1A met'o#. !ter 2 milisecon#s" it &ill s@ip remaining e7ecutionan# test1A &ill be mar@e# &it' error.;TestAtimeoutG2 public voi# test1A L

Page 81: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 81/292

Q

87pecte# 87ception Test In J:nit For WebDriverWe can speci!y e7pecte# e7ception con#ition &it' ;Test annotation asbello&. 6ere my e7pecte# e7ception is null pointer e7ception so mye7pecte# con#ition is ull ointer87ception.class. ou can &rite youre7pecte# e7ception li@e In#e7)ut)!Boun#s87ception.class"

rit'metic87ception.class" ect.;TestAe7pecte# G ull ointer87ception.class public voi# e7ceptiontest2A L Q

Junit 87ample )! Timeout n# 87pecte# 87ception Test-9o to T6IS 98-Copy e7ample given on Step - Create 2n# test case an# paste it in your

eclipse.-0emove all ;Test met'o#s !rom t'at e7ample an# paste bello& given;Test met'o#s an# t'en run your test class A4unittest2.4ava .;TestAtimeoutG2 public voi# test1A t'ro&s Interrupte#87ceptionL

#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK4unittest2 class-test1K H System.out.printAKZn4unittest2 class-test1 e7ecute# be!ore sleepK H T'rea#.sleepA% H System.out.printAKZn4unittest2 class-test1 e7ecute# a!ter sleepK H

Q ;Test

public voi# e7ceptiontest1A L t'ro& ne& ull ointer87ceptionA H Q ;TestAe7pecte# G ull ointer87ception.class

public voi# e7ceptiontest2A L t'ro& ne& ull ointer87ceptionA H Q

)n completion o! e7ecution" console &ill s'o& bello& given out put.Console )utput Bro&ser open

4unittest2 class-test1 e7ecute# be!ore sleepBro&ser close

J:nit test result loo@s li@e bello&.

Page 82: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 82/292

• test1A met'o# is #isplay &it' error because test &as time out a!ter2 milisecon# as per our con#ition.

• e7ceptiontest2A pass success!ully because &e 'ave place#e7pecte# e7ception con#ition &it' it.

e7ceptiontest1A #isplay &it' error because t'ere &as notany e7pecte# e7ception con#ition.

• Day $ - 9enerate Test 0eport :sing J:nit• *. Selenium WebDriver Test report 9eneration using J:nit - Steps • 9enerate &eb#river test report in 6T+5 by running buil# 7ml le

art - • We 'ave seen 'o& to con gure system an# eclipse in con guration

part 1 an# con guration part 2 posts to generate &eb#river testreport. o& &e 'ave a test case an# buil#.7ml le in our pro4ecttree. 5et me #escribe you !e& steps o! con guring an#running buil#.7ml le in eclipse. o& !ollo& t'e bello& given

• buil#.7ml le con guration steps to generate 6T+5 report.•

• Step 1 3eri!y buil#.7ml le &or@ space• )pen 87ternal Tools Con guration #ialog as bello&• 0ig't clic@ on Buil#.7ml - O 0un s -O 87ternal Tools Con guration. It

&ill open 87ternal Tools Con guration #ialog as s'o&n bello&.•

Page 83: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 83/292

• In +ain tab" you nee# to veri!y t'at your current pro4ect s buil#.7mlis selecte# or not.+y current pro4ect name is KJ:nit0eportK so it iscorrect !or me. I! it is o! any ot'er pro4ect t'en you nee# to c'ange itby clic@ing on Bro&se Wor@space button.

• Step 2 Set target e7ecution or#er• 9o to t'e target tab an# t'en set target e7ecution or#er G buil#"

For0eport A+y ro4ect name " 4unitreport. I! it is not in t'is or#er t'enyou can c'ange it by unc'ec@ing an# c'ec@ing an# t'e c'ec@ bo7 orby clic@ing on or#er button. So no& my target e7ecution or#er &illloo@s li@e bello&.

• Step Set 0untime J08• 9o to J08 tab. 6ere you nee# to set 0untime J08 G JDY at place o!

J08 as s'o&n in bello& image.

Page 84: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 84/292

• I! JDY is not #isplay in Separate J08 list t'en you can a## it by• - Clic@ on Installe# J08s button. It &ill open re!erences #ialog.• - Clic@ on ## button !rom re!erences #ialog. It &ill open ## J08

#ialog.• - Select Stan#ar# 3+ !rom ## J08 #ialog an# clic@ on ne7t button

as s'o&n in bello& image.•

• )n e7t &in#o&" Select JDY !ol#er pat'AW'ic' is locate# atC Z rogram FilesZJavaZ in my system In J08 'ome el# an# clic@ onFinis' button. It &ill a## JDY option in Separate J08 #rop #o&n.

Page 85: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 85/292

• o& buil# le con guration is nis'e# an# nally you are rea#y torun your buil#.7ml le an# generate your test report.

• Step $ 0un buil#.7ml• o& you can run buil#.7ml le in &ays.• 1st &ay 0un !rom 87ternal Tools Con guration #ialog as s'o&n.•

• 2n# &ay 0un #irectly !rom buil#.7ml le

Page 86: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 86/292

• r# &ay 0un !rom +enu icons•

• Step % 3ie& 0eport• W'en you run buil#.7ml le" eclipse &ill run your 4unit test case an#

on completion o! e7ecution report le &ill be generate# in your 4unit!ol#er &'ic' is locate# in your pro4ect !ol#er as bello&.

Page 87: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 87/292

• )pen in#e7.'tml le. It &ill loo@s li@e bello&.•

• 8clipse n# nt Con guration Steps To 9enerate &eb#river teste7ecution report using J:nit art - 2

• Web#river test result generation is very important an# essential part

o! automation testing an# !or t'at you nee# to con gure youreclipse accor#ingly. Be!ore eclipse an# ant con guration" you nee#to con gure your system as #escribe# in art - 1 . o& let me#escribe you t'e steps o! eclipse con guration to generate &eb

• #river test e7ecution report using J:nit. er!orm bello& given steps.•

• Step 1 Create ne& J:nit Test Case• First o! all" you nee# to create J:nit test case in eclipse as #escribe#

in J:nit Test Creation ost 1 an# ost 2 .•

• Step 2 Set nt 6ome 8ntries• To set ant 'ome entries"

Page 88: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 88/292

• - 9o to eclipse +enu -O Win#o& -O re!erences. It &ill open eclipsepre!erence #ialog.

• - Select nt -O 0untime !rom le!t si#e pre!erence tree. o& selectClasspat' at rig't si#e.

• - Select 1st class pat' nt 6ome 8ntries an# clic@ on nt 6omebutton an# select apac'e-ant-1./.2 !ol#er pat' an# t'en clic@ on)Y as s'o&n in bello& image.

• Step Set 4unit.4ar pat' in Blobal 8ntries o! nt 0untime re!erencei! it is not t'ere

• s #escribe# above" 9o to nt -O 0untime pre!erence settings an#select 9lobal 8ntries.

• o& clic@ on ## 87ternal Jars button an# select 4unit.4ar locate# ateclipse -O plugins -O org.4unit $.11. .v2 1 , AT'is !ol#ername may be #i=erent i! you 'ave #i=erent version o! 4unit -O

4unit.4ar. an# t'en clic@ on )Y as s'o&n in bello& image.•

• Step $ 9enerate buil#.7ml !or your pro4ect

Page 89: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 89/292

• o& you nee# to generate buil#.7ml !or your pro4ect. To generate it"0ig't clic@ on your pro4ect !ol#er an# select 87port. It &ill open87port #ialog as s'o&n in bello& image.

• Select nt Buil# les !rom e7port #ialog an# clic@ing on e7t button&ill s'o& you all your pro4ects list. )n e7t #ialog" select yourcurrent pro4ect an# clic@ on Finis' button as s'o&n bello&.

• 5oo@ in above image. W'en you &ill run buil#.7ml le" your testreports &ill be save# in J:nit output #irectory G 4unit !ol#er. It &illbe create# automatically in your pro4ect !ol#er &'en you run

buil#.7ml le. We &ill learn 'o& to run buil#.7ml le in my ne7t post .

Page 90: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 90/292

W'en you clic@ on Finis' button" buil#.7ml le &ill be generate#insi#e your pro4ect !ol#er as s'o&n bello&.

• 3ie& my e7t ost to see 'o& to con gure an# run buil#.7ml le togenerate your test case report in 6T+5 vie&.

System Con guration Steps To 9enerate &eb#river test e7ecution reportusing J:nit art -1

+any people !acing issues in J:nit report generation con guration usingant an# eclipse. I! every t'ing is not setup properly t'en it &ill give youone or anot'er error an# you &ill be not able to generate J:nit test casee7ecution report !or your &eb #river test case. 5et me #escribe you !e&system con guration steps in t'ispost an# eclipse con guration steps in my ne7t post to generate J:nit testcase e7ecution report very easily an# &it'out any error.

Step 1 Do&nloa# an# install latest Java S8Do&nloa# an# install latest Java S8 Development Yit AJava #evelopmentYitAJDY an# Java 0untime 8nvironmentAJ05 as per your systemcon guration. ou can #o&nloa# it !rom Java S8 Development Yit#o&nloa# page.

Step 2 Do&nloa# 5atest 3ersion o! apac'e-ant ?ipDo&nloa# latest version o! apac'e-ant!rom 'ttp <<ant.apac'e.org<bin#o&nloa#.cgi . Current latest versionis apac'e-ant-1./.2 as s'o&n in bello& image an# it may be #i=erent in

!uture.

Page 91: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 91/292

87tract ?ip le an# save it in your local #rive.

Step Steps to Set 8nvironment variables in system property !or 4ava

- Set J 3 6)+8 8nvironment variable ou nee# to set J 3 6)+8 8nvironment variable in your system property. To set 8nvironment variable" 0ig't clic@ on +y Computer -O roperties an#go to -O #vance# tab. Clic@ing on 8nvironment variables button &ill open8nvironment variable #ialog. Clic@ on e& button !rom System 3ariables

bo7 an# a## 3ariable ame G J 3 6)+8 an# 3ariable 3alue G at' o! your 4#@ !ol#er. 6ere my JDY !ol#er pat' is C Z rogramFilesZJavaZ4#@1.*. $ as s'o&n in bello& image.

Page 92: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 92/292

- Set T 6)+8 8nvironment variableSame as J 3 6)+8 8nvironment variable" ## ne& system variablename G T 6)+8 an# 3ariable 3alue G at' o! your apac'e-ant-1./.2!ol#er as #escribe# in bello& image.

Page 93: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 93/292

- 8#it at' 3ariable!ter setting J 3 6)+8 an# T 6)+8 system variable" 8#it e7istingat' variable by selecting at' variable !rom System variables list an#

clic@ 8#it button an# insert J 3 6)+8 ZbinH T 6)+8 Zbin at t'een# o! at' variable value string. Do not !orget to put semicolon AH be!ore

J 3 6)+8 Zbin as s'o&n in bello& image.

- Copy tools.4ar !rom 4#@<lib an# paste It In 4re<lib

• T'ere &ill be 2 !ol#ers In your C rogram FilesZJava !ol#er.

• In my system It Is 4#@1.*. $ an# 4re*.

• )pen C Z rogram FilesZJavaZ4#@1.*. $ Zlib !ol#er an# copy tools.4arle.

• )pen C Z rogram FilesZJavaZ4re*Zlib an# paste tools.4ar In to It.

o& restart your system to getting system variables c'anges e=ect. o&system con guration is complete# !or generating &eb#river test e7ecutionreport using J:nit. o& you nee# to con gure eclipse as #escribe# in my

Day % - 8lement 5ocators In WebDriver

Page 94: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 94/292

,. Di=erent Ways )! 5ocating 8lements In WebDriver

We 'ave learnt most o! all t'e &ay o! locating element in &eb#riverli@e 5ocating 8lement By ID " 5ocating 8lement By ame " 5ocating 8lementBy Class ame " 5ocating 8lement By Tag ame " 5ocating 8lement By 5in@

Te7t )r artial 5in@ an# 5ocating 8lement By CSS Selector

6o& To 5ocate 8lements By ID In Selenium WebDriver Wit' 87ample

Be!ore using WebDriver" ou must be a&are about #i=erent &ays o! locating an elements in WebDriver. 5ocating an element is essential partin selenium WebDriver because &'en you &ants to ta@e some action onelement Atyping te7t or clic@ing on button " rst you nee# to locate t'atspeci c element to per!orm action.

First o! all I recommen# you to rea# all t'ese selenium ID8 elementlocating met'o#s an# t'en rea# t'is article about Selenium WebDriverelement locators to get all locating met'o#s in better &ay.

WebDriver &'ic' is also @no&n as a Selenium 2 'as many #i=erent &ayso! locating element. 5et me e7plain eac' o! t'em &it' e7amples. 6ere Iam e7plaining 'o& to locate element By i# an# &ill #escribe 5ocating Web8lement By Class ame in my e7t ost an# ot'ers in latter posts.

5ocating :I 8lement By ID

I! your &ebpage element 'as uni ue an# static ID t'en you can locateyour page element by ID. 5oo@ in to bello& given image. ou can veri!yt'at your &ebelement 'as any i# or not using t'e rebug.

In above image" Submit ]uery button 'as uni ue i# G submitButton . Ican use t'at i# to locate button as bello&.

#river. n#8lementABy.i#AKsubmitButtonK H

Page 95: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 95/292

Bello& given e7ample &ill s'o& you 'o& to locate element by i# an# t'en'o& to clic@ on it. Copy bello& given ;Test met'o# part an# replace it&it' t'e ;Test met'o# part o! e7ample given on t'is page .A ote ;Testmet'o# is mar@e# &it' pin@ color in t'at e7ample .

;Test public voi# testA L #river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H !or Aint i G H i G2 H iPP L Web8lement btn G #river. n#8lementABy.i#AKsubmitButtonK H<<5ocatingelement by i# i! Abtn.is8nable#A

L <<i! &ebelement s attribute !oun# enable# t'en t'is co#e &ill bee7ecute#. System.out.printAKZnCongr,s... Button is enable# an# &eb#river isclic@ing on it no&K H

<<5ocating button by i# an# t'en clic@ing on it. #river. n#8lementABy.i#AKsubmitButtonK .clic@A H

iG2 H

Q else

L <<i! &ebelement s attribute !oun# #isable# t'en t'is co#e &ill bee7ecute#. System.out.printAKZnSorry but Button is #isable# rig't no&..K H Q try L T'rea#.sleepA% H Q catc' AInterrupte#87ception e L e.printStac@TraceA H Q Q Q

Selenium Web#river 8lement 5ocator - 5ocating 8lement By ame Wit'87ample

Selenium WebDriver<Selenium 2 is &eb an# mobile application regressiontesting tool an# it is using element locators to n# out an# per!ormactions on &eb elements. We 'ave learn 8lement 5ocating by ID " 8lement5ocating By Tag ame an# 5ocating element by Class ame &it'e7amples in my previousposts. 5ocating 8lement By ame is very use!ul met'o# an# manypeoples are using t'is met'o# in t'eir &eb#river automation test casepreparation. 5ocating 8lement By ame an# 5ocating 8lement by ID are

Page 96: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 96/292

nearly same. In 5ocating 8lement by ID met'o#" &eb#river &ill loo@ !or t'especi e# ID attribute an# in 5ocating 8lement By ame met'o#"&eb#river &ill loo@ !or t'e speci e# ame attribute.

5et me s'o& you one e7ample o! 'o& to locate element by name an#t'en &e &ill use it in our &eb#river test case.

5oo@ in to above image" &eb element First ame input bo7 #o not 'aveany ID so &eb#river can not locate it By ID but t'at element containsname attribute. So &eb#river can locate it By ame attribute. Synta7 o! locating element in &eb#river is as bello&.

#river. n#8lementABy.nameAK!nameK H

o& let &e use it in one practical e7ample as bello&.Copy bello& given;Test met'o# part an# replace it &it' t'e ;Test met'o# part o! e7amplegiven on t'is page .A ote ;Test met'o# is mar@e# &it' pin@ color in t'at e7ample .

;Test public voi# testA

L #river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H <<5ocating element by ame an# type given te7ts in to input bo7. #river. n#8lementABy.nameAK!nameK .sen#YeysAK+y First ameK H Q

bove e7ample &ill type t'e te7t in te7t bo7 using By ame elementlocating met'o#.

5ocating Web 8lement By Class ame In Selenium WebDriver &it' e7ample

T'ere are many #i=erent &ays o! locating elements in &eb#river !or yourso!t&are &eb application page an# I 'ave #escribe# one o! t'em in my

083I):S )ST &it' e7ample. In &eb#river" &e can t #o any t'ing on &ebpage i! you #on t @no& 'o& to locate an element. s you @no&" &e can

use element s ID to locate t'atspeci c element but suppose i! your element #o not 'ave any ID t'en 'o&

Page 97: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 97/292

&ill you locate t'at element R 5ocating &eb element by class ame is goo#alternative i! your element contains class name. We can locate elementBy Tag ame" By ame" By 5in@ Te7t" By artial 5in@ Te7t" By CSS an# ByV T6 too but &ill loo@ about t'em in my ne7t posts.

6o& to get t'e class name o! element ou can get t'e class name o! element using rebug as s'o&n in bello&given image.

5oo@ in to above image. ost #ate content 'as a class an# &e can use t'atclass name to store t'at blog post #ate string. otice one more t'ing inabove image" blog post #ate string 'as not any ID so &e can not locate itby ID.

A ote ou can vie& more &eb#river tutorials on T6IS 5I Y .

87ample )! 5ocating Web 8lement By Class ameWe can use bello& given synta7 to locate t'at element.

#river. n#8lementABy.class ameAK#ate-'ea#erK H

ractical e7ample o! locating &eb element by class ame is as bello&.pac@age 4unitreportpac@ageH

import 4ava.util.concurrent.Time:nitH

import org.4unit. !terHimport org.4unit.Be!oreHimport org.4unit.TestHimport org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium.Web8lementHimport org.open a.selenium. re!o7.Fire!o7DriverH

public class +ytest1 L<<To open Fire!o7 bro&ser

Page 98: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 98/292

WebDriver #river G ne& Fire!o7DriverA H ;Be!ore

public voi# be!oretestA L

<<To +a7imi?e Bro&ser Win#o& #river.manageA .&in#o&A .ma7imi?eA H

<<To )pen :05 In bro&ser #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1 <11<ne&-test.'tmlK H Q ; !ter

public voi# a!tertestA L #river. uitA H Q

;Test

public voi# testAL

#river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H String #atentime G #river. n#8lementABy.class ameAK#ate-'ea#erK .getTe7tA H<<5ocating element by class ame an# store its te7t tovariable #atentime. System.out.printA#atentime H

Q Q

In above e7ample"WebDriver #river G ne& Fire!o7DriverA &ill open &eb#river s Fire!o7bro&ser instance. 3 I8W T6IS )ST T) Y )W 6)W T) 0: W8BD0I380

T8ST I 9))958 C60)+8 .#river.getA H &ill open targete# :05 in bro&ser.

8lement 5ocators In Selenium 2 or WebDriver - 5ocating 8lement By Tagame

We 'ave learn about 5ocating element by ID an# 5ocating element byClass ame &it' e7amples in my previous posts. 5et me repeat once moret'at i! element contains ID t'en &e can locate element by its ID an# i! element #o not 'ave ID an# contains Class ame t'en &e can locate anelement by Class ame.

o& supposing" &eb element #o not 'ave any ID or Class ame t'en 'o&to locate t'at element in selenium WebDriver R ns&er is t'ere are manyalternatives o! selenium WebDriver element locators an# one o! t'emis 5ocating 8lement By Tag ame.

Page 99: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 99/292

5ocating 8lement By Tag ame is not too muc' popular because in mosto! cases" &e &ill 'ave ot'er alternatives o! element locators. But yes i! t'ere is not any alternative t'en you can use element s D)+ Tag ame tolocate t'at element in &eb#river.

A ote ou can vie& all tutorials o! &eb#river element locators5oo@ in to above image. T'at select bo7 #rop #o&n #o not 'ave any ID orClass ame. o& i! &e &ants to locate t'at element t'en &e can use it sD)+ tag name select to locate t'at element. 8lement locator sysnte7 &illbe as bello&.

#river. n#8lementABy.tag ameAKselectK H

Bello& given e7ample &ill locate #rop#o&n element by it s tag ame tostore its values in variable. Copy bello& given ;Test met'o# part an#replace it &it' t'e ;Test met'o# part o! e7ample given on t'is page .A ote

;Test met'o# is mar@e# &it' pin@ color in t'at e7ample .

;Test public voi# testA

L #river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H <<5ocating element by tag ame an# store its te7t in to variable#rop#o&n. String #rop#o&n G #river. n#8lementABy.tag ameAKselectK .getTe7tA H System.out.printAKDrop #o&n list values are as bello& ZnKP#rop#o&n H Q

6o& To 5ocate 8lement By 5in@ Te7t )r artial 5in@ Te7t In SeleniumWebDriver

In my previous posts &'at &e 'ave learn is &e can 5ocate 8lement By

ame " 5ocate 8lement By ID " 5ocate 8lement By Class ame n# 5ocate8lement By Tag ame to ta@e any action on t'at element. We &ill loo@

Page 100: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 100/292

about #i=erent &eb #river actions an# operations in my upcoming posts.o& let &e

learn t&o more element locators as bello&.

5ocate 8lement By 5in@ Te7tI! your targete# element is lin@ te7t t'en you can use by lin@ te7t elementlocator to locate t'at element. 5oo@ in to bello& given image.

+y Targete# element Is 5in@ te7t Clic@ 6ere an# I &ants to clic@ on It t'enI can use It as bello&.

#river. n#8lementABy.lin@Te7tAKClic@ 6ereK .clic@A H

5ocate 8lement By artial 5in@ Te7tSame as 5in@ te7t" We can locate element by partial lin@ te7t too. In t'atcase &e nee# to use By.partial5in@Te7t at place o! By.lin@Te7t as bello&.

#river. n#8lementABy.partial5in@Te7tAKClic@ K .clic@A H

For locating element by lin@ te7t" &e nee# to use !ull &or# Clic@ 6ere butin case o! locating element by partial lin@ te7t" &e can locate element bypartial &or@ Clic@ too.

5oo@ in to bello& given e7ample. I 'ave use# bot' t'e element locators tolocate lin@s.

Copy bello& given ;Test met'o# part an# replace it &it' t'e ;Testmet'o# part o! e7ample given on t'is page . A ote ;Test met'o# ismar@e# &it' pin@ color in t'at e7ample .

;Test

public voi# testAL

Page 101: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 101/292

#river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H #river. n#8lementABy.lin@Te7tAKClic@ 6ereK .clic@A H<<5ocate element bylin@Te7t an# t'en clic@ on it. WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H &ait.untilA87pecte#Con#[email protected]@Te7tAK1,

K H <<5ocate element by partial [email protected]

Selenium WebDriver By.cssSelector 8lement 5ocators Wit' 87ample

WebDriver support many #i=erent element locating met'o#s an# locatingelement by it s CSS at' is one o! t'e most popular &ay in &eb#river. I! you not &ant to use by i#" name" tag name" class name or lin@ te7t asyour locator t'en you can locate t'at element by it s CSS pat'. 0ea# t'istutorial to learn #i=erent&ays o! &riting CSS pat' o! any element. ou can get CSS pat' o! anyelement using selenium ID8 too by setting CSS as your 5ocator Buil#ers

re!erence an# t'en per!orming some action on t'at element in SeleniumID8 recor#ing mo#e. ou can get CSS pat' o! any element by Firebug too.

o& let &e learn 'o& &e can use By.cssSelector in &eb#river. 5oo@ in tobello& given image.

6ere &e can locate input bo7 First name using bello& given &eb#riversynta7.

#river. n#8lementABy.cssSelectorAKinputMnameG !name NK H

Page 102: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 102/292

Bello& given e7ample &ill locate input bo7 by CSS Selector to type te7t init.Copy bello& given ;Test met'o# part an# replace it &it' t'e ;Testmet'o# part o! e7ample given on t'is page .A ote ;Test met'o# is mar@e# &it' pin@ color in t'at e7ample .

;Testpublic voi# testA

L#river.manageA .timeoutsA .implicitlyWaitA1%"

Time:nit.S8C) DS H

#river. n#8lementABy.cssSelectorAKinputMnameG !name NK .sen#YeysAK+yameK HH<<5ocate element by cssSelector an# t'en type t'e te7t in it.

Q

6o& To 5ocate 8lement By V T6 In Selenium 2<WebDriver Wit' 87ample

We 'ave learnt most o! all t'e &ay o! locating element in &eb#riverli@e 5ocating 8lement By ID " 5ocating 8lement By ame " 5ocating 8lementBy Class ame " 5ocating 8lement By Tag ame " 5ocating 8lement By 5in@

Te7t )r artial 5in@ an# 5ocating 8lement By CSS Selector . )neanot'er met'o# o! locating element in selenium &eb#river is By V T6 o!element. It is most popular an# best &ay to locate element in WebDriver.

6o&ever you can use ot'er &ays too to locate element. ou can &rite Vpat' o! any element by many #i=erent &ays as #escribe#in T'is ost .

Page 103: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 103/292

bove given image s'o&s t'e rebug vie& o! First name te7t bo7. o& letme give you !e& e7amples o! 'o& to &rite synta7 to locate it in &eb#riverusing 7 at' in 2 #i=erent &ays.

1. #river. n#8lementABy.7pat'AK<<inputM;nameG !name NK H2. #river. n#8lementABy.7pat'AK<<inputMcontainsA;name" !name NK H

I 'ave use# 7 at' to locate element in above bot' t'e synta7. We can useanyone !rom above to locate t'at speci c element. 5et &e use it inpractical e7ample as bello&.

Copy bello& given ;Test met'o# part an# replace it &it' t'e ;Testmet'o# part o! e7ample given on t'is page .A ote ;Test met'o# is mar@e# &it' pin@ color in t'at e7ample .

;Test public voi# testA

L #river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H #river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK+y

ameK H<<5ocate element by cssSelector an# t'en type t'e te7t in it. Q

Selenium css locators tutorial &it' e7ample

s you @no&" 5ocators in selenium are main elements an# CSS 5ocator isanot'er alternative o! Vpat' element locator " ID or ame locator or anyot'er element locators in selenium. Full !orm o! CSS is KCasca#ing StyleS'eetsK an# it #e ne t'at 'o& to #isplay 6T+5 elements on &ebpage.Clic@ 'ere to rea# more about CSS. T'ere are !e& a#vantages an# also !e&#isa#vantages o! using CSS element locators atplace o! Vpat' element locators in selenium.

CSS 5ocators +ain #vantage+ain a#vantage o! using CSS locator is - It is muc' more !aster an#simpler t'an t'e Vpat' 5ocators in I8 an# also t'ey are more rea#ablecompare# to Vpat' locators. lso CSS locators are little !aster compare#to Vpat' locators in ot'er bro&sers.

ote ee# goo# &or@ing e7amples on selenium ID8R 3isit t'is lin@ !orgreat tutorials on selenium ID8 .

o& let me come to our main point - 6o& to &rite CSS locator synta7manually !or selenium. I 'ave #erive# couple o! CSS locator synta7 &it'e7ample as bello&. I &ritten CSS locator synta7 !or t'ree elementsASearc'te7t bo7" Select language #rop #o&n an# K9oK button o! &i@ipe#ia&ebsite 'ome page as s'o&n in bello& image.

Page 104: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 104/292

CSS locator 87amples

1. Selenium CSS locator using Tag an# any ttributecssGinputMtypeGsearc'N ZZZZ T'is synta7 &ill n# KinputK tag no#e &'ic'contains KtypeGsearc'K attribute.

cssGinputMi#Gsearc'InputN ZZZZ T'is synta7 &ill n# KinputK tag no#e &'ic'contains Ki#Gsearc'InputK attribute.

cssG!orm inputMi#Gsearc'InputN ZZZZ T'is synta7 &ill n# !orm containingKinputK tag no#e &'ic' contains Ki#Gsearc'InputK attribute.

A ll t'ree CSS pat' e7amples given above &ill locate Searc' te7t bo7.

2. Selenium CSS locator using Tag an# ID attributecssGinput\searc'Input ZZZZ 6ere" \ sign is specially use# !or Ki#K attributeonly. It &ill n# KinputK tag no#e &'ic' contains Ki#Gsearc'InputKattribute. T'is synta7 &ill locate Searc' te7t bo7.

. Selenium CSS locator using Tag an# class attributecssGinput.!ormBtn ZZZZ 6ere" . is specially use# !or KclassK attribute only.It &ill n# KinputK tag no#e &'ic' contains KclassG!ormBtnK attribute. T'issynta7 &ill locate Searc' button Ago .

$. Selenium CSS locator using tag" class" an# any attributecssGinput.!ormBtnMnameGgoN ZZZZ It &ill n# KinputK tag no#e &'ic'contains KclassG!ormBtnK class an# KnameGgoK attribute. T'is synta7 &illlocate Searc' button Ago .

%. Tag an# multiple ttribute CSS locatorcssGinputMtypeGsearc'NMnameGsearc'N ZZZZ It &ill n# KinputK tag no#e&'ic' contains KtypeGsearc'K attribute an# KnameGsearc'K attribute. T'issynta7 &ill locate Searc' te7t bo7.

Page 105: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 105/292

(. CSS 5ocator using Sub-string matc'esAStart" en# an# containing te7t inseleniumcssGinputMi#^G searc' N ZZZZ It &ill n# input no#e &'ic' contains i#attribute starting &it' searc' te7t.A6ere" ̂ #escribes t'e starting te7t .

cssGinputMi#_G c'Input N ZZZZ It &ill n# input no#e &'ic' contains i#attribute starting &it' c'Input te7t. A6ere" _ #escribes t'e en#ing te7t .

cssGinputMi#>G arc'In N ZZZZ It &ill n# input no#e &'ic' contains i#attribute containing arc'In te7t. A6ere" > #escribes t'e containing te7t .

A ll t'ree CSS pat' e7amples given above &ill locate Searc' te7t bo7.

*. CSS 8lement locator synta7 using c'il# SelectorscssG#iv.searc'-containerO!ormO el#setOinputMi#Gsearc'InputN ZZZZ Firstit &ill n# #iv tag &it' Kclass G searc'-containerK an# t'en it &ill !ollo&remaining pat' to locate c'il# no#e. T'is synta7 &ill locate Searc' te7tbo7.

,. CSS 8lement locator synta7 using a#4acent selectorscssGinput P input ZZZZ It &ill locate KinputK no#e &'ere anot'er KinputKno#e is present be!ore it on page.A!or searc' tect bo7 .

cssGinput P select or cssGinput P input P select ZZZZ It &ill locate KselectKno#e" &'ere KinputK no#e is present be!ore it on pageA!or language #rop#o&n .

/. CSS 8lement locator using contains @ey&or#cssGstrong containsAK8nglis'K ZZZZ It &ill loo@s !or t'e element containingte7t K8nglis'K as a value on t'e page.

Day % - WebDriver Basic ction Comman#s Wit' 87ample

Selenium WebDriver Tutorials - Basic ction Comman#s n# )perationsWit' 87amples

I 'ave alrea#y poste# Selenium WebDrier Tutorials posts 'o& to setup &eb#river &it' eclipse an# 0un rst test &it' &eb#river " 'o& to con gure 4unit&it' eclipse to generate &eb#river test report . We 'ave also learn#i=erent met'o#s o! locating elements in &eb#river . ll t'ese t'ings arevery basict'ings an# you nee# to learn all o! t'em be!ore starting your test casecreation in selenium 2. o& &e are rea#y to learn ne7t step o! per!ormingbasic actions in &eb #river &it' 4ava !or your so!t&are &eb application.

Page 106: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 106/292

3I8W ST8 B ST8 T:T)0I 5S ) S858 I:+ W8BD0I380

To#ay I &ants to #escribe you !e& basic &eb#river comman#s to per!ormactions on &eb element o! your &eb page. We can per!orm too manycomman# operations in &eb#river an# &ill loo@ about t'em one by one innear !uture. 0ig't no& I am #escribing you !e& o! t'em !or your @in#in!ormation.

1. Creating e& Instance )! Fire!o7 Driver

WebDriver #river G ne& Fire!o7DriverA Hbove given synta7 &ill create ne& instance o! Fire!o7 #river. 3I8W0 CTIC 5 8V + 58

2. Comman# To )pen :05 In Bro&ser

#river.getAK'ttp <<only-testing-blog.blogspot.in<2 1 <11<ne&-test.'tmlK H T'is synta7 &ill open speci e# :05 in &eb bro&ser. 3I8W 0 CTIC 58V + 58 )F ) 8 :05

. Clic@ing on any element or button o! &ebpage

Page 107: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 107/292

#river. n#8lementABy.i#AKsubmitButtonK .clic@A Hbove given synta7 &ill clic@ on targete# element in &eb#river. 3I8W0 CTIC 5 8V + 58 )F C5ICY ) 858+8 T

$. Store te7t o! targete# element in variable

String #rop#o&n G #river. n#8lementABy.tag ameAKselectK .getTe7tA H T'is synta7 &ill retrieve te7t !rom targete# element an# &ill store it invariable G #rop#o&n. 3I8W 0 CTIC 5 8V + 58 )F 9et Te7t

%. Typing te7t in te7t bo7 or te7t area.#river. n#8lementABy.nameAK!nameK .sen#YeysAK+y First ameK H

bove synta7 &ill type speci e# te7t in targete# element. 3I8W0 CTIC 5 8V + 58 )F Sen#Yeys

(. pplying Implicit &ait in &eb#river#river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H

T'is synta7 &ill !orce &eb#river to &ait !or 1% secon# i! element not !oun#on page. 3I8W 0 CTIC 5 8V + 58 )F I+ 5ICIT W IT

*. pplying 87plicit &ait in &eb#river &it' WebDriver canne# con#itions.

WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H&ait.untilA87pecte#Con#itions.te7tToBe resentIn8lement5ocate#ABy.7pat'AK<<#ivM;i#G time5e!t NK " KTime le!t * secon#sK H

bove 2 synta7 &ill &ait !or till 1% secon#s !or e7pecte# te7t KTime le!t *secon#sK to be appear on targete# element. 3IW8 DIFF808 T 0 CTIC 58V + 58S )F 8V 5ICIT W IT

,. 9et page title in selenium &eb#river

#river.getTitleA HIt &ill retrieve page title an# you can store it in variable to use in ne7tsteps. 3I8W 0 CTIC 5 8V + 58 )F 98T TIT58

/. 9et Current age :05 In Selenium WebDriver

#river.getCurrent:rlA HIt &ill retrieve current page :05 an# you can use it to compare &it' youre7pecte# :05. 3I8W 0 CTIC 5 8V + 58 )F 98T C:008 T :05

1 . 9et #omain name using 4ava script e7ecutor

Javascript87ecutor 4avascript G AJavascript87ecutor #riverHString Current:05:singJSGAString 4avascript.e7ecuteScriptAKreturn#ocument.#omainK H

bove synta7 &ill retrieve your so!t&are application s #omain name using&eb#river s 4ava script e7ecutor inter!ace an# store it in to variable.

Page 108: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 108/292

3I8W 98T D)+ I +8 0 CTIC 5 8V + 58 .

11. 9enerate alert using &eb#river s 4ava script e7ecutor inter!ace

Javascript87ecutor 4avascript G AJavascript87ecutor #riverH 4avascript.e7ecuteScriptAKalertA Test Case 87ecution Is starte# o&.. HK HIt &ill generate alert #uring your selenium &eb#river test case e7ecution.3I8W 0 CTIC 5 8V + 58 )F 98 80 T8 580T :SI 9 S858 I:+W8BD0I380 .

12. Selecting or Deselecting value !rom #rop #o&n in selenium &eb#river.

• Select By 3isible Te7t

Select my#rp#&n G ne& SelectA#river. n#8lementABy.i#AKCarlistK H

my#rp#&n.selectBy3isibleTe7tAK u#iK HIt &ill select value !rom #rop #o&n list using visible te7t value G K u#iK.

• Select By 3alue

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.selectBy3alueAKItalyK HIt &ill select value by value G KItalyK.

• Select By In#e7

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.selectByIn#e7A HIt &ill select value by in#e7G AFirst option .

3I8W 0 CTIC 5 8V + 58S )F S858CTI 9 3 5:8 F0)+ D0) D)W5IST.

• Deselect by 3isible Te7t

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.#eselectBy3isibleTe7tAK0ussiaK HIt &ill #eselect option by visible te7t G 0ussia !rom list bo7.

• Deselect by 3alue

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.#eselectBy3alueAK+e7icoK HIt &ill #eselect option by value G +e7ico !rom list bo7.

Page 109: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 109/292

• Deselect by In#e7

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.#eselectByIn#e7A% HIt &ill #eselect option by In#e7 G % !rom list bo7.

• Deselect ll

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.#eselect llA HIt &ill remove all selections !rom list bo7.

3I8W 0 CTIC 5 8V + 58S )F D8S858CT S 8CIFIC ) TI) F0)+ 5IST

B)V• is+ultipleA

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hboolean value G listbo7.is+ultipleA HIt &ill return true i! select bo7 is multiselect else it &ill return !alse. 3I8W

0 CTIC 5 8V + 58 )F is+ultipleA

1 . avigate to :05 or Bac@ or For&ar# in Selenium Web#river

#river.navigateA .toAK'ttp <<only-testing-blog.blogspot.in<2 1$< 1<te7tbo7.'tmlK H#river.navigateA .bac@A H#river.navigateA .!or&ar#A H1st comman# &ill navigate to speci c :05" 2n# &ill navigate one stepbac@ an# r# comman# &ill navigate one step !or&ar#. 3I8W 0 CTIC 58V + 58S )F 3I9 TI) C)++ DS .

1$. 3eri!y 8lement resent in Selenium WebDriverBoolean iselementpresent G#river. n#8lementsABy.7pat'AK<<inputM;i#G te7t2 NK .si?eA [G HIt &ill return true i! element is present on page" else it &ill return !alse invariable iselementpresent. 3I8W 0 CTIC 5 8V + 58 )F 380IF 858+8 T

08S8 T .

1%. Capturing entire page screens'ot in Selenium WebDriver

File screens'ot GAATa@esScreens'ot #river .getScreens'ot sA)utputType.FI58 HFile:tils.copyFileAscreens'ot" ne& FileAKD ZZscreens'ot.4pgK H

Page 110: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 110/292

It &ill capture page screens'ot an# store it in your D #rive. 3I8W0 CTIC 5 8V + 58 ) T6IS 98 .

1(. 9enerating +ouse 6over 8vent In WebDriver

ctions actions G ne& ctionsA#river HWeb8lement moveonmenu G#river. n#8lementABy.7pat'AK<<#ivM;i#G menu1 N<#ivK Hactions.moveTo8lementAmoveonmenu Hactions.per!ormA H

bove e7ample &ill move mouse on targete# element. 3I8W 0 CTIC 58V + 58 )F +):S8 6)380 .

1*. 6an#ling +ultiple Win#o&s In Selenium WebDriver.

1. 9et ll Win#o& 6an#les.

Set StringO llWin#o&6an#les G #river.getWin#o&6an#lesA H

2. 87tract parent an# c'il# &in#o& 'an#le !rom all &in#o& 'an#les.

String &in#o&1 G AString llWin#o&6an#les.to rrayA M NHString &in#o&2 G AString llWin#o&6an#les.to rrayA M1NH

. :se &in#o& 'an#le to s&itc' !rom one &in#o& to ot'er &in#o&.

#river.s&itc'ToA .&in#o&A&in#o&2 Hbove given steps &it' 'elps you to get &in#o& 'an#le an# t'en 'o& to

s&itc' !rom one &in#o& to anot'er &in#o&. 3I8W 0 CTIC 5 8V + 58 )F6 D5I 9 +:5TI 58 WI D)WS I W8BD0I380

1,. C'ec@ W'et'er 8lement is 8nable# )r Disable# In Selenium Web#river.

boolean !name G#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .is8nable#A HSystem.out.printA!name H

bove synta7 &ill veri!y t'at element Ate7t bo7 !name is enable# or not. ou can use it !or any input element. 3I8W 0 CTIC 5 8V + 58 )F 380IF858+8 T IS 8 B58D )0 )T .

1/. 8nable<Disable Te7tbo7 During Selenium Web#river Test Case87ecution.

Javascript87ecutor 4avascript G AJavascript87ecutor #riverHString to#isable G K#ocument.get8lementsBy ameA !nameM N.set ttributeA #isable# " HKH

4avascript.e7ecuteScriptAto#isable H

String toenable G K#ocument.get8lementsBy ameA lnameM N.remove ttributeA #isable# HKH

Page 111: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 111/292

4avascript.e7ecuteScriptAtoenable HIt &ill #isable !name element using set ttributeA met'o# an# enablelname element using remove ttributeA met'o#. 3I8W 0 CTIC 58V + 58 )F 8 B58<DIS B58 T8VTB)V .

2 . Selenium WebDriver ssertions Wit' Test 9 Frame&or@

• assert8 uals

ssert.assert8 ualsAactual" e7pecte# Hassert8 uals assertion 'elps you to assert actual an# e7pecte# e ualvalues. 3I8W 0 CTIC 5 8V + 58 )F assert8 uals SS80TI)

• assert ot8 uals

ssert.assert ot8 ualsAactual" e7pecte# Hassert ot8 uals assertion is use!ul to assert not e ual values. 3I8W0 CTIC 5 8V + 58 )F assert ot8 uals SS80TI) .

• assertTrue

ssert.assertTrueAcon#ition HassertTrue assertion &or@s !or boolean value true assertion. 3I8W

0 CTIC 5 8V + 58 )F assertTrue SS80TI) .

assertFalsessert.assertFalseAcon#ition H

assertFalse assertion &or@s !or boolean value !alse assertion. 3I8W0 CTIC 5 8V + 58 )F assertFalse SS80TI) .

21. SubmitA met'o# to submit !orm

#river. n#8lementABy.7pat'AK<<inputM;nameG Company NK .submitA HIt &ill submit t'e !orm. 3I8W 0 CTIC 5 8V + 58 )F S:B+IT F)0+ .

22. 6an#ling lert" Con rmation an# rompts opups

String myalert G #river.s&itc'ToA .alertA .getTe7tA H To store alert te7t. 3I8W 0 CTIC 5 8V + 58 )F ST)08 580T T8VT

#river.s&itc'ToA .alertA .acceptA H To accept alert. 3I8W 0 CTIC 5 8V + 58 )F 580T CC8 T

#river.s&itc'ToA .alertA .#ismissA H

Page 112: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 112/292

To #ismiss con rmation. 3I8W 0 CTIC 5 8V + 58 )F C C85C) FI0+ TI)

#river.s&itc'ToA .alertA .sen#YeysAKT'is Is Jo'nK H To type te7t In te7t bo7 o! prompt popup.

elenium WebDriver 6an#ling Javascript lerts" Con rmations n#rompts

lerts" Con rmation an# rompts are very commonly use# elements o! any &ebpage an# you must @no& 'o& to 'an#le all t'ese popups Inselenium &eb#river. I! you @no&" Selenium ID8 'as many comman#s to'an#le alerts" con rmations an# prompt popups li@eassert lert " assertCon rmation " store lert "veri!y lert resent " etc.. First o! all" 5et me s'o& you all t'ree #i=erentpopup types to remove con!usion !rom your min# an# t'en &e &ill see'o& to 'an#le t'em In selenium &eb#river.

lert opup9enerally alert message popup #isplay &it' alert te7t an# )@ button ass'o&n In bello& given Image.

Con rmation opupCon rmation popup #isplays &it' con rmation te7t" )@ an# Cancel buttonas s'o&n In bello& given Image.

rompt opuprompts &ill 'ave prompt te7t" Input te7t bo7" )@ an# Cancel buttons.

Page 113: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 113/292

Selenium &eb#river 'as Its o&n lert Inter!ace to 'an#le all above#i=erent popups. lert Inter!ace 'as #i=erent met'o#s li@e acceptA "#ismissA " getTe7tA " sen#YeysA4ava.lang.String @eysToSen# an# &e canuse all t'ese met'o#s to per!orm #i=erent actions on popups.

3I8W W8BD0I380 8V + 58S ST8 B ST8

5oo@ at t'e bello& given simple e7ample o! 'an#ling alerts" con rmationsan# prompts In selenium &eb#river. Bello& given e7ample &ill per!orm#i=erent actions A5i@e clic@ on )@ button" Clic@ on cancel button" retrievealert te7t" type te7t In prompt te7t bo7 etc.. on all t'ree @in# o! popupsusing #i=erent met'o#s o# lert Inter!ace.

0un bello& given e7ample In eclipse &it' testng an# observe t'e result.

pac@age Testng ac@Himport 4ava.util.concurrent.Time:nitH

import org.open a.selenium. lertHimport org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

public class une7pecte# alert L WebDriver #riverH

;Be!oreTest public voi# setupA t'ro&s 87ception L #river Gne& Fire!o7DriverA H

#river.manageA .&in#o&A .ma7imi?eA H #river.manageA .timeoutsA .implicitlyWaitA%" Time:nit.S8C) DS H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< 1<te7tbo7.'tmlK H Q

; !terTest

Page 114: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 114/292

public voi# tearDo&nA t'ro&s 87ception L #river. uitA H Q

;Test public voi# Te7tA t'ro&s Interrupte#87ception L << lert op up 6an#ling. #river. n#8lementABy.7pat'AK<<inputM;valueG S'o& +e lert NK .clic@A H <<To locate alert. lert 1 G #river.s&itc'ToA .alertA H <<To rea# t'e te7t !rom alert popup. String lert1 G 1.getTe7tA H System.out.printlnA lert1 H T'rea#.sleepA2 H <<To accept<Clic@ )@ on alert popup. 1.acceptA H

<<Con rmation op up 6an#ling. #river. n#8lementABy.7pat'AK<<buttonM;onclic@G myFunctionA NK .clic@A H lert 2 G #river.s&itc'ToA .alertA H String lert2 G 2.getTe7tA H System.out.printlnA lert2 H T'rea#.sleepA2 H <<To clic@ )n cancel button o! con rmation bo7. 2.#ismissA H

<< rompt op up 6an#ling. #river. n#8lementABy.7pat'AK<<buttonMcontainsA." S'o& +erompt NK .clic@A H

lert G #river.s&itc'ToA .alertA H String lert G .getTe7tA H System.out.printlnA lert H <<To type te7t In te7t bo7 o! prompt pop up. .sen#YeysAKT'is Is Jo'nK H T'rea#.sleepA2 H .acceptA HQ

Q

T'is &ay you can 'an#le #i=erent @in# o! alerts very easily using lertInter!ace o! selenium &eb#river. 8VT )ST &ill s'o& you 'o& to 'an#leune7pecte# alerts In selenium &eb#river.

6o& To 6an#le :ne7pecte# lerts In Selenium WebDriver

Some times &'en &e bro&sing so!t&are &eb application" Display someune7pecte# alerts #ue to some error or some ot'er reasons. T'is @in# o! alerts not #isplay every time but t'ey are #isplaying only some time. I!

Page 115: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 115/292

you 'ave create# &eb#river test case !or suc' page an# not 'an#le# t'is@in# o! alerts In your co#e t'enyour script &ill !ail Imme#iately I! suc' une7pecte# alert pop up #isplaye#.

We 'ave alrea#y learnt" 6o& to 'an#le e7pecte# alert popups In selenium&eb#river In T6IS 8V + 58 . o& une7pecte# alert appears only sometimes so &e can not &rite #irect co#e to accept or #ismiss t'at alert. Int'is @in# o! situation" &e 'ave to 'an#le t'em specially.

ou can 3I8W 55 W8BD0I380 T:T)0I 5S ) 8 B ) 8 .

To 'an#le t'is @in# o! une7pecte# alerts" ou must at least a&are about on&'ic' action suc' une7pecte# alert Is generate#. Sometimes" T'ey aregenerate# #uring page loa# an# sometime t'ey are generate# &'en youper!orm some action. So rst o! all &e 'ave to note #o&n t'e action &'eresuc' une7pecte# alert Is generate# an# t'en &e can c'ec@ !or alert a!terper!orming t'at action. We nee# to use try catc' bloc@ !or c'ec@ing suc'une7pecte# alters because I! &e &ill use #irect co#eA&it'out try catc' toaccept or #ismiss alert an# I! alert not appears t'en our test case &ill !ail.try catc' can 'an#le bot' situations.

I 'ave one e7ample &'ere alert Is #isplaying &'en loa#ing page. So &ecan c'ec@ !or alert Insi#e try catc' bloc@ a!ter page loa# as s'o&n Inbello& given e7ample. !ter loa#ing page" It &ill c'ec@ !or alert. I! alert Ist'ere on t'e page t'en It &ill #ismiss It else It &ill go to catc' bloc@ an#print message as s'o&n In bello& given e7ample.

0un bello& given e7ample In eclipse &it' testng an# observe t'e result.

pac@age Testng ac@H

import 4ava.util.concurrent.Time:nitH

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

public class une7pecte# alert L WebDriver #riverH

;Be!oreTest public voi# setupA t'ro&s 87ception L #river Gne& Fire!o7DriverA H

#river.manageA .&in#o&A .ma7imi?eA H

#river.manageA .timeoutsA .implicitlyWaitA%" Time:nit.S8C) DS H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< (<alert (.'tmlK H

Page 116: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 116/292

Q

; !terTest public voi# tearDo&nA t'ro&s 87ception L #river. uitA H Q

;Test public voi# Te7tA t'ro&s Interrupte#87ception L <<To 'an#le une7pecte# alert on page loa#. tryL

#river.s&itc'ToA .alertA .#ismissA HQcatc'A87ception e LSystem.out.printlnAKune7pecte# alert not presentK H

Q

#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK!nameK H #river. n#8lementABy.7pat'AK<<inputM;nameG lname NK .sen#YeysAKlnameK H #river. n#8lementABy.7pat'AK<<inputM;typeG submit NK .clic@A H QQ

bove given &eb#river co#e Is 4ust !or e7ample. It Is 4ust e7plaining t'e&ay o! using try catc' bloc@ to 'an#le une7pecte# alert. ou can use suc'try catc' bloc@ In t'at area &'ere you are !acing une7pecte# alerts very!re uently.

> )pen Fire!o7 Bro&ser 5ocating Web 8lement By Class ame In Selenium WebDriver &it' e7ample

T'ere are many #i=erent &ays o! locating elements in &eb#river !or yourso!t&are &eb application page an# I 'ave #escribe# one o! t'em in my

083I):S )ST &it' e7ample. In &eb#river" &e can t #o any t'ing on &ebpage i! you #on t @no& 'o& to locate an element. s you @no&" &e canuse element s ID to locate t'atspeci c element but suppose i! your element #o not 'ave any ID t'en 'o&&ill you locate t'at element R 5ocating &eb element by class ame is goo#alternative i! your element contains class name. We can locate elementBy Tag ame" By ame" By 5in@ Te7t" By artial 5in@ Te7t" By CSS an# ByV T6 too but &ill loo@ about t'em in my ne7t posts.

6o& to get t'e class name o! element ou can get t'e class name o! element using rebug as s'o&n in bello&given image.

Page 117: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 117/292

5oo@ in to above image. ost #ate content 'as a class an# &e can use t'atclass name to store t'at blog post #ate string. otice one more t'ing in

above image" blog post #ate string 'as not any ID so &e can not locate itby ID.

A ote ou can vie& more &eb#river tutorials on T6IS 5I Y .

87ample )! 5ocating Web 8lement By Class ameWe can use bello& given synta7 to locate t'at element.

#river. n#8lementABy.class ameAK#ate-'ea#erK H

ractical e7ample o! locating &eb element by class ame is as bello&.

pac@age 4unitreportpac@ageH

import 4ava.util.concurrent.Time:nitH

import org.4unit. !terHimport org.4unit.Be!oreHimport org.4unit.TestHimport org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium.Web8lementHimport org.open a.selenium. re!o7.Fire!o7DriverH

public class +ytest1 L<<To open Fire!o7 bro&ser

WebDriver #river G ne& Fire!o7DriverA H ;Be!ore

public voi# be!oretestA L

<<To +a7imi?e Bro&ser Win#o& #river.manageA .&in#o&A .ma7imi?eA H

<<To )pen :05 In bro&ser

Page 118: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 118/292

#river.getAK'ttp <<only-testing-blog.blogspot.in<2 1 <11<ne&-test.'tmlK H Q ; !ter

public voi# a!tertestA L #river. uitA H Q

;Test

public voi# testAL

#river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H String #atentime G #river. n#8lementABy.class ameAK#ate-'ea#erK .getTe7tA H<<5ocating element by class ame an# store its te7t tovariable #atentime. System.out.printA#atentime H Q Q

In above e7ample"WebDriver #river G ne& Fire!o7DriverA &ill open &eb#river s Fire!o7bro&ser instance. 3 I8W T6IS )ST T) Y )W 6)W T) 0: W8BD0I380

T8ST I 9))958 C60)+8 .#river.getA H &ill open targete# :05 in bro&ser.

6o& To 5ocate 8lements By ID In Selenium WebDriver Wit' 87ample

Be!ore using WebDriver" ou must be a&are about #i=erent &ays o! locating an elements in WebDriver. 5ocating an element is essential partin selenium WebDriver because &'en you &ants to ta@e some action onelement Atyping te7t or clic@ing on button " rst you nee# to locate t'atspeci c element to per!orm action.

First o! all I recommen# you to rea# all t'ese selenium ID8 element

locating met'o#s an# t'en rea# t'is article about Selenium WebDriverelement locators to get all locating met'o#s in better &ay.

WebDriver &'ic' is also @no&n as a Selenium 2 'as many #i=erent &ayso! locating element. 5et me e7plain eac' o! t'em &it' e7amples. 6ere Iam e7plaining 'o& to locate element By i# an# &ill #escribe 5ocating Web8lement By Class ame in my e7t ost an# ot'ers in latter posts.

5ocating :I 8lement By IDI! your &ebpage element 'as uni ue an# static ID t'en you can locateyour page element by ID. 5oo@ in to bello& given image. ou can veri!yt'at your &ebelement 'as any i# or not using t'e rebug.

Page 119: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 119/292

In above image" Submit ]uery button 'as uni ue i# G submitButton . Ican use t'at i# to locate button as bello&.

#river. n#8lementABy.i#AKsubmitButtonK H

Bello& given e7ample &ill s'o& you 'o& to locate element by i# an# t'en'o& to clic@ on it. Copy bello& given ;Test met'o# part an# replace it&it' t'e ;Test met'o# part o! e7ample given on t'is page .A ote ;Testmet'o# is mar@e# &it' pin@ color in t'at e7ample .

;Test

public voi# testA L #river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H !or Aint i G H i G2 H iPP L Web8lement btn G #river. n#8lementABy.i#AKsubmitButtonK H<<5ocatingelement by i# i! Abtn.is8nable#A

L <<i! &ebelement s attribute !oun# enable# t'en t'is co#e &ill bee7ecute#. System.out.printAKZnCongr,s... Button is enable# an# &eb#river isclic@ing on it no&K H

<<5ocating button by i# an# t'en clic@ing on it. #river. n#8lementABy.i#AKsubmitButtonK .clic@A H

iG2 H

Q else L <<i! &ebelement s attribute !oun# #isable# t'en t'is co#e &ill be

e7ecute#. System.out.printAKZnSorry but Button is #isable# rig't no&..K H

Page 120: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 120/292

Q try L T'rea#.sleepA% H Q catc' AInterrupte#87ception e L e.printStac@TraceA H Q Q Q > Submitting Form :sing .submitA +et'o# Submitting Form :sing submitA +et'o# )! Selenium WebDriver

ou &ill n# many !orms In any &ebsite li@e Contact :s !orm" e& :ser0egistration Form" In uiry Form" 5ogIn Form etc.. Supposing you aretesting one site &'ere you 'ave to prepare 5ogin !orm submission testcase In selenium &eb#river t'en 'o& &ill you #o ItR Simplest &ay Is

#escribe# In T6IS )ST . I! you &illsee In t'at e7ample post" &e 'ave use# .clic@A met'o# to clic@ on 5oginbutton.

Selenium Web#river 'as one special met'o# to submit any !orm an# t'atmet'o# name Is submitA . submitA met'o# &or@s same as clic@ing onsubmit button.

W8BD0I380 T:T)0I 5 0T 2

W'en to use .clic@A met'o#

ou can use .clic@A met'o# to clic@ on any button. +eans element s typeG KbuttonK or type G KsubmitK" .clic@A met'o# &ill &or@s !or bot'.

W'en to use .submitA met'o#I! you &ill loo@ at rebug vie& !or any !orm s submit button t'en al&aysIt s type &ill be KsubmitK as s'o&n In bello& given Image. In t'is case".submitA met'o# Is very goo# alternative o! .clic@A met'o#.

Final otes 1. I! any !orm 'as submit button &'ic' 'as type G KbuttonK t'en .submitA

met'o# &ill not &[email protected]. I! button Is not Insi#e !ormO tag t'en .submitA met'o# &ill not &or@.

Page 121: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 121/292

o& let us ta@e a loo@ at very simple e7ample &'ere I 'ave use# .submitAmet'o# to submit !orm. In bello& given e7ample" I 'ave not use# .clic@Amet'o# but use# .submitA met'o# &it' company name el#. 0un bello&give e7ample In eclipse &it' testng an# veri!y t'e result.pac@age Testng ac@H

import 4ava.util.concurrent.Time:nitH

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

public class Form Submit L WebDriver #river G ne& Fire!o7DriverA H

;Be!oreTest public voi# setupA t'ro&s 87ception L

#river.manageA .&in#o&A .ma7imi?eA H #river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< %<!orm.'tmlK H

Q; !terTest

public voi# tearDo&nA t'ro&s 87ception L#river. uitA H

Q

;Test public voi# 5ogIn TestA L #river. n#8lementABy.7pat'AK<<inputM;nameG First ame NK .sen#YeysAK+

yF ameK H #river. n#8lementABy.7pat'AK<<inputM;nameG 5ast ame NK .sen#YeysAK+y5 ameK H #river. n#8lementABy.7pat'AK<<inputM;nameG 8mailID NK .sen#YeysAK+y8mail IDK H #river. n#8lementABy.7pat'AK<<inputM;nameG +ob o NK .sen#YeysAK+y+ob o.K H #river. n#8lementABy.7pat'AK<<inputM;nameG Company NK .sen#YeysAK+yComp ameK H

<<To submit !orm.

Page 122: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 122/292

<< ou can use any ot'er Input el# sAFirst ame" 5ast ame etc. 7pat'too In bello& given synta7. #river. n#8lementABy.7pat'AK<<inputM;nameG Company NK .submitA H String alrt G #river.s&itc'ToA .alertA .getTe7tA H #river.s&itc'ToA .alertA .acceptA H System.out.printlnAalrt H QQ

bove e7ample &ill simply submit t'e !orm an# retrieve submission alertto print. So t'is &ay &e can use &eb#river s submit met'o# to submit any!orm. ou can try #i=erent !orm !or your better un#erstan#ing. > Store Te7t )! 8lement 8lement 5ocators In Selenium 2 or WebDriver - 5ocating 8lement By Tag

ame

We 'ave learn about 5ocating element by ID an# 5ocating element byClass ame &it' e7amples in my previous posts. 5et me repeat once moret'at i! element contains ID t'en &e can locate element by its ID an# i! element #o not 'ave ID an# contains Class ame t'en &e can locate anelement by Class ame.

o& supposing" &eb element #o not 'ave any ID or Class ame t'en 'o&to locate t'at element in selenium WebDriver R ns&er is t'ere are manyalternatives o! selenium WebDriver element locators an# one o! t'emis 5ocating 8lement By Tag ame.

5ocating 8lement By Tag ame is not too muc' popular because in mosto! cases" &e &ill 'ave ot'er alternatives o! element locators. But yes i! t'ere is not any alternative t'en you can use element s D)+ Tag ame tolocate t'at element in &eb#river.

A ote ou can vie& all tutorials o! &eb#river element locators5oo@ in to above image. T'at select bo7 #rop #o&n #o not 'ave any ID or

Class ame. o& i! &e &ants to locate t'at element t'en &e can use it sD)+ tag name select to locate t'at element. 8lement locator sysnte7 &ill

Page 123: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 123/292

be as bello&.

#river. n#8lementABy.tag ameAKselectK H

Bello& given e7ample &ill locate #rop#o&n element by it s tag ame to

store its values in variable. Copy bello& given ;Test met'o# part an#replace it &it' t'e ;Test met'o# part o! e7ample given on t'is page .A ote ;Test met'o# is mar@e# &it' pin@ color in t'at e7ample .

;Test public voi# testA

L #river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H <<5ocating element by tag ame an# store its te7t in to variable#rop#o&n. String #rop#o&n G #river. n#8lementABy.tag ameAKselectK .getTe7tA H System.out.printAKDrop #o&n list values are as bello& ZnKP#rop#o&n H Q

> Typing Te7t In To Te7t bo7 Selenium Web#river 8lement 5ocator - 5ocating 8lement By ame Wit'87ample

Selenium WebDriver<Selenium 2 is &eb an# mobile application regressiontesting tool an# it is using element locators to n# out an# per!ormactions on &eb elements. We 'ave learn 8lement 5ocating by ID " 8lement

5ocating By Tag ame an# 5ocating element by Class ame &it'e7amples in my previousposts. 5ocating 8lement By ame is very use!ul met'o# an# manypeoples are using t'is met'o# in t'eir &eb#river automation test casepreparation. 5ocating 8lement By ame an# 5ocating 8lement by ID arenearly same. In 5ocating 8lement by ID met'o#" &eb#river &ill loo@ !or t'especi e# ID attribute an# in 5ocating 8lement By ame met'o#"&eb#river &ill loo@ !or t'e speci e# ame attribute.

5et me s'o& you one e7ample o! 'o& to locate element by name an#t'en &e &ill use it in our &eb#river test case.

Page 124: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 124/292

5oo@ in to above image" &eb element First ame input bo7 #o not 'aveany ID so &eb#river can not locate it By ID but t'at element containsname attribute. So &eb#river can locate it By ame attribute. Synta7 o! locating element in &eb#river is as bello&.

#river. n#8lementABy.nameAK!nameK H

o& let &e use it in one practical e7ample as bello&.Copy bello& given;Test met'o# part an# replace it &it' t'e ;Test met'o# part o! e7amplegiven on t'is page .A ote ;Test met'o# is mar@e# &it' pin@ color in t'at e7ample .

;Test public voi# testA

L #river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H <<5ocating element by ame an# type given te7ts in to input bo7. #river. n#8lementABy.nameAK!nameK .sen#YeysAK+y First ameK H Q

bove e7ample &ill type t'e te7t in te7t bo7 using By ame elementlocating met'o#. > 9et age Title Selenium WebDriver &ait !or title &it' e7ample

WebDriver 'as many Canne# 87pecte# Con#itions by &'ic' &e can !orce

&eb#river to &ait e7plicitly. 6o&ever Implicit &ait is more practical t'ane7plicit &ait in WebDriver. But in some special cases &'ere implicit &ait isnot able to 'an#le your scenario t'en in t'at case you nee# to use e7plicit&aits in your testcases. ou can vie& #i=erent posts on e7plicit &aits &'ere I 'ave#escribe# WebDriver s #i=erent Canne# 87pecte# con#itions &it'e7amples.

I! you 'ave a scenario &'ere you nee# to &ait !or title t'en you canuse titleContainsA4ava.lang.String title &it' &eb#river &ait. ou nee# toprovi#e some part o! your e7pecte# so!t&are &eb application page title&it' t'is con#ition as bello&.

WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H&ait.untilA87pecte#Con#itions.titleContainsAK +yTestK H

In above synta7" K +yTestK is my &eb page s e7pecte# title an# 1%secon#s is ma7 &aiting time to appear title on &eb page. I! title &ill notappears &it'in 1% secon#s #ue to t'e any reason t'en your test case &ill!ails &it' timeout error.

First run bello& given test case in your eclipse an# t'en try same test case!or your o&n so!t&are application.

Page 125: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 125/292

Copy bello& given ;Test met'o# part o! &ait !or title e7ample an# replaceit &it' t'e ;Test met'o# part o! e7ample given on t'ispage . A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e# page .

;Test public voi# test A

L#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK+yameK H

#river. n#8lementABy.7pat'AK<<aMcontainsAte7tA " Clic@ 6ere NK .clic@A H

<<Wait !or page title WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H &ait.untilA87pecte#Con#itions.titleContainsAK +yTestK H

<<9et an# store page title in to variable String title G #river.getTitleA H System.out.printAtitle H Q

In above e7ample" &'en &eb#river &ill clic@ on Clic@ 6ere lin@" anot'erpage &ill open. During page navigation" &eb#river &ill &ait !or e7pecte#page title.

> 9et Current age :05 87ecuting 4avascript in selenium &eb#river to get page title &it' e7ample

Javascript87ecutor is very use!ul Inter!ace in &eb#river. Inter!ace Javascript87ecutor 'elps you to e7ecute 4avascript in your test case&'enever re uire#. I! you @no&s<remember" We 'a# seen Di=erente7amples o! e7ecuting 4avascript in selenium ID8 to per!orm #i=erentactions. 5et me you one e7ample o!e7ecuting 4avascript in selenium WebDriver.

Sometimes in your test case" you nee#s to store your so!t&are &ebapplication page title to compare it &it' e7pecte# page title. In seleniumID8" &e can use K storeTitle K comman# to store it in variable. In&eb#river "&e can #o it #irectly using #river.getTitleA H as s'o&n in t'ise7ample . But i! you &ants to #o it using 4avascript t'en 'o& &ill you #o itR

87ample 9et page title using 4avascript in selenium &eb#river

Copy bello& given ;Test met'o# part o! get page title using 4avascripte7ample an# replace it &it' t'e ;Test met'o# part o! e7ample given

on t'is page . A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e#

Page 126: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 126/292

page .

;Test public voi# test AL Javascript87ecutor 4avascript G AJavascript87ecutor #riverH

<<9et current page title

String pagetitleGAString 4avascript.e7ecuteScriptAKreturn#ocument.titleK H

System.out.printlnAK+y age Title Is KPpagetitle H

<<9et current page :05 String Current:05 G #river.getCurrent:rlA H System.out.printlnAK+y Current :05 Is KPCurrent:05 H Q

A3ie& more Javascript87ecutor e7amples in &eb#river

In above e7ample" I 'ave use# Javascript87ecutor to e7ecute 4ava script inselenium &eb#river. Inner 4avascript &ill return current page title an#store it in variable G pagetitle. T'en e7t statement &ill print it in t'econsole. ou can use t'at variable value to compare &it' your e7pecte#page title i! re uire#.

5ast 2 synta7 &ill get current page :05s an# rint it in console.

> 9et Domain ame Selenium WebDriver - 9et Domain ame :sing Javascript87ecutor

ou nee# current page :05 &'en you 'ave to compare it &it' youre7pecte# :05. ou can #o it using WebDriver basic operation comman##river.getCurrent:rlA H. T'is met'o# &e 'ave seen in my revious ost . I! you remember" &e can use K store5ocation K comman# to get current page:05 in seleniumID8. +ain purpose o! t'is post is to e7plore 'o& to get #omain name in&eb#river. So i! you &ants to store application s #omain name t'en younee# to use Javascript87ecutor in your test case.

5oo@ 'ere" so!t&are application s #omain name is #i=erent t'an t'ecurrent page :05.

I! my current page :05 G 'ttp <<only-testing-blog.blogspot.in<2 1 <11<ne&-test.'tml T'en+y #omain name is G only-testing-blog.blogspot.in

5et me provi#e you simple e7ample o! 'o& to get current page :05 using#river.getCurrent:rlA H an# 'o& to get #omain name using

Javascript87ecutor.

Page 127: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 127/292

87ample 9et current :05 o! page an# #omain name in selenium&eb#riverCopy bello& given ;Test met'o# part o! get current page :05 using

4avascript e7ample an# replace it &it' t'e ;Test met'o# part o! e7amplegiven on T6IS 98 . A ote ;Test met'o# is mar@e# &it' pin@ colorin t'at lin@e# page .

;Test public voi# test ALString Current:05 G #river.getCurrent:rlA H

System.out.printlnAK+y Current :05 Is KPCurrent:05 H

<<9et an# store #omain name in variable Javascript87ecutor 4avascript G AJavascript87ecutor #riverH String Domain:singJSGAString 4avascript.e7ecuteScriptAKreturn#ocument.#omainK H

System.out.printlnAK+y Current :05 Is KPDomain:singJS H Q

A3ie& more Javascript e7amples in selenium &eb#river

In above e7ample" 1st 2 synta7 &ill get an# print current page :05. 5ast synta7 &ill get #omain name using 4avascript an# &ill print it in console.

> 9enerating lert +anually 9enerating lert In Selenium Web#river using Javascript87ecutor

I! you nee# to generate alert #uring your test case e7ecution t'en you canuse 4ava script 87ecutor in selenium &eb#river. Java Script 87ecutor isvery 'elp!ul inter!ace o! selenium &eb#river because using it &e cane7ecute any 4ava script. I t'in@ manual alert generation in &eb#river testis re uire# only in rare case butmain purpose o! t'is post is to e7plore Java Script 87ecutor. ou can vie&more e7amples o! Javascript87ecutor on T6IS 5I Y .

I! you remember" &e can generate alert in selenium ID8 too usingKrunScriptK comman#. T6IS )ST &ill #escribe you 'o& to generate alertin selenium ID8 using K runScript K comman# &it' e7ample.

o& let me #escribe you 'o& to generate alert in selenium &eb#river i! re uire# #uring test case e7ecution o! your so!t&are &eb application.

Copy bello& given ;Test met'o# part o! generate alert using 4avascripte7ample an# replace it &it' t'e ;Test met'o# part o! e7ample givenon T6IS 98 . A ote ;Test met'o# is mar@e# &it' pin@ color in t'atlin@e# page .

Page 128: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 128/292

87ample Javascript87ecutor to generate alert in selenium &eb#river

;Test public voi# test A t'ro&s Interrupte#87ceptionL<<9enerating lert :sing Javascript 87ecutor

Javascript87ecutor 4avascript G AJavascript87ecutor #riverH 4avascript.e7ecuteScriptAKalertA Test Case 87ecution Is starte# o&.. HK H T'rea#.sleepA2 H #river.s&itc'ToA .alertA .acceptA H Q

In above e7ample" Java Script 87ecutor &ill simply e7ecute t'e 4avascriptan# generate alert. ou can your o&n alert te7t in it. Try it in your o&nso!t&are application test i! re uire#.

> Selecting 3alue From Drop#o&n )r 5istbo7

6o& To Select )ption By 3alue or By In#e7 In Selenium WebDriver Wit'87ample

In my earlier post" We 'ave seen e7ample o! 6)W T) S858CT ) TI) B3ISIB58 T8VT !rom #rop #o&n. T'ere are t&o more alternatives to selectoption value !rom #rop #o&n or list bo7 in selenium &eb#river. 1. Selecting)ption By 3alue an# 2. Selecting )ption By In#e7. ou can use any one

!rom t'ese

6o& To Select )ption By 3alue or By In#e7 In Selenium WebDriver Wit'87ample

In my earlier post" We 'ave seen e7ample o! 6)W T) S858CT ) TI) B3ISIB58 T8VT !rom #rop #o&n. T'ere are t&o more alternatives to selectoption value !rom #rop #o&n or list bo7 in selenium &eb#river. 1. Selecting)ption By 3alue an# 2. Selecting )ption By In#e7. ou can use any one!rom t'eset'ree to select value !rom list bo7 o! your so!t&are &eb application page.I! you @no&" &e can use K select K comman# or K a##Selection K comman# inselenium ID8 to select option !rom list bo7.

First o! all let me s'o& you #i=erence bet&een visible te7t" value an#in#e7 o! list bo7 option. Bello& given image &ill s'o& you clear #i=erencebet&een value" visible te7t an# in#e7.

Page 129: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 129/292

1. Select )ption By 3alue In WebDriverSynta7 !or selecting option by value in &eb#river is as bello&. First synta7&ill locate t'e select element !rom page an# 2n# synta7 &ill select t'eoption !rom select bo7 by value G Italy.Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.selectBy3alueAKItalyK H

2. Select )ption By In#e7 In WebDriverSynta7 !or selection option by in#e7 !rom list bo7 is as bello&. It &ill select1st elementAin#e7 G !rom select bo7.Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.selectByIn#e7A H

87ecute bello& given e7ample in your eclipse an# veri!y result. Try to useit in #i=erent applications.

Copy bello& given ;Test met'o# part o! select option by value orin#e7 an# replace it &it' t'e ;Test met'o# part o! e7ample given on T6IS

98 .A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e# page .;Test public voi# test A t'ro&s Interrupte#87ceptionL#river. n#8lementABy.i#AKte7t1K .sen#YeysAK+y First ameK H

<<Selecting value !rom #rop #o&n by value

Page 130: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 130/292

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK H listbo7.selectBy3alueAKItalyK H listbo7.selectBy3alueAK+e7icoK H listbo7.selectBy3alueAKSpainK H

#river. n#8lementABy.7pat'AK<<inputM;valueG -O NK .clic@A H WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H &ait.untilA87pecte#Con#[email protected]#AKte7t2K H

<<Selecting value !rom #rop #o&n by in#e7 listbo7.selectByIn#e7A H listbo7.selectByIn#e7A H #river. n#8lementABy.7pat'AK<<inputM;valueG -O NK .clic@A H T'rea#.sleepA2 H Q

Selenium Web#river tutorial 6o& To Select Drop#o&n 3alue Wit'87ample

In my earlier tutorial o! selenium &eb#river" &e learn !e& basic actioncomman#s o! selenium WebDriver li@e 6)W T) C5ICY ) B:TT) "I+ 5ICIT W IT " 8V 5ICIT W IT etc. T6IS 5I Y &ill s'o& you more #i=erente7amples o! basic action comman#s o! selenium &eb#river. s you@no&" Drop #o&n is also essential element o! any so!t&are &eb

application page. So it is important to @no&t'at 'o& to control or ta@e action on #rop #o&n list li@e selecting speci cvalue !rom #rop #o&n list using selenium &eb#river<selenium 2.

I! you @no&" In selenium ID8 &e can select value !rom #rop #o&n list usingKselect K or Ka##Selection K comman#. o& let me give you e7ample o! 'o&to select value !rom #rop #o&n list in &eb#river.

In bello& given e7ample" First importe# &eb#river [email protected] a.selenium.support.ui.SelectK to get support o! &eb#river classKSelectK.import org.open a.selenium.support.ui.SelectH

o& &e can use KSelectK class to i#enti!y #rop #o&n !rom &eb page by&riting bello& given synta7.

Select my#rp#&n G ne& SelectA#river. n#8lementABy.i#AKCarlistK H

o& &e can select any value !rom selecte# #rop #o&n as bello&.

my#rp#&n.selectBy3isibleTe7tAK u#iK H

Full 87ample to select value !rom #rop #o&n is as bello&.

Page 131: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 131/292

8V + 58

pac@age 4unitreportpac@ageH

import 4ava.util.concurrent.Time:nitH

import org.4unit. !terHimport org.4unit.Be!oreHimport org.4unit.TestHimport org.open a.selenium.ByHimport org.open a.selenium.Javascript87ecutorHimport org.open a.selenium.WebDriverHimport org.open a.selenium.Web8lementHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.open a.selenium.support.ui.87pecte#Con#itionsHimport org.open a.selenium.support.ui.SelectHimport org.open a.selenium.support.ui.WebDriverWaitH

public class +ytesting L WebDriver #river G ne& Fire!o7DriverA H ;Be!ore

public voi# be!oretestA L #river.manageA .&in#o&A .ma7imi?eA H

#river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< 1<te7tbo7.'tmlK H Q ; !ter

public voi# a!tertestA L #river. uitA H Q

;Test

public voi# test A t'ro&s Interrupte#87ception

L#river. n#8lementABy.i#AKte7t1K .sen#YeysAK+y First ameK H

<<Selecting value !rom #rop #o&n using visible te7t Select my#rp#&n G ne& SelectA#river. n#8lementABy.i#AKCarlistK H my#rp#&n.selectBy3isibleTe7tAK u#iK H WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H &ait.untilA87pecte#Con#[email protected]#AKte7t2K H Q Q

Page 132: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 132/292

Same t'ing &e can #o !or list bo7 to select option !rom list bo7 by visiblete7t.

6o& to use implicit &ait in selenium &eb#river an# &'y

T'ere are 2 types o! &aits available in Web#river<Selenium 2. )ne o! t'emis implicit &ait an# anot'er one is e7plicit &ait. Bot' AImplicit &ait an#e7plicit &ait are use!ul !or &aiting in WebDriver. :sing &aits" &e aretelling WebDriver to &ait !or a certain amount o! time be!ore going to ne7tstep. We &ill see about e7plicit&ait in my upcoming posts . In t'is post let me tell you &'y an# 'o& to useimplicit &ait in &eb#river.

W'y ee# Implicit Wait In WebDrivers you @no&s sometimes" some elements ta@es some time to appear on

page &'en bro&ser is loa#ing t'e page. In t'is case" sometime your&eb#river test &ill !ail i! you 'ave not applie# Implicit &ait in your testcase. I! implicit &ait is applie# in your test case t'en &eb#river &ill &ait!or speci e# amount o! time i! targete# element not appears on page. syou @no&" &e can Set #e!ault timeout or use KsetTimeoutK comman# inselenium ID8 &'ic' is same as implicit &ait in &eb#river.

I! you &rite implicit &ait statement in you &eb#river script t'en it &ill beapplie# automatically to all elements o! your test case. I am suggesting

you to use Implicit &ait in your all test script o! so!t&are &eb application&it' 1 to 1% secon#s. In &eb#river" Implicit &ait statement is as bello&.

6o& To Write Implicit Wait In WebDriver

#river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H

bove statement &ill tell &eb#river to &ait !or 1% secon#s i! targete#element not !oun#<not appears on page. 5e &e loo@ at simple e7emple toun#erstan# implicit &ait better.

Copy bello& given ;Test met'o# part an# replace it &it' t'e ;Testmet'o# part o! e7ample given on t'is page . A ote ;Test met'o# ismar@e# &it' pin@ color in t'at e7ample .

;Test public voi# test A

L#river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H

#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK+yameK H

#river. n#8lementABy.7pat'AK<<inputM;nameG name7y? NK H Q

Page 133: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 133/292

In bove &eb#river test case &it' J:nit e7ample" 1st 8lement7pat'AK<<inputM;nameG !name NK &ill be !oun# on page but element

7pat'AK<<inputM;nameG name7y? NK is not t'ere on page. So in t'is case&eb#river &ill &ait !or 1% to locate t'at element on page because &e 'ave&ritten implicit &ait statement in our co#e. t last &eb#river test &ill !ailbecause 7pat'AK<<inputM;nameG name7y? NK is not on t'e page.

6o& to apply &ait in &eb#river till element becomes invisible or 'i##en

s you @no&" WebDriver is so!t&are &eb application regression testingtool an# you &ill !ace many problems #uring your &eb#river test casecreation an# e7ecution. ou must @no&s alternate solution i! one not&or@s !or your test scenario. For e7ample" you 'ave use# I+ 5ICIT W IT inyour test case but i! it isnot su`cient to 'an#le your con#ition o! &aiting till element becomesinvisible !rom page t'en you can use it s alternative &ay o! using 8V 5ICITW IT in your test case.

In my 083I):S )ST " I 'ave #escribe useo! visibility)!8lement5ocate#ABy locator met'o# &it' e7plicit &ait to &aittill element becomes visible Anot 'i##en on page. o& let &e loo@ atopposite si#e. I! you &ants &eb#river to &ait till element becomesinvisible or 'i##en !rom page t'en &e canuse invisibility)!8lement5ocate#ABy locator met'o# &it' &ait con#ition.

Synta7 !or &ait till element invisible !rom page is as bello&.

WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H&ait.untilA87pecte#Con#itions.invisibility)!8lement5ocate#ABy.7pat'AK<<inputM;i#G te7t$ NK H

In above synta7" you can c'ange &eb#river &ait time !rom 1% to 2 " ormore as per your re uirement. lso you can use any )T680 858+8 T5)C TI 9 +8T6)DS e7cept By.7pat'. ractical e7ample o! &eb#river &aittill element becomes invisible is as bello&.

Copy bello& given ;Test met'o# part o! &ait till element invisible an#replace it &it' t'e ;Test met'o# part o! e7ample given on T6IS 98 .A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e# page .

;Test public voi# test A t'ro&s Interrupte#87ception" I)87ceptionL<<Wait !or element invisible

WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H

Page 134: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 134/292

&ait.untilA87pecte#Con#itions.invisibility)!8lement5ocate#ABy.7pat'AK<<inputM;i#G te7t$ NK H

System.out.printAKTe7t bo7 te7t$ is no& invisibleK HQ

ou can 3I8W 8V + 58 )F K&aitFor8lement ot resentK I S858 I:+ID8 i! you are using selenium ID8.

Selenium WebDriver 3eri!y 8lement resent In Selenium WebDriver

Some times you nee# to veri!y t'e presence o! element be!ore ta@ingsome action on so!t&are &eb application page. s you @no&" Selenium ID8'as many built in comman#s to per!orm #i=erent types o! actions on yourso!t&are &eb application page. ou can veri!y presence o! element byusingKveri!y8lement resent K comman# in selenium ID8. lso you can vie&e7ample o! selenium ID8 K veri!y8lement ot resent K comman#. Web#river 'ave not any built in met'o# or inter!ace by &'ic' &e can veri!ypresence o! element on t'e page.

es &e can #o it very easily in WebDriver too using bello& given synta7.Boolean iselementpresent G#river. n#8lementsABy.7pat'AK<<inputM;i#G te7t2 NK .si?eA [G H

We 'ave to use n#8lementsA met'o# !or t'is purpose. bove synta7 &illreturn true i! element is present on page. 8lse it &ill return !alse. ou canput i! con#ition to ta@e action base# on presence o! element.

Bello& given e7ample &ill c'ec@ t'e presence o! #i=erent te7t bo7 onpage. It &ill print message in console base# on presence o! element.

Copy bello& given ;Test met'o# part o! iselementpresent e7ample an#replace it &it' t'e ;Test met'o# part o! e7ample given on T6IS

98 . A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e# page .

;Test public voi# test A t'ro&s Interrupte#87ceptionL!or Aint iG1H i (H iPP

L

<<To veri!y element is present on page or not. String V at' G K<<inputM;i#G te7tKPiPK NKH Boolean iselementpresent G #river. n#8lementsABy.7pat'AV at' .si?eA [G H

i! Aiselementpresent GG true L

Page 135: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 135/292

System.out.printAKZnTargete# Te7tBo7KPiPK Is resent )n T'e ageK H Q else L System.out.printAKZnTargete# Te7t Bo7KPiPK Is ot resent )n T'e

ageK H Q Q Q

> Deselecting 3alue From Drop#o&n )r 5istbo7 :se )! is+ultipleA n# #eselect llA In Selenium WebDriver Wit' 87ample

o& you can easily select or #eselect any speci c option !rom select bo7or #rop #o&n as #escribe# in my earlier )ST 1 " )ST 2 an# )ST . ll

t'ese t'ree posts &ill #escribe you #i=erent alternative &ays o! selectingoptions !rom list bo7 or #rop #o&n. o& let me ta@e you one step a'ea# to#escribe you t'e useo! #eselect llA an# is+ultipleA met'o#s in selenium &eb#river.

#eselect llA +et'o##eselect llA met'o# is use!ul to remove selection !rom all selecte#options o! select bo7. It &ill &or@s &it' multiple select bo7 &'en you nee#to remove all selections. Synta7 !or #eselect llA met'o# is as bello&.Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK H

listbo7.#eselect llA H

is+ultipleA +et'o#is+ultipleA met'o# is use!ul to veri!y i! targete# select bo7 is multipleselect bo7 or not means &e can select multiple options !rom t'at selectbo7 or not. It &ill return boolean Atrue or !alse value. We can use it &it' i! con#ition be!ore &or@ing &it' select bo7. Synta7 is as bello&.Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hboolean value G listbo7.is+ultipleA HSystem.out.printAvalue H

Bello& given e7ample &ill s'o& you use o! is+ultipleA an# #eselect llAmet'o#s very clearly in selenium &eb#river.

;Test public voi# test A t'ro&s Interrupte#87ceptionLSelect listbo7 G ne&

SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK H

<<To veri!y t'at speci c select bo7 supports multiple select or not. i!Alistbo7.is+ultipleA

Page 136: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 136/292

L System.out.printAK+ultiple selections is supporte#K H listbo7.selectBy3isibleTe7tAK:S K H listbo7.selectBy3alueAK0ussiaK H listbo7.selectByIn#e7A% H T'rea#.sleepA H

<<To #eselect all selecte# options. listbo7.#eselect llA H T'rea#.sleepA2 H Q else L System.out.printAK ot supporte# multiple selectionsK H Q Q

In above e7ample" i! con#ition &ill c'ec@ t'at listbo7 is multiple select bo7or notR :se# listbo7.#eselect llA H to remove all selecte# options.

6o& To Deselect )ption By 3isible Te7t )r 3alue )r By In#e7 In SeleniumWebDriver Wit' 87ample

##ing selection or removing selection !rom list bo7 are very commonactions !or list bo7. WebDriver 'as alternate options to select or #eselectvalue !rom list bo7. It is very important to learn all t'ree met'o#s o!

selecting or #eselecting option !rom list bo7 because i! one is not possibleto implement in your test t'en youmust be a&are about alternate option. I alrea#y #escribe# selectionoptions in my earlier posts - Selecting speci c option B 3ISIB58 T8VT " B3 5:8 or B I D8V !rom #rop #o&n or list bo7.

o& let me #escribe you all t'ree #eselect met'o#s to #eselect speci coption !rom list bo7. Do you remember t'at &e can use KremoveSelectionKcomman# to #eselect option !rom list bo7R 3I8W T6IS S858 I:+ ID88V + 58 !or KremoveSelectionK comman#.

1. Deselect By 3isible Te7tI! you &ants to remove selection !rom list bo7 t'en you can use&eb#river s #eselectBy3isibleTe7tA met'o#. Synta7 is as bello&. 1stsynta7 &ill locate t'e select bo7 an# 2n# synta7 &ill remove selection byvisible te7t G 0ussia.Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.#eselectBy3isibleTe7tAK0ussiaK H

2. Deselect By 3alue

ou can also #eselect option !rom list bo7 by value. 2n# synta7 &ill

Page 137: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 137/292

#eselect option by value G +e7ico.

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.#eselectBy3alueAK+e7icoK H

. Deselect By In#e7Bello& given synta7 &ill remove selection by in#e7 G %.

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.#eselectByIn#e7A% H

87ecute bello& given e7ample in eclipse an# observe results #uringe7ecution.

Copy bello& given ;Test met'o# part o! #eselect option by visible te7t orvalue or in#e7 an# replace it &it' t'e ;Test met'o# part o! e7ample givenon T6IS 98 .A ote ;Test met'o# is mar@e# &it' pin@ color in t'atlin@e# page .

;Test public voi# test A t'ro&s Interrupte#87ceptionL

#river. n#8lementABy.i#AKte7t1K .sen#YeysAK+y First ameK H Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK H listbo7.selectBy3isibleTe7tAK:S K H listbo7.selectBy3isibleTe7tAK0ussiaK H T'rea#.sleepA1 H <<To #eselect by visible te7t listbo7.#eselectBy3isibleTe7tAK0ussiaK H T'rea#.sleepA1 H

listbo7.selectBy3alueAKJapanK H listbo7.selectBy3alueAK+e7icoK H T'rea#.sleepA1 H <<To #eselect by value listbo7.#eselectBy3alueAK+e7icoK H T'rea#.sleepA1 H

listbo7.selectByIn#e7A$ H listbo7.selectByIn#e7A% H T'rea#.sleepA1 H

Page 138: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 138/292

<<To #eselect by in#e7 listbo7.#eselectByIn#e7A% H T'rea#.sleepA1 H

#river. n#8lementABy.7pat'AK<<inputM;valueG -O NK .clic@A H T'rea#.sleepA1 H QQ

ou can vie& +)08 &eb#river e7amples on T6IS 5I Y .

Common Function To Compare T&o Double 3alues In Selenium WebDriver

Comparison o! #ouble values Is not muc' more #i=erent t'an comparisono! Integer values as #escribe# In my 083I):S )ST but let me give youe7ample o! same so t'at you can get It better. 5et me tell you one t'ingt'at comparison o! #i=erent #ata types A compare t&o strings " comparet&o Integers" etc.. Ispure 4ava concept an# It 'as not any relation &it' selenium &eb#river. +ymain Intention Is to s'o& you t'at 'o& to 'an#le rest o! test e7ecutionbase# on e7ecution result an# 'o& to mar@ your test !ail In &eb#river testreport &it'out any Interruption In e7ecution.

5et me try to #escribe you 'o& to compare t&o #ouble values &it'e7ample.

s e7plaine# In my previous post" I! you are retrieving value !rom &ebpage t'en It &ill be al&ays In string !ormat because &eb#river s .getTe7tA!unction Is returning It as a string. So my rst tas@ Is to convert It !romstring to #ouble using bello& given synta7.

String actual3alString G #river. n#8lementABy.7pat'AK<<>M;i#G post-bo#y-$2/2$1*,$* ,$/, ,/ N<#ivM1N<table<tbo#y<trM2N<t#M$NK .getTe7tA H<<To convert actual value string to Double value.#ouble actual3al G Double.parseDoubleAactual3alString H

Secon# tas@ Is to create general !unction In CommonFunctions class tocompare t&o #ouble values. 5atter on I &ill use t'is !unction &'eneverre uire# In my test. +ain &or@ o! t'is !unction Is to accept t&o #oublevalues t'en compare t'em an# return true or !alse base# on comparisonresult. See bello& given e7ample.

pac@age Testng ac@H

import org.testng. ssertH

import org.testng.asserts.So!t ssertH

Page 139: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 139/292

public class CommonFunctions L So!t ssert So!t ssert G ne& So!t ssertA H

public boolean compareDouble3alsA#ouble actualDob3al" #ouble

e7pecte#Dob3al L tryL <<I! t'is assertion &ill !ail" It &ill t'ro& e7ception an# catc' bloc@ &ill bee7ecute#. ssert.assert8 ualsAactualDob3al" e7pecte#Dob3al H Qcatc'AT'ro&able t L <<T'is &ill t'ro& so!t assertion to @eep continue your e7ecution evenassertion !ailure. <<:n-comment bello& given 'ar# assertion line an# commnet so!tassertion line I! you &ants to stop test e7ecution on assertion !ailure.

<< ssert.!ailAK ctual 3alue KPactualDob3alPK n# 87pecte# 3alueKPe7pecte#Dob3alPK Do ot +atc'.K H

So!t ssert.!ailAK ctual 3alue KPactualDob3alPK n# 87pecte# 3alueKPe7pecte#Dob3alPK Do ot +atc'.K H

<<I! #ouble values &ill not matc'" return !alse. return !alseH Q <<I! #ouble values matc'" return true. return trueH QQ

o& I can use compareDouble3als !unction In my test &'enever re uire#as #escribe# In bello& given e7ample.

pac@age Testng ac@H

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

public class Common Functions Test e7ten#s CommonFunctionsLWebDriver #riverH ;Be!oreTest

public voi# StartBro&ser av:05A L #river G ne& Fire!o7DriverA H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< 1<te7tbo7.'tmlK H Q

Page 140: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 140/292

; !terTest

public voi# CloseBro&serA L#river. uitA H

Q ;Test public voi# testToCompareDoublesA L

String actual3alString G #river. n#8lementABy.7pat'AK<<>M;i#G post-bo#y-$2/2$1*,$* ,$/, ,/ N<#ivM1N<table<tbo#y<trM2N<t#M$NK .getTe7tA H <<To convert actual value string to Double value. #ouble actual3al G Double.parseDoubleAactual3alString H <<Call compareDouble3als met'o# Insi#e I! con#ition to c'ec@ Doublevalues matc' or not. i!AcompareDouble3alsAactual3al" 2 .( L <<I! values matc'" T'is bloc@ &ill be e7ecute#. System.out.printlnAKWrite ction ta@ing lines In t'is bloc@ &'en 3aluesmatc'.K H QelseL <<I! values not matc'" T'is bloc@ &ill be e7ecute#. System.out.printlnAKWrite ction ta@ing lines In t'is bloc@ &'en 3aluesnot matc'.K H Q

<<To mar@ test !ail In report at t'e en# o! e7ecution I! values not matc'. So!t ssert.assert llA H Q

Qbove test &ill call compareDouble3als !unction an# It &ill return !alse

because actual an# e7pecte# values &ill not matc'. So test &ill be mar@e#as !ail. ou can use 'ar# assertion too Insi#e compareDouble3als !unctionto stop your test on assertion !ailure.

+et'o# To Compare T&o Integer 3alues In Selenium WebDriver Test

We 'ave learnt 'o& to create common !unction to compare t&o strings Inmy 083I):S )ST . Same &ay" +any times you nee# to compare t&oInteger values. n# base# on comparison result" ou can per!orm yourne7t step o! test e7ecution. ou can also stop or continue your teste7ecution on assertion!ailure by using so!t or 'ar# Test 9 assertions. In t'is post" &e &ill see'o& to compare t&o Integer values using common !unction.

+ain Intention o! creating t'is @in# o! common !unctions Is to minimi?eyour co#e si?e. ou 'ave to create It only once an# t'en you can call Itagain an# again &'enever re uire# In your test. n# !or t'at" ou 'ave tocreate common class &'ic' s'oul# be accessible !rom all t'e test classes.

Page 141: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 141/292

I! you are retrieving te7tA&'ic' Is Integer value !rom &eb page using&eb#river s .getTe7tA !unction t'en It &ill be string an# you 'ave toconvert It In Integer value using bello& given synta7 be!ore comparision.

String actual3alString G #river. n#8lementABy.7pat'AK<<>M;i#G post-bo#y-,22,*1,,,/,$2,(1(, N<#ivM1N<table<tbo#y<trM1N<t#M2NK .getTe7tA H<<To convert actual value string to Integer value.int actual3al G Integer.parseIntAactual3alString H

Bello& given e7ample &ill e7plain you 'o& to create common !unction tocompare t&o Integers In your CommonFunctions class.

pac@age Testng ac@H

import org.testng. ssertHimport org.testng.asserts.So!t ssertH

public class CommonFunctions L So!t ssert So!t ssert G ne& So!t ssertA H

public boolean compareInteger3alsAint actualInteger3al" inte7pecte#Integer3al L tryL

<<I! t'is assertion &ill !ail" It &ill t'ro& e7ception an# catc' bloc@ &ill bee7ecute#. ssert.assert8 ualsAactualInteger3al" e7pecte#Integer3al H Qcatc'AT'ro&able t L <<T'is &ill t'ro& so!t assertion to @eep continue your e7ecution evenassertion !ailure. <<:n-comment bello& given 'ar# assertion line an# commnet so!tassertion line I! you &ants to stop test e7ecution on assertion !ailure.

<< ssert.!ailAK ctual 3alue KPactualInteger3alPK n# 87pecte# 3alueKPe7pecte#Integer3alPK Do ot +atc'.K H

So!t ssert.!ailAK ctual 3alue KPactualInteger3alPK n# 87pecte#

3alue KPe7pecte#Integer3alPK Do ot +atc'.K H <<I! Integer values &ill not matc'" return !alse. return !alseH Q <<I! Integer values matc'" return true. return trueH QQ

o& you can call compareInteger3alsA !unction In your test to comparet&o Integer values as s'o&n In bello& given e7ample.

Page 142: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 142/292

pac@age Testng ac@H

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

public class Common Functions Test e7ten#s CommonFunctionsLWebDriver #riverH ;Be!oreTest

public voi# StartBro&ser av:05A L #river G ne& Fire!o7DriverA H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< %<!orm.'tmlK H Q ; !terTest

public voi# CloseBro&serA L#river. uitA H

Q ;Test

public voi# testToCompareIntegersA LString actual3alString G #river. n#8lementABy.7pat'AK<<>M;i#G post-bo#y-,22,*1,,,/,$2,(1(, N<#ivM1N<table<tbo#y<trM1N<t#M2NK .getTe7tA H <<To convert actual value string to Integer value. int actual3al G Integer.parseIntAactual3alString H <<Call compareInteger3als met'o# Insi#e I! con#ition to c'ec@ Integervalues matc' or not. i!AcompareInteger3alsAactual3al" % L <<I! values matc'" T'is bloc@ &ill be e7ecute#. System.out.printlnAKWrite ction ta@ing lines In t'is bloc@ &'en 3aluesmatc'.K H QelseL <<I! values not matc'" T'is bloc@ &ill be e7ecute#. System.out.printlnAKWrite ction ta@ing lines In t'is bloc@ &'en 3aluesnot matc'.K H Q

<<To mar@ test !ail In report at t'e en# o! e7ecution I! values not matc'. So!t ssert.assert llA H QQ

9eneral Function To Comparing T&o Strings In Selenium WebDriver TestCase

Page 143: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 143/292

W'en you &ill use selenium &eb#river to automate real &orl#applications" +any times you 'ave to compare t&o strings li@e page title"pro#uct name or any ot'er string. n# base# on comparison result you'ave to ta@e speci c action. Simple e7ample o! suc' scenario Is" ou arenavigating on any vie& pro#uctpage to a## pro#uct In to bas@et. But be!ore a##ing pro#uct In bas@et" ou&ants to veri!y pro#uct s name to c'ec@ It Is correct pro#uct or not.

+any time you 'ave to compare strings In automating any singleapplication. So W'at you &ill #oR ou &ill &rite same co#e multiple time Inyour #i=erent scriptsR 9eneral &ayAW'ic' I am using Is to create commonmet'o# In base class AW'ic' Is accessible !rom all test classes tocompare t&o strings. T'en &e can call t'at met'o# by passing actual an#e7pecte# string to compare strings &'enever re uire# In out tests.

5et me e7plain you 'o& to #o It.

3I8W +)08 W8BD0I380 T:T)0I 5S ST8 B ST8

Step 1 Create CommonFunctions Class In your pac@age &'ic' can beaccessible by any ot'er class.Step 2 Create met'o# compareStrings &'ic' can accept t&o stringvalues. lso Create ob4ect o! Test 9 So!t ssert class to assert assertionso!tly as s'o&n In bello& given e7ample.

pac@age Testng ac@H

import org.testng. ssertHimport org.testng.asserts.So!t ssertH

public class CommonFunctions L So!t ssert So!t ssert G ne& So!t ssertA H

public boolean compareStringsAString actualStr" String e7pecte#Str L

tryL <<I! t'is assertion &ill !ail" It &ill t'ro& e7ception an# catc' bloc@ &ill bee7ecute#. ssert.assert8 ualsAactualStr" e7pecte#Str H Qcatc'AT'ro&able t L <<T'is &ill t'ro& so!t assertion to @eep continue your e7ecution evenassertion !ailure. <<:se 'ere 'ar# assertion K ssert.!ailAKFailure +essage StringK K I! you&ants to stop your test on assertion !ailure. So!t ssert.!ailAK ctual String KPactualStrPK n# 87pecte# String

KPe7pecte#Str PK Do ot +atc'.K H <<I! Strings &ill not matc'" return !alse. return !alseH Q

Page 144: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 144/292

<<I! Strings matc'" return true. return trueH QQ

Step o& &e can call compareStrings met'o# In our test class&'enever re uire# as s'o&n In bello& given e7ample. We 'ave In'erite#CommonFunctions class to use Its !unction In Common Functions Test.

lso call So!t ssert.assert llA met'o# at last o! your test to mar@ yourtest !ail I! bot' strings not matc'. 0ea# +ore about usage o! Test 9 So!t

ssertion In &eb#river.

pac@age Testng ac@H

import org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

<<:se# In'eritancepublic class Common Functions Test e7ten#s CommonFunctions LWebDriver #riverH;Be!oreTest

public voi# StartBro&ser av:05A L

#river G ne& Fire!o7DriverA H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< %<login.'tmlK H Q ; !terTest

public voi# CloseBro&serA L#river. uitA H

Q ;Test

public voi# testToCompareStringsA LString actualTitle G #river.getTitleA H

<<Call compareStrings met'o# Insi#e I! con#ition to c'ec@ string matc' ornot. i!AcompareStringsAactualTitle" K)nly Testing 5ogInK L <<I! strings matc'" T'is bloc@ &ill be e7ecute#. System.out.printlnAKWrite ction ta@ing lines In t'is bloc@ &'en titlematc'.K H QelseL <<I! strings not matc'" T'is bloc@ &ill be e7ecute#. System.out.printlnAKWrite ction ta@ing lines In t'is bloc@ &'en title notmatc'.K H Q

Page 145: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 145/292

<<To mar@ test !ail In report at t'e en# o! e7ecution I! strings not matc'. So!t ssert.assert llA H QQ

bove given e7ample &ill compare actual an# e7pecte# title strings an#mar@ test !ailure at t'e en# o! test e7ecution I! bot' strings not matc'&it'out any Interruption In e7ecution. o& you can call compareStringsmet'o# any time to compare strings. ou 4ust nee# to provi#e e7pecte#an# actual string values.

ote ou can use 'ar# assertion In your common !unction catc' bloc@ atplace o! so!t assertion I! you &ants to stop test on !ailure. See bello&. I! you &ill use bello& given synta7 In catc' bloc@ t'en It &ill stop teste7ecution on !ailure.

ssert.!ailAK ctual String KPactualStrPK n# 87pecte# StringKPe7pecte#StrPK Do ot +atc'.K H

6o& To 6an#le Dynamic Web Table In Selenium WebDriver

I! &eb table 'as same number o! ro&s an# same number o! cells In eac'ro&s every time you loa# page t'en It Is very easy to 'an#le t'at table s#ata In selenium WebDriver as #escribe# In 6o& To 87tract Table

Data<0ea# Table Data :sing Selenium WebDriver post. o& supposingyour table s ro&s an#columns are increasing<#ecreasing every time you loa#ing page or somero&s 'as more cells an# some ro&s 'as less cells t'en you nee# to putsome e7tra co#e In your &eb#river test case &'ic' can retrieve cell #atabase# on number o! cells In speci c ro&. Consi#er t'e table s'o&n Inbello& given Image.

In t'is table" 0o& number 1" 2 an# $ 'as cells" 0o& number 'as 2 Cellsan# 0o& % 'as 1 cell. In t'is case" ou nee# to #o some e7tra co#e to'an#le t'ese #ynamic cells o! #i=erent ro&s. To #o It" ou nee# to n# outt'e number o! cells o! t'at speci c ro& be!ore retrieving #ata !rom It.

ou can vie& more &eb#river tutorials &it' testng an# 4ava W8BD0I380 T:T)0I 5 ; 0T 1 an# W8BD0I380 T:T)0I 5 ; 0T 2 .

Page 146: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 146/292

Bello& given e7ample &ill rst locate t'e ro& an# t'en It &ill calculate t'ecells !rom t'at ro& an# t'en base# on number o! cells" It &ill retrieve cell#ata In!ormation.

0un bello& given e7ample In your eclipse &it' testng &'ic' Is #esigne#!or above given #ynamic &eb table.pac@age Testng ac@H

import 4ava.util.5istHimport 4ava.util.concurrent.Time:nitH

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium.Web8lementHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

public class #ynamic table L WebDriver #river G ne& Fire!o7DriverA H

;Be!oreTest public voi# setupA t'ro&s 87ception L #river.manageA .&in#o&A .ma7imi?eA H #river.manageA .timeoutsA .implicitlyWaitA%" Time:nit.S8C) DS H

#river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< %<!orm.'tmlK H Q

; !terTest public voi# tearDo&nA t'ro&s 87ception L #river. uitA H Q

;Test public voi# 6an#le Dynamic WebtableA L

<<To locate table. Web8lement mytable G #river. n#8lementABy.7pat'AK.<<>M;i#G post-bo#y-,22,*1,,,/,$2,(1(, N<#ivM1N<table<tbo#yK H <<To locate ro&s o! table. 5ist Web8lementO ro&s table Gmytable. n#8lementsABy.tag ameAKtrK H <<To calculate no o! ro&s In table. int ro&s count G ro&s table.si?eA H

<<5oop &ill e7ecute till t'e last ro& o! table. !or Aint ro&G H ro& ro&s countH ro&PP L <<To locate columnsAcells o! t'at speci c ro&.

Page 147: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 147/292

5ist Web8lementO Columns ro& Gro&s table.getAro& . n#8lementsABy.tag ameAKt#K H <<To calculate no o! columnsAcells In t'at speci c ro&. int columns count G Columns ro&.si?eA H System.out.printlnAK umber o! cells In 0o& KPro&PK areKPcolumns count H

<<5oop &ill e7ecute till t'e last cell o! t'at speci c ro&. !or Aint columnG H column columns countH columnPP L <<To retrieve te7t !rom t'at speci c cell. String celte7t G Columns ro&.getAcolumn .getTe7tA H System.out.printlnAKCell 3alue )! ro& number KPro&PK an# columnnumber KPcolumnPK Is KPcelte7t H Q System.out.printlnAK--------------------------------------------------K H QQ

Q

bove e7ample &ill &or@s !or #ynamic c'anging &eb table too &'erenumber o! ro&s c'anging every time you loa# t'e page or searc' !orsomet'ing.

6o& To 6an#le 4a7 uto Suggest Drop 5ist In Selenium Web#river

W'at Is 4a7 uto Suggest Drop 5istRBe!ore learning about 'o& to 'an#le a4a7 auto suggest #rop list Inselenium &eb#river" 5et me tell you &'at Is a4a7 auto suggest #rop list. I! you 'ave visite# any site &it' searc' bo7 an# t'at searc' bo7 Is s'o&ingauto suggest list &'en you type some t'ing Insi#e It. Simplest e7ample o! a4a7 auto suggest #rop list I#9oogle searc' suggestion. W'en you &ill type somet'ing Insi#e It" It &ills'o& you a list o! suggestions as s'o&n In bello& given Image.

T'is Is only e7ample !or your re!erence. ou &ill n# t'is @in# o! a4a7 #roplists In many sites.

6o& to 'an#le a4a7 #rop list In selenium WebDrivero& supposing you &ants to select some value !rom t'at auto suggest list

or you &ants to store an# print all t'ose suggestions t'en 'o& &ill you #o

ItR. I! you @no&" It Is very 'ar# to 'an#le suc' a4a7 In selenium ID8 . 5et &etry to print all t'ose suggestions In console.

Page 148: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 148/292

6ere" Vpat' pattern Is same !or all a4a7 auto suggest #rop list Items. )nlyc'anging Is 3alue Insi#e trO tag. See bello& 7pat' o! 1st t&o Items o! #rop list.

<<>M;i#G gsr N<table<tbo#y<trM1N<t#M2N<table<tbo#y<trM1N<t#<#iv<table<tbo#y<trM1N<t#M1N<span<<>M;i#G gsr N<table<tbo#y<trM1N<t#M2N<table<tbo#y<trM2N<t#<#iv<table<tbo#y<trM1N<t#M1N<span

So &e &ill use !or loopA s #escribe# In my T6IS )ST to !ee# t'atc'anging values to 7pat' o! #rop list #i=erent Items. )t'er one t'ing &enee# to consi#er Is &e #on t @no& 'o& many Items It &ill s'o& In a4a7#rop list. Some @ey&or#s s'o& you $ Items an# some ot'er &ill s'o& youmore t'an $ Items In #rop list. To 'an#le t'is #ynamic c'anging list" We&ill :S8 T0 C TC6 B5)CY to catc' t'e

oSuc'8lement87ception e7ception i! not !oun# ne7t Item In a4a7 #roplist.

In bello& given e7ample" &e 'ave use# testng ;Data rovi#er annotation.I! you @no&" ;Data rovi#er annotation Is use!ul !or #ata #riven testing.

T6IS 5I Y8D )ST &ill #escribe you t'e usage o! ;Data rovi#erannotation &it' #etaile# e7ample an# e7planation.

0un bello& given e7ample In your eclipse &it' testng an# see 'o& It Isretrieving values !rom a4a7 #rop list an# print t'em In console.

pac@age Testng ac@H

import 4ava.util.concurrent.Time:nitH

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.Data rovi#erHimport org.testng.annotations.TestH

public class 4a7 6an#le L

WebDriver #river G ne& Fire!o7DriverA H

;Be!oreTest public voi# setupA t'ro&s 87ception L

#river.manageA .&in#o&A .ma7imi?eA H #river.manageA .timeoutsA .implicitlyWaitA%" Time:nit.S8C) DS H #river.getAK'ttp <<&&&.google.comK H

Q

Page 149: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 149/292

; !terTestpublic voi# tearDo&nA t'ro&s 87ception L

#river. uitA H Q

<<Data provi#er Is use# !or supplying 2 #i=erent values toSearc' Test met'o#.

;Data rovi#erAnameGKsearc'-#ataKpublic )b4ectMNMN #ata rovi#erTestA L return ne& )b4ectMNMNLLKselenium &eb#river tutorialKQ"LKauto

sKQQHQ ;TestA#ata rovi#erGKsearc'-#ataKpublic voi# Searc' TestAString Searc' L

#river. n#8lementABy.7pat'AK<<inputM;i#G gb ! NK .clearA H

#river. n#8lementABy.7pat'AK<<inputM;i#G gb ! NK .sen#YeysASearc' H

int iG1H int 4GiP1H

tryL<<!or loop &ill run till t'e

oSuc'8lement87ception e7ception.!orAiG1H i 4HiPP

L<<3alue o! variable i Is use# !or

creating 7pat' o! #rop list s #i=erent elements.String suggestion G

#river. n#8lementABy.7pat'AK<<>M;i#G gsr N<table<tbo#y<trM1N<t#M2N<table<tbo#y<trMKPiPKN<t#<#iv<table<tbo#y<tr<t#M1N<spanK .getTe7tA H

System.out.printlnAsuggestion H 4PPHQ

Qcatc'A87ception e L <<Catc' bloc@ &ill catc'an# 'an#le t'e e7ception.

System.out.printlnAK>>> lease searc' !oranot'er &or#>>>K H System.out.printlnA H Q

QQ

T'is &ay you can also select speci c !rom t'at suggestion !or yoursearc'ing purpose. It Is very simple In selenium &eb#river. ou can useot'er or more searc' values In above e7ample. To #o It simply mo#i!y;Data rovi#er annotation met'o#.

Page 150: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 150/292

Submitting Form :sing submitA +et'o# )! Selenium WebDriver

ou &ill n# many !orms In any &ebsite li@e Contact :s !orm" e& :ser0egistration Form" In uiry Form" 5ogIn Form etc.. Supposing you aretesting one site &'ere you 'ave to prepare 5ogin !orm submission testcase In selenium &eb#river t'en 'o& &ill you #o ItR Simplest &ay Is#escribe# In T6IS )ST . I! you &illsee In t'at e7ample post" &e 'ave use# .clic@A met'o# to clic@ on 5oginbutton.

Selenium Web#river 'as one special met'o# to submit any !orm an# t'atmet'o# name Is submitA . submitA met'o# &or@s same as clic@ing onsubmit button.

W8BD0I380 T:T)0I 5 0T 2

W'en to use .clic@A met'o# ou can use .clic@A met'o# to clic@ on any button. +eans element s typeG KbuttonK or type G KsubmitK" .clic@A met'o# &ill &or@s !or bot'.

W'en to use .submitA met'o#I! you &ill loo@ at rebug vie& !or any !orm s submit button t'en al&aysIt s type &ill be KsubmitK as s'o&n In bello& given Image. In t'is case".submitA met'o# Is very goo# alternative o! .clic@A met'o#.

Final otes 1. I! any !orm 'as submit button &'ic' 'as type G KbuttonK t'en .submitAmet'o# &ill not &[email protected]. I! button Is not Insi#e !ormO tag t'en .submitA met'o# &ill not &or@.

o& let us ta@e a loo@ at very simple e7ample &'ere I 'ave use# .submitAmet'o# to submit !orm. In bello& given e7ample" I 'ave not use# .clic@Amet'o# but use# .submitA met'o# &it' company name el#. 0un bello&give e7ample In eclipse &it' testng an# veri!y t'e result.pac@age Testng ac@H

import 4ava.util.concurrent.Time:nitH

Page 151: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 151/292

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

public class Form Submit L WebDriver #river G ne& Fire!o7DriverA H

;Be!oreTest public voi# setupA t'ro&s 87ception L

#river.manageA .&in#o&A .ma7imi?eA H #river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< %<!orm.'tmlK H

Q

; !terTest public voi# tearDo&nA t'ro&s 87ception L

#river. uitA H Q

;Test public voi# 5ogIn TestA L #river. n#8lementABy.7pat'AK<<inputM;nameG First ame NK .sen#YeysAK+yF ameK H #river. n#8lementABy.7pat'AK<<inputM;nameG 5ast ame NK .sen#YeysAK+y5 ameK H #river. n#8lementABy.7pat'AK<<inputM;nameG 8mailID NK .sen#YeysAK+y8mail IDK H #river. n#8lementABy.7pat'AK<<inputM;nameG +ob o NK .sen#YeysAK+y+ob o.K H

#river. n#8lementABy.7pat'AK<<inputM;nameG Company NK .sen#YeysAK+yComp ameK H <<To submit !orm. << ou can use any ot'er Input el# sAFirst ame" 5ast ame etc. 7pat'too In bello& given synta7. #river. n#8lementABy.7pat'AK<<inputM;nameG Company NK .submitA H String alrt G #river.s&itc'ToA .alertA .getTe7tA H #river.s&itc'ToA .alertA .acceptA H System.out.printlnAalrt H QQ

Page 152: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 152/292

bove e7ample &ill simply submit t'e !orm an# retrieve submission alertto print. So t'is &ay &e can use &eb#river s submit met'o# to submit any!orm. ou can try #i=erent !orm !or your better un#erstan#ing.

6o& To 87tract Table Data<0ea# Table Data :sing Selenium WebDriver87ample

Table Is very !re uently use# element In &eb pages. +any times you nee#to e7tract your &eb table #ata to compare an# veri!y as per your testcase. ou can rea# about 6o& To 87tracting ll 5in@s From age I! younee# It In your test scenarios. I! you @no&s" Selenium ID8 'as also manycomman#s relate# to Tablean# you can vie& all o! t'em on 5I Y 1 an# 5I Y 2 &it' e7amples. o& letme come to our current #iscussion point about 'o& to rea# values !romtable. Be!ore rea#ing values !rom &eb table" ou must @no& t'ere are 'o&many ro&s an# 'o& many columns In your table. 5et &e try to get numbero! ro&s an# columns !rom table.

S858 I:+ W8BD0I380 ST8 B ST8 T:T)0I 5S .

6o& To 9et umber )! 0o&s :sing Selenium WebDriver

5oo@ at t'e rebug vie& o! above 6T+5 table. In any &eb table" umbero! trO tags Insi#e tbo#yO tag #escribes t'e number o! ro&s o! table. Soyou nee# to calculate t'at t'ere are 'o& many trO tags Insi#e tbo#yOtag. To get ro& count !or above given e7ample table" &e can use&eb#river statement li@e bello&.

int 0o& count G #river. n#8lementsABy.7pat'AK<<>M;i#G post-bo#y-(%22,% /,1/ *% $/ N<#ivM1N< table<tbo#y<tr K .si?eA H

In above synta7" .si?eA met'o# &it' &eb#river s n#8lements met'o# &illretrieve t'e count o! trO tags an# store It In 0o& count variable.

Page 153: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 153/292

6o& To 9et umber )! Columns :sing Selenium WebDriver

Same &ay In any &eb table" umber o! t#O tags !rom any trO tag#escribes t'e number o! columns o! table as s'o&n In bello& given Image.

So !or getting number o! columns" &e can use synta7 li@e bello&.

int Col count G #river. n#8lementsABy.7pat'AK<<>M;i#G post-bo#y-(%22,% /,1/ *% $/ N<#ivM1N<table< tbo#y<trM1N<t#K .si?eA H

o& &e 'ave ro& count an# column count. )ne more t'ing you re uire torea# #ata !rom table Is generating Vpat' !or eac' cell o! column. 5oo@ atbello& given 7pat' synta7 o! 1st cell s 1st an# 2n# ro&s.

<<>M;i#G post-bo#y-(%22,% /,1/ *% $/ N<#ivM1N<table<tbo#y< trM1N<t#M1N<<>M;i#G post-bo#y-(%22,% /,1/ *% $/ N<#ivM1N<table<tbo#y< trM2N<t#M1N

6ig'lig'te# string Is common !or bot' cell s 7pat'. )nly c'anging Is ro&number Insi#e tr no#e. So 'ere &e can pass 0o& count variable svalue Insi#e tr no#e.

Same &ay" loo@ at bello& given 7pat' synta7 o! 1st an# 2n# cells o! 1stro&. 6ig'lig'te# string Is common !or bot' cell s 7pat'. )nly c'anging Iscolumn number Insi#e t# no#e. So 'ere &e can pass Col count variable svalue Insi#e t# no#e.

<<>M;i#G post-bo#y-(%22,% /,1/ *% $/ N<#ivM1N<table<tbo#y<trM1N< t#M1N<<>M;i#G post-bo#y-(%22,% /,1/ *% $/ N<#ivM1N<table<tbo#y<trM1N< t#M2N

ll above t'ing Is applie# In bello& given e7ample. Simply run It In your

eclipse using testng an# observe output In console.pac@age Testng ac@H

Page 154: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 154/292

import 4ava.util.concurrent.Time:nitH

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

public class table L

WebDriver #river G ne& Fire!o7DriverA H ;Be!oreTest public voi# setupA t'ro&s 87ception L

#river.manageA .&in#o&A .ma7imi?eA H #river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1 < /<test.'tmlK H

Q

; !terTest public voi# tearDo&nA t'ro&s 87ception L

#river. uitA H Q

;Test

public voi# print #ataA L <<9et number o! ro&s In table.

int 0o& count G #river. n#8lementsABy.7pat'AK<<>M;i#G post-bo#y-(%22,% /,1/ *% $/ N<#ivM1N<table<tbo#y<trK .si?eA H System.out.printlnAK umber )! 0o&s G KP0o& count H <<9et number o! columns In table.

int Col count G #river. n#8lementsABy.7pat'AK<<>M;i#G post-bo#y-(%22,% /,1/ *% $/ N<#ivM1N<table<tbo#y<trM1N<t#K .si?eA H System.out.printlnAK umber )! Columns G KPCol count H

<<#ivi#e# 7pat' In t'ree parts to pass 0o& count an# Col count values. String rst part G K<<>M;i#G post-bo#y-(%22,% /,1/ *% $/ N<#ivM1N<table<tbo#y<trMKH String secon# part G KN<t#MKH String t'ir# part G KNKH <<:se# !or loop !or number o! ro&s.

!or Aint iG1H i G0o& countH iPP L <<:se# !or loop !or number o! columns. !orAint 4G1H 4 GCol countH 4PP L << repare# nal 7pat' o! speci c cell as per values o! i an# 4. String nal 7pat' G rst partPiPsecon# partP4Pt'ir# partH

Page 155: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 155/292

<<Will retrieve value !rom locate# cell an# print It. String Table #ata G #river. n#8lementABy.7pat'A nal 7pat' .getTe7tA H System.out.printATable #ata PK K H

Q System.out.printlnAKK H System.out.printlnAKK HQQ

Q

Console output o! above given e7ample &ill loo@s li@e bello&.

umber )! 0o&s G umber )! Columns G (

11 12 1 1$ 1% 1(

21 22 2 2$ 2% 2(

1 2 $ % (6ere you can see" all t'e values o! table are e7tracte# an# printe#.

Selenium WebDriver 87tracting ll Te7t Fiel#s From Web age

Sometimes you nee# to e7tract speci c types o! &eb elements !rom pageli@e e7tract all 5in@s to open all o! t'em one by one" e7tract all te7t bo7es

!rom page to type some te7t In all o! t'em or In some o! t'em one by one.reviously &e 'ave learnt 'o& to e7tract all lin@s !rom page In T6IS )ST .o& supposing you 'ave

a scenario &'ere you nee#s to e7tract all te7t el#s !rom page to sen#some te7t In all o! t'em 'o& to #o It In selenium WebDriverR

ou can 3I8W +)08 T:T)0I 5S ) S858 I:+ W8BD0I380 .

WebDriver s In built n#elements met'o# &ill 'elp us to n# all te7t bo7es!rom page. I! you @no&" 8ac' simple te7t bo7 are Input el#s an# &ill 'aveal&ays attribute type G te7t an# I! It Is pass&or# te7t bo7 t'en It s type&ill be pass&or# as s'o&n In bello& given Image.

Page 156: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 156/292

In s'ort &e 'ave to n# all t'ose Input el#s &'ere type G Kte7tK orKpass&or#K. In bello& given e7ample" I 'ave use# n#elements met'o# to

n# an# store all t'ose elements In t7t el#s array list an# t'en use# !orloop to type some te7t In all o! t'em.

pac@age Testng ac@H

import 4ava.util.5istH

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium.Web8lementHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

public class 87tract Te7t L WebDriver #riverH ;Be!oreTest

public voi# StartBro&serA L #river G ne& Fire!o7DriverA HQ

;Test

public voi# Te7tA t'ro&s Interrupte#87ceptionL #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< %<login.'tmlK H << n# all input el#s &'ere type G te7t or pass&or# an# store t'em Inarray list t7t el#s.

Page 157: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 157/292

5ist Web8lementO t7t el#s G#river. n#8lementsABy.7pat'AK<<inputM;typeG te7t or;typeG pass&or# NK H

<<!or loop to sen# te7t In all te7t bo7 one by one. !orAint aG H a t7t el#s.si?eA HaPP L

t7t el#s.getAa .sen#YeysAKTe7tKPAaP1 HQ

T'rea#.sleepA H Q ; !terTest

public voi# CloseBro&serA L#river. uitA H

QQ

T'is &ay &e can e7tract all te7t bo7 !rom page I! re uire# In any scenario.

6o& To Do&nloa# Di=erent Files :sing Selenium WebDriver

In my 083I):S )ST " &e 'ave learnt about 'o& to create create an# usecustom pro le Fire!o7 bro&ser to use It In selenium &eb#river. o& let mes'o& you 'o& to create Fire!o7 custom pro le run time an# set Itsproperties to #o&nloa# any le using selenium &eb#river. +any times you

nee# to #o&nloa##i=erent les !rom sites li@e +S 87cel le" +S Wor# File" Xip le" DF le"CS3 le" Te7t le" ect..

It Is tric@y &ay to #o&nloa# le using selenium &eb#river. +anually &'enyou clic@ on lin@ to #o&nloa# le" It &ill s'o& you #ialogue to save le Inyour local #rive as s'o&n In bello& given Image.

Page 158: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 158/292

o& selenium &eb#river #o not 'ave any !eature to 'an#le t'is save le#ialogue. But yes" Selenium &eb#river 'as one more very goo# !eature by&'ic' you #o not nee# to 'an#le t'at #ialogue an# you can #o&nloa# any

le very easily. We can #o It using &eb#river s Inbuilt class Fire!o7 ro lean# Its #i=erent met'o#s. Be!ore loo@ing at e7ample o! #o&nloa#ing le"5et me #escribe you some t'ing about le s +I+8 types. es you must@no& +I+8 type o! le &'ic' you &ants to #o&nloa# using selenium&eb#river.

W'at Is +I+8 o! File+I+8 Is !ull !orm o! +ulti-purpose Internet +ail 87tensions &'ic' Is use!ulto I#enti!y le type by bro&ser or server to trans!er online.

ou nee# to provi#e +I+8 type o! le In your selenium &eb#river test sot'at you must be a&are about It. T'ere are many online tools available to@no& +I+8 type o! any le. Just google &it' K+I+8 c'ec@erK to n# t'is@in# o! tools.

In our e7ample given bello&" I 'ave use# +I+8 types as s'o&n bello& !or#i=erent le types In bello& given selenium &eb#river test.

1. Te7t File A.t7t - te7t<plain

2. DF File A.p#! - application<p#!

. CS3 File A.csv - te7t<csv

$. +S 87cel File A.7ls7 - application<vn#.open7ml!ormats-o`ce#ocument.sprea#s'eetml.s'eet

%. +S &or# File A.#oc7 - application<vn#.open7ml!ormats-o`ce#ocument.&or#processingml.#ocument

87ample )! #o&nloa#ing #i=erent les using selenium &eb#river

pac@age Testng ac@H

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.open a.selenium. re!o7.Fire!o7 ro leHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

public class #o&nloa#ing le L WebDriver #riverH

Page 159: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 159/292

;Be!oreTest

public voi# StartBro&serA L <<Create ob4ect o! Fire!o7 ro le in built class to access Its properties. Fire!o7 ro le !pro le G ne& Fire!o7 ro leA H <<Set 5ocation to store les a!ter #o&nloa#ing. !pro le.set re!erenceAKbro&ser.#o&nloa#.#irK"KD ZZWebDriver#o&nloa#sK H !pro le.set re!erenceAKbro&ser.#o&nloa#.!ol#er5istK" 2 H <<Set re!erence to not s'o& le #o&nloa# con rmation #ialogue using+I+8 types )! #i=erent le e7tension types. !pro le.set re!erenceAKbro&ser.'elper pps.never [email protected]@K"

Kapplication<vn#.open7ml!ormats-o`ce#ocument.sprea#s'eetml.s'eetHK <<+I+8 types )! +S 87cel File. P Kapplication<p#!HK <<+I+8 types )! DF File. P Kapplication<vn#.open7ml!ormats-o`ce#ocument.&or#processingml.#ocumentHK <<+I+8 types )! +S #ocFile. P Kte7t<plainHK <<+I+8 types )! te7t File. P Kte7t<csvK H <<+I+8 types )! CS3 File. !pro le.set re!erenceA Kbro&ser.#o&nloa#.manager.s'o&W'enStartingK"!alse H !pro le.set re!erenceA Kp#!4s.#isable#K" true H << ass !pro le parameter In &eb#river to use pre!erences to #o&nloa#

le. #river G ne& Fire!o7DriverA!pro le H

Q;Test

public voi# )pen:05A t'ro&s Interrupte#87ceptionL #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< %<login.'tmlK H <<Do&nloa# Te7t File #river. n#8lementABy.7pat'AK<<aMcontainsA." Do&nloa# Te7tFile NK .clic@A H T'rea#.sleepA% H <<To &ait till le gets #o&nloa#e#. <<Do&nloa# DF File #river. n#8lementABy.7pat'AK<<aMcontainsA." Do&nloa# DFFile NK .clic@A H T'rea#.sleepA% H <<Do&nloa# CS3 File #river. n#8lementABy.7pat'AK<<aMcontainsA." Do&nloa# CS3File NK .clic@A H T'rea#.sleepA% H <<Do&nloa# 87cel File #river. n#8lementABy.7pat'AK<<aMcontainsA." Do&nloa# 87celFile NK .clic@A H T'rea#.sleepA% H <<Do&nloa# Doc File

Page 160: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 160/292

#river. n#8lementABy.7pat'AK<<aMcontainsA." Do&nloa# DocFile NK .clic@A H T'rea#.sleepA% HQ

; !terTest

public voi# CloseBro&serA L#river. uitA H

QQ

W'en you run above e7ample" It &ill #o&nloa# all %ATe7t" p#!" CS3" #oc7an# 7ls7 les one by one an# store t'em In D ZWebDriver#o&nloa#s!ol#er automatically as s'o&n In bello& given e7ample.

Interrupte#87ception Is use# &it' met'o# )pen:05 to 'an#le c'ec@e#e7ception o! T'rea#.sleepA% . 3ie& #etaile# tutorials o! e7ception'an#ling In selenium WebDriver on T6IS 5I Y .

T'is &ay you can #o&nloa# any le using selenium &eb#river li@e ?ip le"

e7e le" etc.. Just @no& your le s +I+8 type an# #o&nloa# It as s'o&n Inabove e7ample.

6o& To 6an#le 87ception In Java Tutorials For Selenium WebDriver

nyone #on t li@e e7ceptions an# at t'e same time" anyone can not 'i#e'imsel! !rom e7ceptions &'en running &eb#river tests because AW'at Is

n 87ceptionR -O e7ception Is an error event generate# #uring t'ee7ecution o! &eb#river test case or 4ava program &'ic' #isrupts teste7ecution In bet&een. 87ceptioncan arise #ue to t'e many reasons li@e" net&or@ connection or 'ar#&are!ailure" Invali# #ata entere# by user" DB server #o&n etc.. So all t'ese

Page 161: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 161/292

t'ings can 'appens any time &'en you run your selenium &eb#river testcase an# &e can not stop It. So Instea# o! t'in@ing about stoppinge7ceptionAW'ic' Is not possible #uring run time" T'in@ about 'an#linge7ceptions. Java provi#es very goo# e7ception 'an#ling mec'anism torecover !rom t'is @in# o! errors. 5et us learn #i=erent &ays o! 'an#linge7ception In 4ava.

T'ere are t&o types o! e7ceptions In 4ava as bello&.1. C'ec@e# 87ception C'ec@e# e7ceptions are t'ose e7ceptions &'ic' are c'ec@e# #uringcompile time an# nee#s catc' bloc@ to caug't t'at e7ception #uringcompilation. I! compiler &ill not n# catc' bloc@ t'en It &ill t'ro&compilation error. 3ery simple e7ample o! c'ec@e# e7ception Is using

T'rea#.sleepA% H statement In your co#e. I! you &ill not put t'isstatement Insi#e try catc' bloc@ t'en It &ill not allo& you to compile yourprogram.

2. :nc'ec@e# 87ceptions :nc'ec@e# e7ception are t'ose e7ception &'ic' are not c'ec@e# #uringcompile time. 9enerally c'ec@e# e7ceptions occurre# #ue to t'e error Inco#e #uring run time. Simplest e7ample o! unc'ec@e# e7ception Is int i G$< H. T'is statement &ill t'ro&s < by ?ero e7ception #uring run time.

6an#ling e7ceptions using try-catc' bloc@We nee# to place try catc' bloc@ aroun# t'at co#e &'ic' mig't generatee7ception. In bello& given e7ample" System.out.printlnAaM/N H Is &ritten

Intentionally to generate an e7ception. I! you see" t'at statement Is&ritten Insi#e try bloc@ so I! t'at statement t'ro& an e7ception - catc'bloc@ can caug't an# 'an#le It.

public class 6an#le e7ce L

public static voi# mainAStringMN args L int aMN G L "1"(QH try L <<I! any e7ception arise Insi#e t'is try bloc@" Control &ill goes tocatc' bloc@. System.out.printlnAKBe!ore 87ceptionK H

<<unc'ec@e# e7ceptionSystem.out.printlnAaM/N H <<87ception &ill arise 'ere

because &e 'ave only values In array. System.out.printlnAK !ter 87ceptionK H Qcatc'A87ception e L System.out.printlnAK87ception Is KPe H

Q System.out.printlnAK)utsi#e T'e try catc'.K H Q

Q

Page 162: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 162/292

I! you &ill run above e7ample In your eclipse" try bloc@ &ill bee7ecute#. KBe!ore 87ceptionK statement &ill be printe# In console an#

e7t statement &ill generate an e7ception so K !ter 87ceptionK statement&ill be not printe# In console an# control &ill goes to catc' bloc@ to 'an#let'at e7ception.

l&ays use try catc' bloc@ to log your e7ception In selenium &eb#riverreports.

6an#ling e7ceptions using t'ro&s @ey&or#not'er &ay o! 'an#ling e7ception Is using t'ro&s @ey&or# &it' met'o#

as s'o&n In bello& given e7ample. Supposing you 'ave a t'ro&e7cmet'o# &'ic' Is t'ro&ing some e7ception an# t'is met'o# Is calle# !romsome ot'er met'o# catc'e7c. o& you &ants to 'an#le e7ception o! t'ro&e7c met'o# In to catc'e7c met'o# t'en you nee# to use t'ro&s@ey&or# &it' t'ro&e7c met'o#.

public class 6an#le e7ce L

public static voi# mainAStringMN args L catc'e7cA H Q private static voi# catc'e7cA L try L <<t'ro&e7cA +et'o# calle#.

t'ro&e7cA H Q catc' A rit'metic87ception e L <<87ception o! t'ro&e7cA &ill becaug't 'ere an# ta@e re uire# action. System.out.printlnAKDevi#e by error.K H Q Q<<T'is met'o# &ill t'ro& rit'metic87ception #ivi#e by .

private static voi# t'ro&e7cA t'ro&s rit'metic87ception Lint iG1%< H

QQ

In above given e7ample" 87ception o! t'ro&e7cA Is 'an#le# Insi#ecatc'e7cA met'o#.

:sing t'ro& Yey&or# To T'ro& n 87ception 87plicitlyDi=erence bet&een t'ro& an# t'ro&s In 4ava

s &e learnt" t'ro&s @ey&or# Is use!ul to t'ro& t'ose e7ceptions to callingmet'o#s &'ic' are not 'an#le# Insi#e calle# met'o#s. T'ro& @ey&or# 'asa #i=erent &or@ - t'ro& @ey&or# Is use!ul to t'ro& an e7ception e7plicitly&'enever re uire#. T'is Is t'e #i=erence bet&een t'ro& an# t'ro&s In

4ava. Intervie&er can as@ t'is uestion. 5et us loo@ at practical e7ample.

Page 163: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 163/292

public class 6an#le e7ce L

public static voi# mainAStringMN args L catc'e7cA H Q private static voi# catc'e7cA L try L <<t'ro&e7cA +et'o# calle#. t'ro&e7cA H Q catc' A rrayIn#e7)ut)!Boun#s87ception e L

System.out.printlnAK rray In#e7 out o! boun# e7ception.K H Q Q

private static voi# t'ro&e7cA L <<T'is statement &ill t'ro& rrayIn#e7)ut)!Boun#s87ception e7ception. t'ro& ne& rrayIn#e7)ut)!Boun#s87ceptionA HQ

Q

In above e7ample" rrayIn#e7)ut)!Boun#s87ception e7ception &ill bet'ro&n by t'ro& @ey&or# o! t'ro&e7c met'o#.

nally @ey&or# an# Its usenally @ey&or# Is use# &it' try catc' bloc@ at t'e en# o! try catc' bloc@.

Co#e &ritten Insi#e nally bloc@ &ill be e7ecute# al&ays regar#less o!

e7ception Is occurre# or not. +ain intention o! using nally &it' try catc'bloc@ Is I! you &ants to per!orm some action &it'out consi#eringe7ception occurre# or not. nally bloc@ &ill be e7ecute# In bot' t'e cases.5et us see simple e7ample o! nally.

public class 6an#le e7ce L

public static voi# mainAStringMN args L tryL int iG%< H<<87ception &ill be t'ro&n.

System.out.printlnAK3alue )! i Is KPi H <<T'is statement &ill be note7ecute#. Qcatc' A87ception e <<87ception &ill be caug't.

L System.out.printlnAKInsi#e catc'.KPe H <<print t'e e7ception. Q nally << nally bloc@ &ill be e7ecute#. L System.out.printlnAKInsi#e nally. lease ta@e appropriate actionK H Q

tryL int 4G%<2H<<87ception &ill be not t'ro&n. System.out.printlnAK3alue )! 4 Is KP4 H <<T'is statement &ill be e7ecute#.

Page 164: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 164/292

Qcatc' A87ception e << o e7ception so catc' bloc@ co#e &ill not e7ecute. L System.out.printlnAKInsi#e catc'.KPe H Q nally << nally bloc@ co#e &ill be e7ecute#. L System.out.printlnAKInsi#e nally. lease ta@e appropriate actionK H Q QQ

In above e7ample" 2 try-catc'- nally bloc@s use#. 0un above e7ample an#observe result.

1st try bloc@ &ill t'ro& an error an# catc' &ill 'an#le an# t'en nallybloc@ &ill be e7ecute#. 2n# try bloc@ &ill not t'ro& any error so catc' &illbe not e7ecute# but nally bloc@ &ill be e7ecute#. So In bot' t'e cases

nally bloc@ &ill be e7ecute#.

So all t'ese t'ings about e7ception an# &ays o! 'an#ling t'em &ill 'elpsyou In your &eb#river test e7ception 'an#ling.

6o& To Create n# :se Custom Fire!o7 ro le For Selenium WebDriver

W'at Is t'e Fire!o7 ro leRW'en you Install Fire!o7 In your computer" Fire!o7 creates one #e!ault

pro le !ol#er In your local #rive to save your pre!erences li@e yourboo@mar@s" your pre!erre# 'ome page on Fire!o7 open" your toolbarsettings" your save# pass&or#s an# all t'e ot'er settings. ou can create#i=erent pro les !or your Fire!o7bro&ser as per your re uirement. o& supposing same computer Is use#by t&o users an# bot' &ants t'eir o&n Fire!o7 settings t'en bot' userscan create t'eir o&n Fire!o7 pro le to access t'eir o&n settings &'en'e<s'e opens Fire!o7 bro&ser.

ou can 58 0 S858 I:+ W8BD0I380 ST8 B ST8 to become master o! selenium Web#river.

I! you 'ave notice#" W'en you &ill run your selenium &eb#river test InFire!o7 bro&ser t'en WebDriver &ill open blan@ Fire!o7 bro&ser li@e oboo@mar@s" o save# pass&or#s" o a##ons etc.. as s'o&n In bello&given Image.

Page 165: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 165/292

I! you &ants access o! all t'ese t'ings In your selenium &eb#river testbro&ser t'en you 'ave to create ne& pro le o! Fire!o7 an# set all re uire#properties In ne&ly create# pro le an# t'en you can access t'at pro le In&eb#river using Fire!o7 ro le class o! &eb#river.

First )! all" 5et us see 'o& to create ne& pro le o! Fire!o7 an# t'en &e &illsee 'o& to use t'at pro le In your test.

6o& to Create e& Custom Fire!o7 ro le For Selenium WebDriverR To create ne& re!o7 pro le manually"

• Close Fire!o7 bro&ser !rom File -O 87it.

• 9o to Start -O 0un an# type K re!o7.e7e -pK In run &in#o& an# clic@)Y. It &ill open KFire!o7 - C'oose :ser ro leK #ialogue as s'o&n Inbello& Image.

Page 166: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 166/292

• o& clic@ on Create pro le button. It &ill open create pro le &i?ar##ialogue. Clic@ )n e7t as s'o&n In bello& given Image.

• )n ne7t screen" 9ive pro le name G KWebDriver ro leK an# clic@on nis' button as s'o&n In bello& given Image.

• It Will create ne& pro le o! Fire!o7.

• To use t'at ne&ly Create# pro le" Select t'at pro le an# clic@ onStart Fire!o7 button as s'o&n In bello& given Image. It &ill openFire!o7 bro&ser &it' ne&ly create# pro le.

Page 167: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 167/292

o& you can ma@e you re uire# settings on t'is ne& create# pro lebro&ser li@e a## your re uire# a##ons" boo@mar@ your re uire# page"net&or@ settings" pro7y settings" etc.. an# all ot'er re uire# settings.

6o& To ccess Custom Fire!o7AC'anging :ser gent ro le In SeleniumWebDriver Test

To access ne&ly create# Fire!o7 pro le In selenium WebDriver" We nee#sto use &eb#river s Inbuilt class ro lesIni an# Its met'o# get ro le ass'o&n In bello& given e7ample.

pac@age Testng ac@H

import org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.open a.selenium. re!o7.Fire!o7 ro leHimport org.open a.selenium. re!o7.internal. ro lesIniHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

public class custom pro le L WebDriver #riverH ;Be!oreTest

public voi# StartBro&serA L <<Create ob4ect o! &eb#river s inbuilt class ro lesIni to access Its met'o#get ro le. ro lesIni r ro les G ne& ro lesIniA H <<9et access o! ne&ly create# pro le WebDriver ro le. Fire!o7 ro le &b#rverpro le G r ro les.get ro leAKWebDriver ro leK H << ass &b#rverpro le parameter to Fire!o7Driver.

#river G ne& Fire!o7DriverA&b#rverpro le HQ

Page 168: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 168/292

;Test

public voi# )pen:05A L #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< %<login.'tmlK HQ

; !terTest public voi# CloseBro&serA L #river. uitA HQ

Q

W'en you &ill run above given e7ample" It &ill open Fire!o7 bro&ser &it'ne&ly create# pro le settings.

W'y nee#s To Set Fire!o7 ro le In Selenium WebDriver To per!orm some actions In your selenium &eb#river test" ou nee# specialFire!o7 pro le. Some e7ample actions are as bello& &'ere &e nee# to setFire!o7 pro le. We &ill learn more about 'o& to per!orm all t'ose actionsIn Selenium &eb#river In my upcoming posts.

1. To Do&nloa# les. 3I8W 8V + 582. To Set ro7y Settings.

. To 0esolve Certi cate relate# errors.

I! you 'ave any e7ample &'ere &e nee# to set Fire!o7 pro le properties

t'en you can s'are It &it' &orl# by commenting bello&.

WebDriver Test Data Driven Testing :sing Test 9 ;Data rovi#ernnotation

Data #riven testing Is most Important topic !or all so!t&are testingautomation tools because you nee# to provi#e #i=erent set o! #ata In yourtests. I! you are selenium ID8 user an# you &ants to per!orm #ata #riventesting I ):0 T8ST t'en T68S8 )STS &ill 'elps you. For SeleniumWeb#river" Data #riven testingusing e7cel le Is very easy. For t'at you nee# support o! Java 87cel Ian# It Is e7plaine# very clearly In T6IS )ST . o& I! you are using Test 9!rame&or@ !or selenium &eb#river t'en t'ere Is one anot'er &ay toper!orm #ata #riven testing.

Test 9 ;Data rovi#er nnotation;Data rovi#er Is Test 9 annotation . ;Data rovi#er nnotation o! testng!rame&or@ provi#es us a !acility o! storing an# preparing #ata set Inmet'o#. Tas@ o! ;Data rovi#er annotate# met'o# Is supplying #ata !or atest met'o#. +eans you can con gure #ata set In t'at met'o# an# t'enuse t'at #ata In your test met'o#. ;Data rovi#er annotate# met'o# mustreturn an )b4ectMNMN &it' #ata.

Page 169: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 169/292

5et us see simple e7ample o! #ata #riven testing using ;Data rovi#erannotation. We 'ave to create 2 #imensional array In ;Data rovi#erannotate# met'o# to store #ata as s'o&n In bello& given e7ample. oucan 3I8W T6IS )ST to @no& more about t&o #imensional array In 4ava.

Bello& given e7ample &ill retrieve useri# an# pass&or# one by one !rom;Data rovi#er annotate# met'o# an# &ill !ee# t'em In 5ogIn TestAString:se#i#" String ass one by one. 0un t'is e7ample In your eclipse an#observe result.

pac@age Testng ac@H

import 4ava.util.concurrent.Time:nitHimport org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.Data rovi#erHimport org.testng.annotations.TestH

public class Sample 5ogin L WebDriver #river G ne& Fire!o7DriverA H

;Be!oreTest public voi# setupA t'ro&s 87ception L#river.manageA .&in#o&A .ma7imi?eA H

#river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< %<login.'tmlK H

Q

; !terTest public voi# tearDo&nA t'ro&s 87ception L

#river. uitA H Q

<<T'is met'o# &ill return t&o #imensional array. <<T'is met'o# be'aves as #ata provi#er !or 5ogIn Test met'o#. ;Data rovi#er public )b4ectMNMN 5oginCre#entialsA L <<Create# t&o #imensional array &it' $ ro&s an# 2 columns. <<$ ro&s represents test 'as to run $ times. <<2 columns represents 2 #ata parameters. )b4ectMNMN Cre# G ne& )b4ectM$NM2NH

Cre#M NM N G K:serI#1KH Cre#M NM1N G K ass1KH

Page 170: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 170/292

Cre#M1NM N G K:serI#2KH Cre#M1NM1N G K ass2KH

Cre#M2NM N G K:serI# KH Cre#M2NM1N G K ass KH Cre#M NM N G K:serI#$KH Cre#M NM1N G K ass$KH return Cre#H <<0eturne# Cre# Q <<9ive #ata provi#er met'o# name as #ata provi#er.

<< asse# 2 string parameters as 5oginCre#entialsA returns 2 parametersIn ob4ect. ;Test A#ata rovi#erGK 5oginCre#entials K public voi# 5ogIn TestA String :se#i#" String ass L #river. n#8lementABy.7pat'AK<<inputM;nameG useri# NK .clearA H #river. n#8lementABy.7pat'AK<<inputM;nameG ps&r# NK .clearA H #river. n#8lementABy.7pat'AK<<inputM;nameG useri# NK .sen#YeysA:se#i#H #river. n#8lementABy.7pat'AK<<inputM;nameG ps&r# NK .sen#YeysA ass H #river. n#8lementABy.7pat'AK<<inputM;valueG 5ogin NK .clic@A H String alrt G #river.s&itc'ToA .alertA .getTe7tA H #river.s&itc'ToA .alertA .acceptA H System.out.printlnAalrt H

QQ

testng.7ml le to run t'is e7ample Is as bello&.

suite nameGKSimple SuiteKO test nameGKSimple S@ip TestKO classesO class name G KTestng [email protected] 5oginK<O <classesO <testO

<suiteO

W'en you &ill run above e7ample In eclipse" It &ill enter :serID an#pass&or#s one by one In :serI# an# pass&or# test bo7 o! &eb page.

Test 9 result report &ill loo@s li@e bello&.

Page 171: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 171/292

Creating )b4ect 0epository :sing roperties File In Selenium WebDriver

In selenium WebDriver" T'ere Is not any built In !acility to create ob4ectrepository. So maintenance o! page ob4ectsA age element locatorsbecomes very 'ar# I! you &ill &rite all ob4ects o! page on co#e level. 5etme give you one simple e7ample - ou 'ave one searc' button an# you'ave use# ItA:sing I# locator In manyo! your test cases. o& supposing someone 'as c'ange# Its I# In yourapplication t'en &'at you &ill #oR ou &ill go !or replacing I# o! t'at

element In all test cases &'erever It Is use#R It &ill create over'ea# !oryou. Simple solution to remove t'is over'ea# Is creating ob4ect repositoryusing properties File support o! 4ava. In properties le" First &e can &riteelement locator o! &eb element an# t'en &e can use It In our test cases.

5et us try to create ob4ect repository !or simple calculator applicationgiven on T6IS 98 an# t'en &ill try to create test case !or t'at calcapplication using ob4ect repository..

6o& to create ne& properties le To create ne& properties le" 0ig't clic@ on your pro4ect pac@age -O e&-O )t'er .

Page 172: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 172/292

It &ill open e& &i?ar#. 87pan# 9eneral !ol#er In e& &i?ar# an# selectFile an# clic@ on e7t button.

9ive le name ob4ects.properties on ne7t &in#o& an# clic@ on Finis'button.

Page 173: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 173/292

It &ill a## ob4ect.properties le un#er your pac@age. o& copy pastebello& given lines in ob4ects.properties le. So no&t'is ob4ects.properties le Is ob4ect repository !or your &eb applicationpage calc.

ob4ects.properties le\Create# uni ue @ey !or I# o! all &eb elements.\ 87ample one Is @ey an# 1 Is ID o! &eb element button 1.oneG1t&oG2t'reeG!ourG$

veG%si7G(sevenG*eig'tG,nineG/?eroGe ualstoGe ualsclerG CresultG0esultbo7plusGplusminusGminusmulGmultiply

In above le" 5e!t si#e value Is @ey an# rig't si#e value Is elementlocatorAby I# o! all &eb elements o! &eb calculator. ou can use ot'er

element locator met'o#s too li@e 7pat'" css ect.. 3ISIT T6IS 5I Y to vie&#i=erent element locator met'o#s o! &eb#river.

Page 174: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 174/292

o& let us create &eb#river test case to per!orm some operations oncalculation. In bello& given e7ample" ll t'e element locators are coming!rom ob4ects.properties le using ob4.get ropertyA@ey . 6ere @ey Isre!erence o! element locator value in ob4ects.properties le.

pac@age )b4ect0epoH

import 4ava.io.FileInputStreamHimport 4ava.io.I)87ceptionHimport 4ava.util. ropertiesH

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !ter+et'o#Himport org.testng.annotations.Be!ore+et'o#Himport org.testng.annotations.TestH

public class CalcTest L WebDriver #river G ne& Fire!o7DriverA H

;Be!ore+et'o# public voi# openbro&serA t'ro&s I)87ception L

#river.manageA .&in#o&A .ma7imi?eA H

#river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< $<calc.'tmlK HQ ; !ter+et'o#

public voi# closebro&serA L#river. uitA H

Q

;Test public voi# Calc )perationsA t'ro&s I)87ceptionL <<Create )b4ect o! roperties Class.

roperties ob4 G ne& ropertiesA H<<Create )b4ect o! FileInputStream Class. ass le pat'. FileInputStream ob4 le G ne&FileInputStreamASystem.get ropertyAKuser.#irKPKZZsrcZZ)b4ect0epoZZob4ects.propertiesK H << ass ob4ect re!erence ob4 le to loa# met'o# o! roperties ob4ect. ob4.loa#Aob4 le H

<<Sum operation on calculator. << ccessing element locators o! all &eb elements usingob4.get ropertyA@ey #river. n#8lementABy.i#Aob4.get ropertyAKeig'tK .clic@A H #river. n#8lementABy.i#Aob4.get ropertyAKplusK .clic@A H

Page 175: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 175/292

#river. n#8lementABy.i#Aob4.get ropertyAK!ourK .clic@A H #river. n#8lementABy.i#Aob4.get ropertyAKe ualstoK .clic@A H String i G#river. n#8lementABy.i#Aob4.get ropertyAKresultK .get ttributeAKvalueK H System.out.printlnAob4.get ropertyAKeig'tK PK PKPob4.get ropertyAK!ourK PK G KPi H

#river. n#8lementABy.i#Aob4.get ropertyAKresultK .clearA H

<<Subtraction operation on calculator. << ccessing element locators o! all &eb elements usingob4.get ropertyA@ey #river. n#8lementABy.i#Aob4.get ropertyAKnineK .clic@A H #river. n#8lementABy.i#Aob4.get ropertyAKminusK .clic@A H #river. n#8lementABy.i#Aob4.get ropertyAKt'reeK .clic@A H #river. n#8lementABy.i#Aob4.get ropertyAKe ualstoK .clic@A H String 4 G#river. n#8lementABy.i#Aob4.get ropertyAKresultK .get ttributeAKvalueK H System.out.printlnAob4.get ropertyAKnineK PK - KPob4.get ropertyAKt'reeKPK G KP4 H QQ

0un above given e7ample In your eclipse an# observe e7ecution.

o& supposing I 'ave many suc' test cases an# some one 'as c'ange# I#o! any button o! calc application t'en &'at I 'ave to #oR I 'ave to mo#i!y

all test casesR ns&er Is o. I 4ust nee# to mo#i!y ob4ects.properties lebecause I 'ave not use element s I# #irectly In any test case but I 'ave:se# 4ust Its @ey re!erence In all test cases.

T'is &ay you can create ob4ect repository o! all your &eb elements Inone .properties le. ou can 3I8W +)08 T:T)0I 5S about selenium&eb#river.

:se )! is+ultipleA n# #eselect llA In Selenium WebDriver Wit' 87ample

o& you can easily select or #eselect any speci c option !rom select bo7or #rop #o&n as #escribe# in my earlier )ST 1 " )ST 2 an# )ST . llt'ese t'ree posts &ill #escribe you #i=erent alternative &ays o! selectingoptions !rom list bo7 or #rop #o&n. o& let me ta@e you one step a'ea# to#escribe you t'e useo! #eselect llA an# is+ultipleA met'o#s in selenium &eb#river.

#eselect llA +et'o##eselect llA met'o# is use!ul to remove selection !rom all selecte#options o! select bo7. It &ill &or@s &it' multiple select bo7 &'en you nee#

to remove all selections. Synta7 !or #eselect llA met'o# is as bello&.

Page 176: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 176/292

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.#eselect llA H

is+ultipleA +et'o#is+ultipleA met'o# is use!ul to veri!y i! targete# select bo7 is multipleselect bo7 or not means &e can select multiple options !rom t'at selectbo7 or not. It &ill return boolean Atrue or !alse value. We can use it &it' i! con#ition be!ore &or@ing &it' select bo7. Synta7 is as bello&.Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hboolean value G listbo7.is+ultipleA HSystem.out.printAvalue H

Bello& given e7ample &ill s'o& you use o! is+ultipleA an# #eselect llA

met'o#s very clearly in selenium &eb#river.;Test public voi# test A t'ro&s Interrupte#87ceptionLSelect listbo7 G ne&

SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK H

<<To veri!y t'at speci c select bo7 supports multiple select or not. i!Alistbo7.is+ultipleA L

System.out.printAK+ultiple selections is supporte#K H listbo7.selectBy3isibleTe7tAK:S K H listbo7.selectBy3alueAK0ussiaK H listbo7.selectByIn#e7A% H T'rea#.sleepA H

<<To #eselect all selecte# options. listbo7.#eselect llA H T'rea#.sleepA2 H Q else L System.out.printAK ot supporte# multiple selectionsK H Q Q

In above e7ample" i! con#ition &ill c'ec@ t'at listbo7 is multiple select bo7or notR :se# listbo7.#eselect llA H to remove all selecte# options.N

6o& To Deselect )ption By 3isible Te7t )r 3alue )r By In#e7 In Selenium

WebDriver Wit' 87ample

Page 177: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 177/292

##ing selection or removing selection !rom list bo7 are very commonactions !or list bo7. WebDriver 'as alternate options to select or #eselectvalue !rom list bo7. It is very important to learn all t'ree met'o#s o! selecting or #eselecting option !rom list bo7 because i! one is not possibleto implement in your test t'en youmust be a&are about alternate option. I alrea#y #escribe# selectionoptions in my earlier posts - Selecting speci c option B 3ISIB58 T8VT " B3 5:8 or B I D8V !rom #rop #o&n or list bo7.

o& let me #escribe you all t'ree #eselect met'o#s to #eselect speci coption !rom list bo7. Do you remember t'at &e can use KremoveSelectionKcomman# to #eselect option !rom list bo7R 3I8W T6IS S858 I:+ ID88V + 58 !or KremoveSelectionK comman#.

1. Deselect By 3isible Te7tI! you &ants to remove selection !rom list bo7 t'en you can use&eb#river s #eselectBy3isibleTe7tA met'o#. Synta7 is as bello&. 1stsynta7 &ill locate t'e select bo7 an# 2n# synta7 &ill remove selection byvisible te7t G 0ussia.Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.#eselectBy3isibleTe7tAK0ussiaK H

2. Deselect By 3alue ou can also #eselect option !rom list bo7 by value. 2n# synta7 &ill#eselect option by value G +e7ico.

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.#eselectBy3alueAK+e7icoK H

. Deselect By In#e7Bello& given synta7 &ill remove selection by in#e7 G %.

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.#eselectByIn#e7A% H

87ecute bello& given e7ample in eclipse an# observe results #uringe7ecution.

Copy bello& given ;Test met'o# part o! #eselect option by visible te7t orvalue or in#e7 an# replace it &it' t'e ;Test met'o# part o! e7ample givenon T6IS 98 .A ote ;Test met'o# is mar@e# &it' pin@ color in t'atlin@e# page .

;Test

Page 178: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 178/292

public voi# test A t'ro&s Interrupte#87ceptionL#river. n#8lementABy.i#AKte7t1K .sen#YeysAK+y First ameK H

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK H listbo7.selectBy3isibleTe7tAK:S K H listbo7.selectBy3isibleTe7tAK0ussiaK H T'rea#.sleepA1 H <<To #eselect by visible te7t listbo7.#eselectBy3isibleTe7tAK0ussiaK H T'rea#.sleepA1 H

listbo7.selectBy3alueAKJapanK H listbo7.selectBy3alueAK+e7icoK H T'rea#.sleepA1 H <<To #eselect by value listbo7.#eselectBy3alueAK+e7icoK H T'rea#.sleepA1 H

listbo7.selectByIn#e7A$ H listbo7.selectByIn#e7A% H T'rea#.sleepA1 H <<To #eselect by in#e7 listbo7.#eselectByIn#e7A% H T'rea#.sleepA1 H

#river. n#8lementABy.7pat'AK<<inputM;valueG -O NK .clic@A H T'rea#.sleepA1 H QQ

6o& To Select )ption By 3alue or By In#e7 In Selenium WebDriver Wit'

87ample

In my earlier post" We 'ave seen e7ample o! 6)W T) S858CT ) TI) B3ISIB58 T8VT !rom #rop #o&n. T'ere are t&o more alternatives to selectoption value !rom #rop #o&n or list bo7 in selenium &eb#river. 1. Selecting)ption By 3alue an# 2. Selecting )ption By In#e7. ou can use any one!rom t'eset'ree to select value !rom list bo7 o! your so!t&are &eb application page.I! you @no&" &e can use K select K comman# or K a##Selection K comman# inselenium ID8 to select option !rom list bo7.

Page 179: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 179/292

First o! all let me s'o& you #i=erence bet&een visible te7t" value an#in#e7 o! list bo7 option. Bello& given image &ill s'o& you clear #i=erencebet&een value" visible te7t an# in#e7.

1. Select )ption By 3alue In WebDriverSynta7 !or selecting option by value in &eb#river is as bello&. First synta7&ill locate t'e select element !rom page an# 2n# synta7 &ill select t'eoption !rom select bo7 by value G Italy.Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.selectBy3alueAKItalyK H

2. Select )ption By In#e7 In WebDriverSynta7 !or selection option by in#e7 !rom list bo7 is as bello&. It &ill select

1st elementAin#e7 G !rom select bo7.Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.selectByIn#e7A H

87ecute bello& given e7ample in your eclipse an# veri!y result. Try to useit in #i=erent applications.

Copy bello& given ;Test met'o# part o! select option by value orin#e7 an# replace it &it' t'e ;Test met'o# part o! e7ample given on T6IS

98 .A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e# page .;Test public voi# test A t'ro&s Interrupte#87ception

Page 180: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 180/292

L#river. n#8lementABy.i#AKte7t1K .sen#YeysAK+y First ameK H

<<Selecting value !rom #rop #o&n by value Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK H listbo7.selectBy3alueAKItalyK H listbo7.selectBy3alueAK+e7icoK H listbo7.selectBy3alueAKSpainK H

#river. n#8lementABy.7pat'AK<<inputM;valueG -O NK .clic@A H WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H &ait.untilA87pecte#Con#[email protected]#AKte7t2K H

<<Selecting value !rom #rop #o&n by in#e7 listbo7.selectByIn#e7A H listbo7.selectByIn#e7A H #river. n#8lementABy.7pat'AK<<inputM;valueG -O NK .clic@A H T'rea#.sleepA2 H Q

oste# by 8mail T'is BlogT'is[ S'are to T&itter S'are to Faceboo@ S'are to interest

arameteri?ation<Data Driven Testing )! Selenium Web#river Test :sing87cel

arameteri?ation or #ata #riven o! your test is must re uire# t'ing o! anyautomation testing tool. I! you can not per!orm #ata #riven testing in anyautomation tool t'en it is biggest #ra&bac@ o! t'at tool. Till no&" Selenium&eb#river 'as not any built in structure or met'o# to parameteri?e yourtest. I! you @no&" &e canper!orm #ata #riven testing using user e7tension in selenium ID8. ou canvie& practical e7ample o! selenium ID8 parameteri?ation test on T6IS

0TIC58 )ST . To parameteri?e selenium &eb#river test" We nee# supporto! Java 87cel I . First o! all" &e nee# to con gure eclipse. Con gurationsteps are as bello&.

not'er &ay o! #ata #riven testing In selenium &eb#river Is using Test 9;Data rovi#er nnotation. 3I8W ST8 S )F D T D0I38 T8STI 9 :SI 9;Data rovi#er .

Step 1 Do&nloa# latest 47l.4ar leCurrent latest version o! 4e7celapi is 2 ( 12. It may c'ange in !uture so#o&nloa# lin@ can also c'ange.

• Clic@ on T6IS 5I Y to go to 4e7celapi 2 ( 12 page.

Page 181: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 181/292

• Clic@ on 4e7celapi 2 ( 12.?ip lin@ as s'o&n in bello& image. It &illstart #o&nloa#ing ?ip !ol#er.

• )n completion o! ?ip !ol#er #o&nloa#" e7tract t'at ?ip !ol#er. ou &illn# 47l.4ar le insi#e it.

Step 2 ## 47l.4ar in your pro4ect !ol#er as a e7ternal 4ar.

• 0ig't clic@ on your pro4ect !ol#er in eclipse an# go to Buil# at' -OCon gure Buil# pat' -O 5ibraries tab -O Clic@ on ## 87ternal J 0sbutton an# select 47l.4ar.

For #etaile# #escription &it' image on 'o& to a## any e7ternal 4ar in yourpro4ect !ol#er" ou can !ollo& step 2 o! T6IS )ST .

Step Do&nloa# #ata leI 'ave prepare# e7ample #ata le !or parameteri?ation. C5ICY 6808 to#o&nloa# #ata le an# save in your D #rive.

Step $ Import 'ea#er lesll Bello& given 'ea#er les are re uire# !or t'is e7ample. lease inclu#e

t'em in your 'ea#er le list i! not inclu#e#.import 4ava.io.FileInputStreamHimport 4ava.io.I)87ceptionHimport 4ava.util.concurrent.Time:nitHimport 47l.S'eetHimport 47l.Wor@boo@Himport 47l.rea#.bi=.Bi=87ceptionHimport org.open a.selenium.Javascript87ecutorH

Step % 0un bello& given e7ample in your eclipse.Bello& given e7ample &ill rea# #ata !rom D ZZ+yDataS'eet.7ls le an#t'en type t'at #ata in First ame an# 5ast ame te7t bo7 one by one.

5ast ame te7t bo7 &ill be #isable# initially so I 'ave use# 4avascript

e7ecutor to enable it. ou can vie& practical e7ample &it' #escription !ort'e same on T6IS 5I Y .

Page 182: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 182/292

Copy bello& given ;Test met'o# part o! #ata #riven testing e7ample an#replace it &it' t'e ;Test met'o# part o! e7ample given on T6IS

98 . A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e# page .

;Test public voi# test A t'ro&s Bi=87ception" I)87ception"Interrupte#87ception

L <<)pen +yDataS'eet.7ls le !rom given location.

FileInputStream leinput G ne& FileInputStreamAKD ZZ+yDataS'eet.7lsK H << ccess rst #ata s'eet. getS'eetA #escribes rst s'eet. Wor@boo@ &b@ G Wor@[email protected]@boo@A leinput H S'eet s'eet G &[email protected]'eetA H

<<0ea# #ata !rom t'e rst #ata s'eet o! 7ls le an# store it in array. String TestDataMNMN G ne& StringMs'eet.get0o&sA NMs'eet.getColumnsA NH

<<To enable 5ast ame te7t bo7. Javascript87ecutor 4avascript G AJavascript87ecutor #riverH String toenable G K#ocument.get8lementsBy ameA lnameM N.remove ttributeA #isable# HKH 4avascript.e7ecuteScriptAtoenable H

<<Type #ata in rst name an# last name te7t bo7 !rom array.

!orAint iG Hi s'eet.get0o&sA HiPP L !or Aint 4G H4 s'eet.getColumnsA H4PP L TestDataMiNM4N G s'eet.getCellA4"i .getContentsA H i!A4 2GG L #river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysATestDataMiNM4N H Q

else L #river. n#8lementABy.7pat'AK<<inputM;nameG lname NK .sen#YeysATestDataMiNM4N H Q Q T'rea#.sleepA1 H #river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .clearA H #river. n#8lementABy.7pat'AK<<inputM;nameG lname NK .clearA H Q T'rea#.sleepA1 H

Page 183: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 183/292

Q

6o& To 8nable<Disable Te7tbo7 In Selenium WebDriver )n T'e Fly

We can @no& element s enable#<#isable# status very easilyusing is8nable#A met'o# in selenium &eb#river as #escribe# in T6IS8V + 58 )ST . o& supposing you 'ave a scenario &'ere you &ants toenable any #isable# te7t bo7 or #isable any enable# te7t bo7 t'en 'o& to#o itR Web#river #o not 'ave any built inmet'o# by &'ic' you per!orm t'is action. But yes" Web#river 'as

Javascript87ecutor inter!ace by &'ic' &e can e7ecute any 4avascriptusing e7ecuteScriptA met'o#.

I 'ave poste# many posts on 'o& to e7ecute 4avascript in selenium&eb#river to per!orm #i=erent actions. ou can vie& all t'ose e7ample on

T6IS 5I Y . Same &ay" &e can enable or #isable any elementusing Javascript87ecutor inter!ace #uring &eb#river test case e7ecution.

To #isable te7t bo7" &e &ill use 'tml #om set ttributeA met'o# an# toenable te7t bo7" &e &ill use 'tml #om remove ttributeA met'o#&it' e7ecuteScriptA met'o#. Javascript synta7 !or bot' o! t'ese is asbello&.

#ocument.get8lementsBy ameA !name M N.set ttributeA #isable# " H

#ocument.get8lementsBy ameA lname M N.remove ttributeA #isable# H

o& let &e use bot' t'ese 4avascripts practically to un#erstan# t'embetter. 3I8W T6IS )ST to see 'o& to enable or #isable te7t bo7 inselenium ID8.

Copy bello& given ;Test met'o# part o! enable or #isable element an#replace it &it' t'e ;Test met'o# part o! e7ample given on T6IS

98 . A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e# page .

;Test public voi# test A t'ro&s Bi=87ception" I)87ception"Interrupte#87ception

L boolean !be!ore G#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .is8nable#A H System.out.printAKZnBe!ore First ame Te7t bo7 enable# status is KP!be!ore H boolean lbe!ore G#river. n#8lementABy.7pat'AK<<inputM;nameG lname NK .is8nable#A H System.out.printAKZnBe!ore 5ast ame Te7t bo7 enable# status is

KPlbe!ore H

Page 184: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 184/292

T'rea#.sleepA2 H

<<To #isable First ame te7t bo7 Javascript87ecutor 4avascript G AJavascript87ecutor #riverH String to#isable G K#ocument.get8lementsBy ameA !nameM N.set ttributeA #isable# " HKH 4avascript.e7ecuteScriptAto#isable H T'rea#.sleepA2 H

<<To enable 5ast ame te7t bo7 String toenable G K#ocument.get8lementsBy ameA lnameM N.remove ttributeA #isable# HKH 4avascript.e7ecuteScriptAtoenable H T'rea#.sleepA H

boolean !a!ter G#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .is8nable#A H System.out.printAKZn !ter First ame Te7t bo7 enable# status is KP!a!ter H boolean la!ter G#river. n#8lementABy.7pat'AK<<inputM;nameG lname NK .is8nable#A H System.out.printAKZn !ter 5ast ame Te7t bo7 enable# status is KPla!ter H

Q

6o& To 3eri!y 8lement Is 8nable# )r Disable# in Selenium WebDriver

During your Selenium WebDriver test case creation" !re uently you nee#to veri!y t'at your targete# element is enable# or #isable# be!oreper!orming action on it. Web#river 'as built in met'o# is8nable#A toc'ec@ t'e element enable status. 3eri cation o! element is #isable an#veri cation o! element invisible is totally #i=erent.8lement is #isable# means it is visible but not e#itable an# element isinvisible means it is 'i##en. 3I8W T6IS 8V + 58 to @no& 'o& to &ait till

element is visible on t'e page.

is8nable#Ais8nable#A &eb#river met'o# &ill veri!y an# return true i! speci e#element is enable#. 8lse it &ill return !alse. 9eneric synta7 to store an#print element s status value is as bello&.boolean !name G#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .is8nable#A HSystem.out.printA!name H

We can use is8nable#A met'o# &it' i! con#ition to ta@e action base# onelement s enable# status. Bello& given e7ample &ill e7plain you #eeplyabout is8nable#A &eb#river met'o#. I! you are selenium ID8 user t'en

Page 185: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 185/292

3I8W T6IS )ST to @no& 'o& to &ait !or targete# element becomese#itable.

Copy bello& given ;Test met'o# part o! c'ec@ element enable# statusan# replace it &it' t'e ;Test met'o# part o! e7ample given on T6IS

98 . A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e# page .

;Test public voi# test A t'ro&s Bi=87ception" I)87ception"Interrupte#87ception

L boolean !name G#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .is8nable#A H System.out.printA!name H

Web8lement rstname G#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK H Web8lement lastname G#river. n#8lementABy.7pat'AK<<inputM;nameG lname NK H

<<3eri!y First name te7t bo7 is enable# or not an# t'en print relate#message. i!A rstname.is8nable#A L System.out.printAKZnTe7t bo7 First name is enable#. Ta@e your action.K H Q

else L System.out.printAKZnTe7t bo7 First name is #isable#. Ta@e your action.K H Q

<<3eri!y 5ast name te7t bo7 is enable# or not an# t'en print relate#message. i!Alastname.is8nable#A L System.out.printAKZnTe7t bo7 5ast name is enable#. Ta@e your action.K H Q

else L System.out.printAKZnTe7t bo7 5ast name is #isable#. Ta@e your action.K H Q Q

bove given e7ample" &ill c'ec@ t'e status o! First name an# 5ast namete7t bo7 an# print message in console base# on element is enable# or#isable#.

87ample o! 6an#ling +ultiple Bro&ser Win#o&s in Selenium WebDriver

Page 186: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 186/292

Selenium WebDriver 'as built in KWebDriver.s&itc'ToA .&in#o&A K met'o#available to s&itc' !rom one &in#o& to anot'er &in#o& so it is very easyto 'an#le multiple &in#o&s in &eb#river. I! you remember" We can useKselectWin#o& K &in#o& comman# in selenium ID8 to select anot'er&in#o&. I! &in#o& #o not'ave any title or bot' &in#o& 'as same title t'en it &as very #i`cult toselect ot'er &in#o& in selenium ID8. WebDriver 'as ma#e it very easy.

ou can 'an#le multiple &in#o&s even i! all t'e &in#o&s #o not 'ave anytitle or contains same title.

WebDriver.getWin#o&6an#lesAIn WebDriver" We can use KWebDriver.getWin#o&6an#lesA K to get t'e'an#les o! all opene# &in#o&s by &eb#river an# t'en &e can use t'at&in#o& 'an#le to s&itc' !rom !rom one &in#o& to anot'er &in#o&.87ample Synta7 !or getting &in#o& 'an#les is as bello&.

Set StringO llWin#o&6an#les G #river.getWin#o&6an#lesA H

WebDriver.s&itc'ToA .&in#o&AWebDriver.s&itc'ToA .&in#o&A met'o# is use!ul to s&itc' !rom one&in#o& to anot'er &in#o& o! so!t&are &eb application. 87ample synta7 isas bello&.

#river.s&itc'ToA .&in#o&A&in#o&2 H

Bello& given &eb#river e7ample o! s&itc'ing &in#o& &ill e7plain you it

#eeply. 87ecute it in your eclipse an# try to un#erstan# 'o& &eb#river #oit.

Copy bello& given ;Test met'o# part o! 'an#ling multiple &in#o&s o!&eb#river an# replace it &it' t'e ;Test met'o# part o! e7ample givenon T6IS 98 .A ote ;Test met'o# is mar@e# &it' pin@ color in t'atlin@e# page .

;Test public voi# test A t'ro&s Interrupte#87ception

L #river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H #river. n#8lementABy.7pat'AK<<bMcontainsA." )pen e& age NK .clic@A H

<< 9et an# store bot' &in#o& 'an#les in array Set StringO llWin#o&6an#les G #river.getWin#o&6an#lesA H String &in#o&1 G AString llWin#o&6an#les.to rrayA M NH System.out.printAK&in#o&1 'an#le co#e G KP llWin#o&6an#les.to rrayAM N H String &in#o&2 G AString llWin#o&6an#les.to rrayA M1NH System.out.printAKZn&in#o&2 'an#le co#e GKP llWin#o&6an#les.to rrayA M1N H

Page 187: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 187/292

<<S&itc' to &in#o&2Ac'il# &in#o& an# per!orming actions on it. #river.s&itc'ToA .&in#o&A&in#o&2 H #river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK+y

ameK H #river. n#8lementABy.7pat'AK<<inputM;valueG Bi@e NK .clic@A H #river. n#8lementABy.7pat'AK<<inputM;valueG Car NK .clic@A H #river. n#8lementABy.7pat'AK<<inputM;valueG Boat NK .clic@A H #river. n#8lementABy.7pat'AK<<inputM;valueG male NK .clic@A H T'rea#.sleepA% H

<<S&itc' to &in#o&1Aparent &in#o& an# per!orming actions on it. #river.s&itc'ToA .&in#o&A&in#o&1 H #river. n#8lementABy.7pat'AK<<optionM;i#G country( NK .clic@A H #river. n#8lementABy.7pat'AK<<inputM;valueG !emale NK .clic@A H #river. n#8lementABy.7pat'AK<<inputM;valueG S'o& +e lert NK .clic@A H #river.s&itc'ToA .alertA .acceptA H T'rea#.sleepA% H

<<)nce gain s&itc' to &in#o&2Ac'il# &in#o& an# per!orming actions onit. #river.s&itc'ToA .&in#o&A&in#o&2 H #river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .clearA H #river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK ameC'ange#K H T'rea#.sleepA% H

#river.closeA H

<<)nce gain s&itc' to &in#o&1Aparent &in#o& an# per!orming actionson it. #river.s&itc'ToA .&in#o&A&in#o&1 H #river. n#8lementABy.7pat'AK<<inputM;valueG male NK .clic@A H T'rea#.sleepA% H

Q

6o& To 9et<87tract ll 5in@s From Web age :sing Selenium WebDriver

s &e all @no&s" 8ac' an# every so!t&are &eb application contains manynumber o! #i=erent lin@s<:05s. Some o! t'em are re#irecting to somepage o! same &ebsite an# ot'ers are re#irecting to any e7ternal &ebsite.)ne o! my blog rea#er 'as re ueste# t'e &eb#river test script e7ample!or printing all t'ese lin@s inconsole. I 'ave create# an# publis'e# many #i=erent W8BD0I3808V + 58 SC0I TS on t'is blog but re ueste# e7ample by blog rea#er isnot pus'e# till no& so I 'ave create# e7ample !or t'e same to s'are &it'all o! you.

Page 188: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 188/292

ou can loo@ at e7ample o! 8VT0 CTI 9 T B58 D T :SI 9 S858 I:+W8BD0I380 .

We 'ave seen 6)W T) :S8 n#8lements in &eb#river to locate multipleelements o! page. We can use same t'ing 'ere to locate multiple lin@s o! t'e page. Bello& given e7ample &ill retrieve all :05s !rom t'e page an#&ill print all o! t'em in console. ou can clic@ on eac' :05 i! you &is' to #oso. Try bello& given e7ample !or #i=erent &ebsites by provi#ing t'at&ebsite s :05 in ;Be!ore annotation.

Copy bello& given ;Test met'o# part o! e7tract all lin@s !rom &ebpage an# replace it &it' t'e ;Test met'o# part o! e7ample given on T6IS

98 .A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e# page .

;Test public voi# test A t'ro&s Interrupte#87ception

L try L 5ist Web8lementO no G #river. n#8lementsABy.tag ameAKaK H int nooEin@s G no.si?eA H

System.out.printlnAnooEin@s H !or AWeb8lement pagelin@ no L String lin@te7t G [email protected] H String lin@ G [email protected] ttributeAK're!K H

System.out.printlnAlin@te7tPK -OK H System.out.printlnAlin@ H Q Qcatc' A87ception e L System.out.printlnAKerror KPe H Q

Q

T6IS 5I Y &ill s'o& you many ot'er basic action comman# e7amples.

Selenium WebDriver Tutorials - Basic ction Comman#s n# )perationsWit' 87amples

I 'ave alrea#y poste# Selenium WebDrier Tutorials posts 'o& to setup &eb#river &it' eclipse an# 0un rst test &it' &eb#river " 'o& to con gure 4unit&it' eclipse to generate &eb#river test report . We 'ave also learn#i=erent met'o#s o! locating elements in &eb#river . ll t'ese t'ings arevery basict'ings an# you nee# to learn all o! t'em be!ore starting your test casecreation in selenium 2. o& &e are rea#y to learn ne7t step o! per!ormingbasic actions in &eb #river &it' 4ava !or your so!t&are &eb application.

Page 189: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 189/292

3I8W ST8 B ST8 T:T)0I 5S ) S858 I:+ W8BD0I380

To#ay I &ants to #escribe you !e& basic &eb#river comman#s to per!ormactions on &eb element o! your &eb page. We can per!orm too manycomman# operations in &eb#river an# &ill loo@ about t'em one by one innear !uture. 0ig't no& I am #escribing you !e& o! t'em !or your @in#in!ormation.

1. Creating e& Instance )! Fire!o7 Driver

WebDriver #river G ne& Fire!o7DriverA Hbove given synta7 &ill create ne& instance o! Fire!o7 #river. 3I8W0 CTIC 5 8V + 58

2. Comman# To )pen :05 In Bro&ser

#river.getAK'ttp <<only-testing-blog.blogspot.in<2 1 <11<ne&-test.'tmlK H T'is synta7 &ill open speci e# :05 in &eb bro&ser. 3I8W 0 CTIC 58V + 58 )F ) 8 :05

. Clic@ing on any element or button o! &ebpage

#river. n#8lementABy.i#AKsubmitButtonK .clic@A H

bove given synta7 &ill clic@ on targete# element in &eb#river. 3I8W0 CTIC 5 8V + 58 )F C5ICY ) 858+8 T

Page 190: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 190/292

$. Store te7t o! targete# element in variable

String #rop#o&n G #river. n#8lementABy.tag ameAKselectK .getTe7tA H T'is synta7 &ill retrieve te7t !rom targete# element an# &ill store it invariable G #rop#o&n. 3I8W 0 CTIC 5 8V + 58 )F 9et Te7t

%. Typing te7t in te7t bo7 or te7t area.#river. n#8lementABy.nameAK!nameK .sen#YeysAK+y First ameK H

bove synta7 &ill type speci e# te7t in targete# element. 3I8W0 CTIC 5 8V + 58 )F Sen#Yeys

(. pplying Implicit &ait in &eb#river

#river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H T'is synta7 &ill !orce &eb#river to &ait !or 1% secon# i! element not !oun#on page. 3I8W 0 CTIC 5 8V + 58 )F I+ 5ICIT W IT

*. pplying 87plicit &ait in &eb#river &it' WebDriver canne# con#itions.

WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H&ait.untilA87pecte#Con#itions.te7tToBe resentIn8lement5ocate#ABy.7pat'AK<<#ivM;i#G time5e!t NK " KTime le!t * secon#sK H

bove 2 synta7 &ill &ait !or till 1% secon#s !or e7pecte# te7t KTime le!t *secon#sK to be appear on targete# element. 3IW8 DIFF808 T 0 CTIC 58V + 58S )F 8V 5ICIT W IT

,. 9et page title in selenium &eb#river

#river.getTitleA HIt &ill retrieve page title an# you can store it in variable to use in ne7tsteps. 3I8W 0 CTIC 5 8V + 58 )F 98T TIT58

/. 9et Current age :05 In Selenium WebDriver

#river.getCurrent:rlA HIt &ill retrieve current page :05 an# you can use it to compare &it' youre7pecte# :05. 3I8W 0 CTIC 5 8V + 58 )F 98T C:008 T :05

1 . 9et #omain name using 4ava script e7ecutor

Javascript87ecutor 4avascript G AJavascript87ecutor #riverHString Current:05:singJSGAString 4avascript.e7ecuteScriptAKreturn#ocument.#omainK H

bove synta7 &ill retrieve your so!t&are application s #omain name using&eb#river s 4ava script e7ecutor inter!ace an# store it in to variable.3I8W 98T D)+ I +8 0 CTIC 5 8V + 58 .

Page 191: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 191/292

11. 9enerate alert using &eb#river s 4ava script e7ecutor inter!ace

Javascript87ecutor 4avascript G AJavascript87ecutor #riverH 4avascript.e7ecuteScriptAKalertA Test Case 87ecution Is starte# o&.. HK HIt &ill generate alert #uring your selenium &eb#river test case e7ecution.3I8W 0 CTIC 5 8V + 58 )F 98 80 T8 580T :SI 9 S858 I:+W8BD0I380 .

12. Selecting or Deselecting value !rom #rop #o&n in selenium &eb#river.

• Select By 3isible Te7t

Select my#rp#&n G ne& SelectA#river. n#8lementABy.i#AKCarlistK Hmy#rp#&n.selectBy3isibleTe7tAK u#iK HIt &ill select value !rom #rop #o&n list using visible te7t value G K u#iK.

• Select By 3alue

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.selectBy3alueAKItalyK HIt &ill select value by value G KItalyK.

• Select By In#e7

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.selectByIn#e7A HIt &ill select value by in#e7G AFirst option .

3I8W 0 CTIC 5 8V + 58S )F S858CTI 9 3 5:8 F0)+ D0) D)W5IST.

• Deselect by 3isible Te7t

Select listbo7 G ne&

SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.#eselectBy3isibleTe7tAK0ussiaK HIt &ill #eselect option by visible te7t G 0ussia !rom list bo7.

• Deselect by 3alue

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.#eselectBy3alueAK+e7icoK HIt &ill #eselect option by value G +e7ico !rom list bo7.

• Deselect by In#e7

Page 192: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 192/292

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.#eselectByIn#e7A% HIt &ill #eselect option by In#e7 G % !rom list bo7.

• Deselect ll

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hlistbo7.#eselect llA HIt &ill remove all selections !rom list bo7.

3I8W 0 CTIC 5 8V + 58S )F D8S858CT S 8CIFIC ) TI) F0)+ 5ISTB)V

is+ultipleA

Select listbo7 G ne&SelectA#river. n#8lementABy.7pat'AK<<selectM;nameG From5B NK Hboolean value G listbo7.is+ultipleA HIt &ill return true i! select bo7 is multiselect else it &ill return !alse. 3I8W

0 CTIC 5 8V + 58 )F is+ultipleA

1 . avigate to :05 or Bac@ or For&ar# in Selenium Web#river

#river.navigateA .toAK'ttp <<only-testing-blog.blogspot.in<2 1$< 1<te7tbo7.'tmlK H#river.navigateA .bac@A H#river.navigateA .!or&ar#A H1st comman# &ill navigate to speci c :05" 2n# &ill navigate one stepbac@ an# r# comman# &ill navigate one step !or&ar#. 3I8W 0 CTIC 58V + 58S )F 3I9 TI) C)++ DS .

1$. 3eri!y 8lement resent in Selenium WebDriver

Boolean iselementpresent G#river. n#8lementsABy.7pat'AK<<inputM;i#G te7t2 NK .si?eA [G HIt &ill return true i! element is present on page" else it &ill return !alse invariable iselementpresent. 3I8W 0 CTIC 5 8V + 58 )F 380IF 858+8 T

08S8 T .

1%. Capturing entire page screens'ot in Selenium WebDriver

File screens'ot GAATa@esScreens'ot #river .getScreens'ot sA)utputType.FI58 HFile:tils.copyFileAscreens'ot" ne& FileAKD ZZscreens'ot.4pgK H

It &ill capture page screens'ot an# store it in your D #rive. 3I8W0 CTIC 5 8V + 58 ) T6IS 98 .

Page 193: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 193/292

1(. 9enerating +ouse 6over 8vent In WebDriver

ctions actions G ne& ctionsA#river HWeb8lement moveonmenu G#river. n#8lementABy.7pat'AK<<#ivM;i#G menu1 N<#ivK Hactions.moveTo8lementAmoveonmenu Hactions.per!ormA H

bove e7ample &ill move mouse on targete# element. 3I8W 0 CTIC 58V + 58 )F +):S8 6)380 .

1*. 6an#ling +ultiple Win#o&s In Selenium WebDriver.

1. 9et ll Win#o& 6an#les.

Set StringO llWin#o&6an#les G #river.getWin#o&6an#lesA H

2. 87tract parent an# c'il# &in#o& 'an#le !rom all &in#o& 'an#les.

String &in#o&1 G AString llWin#o&6an#les.to rrayA M NHString &in#o&2 G AString llWin#o&6an#les.to rrayA M1NH

. :se &in#o& 'an#le to s&itc' !rom one &in#o& to ot'er &in#o&.

#river.s&itc'ToA .&in#o&A&in#o&2 Hbove given steps &it' 'elps you to get &in#o& 'an#le an# t'en 'o& to

s&itc' !rom one &in#o& to anot'er &in#o&. 3I8W 0 CTIC 5 8V + 58 )F6 D5I 9 +:5TI 58 WI D)WS I W8BD0I380

1,. C'ec@ W'et'er 8lement is 8nable# )r Disable# In Selenium Web#river.

boolean !name G#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .is8nable#A HSystem.out.printA!name H

bove synta7 &ill veri!y t'at element Ate7t bo7 !name is enable# or not. ou can use it !or any input element. 3I8W 0 CTIC 5 8V + 58 )F 380IF858+8 T IS 8 B58D )0 )T .

1/. 8nable<Disable Te7tbo7 During Selenium Web#river Test Case87ecution.

Javascript87ecutor 4avascript G AJavascript87ecutor #riverHString to#isable G K#ocument.get8lementsBy ameA !nameM N.set ttributeA #isable# " HKH

4avascript.e7ecuteScriptAto#isable HString toenable G K#ocument.get8lementsBy ameA lnameM N.remove ttributeA #isable# HKH

4avascript.e7ecuteScriptAtoenable H

Page 194: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 194/292

It &ill #isable !name element using set ttributeA met'o# an# enablelname element using remove ttributeA met'o#. 3I8W 0 CTIC 58V + 58 )F 8 B58<DIS B58 T8VTB)V .

2 . Selenium WebDriver ssertions Wit' Test 9 Frame&or@

• assert8 uals

ssert.assert8 ualsAactual" e7pecte# Hassert8 uals assertion 'elps you to assert actual an# e7pecte# e ualvalues. 3I8W 0 CTIC 5 8V + 58 )F assert8 uals SS80TI)

• assert ot8 uals

ssert.assert ot8 ualsAactual" e7pecte# H

assert ot8 uals assertion is use!ul to assert not e ual values. 3I8W0 CTIC 5 8V + 58 )F assert ot8 uals SS80TI) .

• assertTrue

ssert.assertTrueAcon#ition HassertTrue assertion &or@s !or boolean value true assertion. 3I8W

0 CTIC 5 8V + 58 )F assertTrue SS80TI) .

• assertFalse

ssert.assertFalseAcon#ition HassertFalse assertion &or@s !or boolean value !alse assertion. 3I8W

0 CTIC 5 8V + 58 )F assertFalse SS80TI) .

21. SubmitA met'o# to submit !orm

#river. n#8lementABy.7pat'AK<<inputM;nameG Company NK .submitA HIt &ill submit t'e !orm. 3I8W 0 CTIC 5 8V + 58 )F S:B+IT F)0+ .

22. 6an#ling lert" Con rmation an# rompts opups

String myalert G #river.s&itc'ToA .alertA .getTe7tA H To store alert te7t. 3I8W 0 CTIC 5 8V + 58 )F ST)08 580T T8VT

#river.s&itc'ToA .alertA .acceptA H To accept alert. 3I8W 0 CTIC 5 8V + 58 )F 580T CC8 T

#river.s&itc'ToA .alertA .#ismissA H To #ismiss con rmation. 3I8W 0 CTIC 5 8V + 58 )F C C85C) FI0+ TI)

Page 195: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 195/292

#river.s&itc'ToA .alertA .sen#YeysAKT'is Is Jo'nK H

> avigating Bac@ n# For&ar# Selenium WebDriver 6o& To avigate :05" For&ar# an# Bac@&ar# Wit'87ample

In my earlier posts" &e 'ave learnt !e& B SIC CTI) C)++ DS o! selenium WebDriver &it' e7amples. ou &ill 'ave to use t'em on #ailybasis !or you so!t&are &eb application s &eb#river test case preparation.It is very important !or you to use t'em on rig't time an# rig't place. o&let &e learn moreaction comman#s o! &eb#river.

1. #river.navigateA .toI! you &ants to navigate on speci c page or :05 in bet&een your test t'enyou can use #river.navigateA .to comman# as bello&.#river.navigateA .toAK'ttp <<only-testing-blog.blogspot.in<2 1$< 1<te7tbo7.'tmlK H

2. #river.navigateA .bac@A H T'is comman# is use!ul to go bac@ on previous page. Same as &e areclic@ing bro&ser bac@ button. ou can use t'is comman# as bello&. InSelenium ID8" &e can use K goBac@ K to per!orm same action.#river.navigateA .bac@A H

. #river.navigateA .!or&ar#A HSame as &e are clic@ing on !or&ar# button o! bro&ser.

Bello& given e7ample &ill cover all t'ree comman#s. 87ecute it in youreclipse to @no& t'em practically.

Copy bello& given ;Test met'o# part o! #river.navigateA comman#e7amples an# replace it &it' t'e ;Test met'o# part o! e7ample givenon T6IS 98 . A ote ;Test met'o# is mar@e# &it' pin@ color in t'atlin@e# page .

;Test public voi# test A t'ro&s Interrupte#87ceptionL#river.navigateA .toAK'ttp <<only-testing-

blog.blogspot.in<2 1$< 1<te7tbo7.'tmlK H

<<To navigate bac@ ASame as clic@ing on bro&ser bac@ button #river.navigateA .bac@A H

<<To navigate !or&ar# ASame as clic@ing on bro&ser !or&ar# button #river.navigateA .!or&ar#A H

Page 196: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 196/292

Q

> 3eri!y 8lement resent Selenium WebDriver 3eri!y 8lement resent In Selenium WebDriver

Some times you nee# to veri!y t'e presence o! element be!ore ta@ingsome action on so!t&are &eb application page. s you @no&" Selenium ID8'as many built in comman#s to per!orm #i=erent types o! actions on yourso!t&are &eb application page. ou can veri!y presence o! element byusingKveri!y8lement resent K comman# in selenium ID8. lso you can vie&e7ample o! selenium ID8 K veri!y8lement ot resent K comman#. Web#river 'ave not any built in met'o# or inter!ace by &'ic' &e can veri!ypresence o! element on t'e page.

es &e can #o it very easily in WebDriver too using bello& given synta7.Boolean iselementpresent G#river. n#8lementsABy.7pat'AK<<inputM;i#G te7t2 NK .si?eA [G H

We 'ave to use n#8lementsA met'o# !or t'is purpose. bove synta7 &illreturn true i! element is present on page. 8lse it &ill return !alse. ou canput i! con#ition to ta@e action base# on presence o! element.

Bello& given e7ample &ill c'ec@ t'e presence o! #i=erent te7t bo7 onpage. It &ill print message in console base# on presence o! element.

Copy bello& given ;Test met'o# part o! iselementpresent e7ample an#replace it &it' t'e ;Test met'o# part o! e7ample given on T6IS

98 . A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e# page .

;Test public voi# test A t'ro&s Interrupte#87ceptionL!or Aint iG1H i (H iPP

L

<<To veri!y element is present on page or not. String V at' G K<<inputM;i#G te7tKPiPK NKH Boolean iselementpresent G #river. n#8lementsABy.7pat'AV at' .si?eA [G H i! Aiselementpresent GG true L System.out.printAKZnTargete# Te7tBo7KPiPK Is resent )n T'e ageK H Q else L System.out.printAKZnTargete# Te7t Bo7KPiPK Is ot resent )n T'e

ageK H

Page 197: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 197/292

Q Q Q

> Capturing 8ntire age Screens'ot Selenium Web#river Tutorial To Capture Screens'ot Wit' 87ample

Capturing screens'ot o! so!t&are &eb application page is very easy inselenium &eb#river. s &e @no&s" It is very basic re uire# t'ing inautomation tools to capture screens'ot on test case !ailure or &'eneverre uire# #uring test case e7ecution. 3I8W +)08 S858 I:+ W8BD0I380B SIC C)++ DS !or your &eb#river @no&le#ge improvement an# uset'em in your so!t&are &eb application test case creation.

I! you remember" We can use K capture8ntire ageScreens'ot K comman# inselenium ID8 to capture t'e screens'ot on so!t&are &eb applicationcurrent page. In Selenium WebDriver<Selenium 2" &e nee# to capturescreens'ot o! &eb page using bello& synta7.File screens'ot GAATa@esScreens'ot #river .getScreens'ot sA)utputType.FI58 H

T'en &e 'ave to store it in our local #rive using bello& given synta7. oucan c'ange<provi#e your o&n le #estination pat' an# name.

File:tils.copyFileAscreens'ot" ne& FileAKD ZZscreens'ot.4pgK H

o& &e nee# to import bello& given 'ea#er les to get support o! capturescreens'ot in &eb#river.import 4ava.io.FileHimport 4ava.io.I)87ceptionHimport org.apac'e.commons.io.File:tilsHimport org.open a.selenium.)utputTypeHimport org.open a.selenium.Ta@esScreens'otH

Copy bello& given ;Test met'o# part o! Capture screen s'ot &eb#rivere7ample an# replace it &it' t'e ;Test met'o# part o! e7ample givenon T6IS 98 . Don t !orget to import above given 'ea#er les in youreclipse test.A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e#page .

;Test public voi# test A t'ro&s Interrupte#87ception" I)87ceptionL<<Capture entire page screens'ot an# t'en store it to #estination #rive

File screens'ot G

AATa@esScreens'ot #river .getScreens'ot sA)utputType.FI58 H File:tils.copyFileAscreens'ot" ne& FileAKD ZZscreens'ot.4pgK H

Page 198: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 198/292

System.out.printAKScreens'ot is capture# an# store# in your D DriveK H Q

ou can put above synta7 un#er catc' bloc@ o! try to capture screens'oton !ailure.

> 9enerating +ouse 6over 8vent

Selenium WebDriver 9enerating +ouse 6over 8vent )n +ain +enu ToClic@ )n Sub +enu

Selenium WebDriver is totally !ree&are testing tool an# &e can use it !orso!t&are &eb application regression purpose. 6overing mouse on main

menu or any ot'er element o! &eb page or simulating mouse movementin &eb#river is not very toug' tas@. s you @no&" We can per!orm most o! actions an#operations #irectly in &eb#river an# you can 3I8W F8W )F T68+ ) T6IS

98 . But t'ere are also some actions &'ic' &e can not per!orm #irectlyin &eb#river an# to per!orm t'em" &e nee# to use some tric@y&ays. 9enerating mouse 'over event is one o! t'em.

To generate mouse 'over event in &eb#river" We can use a#vance# userinteractions I constructor K ctionsK &it' KmoveTo8lementK met'o#.Bunc' o! synta7 to 'over mouse on main menu is as bello&. ou can

replace 7pat' o! an element as per your re uirement in bello& givensynta7.

ctions actions G ne& ctionsA#river HWeb8lement moveonmenu G#river. n#8lementABy.7pat'AK<<#ivM;i#G menu1 N<#ivK Hactions.moveTo8lementAmoveonmenu Hactions.per!ormA H

bove e7ample &ill only move mouse on targete# element. It &ill notper!orm clic@ operation. To per!orm clic@ operation on sub menu" &e nee#to use clic@A action be!ore per!ormA as s'o&n in bello& e7ample.

pac@age 4unitreportpac@ageH

import 4ava.io.I)87ceptionH

import org.4unit. !terH

import org.4unit.Be!oreHimport org.4unit.TestH

Page 199: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 199/292

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium.Web8lementHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.open a.selenium.interactions. ctionsHimport org.open a.selenium.support.ui.87pecte#Con#itionsHimport org.open a.selenium.support.ui.WebDriverWaitH

public class +ytesting L WebDriver #river G ne& Fire!o7DriverA H ;Be!ore

public voi# be!oretestA L #river.manageA .&in#o&A .ma7imi?eA H #river.getAK'ttp <<only-testing-blog.blogspot.in<p<mouse-'over.'tmlK H Q ; !ter

public voi# a!tertestA L #river. uitA H Q

;Test

public voi# test A t'ro&s Interrupte#87ception" I)87ception

L<<9enerate mouse 'over event on main menu to clic@ on sub menu

ctions actions G ne& ctionsA#river H Web8lement moveonmenu G#river. n#8lementABy.7pat'AK<<#ivM;i#G menu1 N<#ivK H actions.moveTo8lementAmoveonmenu .moveTo8lementA#river. n#8lementABy.7pat'AK<<#ivM;i#G menu1c'oices N<aK .clic@A .per!ormA H

WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H

&ait.untilA87pecte#Con#itions.titleContainsAK9oogleK H Q Q > 6an#ling +ultiple Win#o&s 87ample o! 6an#ling +ultiple Bro&ser Win#o&s in Selenium WebDriver

Selenium WebDriver 'as built in KWebDriver.s&itc'ToA .&in#o&A K met'o#available to s&itc' !rom one &in#o& to anot'er &in#o& so it is very easyto 'an#le multiple &in#o&s in &eb#river. I! you remember" We can useKselectWin#o& K &in#o& comman# in selenium ID8 to select anot'er&in#o&. I! &in#o& #o not'ave any title or bot' &in#o& 'as same title t'en it &as very #i`cult to

Page 200: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 200/292

select ot'er &in#o& in selenium ID8. WebDriver 'as ma#e it very easy. ou can 'an#le multiple &in#o&s even i! all t'e &in#o&s #o not 'ave anytitle or contains same title.

WebDriver.getWin#o&6an#lesAIn WebDriver" We can use KWebDriver.getWin#o&6an#lesA K to get t'e'an#les o! all opene# &in#o&s by &eb#river an# t'en &e can use t'at&in#o& 'an#le to s&itc' !rom !rom one &in#o& to anot'er &in#o&.87ample Synta7 !or getting &in#o& 'an#les is as bello&.

Set StringO llWin#o&6an#les G #river.getWin#o&6an#lesA H

WebDriver.s&itc'ToA .&in#o&AWebDriver.s&itc'ToA .&in#o&A met'o# is use!ul to s&itc' !rom one&in#o& to anot'er &in#o& o! so!t&are &eb application. 87ample synta7 isas bello&.

#river.s&itc'ToA .&in#o&A&in#o&2 H

Bello& given &eb#river e7ample o! s&itc'ing &in#o& &ill e7plain you it#eeply. 87ecute it in your eclipse an# try to un#erstan# 'o& &eb#river #oit.

Copy bello& given ;Test met'o# part o! 'an#ling multiple &in#o&s o!&eb#river an# replace it &it' t'e ;Test met'o# part o! e7ample givenon T6IS 98 .A ote ;Test met'o# is mar@e# &it' pin@ color in t'at

lin@e# page .

;Test public voi# test A t'ro&s Interrupte#87ception

L #river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H #river. n#8lementABy.7pat'AK<<bMcontainsA." )pen e& age NK .clic@A H

<< 9et an# store bot' &in#o& 'an#les in array Set StringO llWin#o&6an#les G #river.getWin#o&6an#lesA H String &in#o&1 G AString llWin#o&6an#les.to rrayA M NH System.out.printAK&in#o&1 'an#le co#e G KP llWin#o&6an#les.to rrayAM N H String &in#o&2 G AString llWin#o&6an#les.to rrayA M1NH System.out.printAKZn&in#o&2 'an#le co#e GKP llWin#o&6an#les.to rrayA M1N H

<<S&itc' to &in#o&2Ac'il# &in#o& an# per!orming actions on it. #river.s&itc'ToA .&in#o&A&in#o&2 H #river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK+y

ameK H #river. n#8lementABy.7pat'AK<<inputM;valueG Bi@e NK .clic@A H #river. n#8lementABy.7pat'AK<<inputM;valueG Car NK .clic@A H

Page 201: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 201/292

#river. n#8lementABy.7pat'AK<<inputM;valueG Boat NK .clic@A H #river. n#8lementABy.7pat'AK<<inputM;valueG male NK .clic@A H T'rea#.sleepA% H

<<S&itc' to &in#o&1Aparent &in#o& an# per!orming actions on it. #river.s&itc'ToA .&in#o&A&in#o&1 H #river. n#8lementABy.7pat'AK<<optionM;i#G country( NK .clic@A H #river. n#8lementABy.7pat'AK<<inputM;valueG !emale NK .clic@A H #river. n#8lementABy.7pat'AK<<inputM;valueG S'o& +e lert NK .clic@A H #river.s&itc'ToA .alertA .acceptA H T'rea#.sleepA% H

<<)nce gain s&itc' to &in#o&2Ac'il# &in#o& an# per!orming actions onit. #river.s&itc'ToA .&in#o&A&in#o&2 H #river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .clearA H #river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK ameC'ange#K H T'rea#.sleepA% H #river.closeA H

<<)nce gain s&itc' to &in#o&1Aparent &in#o& an# per!orming actionson it. #river.s&itc'ToA .&in#o&A&in#o&1 H #river. n#8lementABy.7pat'AK<<inputM;valueG male NK .clic@A H T'rea#.sleepA% H

Q

> 3eri!y 8lement Is 8nable# )r ot

6o& To 3eri!y 8lement Is 8nable# )r Disable# in Selenium WebDriver

During your Selenium WebDriver test case creation" !re uently you nee#to veri!y t'at your targete# element is enable# or #isable# be!oreper!orming action on it. Web#river 'as built in met'o# is8nable#A toc'ec@ t'e element enable status. 3eri cation o! element is #isable an#veri cation o! element invisible is totally #i=erent.8lement is #isable# means it is visible but not e#itable an# element isinvisible means it is 'i##en. 3I8W T6IS 8V + 58 to @no& 'o& to &ait tillelement is visible on t'e page.

is8nable#Ais8nable#A &eb#river met'o# &ill veri!y an# return true i! speci e#element is enable#. 8lse it &ill return !alse. 9eneric synta7 to store an#print element s status value is as bello&.

Page 202: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 202/292

boolean !name G#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .is8nable#A HSystem.out.printA!name H

We can use is8nable#A met'o# &it' i! con#ition to ta@e action base# onelement s enable# status. Bello& given e7ample &ill e7plain you #eeplyabout is8nable#A &eb#river met'o#. I! you are selenium ID8 user t'en3I8W T6IS )ST to @no& 'o& to &ait !or targete# element becomese#itable.

Copy bello& given ;Test met'o# part o! c'ec@ element enable# statusan# replace it &it' t'e ;Test met'o# part o! e7ample given on T6IS

98 . A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e# page .

;Test public voi# test A t'ro&s Bi=87ception" I)87ception"Interrupte#87ception

L boolean !name G#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .is8nable#A H System.out.printA!name H

Web8lement rstname G#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK H Web8lement lastname G#river. n#8lementABy.7pat'AK<<inputM;nameG lname NK H

<<3eri!y First name te7t bo7 is enable# or not an# t'en print relate#message. i!A rstname.is8nable#A L System.out.printAKZnTe7t bo7 First name is enable#. Ta@e your action.K H Q else L System.out.printAKZnTe7t bo7 First name is #isable#. Ta@e your action.K H Q

<<3eri!y 5ast name te7t bo7 is enable# or not an# t'en print relate#message. i!Alastname.is8nable#A L System.out.printAKZnTe7t bo7 5ast name is enable#. Ta@e your action.K H Q else L System.out.printAKZnTe7t bo7 5ast name is #isable#. Ta@e your action.K H Q Q

Page 203: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 203/292

bove given e7ample" &ill c'ec@ t'e status o! First name an# 5ast namete7t bo7 an# print message in console base# on element is enable# or#isable#.

> 8nable<Disable 8lement 6o& To 8nable<Disable Te7tbo7 In Selenium WebDriver )n T'e Fly

We can @no& element s enable#<#isable# status very easilyusing is8nable#A met'o# in selenium &eb#river as #escribe# in T6IS8V + 58 )ST . o& supposing you 'ave a scenario &'ere you &ants toenable any #isable# te7t bo7 or #isable any enable# te7t bo7 t'en 'o& to#o itR Web#river #o not 'ave any built inmet'o# by &'ic' you per!orm t'is action. But yes" Web#river 'as

Javascript87ecutor inter!ace by &'ic' &e can e7ecute any 4avascriptusing e7ecuteScriptA met'o#.

I 'ave poste# many posts on 'o& to e7ecute 4avascript in selenium&eb#river to per!orm #i=erent actions. ou can vie& all t'ose e7ample on

T6IS 5I Y . Same &ay" &e can enable or #isable any elementusing Javascript87ecutor inter!ace #uring &eb#river test case e7ecution.

To #isable te7t bo7" &e &ill use 'tml #om set ttributeA met'o# an# toenable te7t bo7" &e &ill use 'tml #om remove ttributeA met'o#&it' e7ecuteScriptA met'o#. Javascript synta7 !or bot' o! t'ese is asbello&.

#ocument.get8lementsBy ameA !name M N.set ttributeA #isable# " H#ocument.get8lementsBy ameA lname M N.remove ttributeA #isable# H

o& let &e use bot' t'ese 4avascripts practically to un#erstan# t'embetter. 3I8W T6IS )ST to see 'o& to enable or #isable te7t bo7 inselenium ID8.

Copy bello& given ;Test met'o# part o! enable or #isable element an#replace it &it' t'e ;Test met'o# part o! e7ample given on T6IS

98 . A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e# page .

;Test public voi# test A t'ro&s Bi=87ception" I)87ception"Interrupte#87ception

L boolean !be!ore G#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .is8nable#A H System.out.printAKZnBe!ore First ame Te7t bo7 enable# status is KP!be!ore H boolean lbe!ore G#river. n#8lementABy.7pat'AK<<inputM;nameG lname NK .is8nable#A H System.out.printAKZnBe!ore 5ast ame Te7t bo7 enable# status is KPlbe!ore H

Page 204: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 204/292

T'rea#.sleepA2 H

<<To #isable First ame te7t bo7 Javascript87ecutor 4avascript G AJavascript87ecutor #riverH String to#isable G K#ocument.get8lementsBy ameA !nameM N.set ttributeA #isable# " HKH 4avascript.e7ecuteScriptAto#isable H T'rea#.sleepA2 H

<<To enable 5ast ame te7t bo7 String toenable G K#ocument.get8lementsBy ameA lnameM N.remove ttributeA #isable# HKH 4avascript.e7ecuteScriptAtoenable H T'rea#.sleepA H

boolean !a!ter G#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .is8nable#A H System.out.printAKZn !ter First ame Te7t bo7 enable# status is KP!a!ter H boolean la!ter G#river. n#8lementABy.7pat'AK<<inputM;nameG lname NK .is8nable#A H System.out.printAKZn !ter 5ast ame Te7t bo7 enable# status is KPla!ter H

Q

> 6an#ling lert" Con rmation an# rompt popup Selenium WebDriver 6an#ling Javascript lerts" Con rmations n#

rompts

lerts" Con rmation an# rompts are very commonly use# elements o! any &ebpage an# you must @no& 'o& to 'an#le all t'ese popups Inselenium &eb#river. I! you @no&" Selenium ID8 'as many comman#s to'an#le alerts" con rmations an# prompt popups li@eassert lert " assertCon rmation " store lert "veri!y lert resent " etc.. First o! all" 5et me s'o& you all t'ree #i=erentpopup types to remove con!usion !rom your min# an# t'en &e &ill see'o& to 'an#le t'em In selenium &eb#river.

lert opup9enerally alert message popup #isplay &it' alert te7t an# )@ button ass'o&n In bello& given Image.

Page 205: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 205/292

Con rmation opupCon rmation popup #isplays &it' con rmation te7t" )@ an# Cancel buttonas s'o&n In bello& given Image.

rompt opuprompts &ill 'ave prompt te7t" Input te7t bo7" )@ an# Cancel buttons.

Selenium &eb#river 'as Its o&n lert Inter!ace to 'an#le all above#i=erent popups. lert Inter!ace 'as #i=erent met'o#s li@e acceptA "#ismissA " getTe7tA " sen#YeysA4ava.lang.String @eysToSen# an# &e canuse all t'ese met'o#s to per!orm #i=erent actions on popups.

3I8W W8BD0I380 8V + 58S ST8 B ST8

5oo@ at t'e bello& given simple e7ample o! 'an#ling alerts" con rmationsan# prompts In selenium &eb#river. Bello& given e7ample &ill per!orm

#i=erent actions A5i@e clic@ on )@ button" Clic@ on cancel button" retrieve

Page 206: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 206/292

Page 207: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 207/292

String lert2 G 2.getTe7tA H System.out.printlnA lert2 H T'rea#.sleepA2 H <<To clic@ )n cancel button o! con rmation bo7. 2.#ismissA H

<< rompt op up 6an#ling. #river. n#8lementABy.7pat'AK<<buttonMcontainsA." S'o& +e

rompt NK .clic@A H lert G #river.s&itc'ToA .alertA H String lert G .getTe7tA H System.out.printlnA lert H <<To type te7t In te7t bo7 o! prompt pop up. .sen#YeysAKT'is Is Jo'nK H T'rea#.sleepA2 H .acceptA HQ

Q

T'is &ay you can 'an#le #i=erent @in# o! alerts very easily using lertInter!ace o! selenium &eb#river. 8VT )ST &ill s'o& you 'o& to 'an#leune7pecte# alerts In selenium &eb#river. > 6an#le :ne7pecte# lert

6o& To 6an#le :ne7pecte# lerts In Selenium WebDriver

Some times &'en &e bro&sing so!t&are &eb application" Display someune7pecte# alerts #ue to some error or some ot'er reasons. T'is @in# o! alerts not #isplay every time but t'ey are #isplaying only some time. I! you 'ave create# &eb#river test case !or suc' page an# not 'an#le# t'is@in# o! alerts In your co#e t'enyour script &ill !ail Imme#iately I! suc' une7pecte# alert pop up #isplaye#.

We 'ave alrea#y learnt" 6o& to 'an#le e7pecte# alert popups In selenium&eb#river In T6IS 8V + 58 . o& une7pecte# alert appears only sometimes so &e can not &rite #irect co#e to accept or #ismiss t'at alert. Int'is @in# o! situation" &e 'ave to 'an#le t'em specially.

ou can 3I8W 55 W8BD0I380 T:T)0I 5S ) 8 B ) 8 .

To 'an#le t'is @in# o! une7pecte# alerts" ou must at least a&are about on&'ic' action suc' une7pecte# alert Is generate#. Sometimes" T'ey aregenerate# #uring page loa# an# sometime t'ey are generate# &'en youper!orm some action. So rst o! all &e 'ave to note #o&n t'e action &'eresuc' une7pecte# alert Is generate# an# t'en &e can c'ec@ !or alert a!terper!orming t'at action. We nee# to use try catc' bloc@ !or c'ec@ing suc'une7pecte# alters because I! &e &ill use #irect co#eA&it'out try catc' to

accept or #ismiss alert an# I! alert not appears t'en our test case &ill !ail.try catc' can 'an#le bot' situations.

Page 208: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 208/292

I 'ave one e7ample &'ere alert Is #isplaying &'en loa#ing page. So &ecan c'ec@ !or alert Insi#e try catc' bloc@ a!ter page loa# as s'o&n Inbello& given e7ample. !ter loa#ing page" It &ill c'ec@ !or alert. I! alert Ist'ere on t'e page t'en It &ill #ismiss It else It &ill go to catc' bloc@ an#print message as s'o&n In bello& given e7ample.

0un bello& given e7ample In eclipse &it' testng an# observe t'e result.

pac@age Testng ac@H

import 4ava.util.concurrent.Time:nitH

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

public class une7pecte# alert L WebDriver #riverH

;Be!oreTest public voi# setupA t'ro&s 87ception L #river Gne& Fire!o7DriverA H

#river.manageA .&in#o&A .ma7imi?eA H #river.manageA .timeoutsA .implicitlyWaitA%" Time:nit.S8C) DS H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< (<alert (.'tmlK H Q

; !terTest public voi# tearDo&nA t'ro&s 87ception L #river. uitA H Q

;Test public voi# Te7tA t'ro&s Interrupte#87ception L <<To 'an#le une7pecte# alert on page loa#. tryL

#river.s&itc'ToA .alertA .#ismissA HQcatc'A87ception e LSystem.out.printlnAKune7pecte# alert not presentK H

Q

#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK!nameK H

Page 209: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 209/292

#river. n#8lementABy.7pat'AK<<inputM;nameG lname NK .sen#YeysAKlnameK H #river. n#8lementABy.7pat'AK<<inputM;typeG submit NK .clic@A H QQ

bove given &eb#river co#e Is 4ust !or e7ample. It Is 4ust e7plaining t'e&ay o! using try catc' bloc@ to 'an#le une7pecte# alert. ou can use suc'try catc' bloc@ In t'at area &'ere you are !acing une7pecte# alerts very!re uently.

Day ( - WebDriver Wait For 87amples :sing J:nit n# 8clipse

1 . 6o& to apply implicit &ait in selenium WebDriver script

6o& to use implicit &ait in selenium &eb#river an# &'y

T'ere are 2 types o! &aits available in Web#river<Selenium 2. )ne o! t'emis implicit &ait an# anot'er one is e7plicit &ait. Bot' AImplicit &ait an#e7plicit &ait are use!ul !or &aiting in WebDriver. :sing &aits" &e aretelling WebDriver to &ait !or a certain amount o! time be!ore going to ne7tstep. We &ill see about e7plicit&ait in my upcoming posts . In t'is post let me tell you &'y an# 'o& to useimplicit &ait in &eb#river.

W'y ee# Implicit Wait In WebDrivers you @no&s sometimes" some elements ta@es some time to appear on

page &'en bro&ser is loa#ing t'e page. In t'is case" sometime your&eb#river test &ill !ail i! you 'ave not applie# Implicit &ait in your testcase. I! implicit &ait is applie# in your test case t'en &eb#river &ill &ait!or speci e# amount o! time i! targete# element not appears on page. syou @no&" &e can Set #e!ault timeout or use KsetTimeoutK comman# inselenium ID8 &'ic' is same as implicit &ait in &eb#river.

I! you &rite implicit &ait statement in you &eb#river script t'en it &ill beapplie# automatically to all elements o! your test case. I am suggestingyou to use Implicit &ait in your all test script o! so!t&are &eb application&it' 1 to 1% secon#s. In &eb#river" Implicit &ait statement is as bello&.

6o& To Write Implicit Wait In WebDriver

#river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H

bove statement &ill tell &eb#river to &ait !or 1% secon#s i! targete#element not !oun#<not appears on page. 5e &e loo@ at simple e7emple to

un#erstan# implicit &ait better.

Page 210: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 210/292

Copy bello& given ;Test met'o# part an# replace it &it' t'e ;Testmet'o# part o! e7ample given on t'is page . A ote ;Test met'o# ismar@e# &it' pin@ color in t'at e7ample .

;Test

public voi# test AL#river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H

#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK+yameK H

#river. n#8lementABy.7pat'AK<<inputM;nameG name7y? NK H Q

In bove &eb#river test case &it' J:nit e7ample" 1st 8lement7pat'AK<<inputM;nameG !name NK &ill be !oun# on page but element

7pat'AK<<inputM;nameG name7y? NK is not t'ere on page. So in t'is case&eb#river &ill &ait !or 1% to locate t'at element on page because &e 'ave&ritten implicit &ait statement in our co#e. t last &eb#river test &ill !ailbecause 7pat'AK<<inputM;nameG name7y? NK is not on t'e page.

11.WebDriver87amples

6o& to &ait !or element to be clic@able in selenium &eb#river usinge7plicit &ait

In my previous post " We 'ave seen 'o& to &ait implicitly in selenium

&eb#river. 5et me remin# you one t'ing is implicit &ait &ill be applie# toall elements o! test case by #e!ault &'ile e7plicit &ill be applie# totargete# element only. T'is is t'e #i=erence bet&een implicit &ait an#e7plicit &ait. Still i am suggestingyou to use implicit &ait in your test script.

I! you @no&s" In selenium ID8 &e can use K &aitFor8lement resent K orKveri!y8lement resent K to &ait !or or veri!y t'at element is present or noton page. In selenium &eb#river" &e can #o same t'ing using e7plicit &aito! elementToBeClic@ableABy locator . Full synta7 is as bello&.

WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H&ait.untilA87pecte#Con#[email protected]\submitButtonK H

6ere above synta7 &ill &ait till 1% secon#s to become targete#elementA\submitButton clic@able i! it is not clic@able or not loa#e# on t'epage o! so!t&are &eb application. s soon as targete# element becomesclic@able" &eb#river &ill go !or per!orm ne7t action. ou can increase or#ecrease &eb#river &ait time !rom 1%.

5et me give you practical e7ample !or t'e element to be clic@able.

Page 211: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 211/292

pac@age 4unitreportpac@ageH

import 4ava.util.concurrent.Time:nitHimport 4ava.io.FileInputStreamHimport 4ava.io.I)87ceptionHimport 47l.S'eetHimport 47l.Wor@boo@Himport 47l.rea#.bi=.Bi=87ceptionHimport org.4unit. !terHimport org.4unit.Be!oreHimport org.4unit.TestHimport org.open a.selenium.ByHimport org.open a.selenium.Javascript87ecutorHimport org.open a.selenium.WebDriverHimport org.open a.selenium.Web8lementHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.open a.selenium.support.ui.87pecte#Con#itionsHimport org.open a.selenium.support.ui.SelectHimport org.open a.selenium.support.ui.WebDriverWaitH

public class +ytest1 L WebDriver #river G ne& Fire!o7DriverA H ;Be!ore

public voi# be!oretestA L

#river.manageA .&in#o&A .ma7imi?eA H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1 <11<ne&-test.'tmlK H #river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H Q ; !ter

public voi# a!tertestA L #river. uitA H Q

;Test public voi# test AL

#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK+yameK H

<<Wait !or element to be clic@able WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H &ait.untilA87pecte#Con#[email protected]\submitButtonK H

#river. n#8lementABy.cssSelectorAK\submitButtonK .clic@A H

Page 212: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 212/292

Q public voi# 6ig'lig't+y8lementAWeb8lement element L

!or Aint i G H i 1 H iPPL Javascript87ecutor 4avascript G AJavascript87ecutor #riverH

4avascript.e7ecuteScriptAKargumentsM N.set ttributeA style "argumentsM1N HK" element" Kcolor orangeH bor#er $p7 soli# orangeHK H 4avascript.e7ecuteScriptAKargumentsM N.set ttributeA style "argumentsM1N HK" element" Kcolor pin@H bor#er $p7 soli# pin@HK H 4avascript.e7ecuteScriptAKargumentsM N.set ttributeA style "argumentsM1N HK" element" Kcolor yello&H bor#er $p7 soli# yello&HK H 4avascript.e7ecuteScriptAKargumentsM N.set ttributeA style "argumentsM1N HK" element" KK H

QQ

Q

0un above given e7ample o! &ait !or element to be clic@able in selenium&eb#river in your eclipse &it' 4unit. Clic@ 'ere to vie& #i=erent postson 'o& to use 4unit &it' eclipse !or your &eb#river test.

>WaitForTe7t

WebDriver - &ait !or te7t to be present &it' e7ample using e7plicit &ait

Some times you nee# to &ait !or te7t" &ait !or element " &ait !or alert

be!ore per!orming actions in your regular test cases. 9enerally &e nee# touse t'is types o! con#itions in our test case &'en so!t&are &ebapplication page is navigating !rom one page to ot'er page. In myprevious post" &e 'ave seen 'o& to &rite an# use &ait !or element synta7in our test case. o& let &e see 'o& can &e !orce &eb#river to &ait i!e7pecte# te7t is not #isplaye# on targete# element.

s you @no&" &e can use K &aitForTe7t K or K&aitForTe7t resent K comman#sin selenium ID8 to &ait till targete# te7t appears on page or element. In&eb#river" &e can use synta7 as s'o&n bello&.

WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H&ait.untilA87pecte#Con#itions.te7tToBe resentIn8lement5ocate#ABy.7pat'AK<<#ivM;i#G time5e!t NK " KTime le!t * secon#sK H

5oo@ in to above synta7. 6ere i 'aveuse# te7tToBe resentIn8lement5ocate#ABy" String to &ait till e7pecte#te7t appears on targete# element. So 'ere &'at WebDriver &ill #o is it &illc'ec@ !or e7pecte# te7t on targete# element on every % millisecon#san# &ill e7ecute ne7t step as soon as e7pecte# te7t appears on targete#element.

0un bello& given practical e7ample.in eclipse an# see 'o& it &or@s.

Page 213: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 213/292

Copy bello& given ;Test met'o# part o! &ait !or te7t e7ample an# replaceit &it' t'e ;Test met'o# part o! e7ample given on t'ispage . A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e# page .

;Test

public voi# test AL

#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAKalpes'K H WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H &ait.untilA87pecte#Con#[email protected]\submitButton1K H #river. n#8lementABy.cssSelectorAK\submitButtonK .clic@A H &ait.untilA87pecte#Con#itions.te7tToBe resentIn8lement5ocate#ABy.7pat'AK<<#ivM;i#G time5e!t NK " KTime le!t * secon#sK H Q

In above e7ample" W'en WebDriver clic@s on \submitButton page &ill bere!res'e# an# t'en WebDriver &ill &ait !or te7t G KTime le!t * secon#sK ontargete# element

WebDriver 'o& to &ait !or alert in selenium 2

s &e 'ave seen in my previous posts" &e canuse te7tToBe resentIn8lement5ocate#ABy" String WebDriver con#ition to&ait !or te7t present on page an# &e can use elementToBeClic@ableABylocator to &ait !or element clic@able<present on page. I! you rea# bot't'ose posts" We 'a# use# e7plicit &aits inbot' t'e cases an# bot' are canne# e7pecte# con#itions o! &eb#river so&e can use t'em #irectly in our test case &it'out any mo#i cations.

Sometimes you &ill !ace &ait !or alert scenario &'ere you 'ave to &ait !oralert be!ore per!orming any action on your so!t&are application &eb page.

I! you @no&" &e can use K &aitFor lert K comman# in selenium ID8 to'an#le alerts. In WebDriver<Selenium 2" ou can use WebDriver s built incanne# con#ition alertIs resentA &it' &ait comman# as bello&.

WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H&ait.untilA87pecte#Con#itions.alertIs resentA H

5oo@ in to above synta7. 1st synta7 #escribe# 'o& muc' time it 'as to&ait an# 2n# synta7 #escribes t'e &aiting con#ition. 6ere &e 'aveuse# alertIs resentA con#ition so it &ill &ait !or t'e alert on page. 5et megive you !ull practical e7ample to #escribe scenario per!ectly.

Page 214: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 214/292

Copy paste bello& given test case in your eclipse &it' J:nit an# t'en runit. ou can 3ie& 'o& to con gure eclipse &it' J:nit i! you 'ave no i#ea.

pac@age 4unitreportpac@ageH

import 4ava.util.concurrent.Time:nitH

import org.4unit. !terHimport org.4unit.Be!oreHimport org.4unit.TestHimport org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.open a.selenium.support.ui.87pecte#Con#itionsHimport org.open a.selenium.support.ui.WebDriverWaitH

public class +ytest1 L WebDriver #river G ne& Fire!o7DriverA H ;Be!ore

public voi# be!oretestA L #river.manageA .&in#o&A .ma7imi?eA H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< 1<ne&-testing.'tmlK H

Q ; !ter

public voi# a!tertestA L #river. uitA H Q

;Test

public voi# test AL

#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK+yameK H WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H &ait.untilA87pecte#Con#itions.alertIs resentA H String alrt G #river.s&itc'ToA .alertA .getTe7tA H System.out.printAalrt H Q

Q

bove e7ample &ill &ait !or t'e alert on page an# as soon as alert appears

Page 215: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 215/292

on page" it &ill store alert te7t in variable alrt an# t'en &ill print to it inconsole.

Selenium WebDriver &ait !or title &it' e7ample

WebDriver 'as many Canne# 87pecte# Con#itions by &'ic' &e can !orce&eb#river to &ait e7plicitly. 6o&ever Implicit &ait is more practical t'ane7plicit &ait in WebDriver. But in some special cases &'ere implicit &ait isnot able to 'an#le your scenario t'en in t'at case you nee# to use e7plicit&aits in your testcases. ou can vie& #i=erent posts on e7plicit &aits &'ere I 'ave#escribe# WebDriver s #i=erent Canne# 87pecte# con#itions &it'e7amples.

I! you 'ave a scenario &'ere you nee# to &ait !or title t'en you canuse titleContainsA4ava.lang.String title &it' &eb#river &ait. ou nee# toprovi#e some part o! your e7pecte# so!t&are &eb application page title&it' t'is con#ition as bello&.

WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H&ait.untilA87pecte#Con#itions.titleContainsAK +yTestK H

In above synta7" K +yTestK is my &eb page s e7pecte# title an# 1%secon#s is ma7 &aiting time to appear title on &eb page. I! title &ill notappears &it'in 1% secon#s #ue to t'e any reason t'en your test case &ill

!ails &it' timeout error.

First run bello& given test case in your eclipse an# t'en try same test case!or your o&n so!t&are application.

Copy bello& given ;Test met'o# part o! &ait !or title e7ample an# replaceit &it' t'e ;Test met'o# part o! e7ample given on t'ispage . A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e# page .

;Test public voi# test A

L#river. n#8lementABy.7pat'AK<<inputM;nameG !name NK .sen#YeysAK+yameK H

#river. n#8lementABy.7pat'AK<<aMcontainsAte7tA " Clic@ 6ere NK .clic@A H

<<Wait !or page title WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H &ait.untilA87pecte#Con#itions.titleContainsAK +yTestK H

<<9et an# store page title in to variable

Page 216: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 216/292

String title G #river.getTitleA H System.out.printAtitle H Q

In above e7ample" &'en &eb#river &ill clic@ on Clic@ 6ere lin@" anot'erpage &ill open. During page navigation" &eb#river &ill &ait !or e7pecte#page title.

Selenium WebDriver 6o& to &ait till element visible or appear or presenton page

In any so!t&are &eb application s &eb#river test case" you can easily &ait!or presence o! element using I+ 5ICIT W IT . I! you 'ave use# implicit&ait in your test case t'en you #o not re uire to &rite any e7tracon#itional statement to &ait !or element visibility. But in some cases" i! any element is ta@ing too muc'time to be visible on page t'en you can use 8V 5ICIT W IT con#ition inyour test case.

I! you 'ave a scenario to &ait till element visible on page t'en selenium&eb#river<Selenium 2 'as its o&n met'o#name# visibility)!8lement5ocate#ABy locator to c'ec@ t'e visibility o! element on page. We can use t'is met'o# &it' e7plicit &eb#river &aitcon#ition to &ait till element visible o! present on page. 6o&ever" &e can&ait !or element to be clic@able using elementToBeClic@ableABy locator

met'o# as #escribe# in T6IS 0TIC58 .

Synta7 to &ait till element visible on page is as bello&. It &ill &ait ma7 1%secon#s !or element. s soon as element visible on page" &eb#river &illgo !or e7ecuting ne7t statement.WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H&ait.untilA87pecte#Con#itions.visibility)!8lement5ocate#ABy.7pat'AK<<inputM;i#G te7t NK H

6ere is practical e7ample o! &ait !or element visible. 87ecute it once an#t'en use it !or your so!t&are &eb application test scenario.

Copy bello& given ;Test met'o# part o! &ait !or visibility o! elementlocate# e7ample an# replace it &it' t'e ;Test met'o# part o! e7amplegiven on T6IS 98 .A ote ;Test met'o# is mar@e# &it' pin@ color in t'atlin@e# page .

;Test public voi# test A t'ro&s Interrupte#87ception" I)87ceptionL

<<To &ait !or element visible WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H

Page 217: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 217/292

&ait.untilA87pecte#Con#itions.visibility)!8lement5ocate#ABy.7pat'AK<<inputM;i#G te7t NK H

#river. n#8lementABy.7pat'AK<<inputM;i#G te7t NK .sen#YeysAKTe7t bo7

is visible no&K H System.out.printAKTe7t bo7 te7t is no& visibleK H Q

6o& to apply &ait in &eb#river till element becomes invisible or 'i##en

s you @no&" WebDriver is so!t&are &eb application regression testingtool an# you &ill !ace many problems #uring your &eb#river test casecreation an# e7ecution. ou must @no&s alternate solution i! one not&or@s !or your test scenario. For e7ample" you 'ave use# I+ 5ICIT W IT inyour test case but i! it isnot su`cient to 'an#le your con#ition o! &aiting till element becomesinvisible !rom page t'en you can use it s alternative &ay o! using 8V 5ICITW IT in your test case.

In my 083I):S )ST " I 'ave #escribe useo! visibility)!8lement5ocate#ABy locator met'o# &it' e7plicit &ait to &aittill element becomes visible Anot 'i##en on page. o& let &e loo@ atopposite si#e. I! you &ants &eb#river to &ait till element becomes

invisible or 'i##en !rom page t'en &e canuse invisibility)!8lement5ocate#ABy locator met'o# &it' &ait con#ition.

Synta7 !or &ait till element invisible !rom page is as bello&.

WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H&ait.untilA87pecte#Con#itions.invisibility)!8lement5ocate#ABy.7pat'AK<<inputM;i#G te7t$ NK H

In above synta7" you can c'ange &eb#river &ait time !rom 1% to 2 " ormore as per your re uirement. lso you can use any )T680 858+8 T5)C TI 9 +8T6)DS e7cept By.7pat'. ractical e7ample o! &eb#river &aittill element becomes invisible is as bello&.

Copy bello& given ;Test met'o# part o! &ait till element invisible an#replace it &it' t'e ;Test met'o# part o! e7ample given on T6IS 98 .A ote ;Test met'o# is mar@e# &it' pin@ color in t'at lin@e# page .

;Test public voi# test A t'ro&s Interrupte#87ception" I)87ceptionL<<Wait !or element invisible

WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H

Page 218: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 218/292

&ait.untilA87pecte#Con#itions.invisibility)!8lement5ocate#ABy.7pat'AK<<inputM;i#G te7t$ NK H

System.out.printAKTe7t bo7 te7t$ is no& invisibleK HQ

Day * - WebDriver )t'er 87amples

12. WebDriver )t'er 87amples -

Selenium WebDriver Di=erence Bet&een n#8lement an# n#8lements&it' e7ample

We 'ave seen many e7amples o! &eb#river s n#8lementA in my previousposts. ou &ill n# synta7 o! n#8lementA &it' e7ample on T6IS 98 i! you &ants to use it in your so!t&are &eb application test case. SeleniumWebDriver 'as one more relate# comman# n#8lements. o& main

uestion is &'at is t'e#i=erence bet&een n#8lement an# n#8lements met'o#s in selenium 2RFirst o! all let me provi#e you #i=erence #escription an# t'en e7ample.

n#8lementA met'o#

• We nee# to use n#8lement met'o# !re uently in our &eb#river testcase because t'is is t'e only &ay to locate any element in&eb#river.

• n#8lement met'o# is use!ul to locating targete# single element.

• I! targete# element is not !oun# on t'e page t'en it &illt'ro& oSuc'8lement87ception.

n#8lementsA met'o#

• We are using n#8lements met'o# 4ust occasionally.

• n#8lements met'o# &ill return list o! all t'e matc'ing elements!rom current page as per given element locator mec'anism.

• I! not !oun# any element on current page as per given elementlocator mec'anism" it &ill return empty list.

Page 219: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 219/292

o& let me provi#e you simple practical e7ample o! n#8lement met'o#an# n#8lements met'o# to s'o& you clear #i=erence. ou can 3I8W 55

0 CTIC 5 8V + 58S )F S858 I:+ W8BD0I380 one by one.

Copy bello& given ;Test met'o# part o! n#8lement an# n#8lementsmet'o# e7amples an# replace it &it' t'e ;Test met'o# part o! e7amplegiven on T6IS 98 . A ote ;Test met'o# is mar@e# &it' pin@ colorin t'at lin@e# page .;Test public voi# test A t'ro&s Interrupte#87ceptionLWeb8lement option G

#river. n#8lementABy.7pat'AK<<optionM;i#G country% NK H System.out.printAoption.get ttributeAKi#K PK - KPoption.getTe7tA H 5ist Web8lementO optionsG #river. n#8lementsABy.7pat'AK<<optionK H System.out.printlnAoptions.si?eA H !orAint iG Hi Goptions.si?eA HiPP L String str G options.getAi .get ttributeAKi#K PK -KPoptions.getAi .getTe7tA H System.out.printlnAstr H

Q Q

In above e7ample" n#8lement &ill locate only targete# element an# t'enprint its i# an# te7t in console &'ile n#8lements &ill locate all t'oseelements o! current page &'ic' are un#er given 7pat' G <<option. !or loopis use# to print all t'ose element s i# an# te7t in console.

Same &ay you can locate all input elements" lin@ elements etc..using n#8lements met'o#.

6o& To 9enerate n# Insert 5og In Selenium Web#river :sing log$4 Jar

ssume you 'ave prepare# test case !or your so!t&are &eb applicationtest scenario Ausing selenium &eb#river &it' eclipse an# 4unit an# no&you &ants generate log le to insert your e7ecution time log messages into it. 6o& you &ill #o itR Selenium &eb#river #o not 'ave any built in!unction or met'o# togenerate log le. In t'is case you can use apac'e logging service. 5et me#escribe you steps to generate log le in selenium &eb#river.

Steps To 9enerate n# Insert 5og In Selenium Web#river

1. Do&nloa# log$4 4ar le !rom logging.apac'e.org

Page 220: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 220/292

• Clic@ 6ere to go to log$4 4ar le #o&nloa#ing page. AT'is 5in@ :05may c'ange in !uture .

• Clic@ on Xip !ol#er lin@ as s'o&n in bello& image. Current versiono! log$4 4ar le is log$4-1.2.1*. 3ersion o! log$4 4ar le may be#i=erent in !uture #ue to t'e !re uent version c'ange.

• W'en you clic@ on ?ip !ol#er lin@" it &ill s'o& you ?ip mirror lin@s to#o&nloa# !ol#er. Do&nloa# an# save ?ip !ol#er by clic@ing on mirrorlin@.

• o& 87tract t'at ?ip !ol#er an# loo@ insi#e t'at e7tracte# !ol#er. ou&ill n# log$4-1.2.1*.4ar le in it.

2. Import log$4-1.2.1*.4ar le in your eclipse pro4ect !rom

• 0ig't clic@ on your pro4ect !ol#er an# go to Buil# at' -O Con gureBuil# pat'. It &ill open 4ava buil# pat' &in#o& as s'o&n in bello&image.

• Clic@ on ## 87ternal J 0s button an# select log$4-1.2.1*.4ar le. It&ill a## log$4-1.2.1*.4ar le in your buil# pat'.

• Clic@ on )Y button to close 4ava buil# pat' &in#o&.

Page 221: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 221/292

. Create roperties !ol#er un#er your pro4ect !ol#er an#a## log$4.properties le in it.

• Create !ol#er &it' name G roperties un#er your pro4ect by rig'tclic@ing on your pro4ect !ol#er.

• Clic@ 6ere to #o&nloa# log$4.properties le an# save it in roperties!ol#er &'ic' is create# un#er your pro4ect !ol#er.

• o& your properties !ol#er un#er pro4ect !ol#er &ill loo@s li@e bello&.

$. Set 0un Con guration T'is is optional step. ou nee# to set your 0un con gure i! log le is notgenerate# an# #isplay bello& given &arning message in your console&'en you run your test case.log$4 W 0 o appen#ers coul# be !oun# !or logger A#ao.'s lmanager .

• 9o to 0un -O 0un Con gurations -O Classpat'

Select :ser 8ntries -O Clic@ on #vance# button -O Select ##Fol#er -O n# t'en select roperties !ol#er !rom your pro4ect an#clic@ on )Y. o& your Class tab &ill loo@s li@e bello&.

Page 222: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 222/292

%. Create e7ampleASample test case un#er your pro4ect.Create your o&n test case or copy e7ample test case !rom 'ere an# pasteit in your class le.

(. Import log$4 'ea#er le in your pro4ectImport bello& given 'ea#er le in your test case class le.import org.apac'e.log$4.>H

*. 0eplace bello& given synta7 in your class le an# 0un your test case.

;Test public voi# test A t'ro&s Interrupte#87ception

L5ogger logH

#river. n#8lementABy.i#AKte7t1K .sen#YeysAK+y First ameK H log G 5ogger.get5oggerA+ytesting.class H log.in!oAKType In Te7t el#.K H Select my#rp#&n G ne& SelectA#river. n#8lementABy.i#AKCarlistK H my#rp#&n.selectBy3isibleTe7tAK u#iK H log G 5ogger.get5oggerA+ytesting.class H log.in!oAKSelect value !rom #rop #o&n.K H WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H &ait.untilA87pecte#Con#[email protected]#AKte7t2K H

Q

W'en your test case is e7ecute#" 9o to !ol#er G C Z5ogs. log.log le &ill becreate# over t'ere. )pen t'at le an# loo@ in to it. 5og &ritten in your testcase &ill be inserte# in your log le. In above e7ample" Synta7 mar@e#&it' in@ color are &ritten !or inserting log. o& you can &rite log at anyplace in your test case as s'o&n in above e7ample.

Creating )b4ect 0epository :sing roperties File In Selenium WebDriver

In selenium WebDriver" T'ere Is not any built In !acility to create ob4ectrepository. So maintenance o! page ob4ectsA age element locators

Page 223: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 223/292

Page 224: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 224/292

9ive le name ob4ects.properties on ne7t &in#o& an# clic@ on Finis'button.

It &ill a## ob4ect.properties le un#er your pac@age. o& copy pastebello& given lines in ob4ects.properties le. So no&t'is ob4ects.properties le Is ob4ect repository !or your &eb applicationpage calc.

ob4ects.properties le\Create# uni ue @ey !or I# o! all &eb elements.\ 87ample one Is @ey an# 1 Is ID o! &eb element button 1.oneG1t&oG2t'reeG

Page 225: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 225/292

!ourG$veG%

si7G(sevenG*eig'tG,nineG/?eroGe ualstoGe ualsclerG CresultG0esultbo7plusGplusminusGminusmulGmultiply

In above le" 5e!t si#e value Is @ey an# rig't si#e value Is elementlocatorAby I# o! all &eb elements o! &eb calculator. ou can use ot'erelement locator met'o#s too li@e 7pat'" css ect.. 3ISIT T6IS 5I Y to vie&#i=erent element locator met'o#s o! &eb#river.

o& let us create &eb#river test case to per!orm some operations oncalculation. In bello& given e7ample" ll t'e element locators are coming!rom ob4ects.properties le using ob4.get ropertyA@ey . 6ere @ey Isre!erence o! element locator value in ob4ects.properties le.

pac@age )b4ect0epoH

import 4ava.io.FileInputStreamHimport 4ava.io.I)87ceptionHimport 4ava.util. ropertiesH

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !ter+et'o#Himport org.testng.annotations.Be!ore+et'o#Himport org.testng.annotations.TestH

public class CalcTest L WebDriver #river G ne& Fire!o7DriverA H

;Be!ore+et'o# public voi# openbro&serA t'ro&s I)87ception L

#river.manageA .&in#o&A .ma7imi?eA H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< $<calc.'tmlK H

Q ; !ter+et'o#

public voi# closebro&serA L

Page 226: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 226/292

#river. uitA H Q

;Test public voi# Calc )perationsA t'ro&s I)87ceptionL <<Create )b4ect o! roperties Class. roperties ob4 G ne& ropertiesA H

<<Create )b4ect o! FileInputStream Class. ass le pat'. FileInputStream ob4 le G ne&FileInputStreamASystem.get ropertyAKuser.#irKPKZZsrcZZ)b4ect0epoZZob4ects.propertiesK H << ass ob4ect re!erence ob4 le to loa# met'o# o! roperties ob4ect. ob4.loa#Aob4 le H

<<Sum operation on calculator. << ccessing element locators o! all &eb elements usingob4.get ropertyA@ey #river. n#8lementABy.i#Aob4.get ropertyAKeig'tK .clic@A H #river. n#8lementABy.i#Aob4.get ropertyAKplusK .clic@A H #river. n#8lementABy.i#Aob4.get ropertyAK!ourK .clic@A H #river. n#8lementABy.i#Aob4.get ropertyAKe ualstoK .clic@A H String i G#river. n#8lementABy.i#Aob4.get ropertyAKresultK .get ttributeAKvalueK H System.out.printlnAob4.get ropertyAKeig'tK PK PKPob4.get ropertyAK!ourK PK G KPi H

#river. n#8lementABy.i#Aob4.get ropertyAKresultK .clearA H

<<Subtraction operation on calculator. << ccessing element locators o! all &eb elements usingob4.get ropertyA@ey #river. n#8lementABy.i#Aob4.get ropertyAKnineK .clic@A H #river. n#8lementABy.i#Aob4.get ropertyAKminusK .clic@A H #river. n#8lementABy.i#Aob4.get ropertyAKt'reeK .clic@A H #river. n#8lementABy.i#Aob4.get ropertyAKe ualstoK .clic@A H String 4 G#river. n#8lementABy.i#Aob4.get ropertyAKresultK .get ttributeAKvalueK H System.out.printlnAob4.get ropertyAKnineK PK - KPob4.get ropertyAKt'reeKPK G KP4 H QQ

0un above given e7ample In your eclipse an# observe e7ecution.

o& supposing I 'ave many suc' test cases an# some one 'as c'ange# I#o! any button o! calc application t'en &'at I 'ave to #oR I 'ave to mo#i!yall test casesR ns&er Is o. I 4ust nee# to mo#i!y ob4ects.properties lebecause I 'ave not use element s I# #irectly In any test case but I 'ave:se# 4ust Its @ey re!erence In all test cases.

Page 227: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 227/292

T'is &ay you can create ob4ect repository o! all your &eb elements Inone .properties le. ou can 3I8W +)08 T:T)0I 5S about selenium&eb#river.

1$. SeleniumWebDriverTestt'in@ all t'ose &'o are intereste# to atten# &eb#river intervie& s'oul#a&are about !e& basic intervie& uestions &it' t'eir correct ans&ers. I'ave create# !re uently as@e# &eb#river<selenium 2 intervie& uestionstest. It is not only !or intervie& purpose but it can improve your basic@no&le#ge too. For #etaile#@no&le#ge on &eb#river<selenium 2" you can re!er my &eb#river tutorialposts one by one.

:se o! &eb#river<Selenium 2 is increasing #ay by #ay an# no& manycompanies using &eb#river !or so!t&are application automation. o&&eb#river is going to be essential element o! so!t&are testing process.+any companies 'iring &eb#river pro!essionals no& a #ays so no& it isimportant !or all o! us to @no& &eb#river very closely.

ou can go t'roug' J: IT WIT6 W8BD0I380 or T8ST 9 WIT6W8BD0I380 tutorials i! you 'ave any relate# uery.

9o t'roug' t'is simple test" an# i 'ope it &ill 'elp you !or your ne7tintervie& preparation.

5oa#ing...

oste# by 8mail T'is BlogT'is[ S'are to T&itter S'are to Faceboo@ S'are to interest

Day , - Test 9 Frame&or@ Tutorials

1%. Intro#uction )! Test 9

Intro#uction )! Test 9 - :nit Testing Frame&or@ For Selenium WebDriver

Test 9 is unit testing !rame&or@ an# it 'as been most popular in very sorttime among 4ava #evelopers an# selenium &eb#river automation testengineers. I can say" Test 9 'as not only most o! all t'e !eatures o! J:nitbut also many more a#vance# !eatures in terms o! selenium &eb#rivertesting !rame&or@. Basically"

Test 9 s #evelopment is inspire# !rom combination o! :nit an# J:nit. oucan 3I8W J: IT WIT6 W8BD0I380 T:T)0I 5S D 8V + 58S be!oregoing to learn Test 9 !rame&or@. T'ere are many ne& !eatures in Test 9&'ic' ma@es it more po&er!ul" stronger an# easier to use. 3I8WSI+I5 0ITI8S D DIFF808 C8S bet&een Test 9 an# J:nit.

Test 9 +a4or Features

Page 228: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 228/292

• Test 9 'as a built in multiple Be!ore an# !ter an# ot'erannotations li@e ;Be!oreSuite" ; !terSuite" ;Be!oreTest";Be!ore9roups" ;Data rovi#er" ; arameters" ;Test etc.. llt'e Test 9 annotations are very easy to un#erstan# an# implement.We &ill loo@ eac' an# every annotation &it' #etaile# #escription inmy latter posts.

• We can con gure #epen#ent test met'o#s in Test 9 means TestT&oA is #epen#ent to Test)neA . We can also con gure t'at i! earlier test met'o# ATest)neA !ails #uring e7ecution t'en#epen#ent test met'o#ATestT&oA 'as to be e7ecute# or not.

• T'ere is not re uire# to e7ten# any class in Test 9.

• We can con gure our test suite using test met'o#s" classes"

pac@ages" groups" etc.. in single testng.7ml le.• Support o! con guring test groups li@e bac@en#test-group"

!ronten#test-group etc.. an# &e can tell Test 9 to e7ecute onlyspeci c group<groups.

• Test 9 is supporte# by many tools an# plug-ins li@e 8clipse" +aven"ID8 " etc..

• Test 9 support parallel testing" arameteri?ation A assing

parameters values t'roug' V+5 con guration " Data Driven TestingAe7ecuting same test met'o# multiple times using #i=erent#ata .

• Test 9 'as built in 6T+5 report an# V+5 report generation !acility. It'as also built in logging !acility.

3I8W +)08 T8ST 9 T:T)0I 5S

ll t'ese are ma4or !eatures o! Test 9 an# some o! t'em are available in J:nit too. I recommen# you to use Test 9 !rame&or@ &it' WebDriver testbecause t'ere are many use!ul !eatures available in it compare# to J:nit.0ea# my latter posts to learn Test 9 !rame&or@ &it' its #etaile# !eatures.

1(. Test 9InstallationSteps

Steps )! Do&nloa#ing n# Installing Testng In 8clipse For WebDriver

To use Test 9 Frame&or@ in 8clipse" First o! all &e 'ave to install it.Installation o! Test 9 in 8clipse is very easy process. s I #escribe# inmy 083I):S )ST " Test 9 is very po&er!ul an# easy to use !rame&or@

an# it is supporte# by tools li@e 8clipse" +aven" ID8 " etc.. 6ere &e aregoing to use Test 9

Page 229: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 229/292

Frame&or@ &it' 8clipse so obviously you must 'ave installe# latest Java#evelopment @it AJDY in your system an# you must 'ave 8clipse. C5ICY 6808 to vie& steps o! #o&nloa#ing JDY an# 8clipse.

)nce you 'ave installe# JDY an# 8clipse in your system" ou are rea#y toinstall Test 9 in 8clipse. Bello& given steps &ill #escribe you 'o& to install

Test 9 in 8clipse.

Test 9 Installation Steps In 8clipse

Step 1 )pen 8clipse an# go to +enu 6elp -O Install e& So!t&are.

It &ill open ne& so!t&are installation &in#o& as s'o&n in bello& givenimage.

Step 2 In ne& so!t&are installation &in#o&" Type :05 G'ttp <<beust.com<eclipse e7actly in Wor@ &it' el# an# clic@ on ## buttonas s'o&n in bello& given image.

Page 230: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 230/292

It &ill s'o& you option Test 9 &it' c'ec@ bo7. Select t'at c'ec@ bo7 an#clic@ on e7t Button as s'o&n in above image.

W'en you clic@ on e7t button" it &ill c'ec@ !or re uirements an##epen#ency rst.

W'en it completes re uirement an# #epen#ency test" clic@ on e7tbutton. )n e7t screen" it &ill as@ you to accept Test g terms an# licenseagreement. ccept it an# clic@ on Finis' button as s'o&n in bello& givenimage.

Page 231: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 231/292

Test 9 &ill ta@e !e& minutes to nis' its installation &'en you clic@ onnis' button.

Step o& you nee# to veri!y t'at Test 9 is installe# in your eclipse ornot.

To veri!y 9o to 8clipse s +enu Win#o& -O S'o& 3ie& -O )t'ers as s'o&nin bello& given image.

It &ill open S'o& 3ie& &in#o&. 87pan# 4ava !ol#er as s'o&n in bello&given image an# veri!y t'at Test 9 is available insi#e it or not. I! it is t'eremeans Test 9 is installe# success!ully in eclipse.

Page 232: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 232/292

1*. Similaritiesan#Di=erenceBet&eenTest 9an#J:nit

W'at re T'e Similarities<Di=erence Bet&een Junit an# Test 9 Frame&or@For WebDriver

s you @no&" J:nit an# Test 9 are very popular unit testing !rame&or@s

!or 4ava #evelopers an# &e can use t'em in our &eb#river test e7ecution. Till no&" I 'ave #escribe# all W8BD0I380 T:T)0I 5S &it' 4unit !rame&or@.+any peoples are as@ing me to #escribe t'e similarities an# #i=erencebet&een J:nit an#

Test 9 Frame&or@. First o! all let me give you !e& similaritieso! J:nit an# Test 9 !rame&or@s an# t'en I &ill tell you #i=erence bet&eenbot' o! t'em.

A ote Bello& given similarities an# #i=erences are base# on J:nit $version.

Similarities Bet&een J:nit an# Test 9

1. We can create test suite in J:nit an# Test 9 bot' !rame&or@s.

2. Timeout Test Is possible very easily in bot' t'e !rame&or@s.

. We can ignore speci c test case e7ecution !rom suite in bot' t'e!rame&or@s.

$. It is possible to create e7pecte# e7ception test in bot' t'e!rame&or@s.

Page 233: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 233/292

Page 234: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 234/292

It &ill a## Test g library in your pro4ect as s'o&n in bello& image. o&clic@ on )Y button to close t'at &in#o&.

Step Create Test 9 Class To a## Test g class

• 0ig't clic@ on pac@age KTest 9)ne ac@K -O e& -O )t'er. It &illopen e& &i?ar# &in#o& as bello&.

Page 235: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 235/292

• Select Test g !rom e& &i?ar# &in#o& an# clic@ on e7t button.

• )n ne7t screen" a## class name G Class)ne

• It &ill a## ne& class AClass)ne un#er pac@age as s'o&n in bello&given image.

Page 236: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 236/292

Step $ ## &eb#river s e7ternal 4ar le in your pro4ect. To 0un &eb#river test" you nee# to a## &eb#river s 4ar les as e7ternal 4arle in your pro4ect. 3I8W T6IS )ST to @no& 'o& to #o&nloa# &eb#river

4ar les an# a## e7ternal 4ar le to 4ava buil# pat'.

A ote Do not a## 4unit 4ar le as e7ternal 4ar le. o& its not re uire# in Test 9 !rame&or@ .

T'is is it. o& you are rea#y to &rite your &eb#river test script insi#e yourclass.

Step % ##<&rite sample &eb#river test script.Write your o&n test script or a## bello& given script in your class le.

pac@age Test 9)ne ac@Himport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !ter+et'o#Himport org.testng.annotations.Be!ore+et'o#Himport org.testng.annotations.TestH

public class Class)ne L WebDriver #river G ne& Fire!o7DriverA H

<<;Be!ore+et'o# #e nes t'is met'o# 'as to run be!ore every ;Test

met'o#s ;Be!ore+et'o# public voi# openbro&serA L

#river.manageA .&in#o&A .ma7imi?eA H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1 <11<ne&-test.'tmlK H

Q

Page 237: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 237/292

<<; !ter+et'o# #e nes t'is met'o# 'as to run a!ter every ;Testmet'o#s ; !ter+et'o# public voi# closebro&serA L System.out.printAKZnBro&ser closeK H #river. uitA H Q ;Test

public voi# testmet'o#oneA L String title G #river.getTitleA H System.out.printAKCurrent page title is KPtitle H System.out.printAKZn Test 9)ne -O Test 9)ne ac@ -O Class)ne -Otestmet'o#one 'as been e7ecute# success!ullyK H QQ

Step ( 0un Web#river test script &it' Test 9

9o to 0un -O 0un s -O n# Clic@ on Test g Test as s'o&n in bello& givenimage.

It &ill run your &eb#river test &it' Test 9. W'en e7ecution complete#" ou &ill see result as s'o&n in bello& given image.

Page 238: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 238/292

Step * 3ie& test e7ecution 6T+5 report generate# by Test 9 To vie& test e7ecution report"

• 0ig't clic@ on KTest 9)neK pro4ect !ol#er in eclipse an# select0e!res'.

• Fol#er &it' name G Ktest-outputK &ill be create# un#er your pro4ect!ol#er as s'o&n in bello& given Image.

• 87plore t'at !ol#er an# open it &it' &eb Bro&ser as s'o&n in aboveimage. It &ill open report in eclipse as s'o&n in bello& given image.

Page 239: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 239/292

1/. Test gannotations&it'e7amples

Test 9 nnotations Wit' Selenium WebDriver 87amples

s you @no&" Test 9 is t'e !rame&or@ &'ic' is very use!ul to use &it'selenium WebDriver. I 'ave s'are# all selenium &eb#river &it' testngtutorials on T6IS 5I Y . T'e main reason be'in# Test 9 s popularity is &ecan create an# con gure test case an# test suite very easily using many#i=erent annotations o! Test 9.Fe& annotations are similar in Test 9 an# 4unit an# you can vie& 4unitannotations &it' e7ample on t'is J: IT )T TI) S )ST .

nnotations are t'ose t'ings in Test 9 &'ic' gui#es it !or &'at to #o ne7tor &'ic' met'o# s'oul# be e7ecute# ne7t. Test 9 'as also !acility to pass

parameters &it' annotations. 5et &e loo@ at Test 9 annotations list &it'its !unctional #escription.

;Test;Test annotation #escribes met'o# as a test met'o# or part o! your test.3I8W 0 CTIC 5 8V + 58 )F ;Test )T TI)

;Be!ore+et'o#ny met'o# &'ic' is mar@e# &it' ;Be!ore+et'o# annotation &ill be

e7ecute# be!ore eac' an# every ;test annotate# met'o#.3I8W 0 CTIC 5 8V + 58 )F ;Be!ore+et'o# )T TI)

; !ter+et'o#Same as ;Be!ore+et'o#" I! any met'o# is annotate# &it' ; !ter+et'o#annotation t'en it &ill be e7ecute# a!ter e7ecution o! eac' an# every;test annotate# met'o#.3I8W 0 CTIC 5 8V + 58 )F ; !ter+et'o# )T TI)

;Be!oreClass+et'o# annotate# using ;Be!oreClass &ill be e7ecute# be!ore rst ;Testmet'o# e7ecution. ;Be!oreClass annotate# met'o# &ill be e7ecute# once

only per class so #on t be con!use#.3I8W 0 CTIC 5 8V + 58 )F ;Be!oreClass )T TI)

Page 240: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 240/292

; !terClassSame as ;Be!oreClass" +et'o# annotate# &it' ; !terClass annotation&ill be e7ecute# once only per class a!ter e7ecution o! all ;Test annotate#met'o#s o! t'at class.3I8W 0 CTIC 5 8V + 58 )F ; !terClass )T TI)

;Be!oreTest;Be!oreTest annotate# met'o# &ill be e7ecute# be!ore t'e any ;Testannotate# met'o# o! t'ose classes &'ic' are insi#e testO tag intestng.7ml le.

; !terTest; !terTest annotate# met'o# &ill be e7ecute# &'en all ;Test annotate#met'o#s completes its e7ecution o! t'ose classes &'ic' are insi#e testOtag in testng.7ml le.

;Be!oreSuite+et'o# mar@e# &it' ;Be!oreSuite annotation &ill run be!ore t'e all suites!rom test.3I8W 0 CTIC 5 8V + 58 )F ;Be!oreSuite )T TI)

; !terSuite; !terSuite annotate# met'o# &ill start running &'en e7ecution o! alltests e7ecute# !rom current test suite.3I8W 0 CTIC 5 8V + 58 )F ; !terSuite )T TI)

;Data rovi#erW'en you use ;Data rovi#er annotation !or any met'o# t'at means youare using t'at met'o# as a #ata supplier. Con guration o! ;Data rovi#erannotate# met'o# must be li@e it al&ays return )b4ectMNMN &'ic' &e canuse in ;Test annotate# met'o#.3I8W 0 CTIC 5 8V + 58 )F ;Data rovi#er )T TI)

;Be!ore9roups;Be!ore9roups annotate# met'o# &ill run be!ore t'e rst test run o! t'atspeci c group.

; !ter9roups; !ter9roups annotate# met'o# &ill run a!ter all test met'o#s o! t'atgroup completes its e7ecution.

; arametersW'en you &ants to pass parameters in your test met'o#s" you nee# touse ; arameters annotation.3I8W 0 CTIC 5 8V + 58 )F ; arameters )T TI)

;Factory

Page 241: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 241/292

W'en you &ants to e7ecute speci c group o! test cases &it' #i=erentvalues" you nee# to use ;Factory annotation. n array o! class ob4ects isreturne# by ;Factory annotate# met'o# an# t'ose Test 9 &ill t'oseob4ects as test classes.

;5isteners;5isteners are use# to &it' test class. It is 'elp!ul !or logging purpose.

2 . Creating n#unningWebDriverTestuit:singtestng.7mlFile

Creating n# 0unning WebDriver Test Suit :sing testng.7ml File

Intro#uction )! testng.7ml FileIn Test 9 !rame&or@" We nee# to create testng.7ml le to create an#'an#le multiple test classes. testng.7ml is t'e le &'ere &e can con gureour &eb#river test" set test #epen#ency" inclu#e or e7clu#e any testmet'o# or class or pac@age" set priority etc.. We &ill learn eac' an# everyt'ing about Test 9 usage inmy !uture posts. 0ig't no& let me #escribe you 'o& to create testng.7ml

Creating testng.7ml File

• First o! all Create a pro4ect an# &eb#river test case as #escribe# inmy 083I):S )ST .

• To create testng.7ml le" 0ig't clic@ on pro4ect !ol#er an# 9o to e&-O File as s'o&n in bello& given image. It &ill open e& File &i?ar#.

Page 242: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 242/292

• In e& le &i?ar#" select KTest 9)neK pro4ect an# a## le name GKtestng.7mlK as s'o&n in bello& given image an# clic@ on Finis'button.

• It &ill a## testng.7ml le un#er your pro4ect !ol#er. o& a## bello&given lines in your testng.7ml le.

suite nameGKSuite )neK O test nameGKTest )neK O classesO class nameGKTest 9)ne [email protected])neK <O <classesO <testO

<suiteO

o& your testng.7ml le &ill loo@s li@e bello&.

Page 243: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 243/292

In above testng.7ml co#e"

• suiteO suite tag #e nes t'e Test 9 suite. ou can give any nameto your suite using name attribute. In above given e7ample" We'ave given KSuite )neK to our test suite.

• testO test tag #e nes t'e Test 9 test. ou can give any name toyour test using name attribute. In above given e7ample" We 'avegiven KTest )neK to our test.

• classesO classes tag #e nes multiple class. We can multiple testclass un#er classes tag. In our e7ample &e 'ave only one class.

• classO class tag #e nes t'e class name &'ic' you &ants toconsi#er in test e7ecution. In above given e7ample" &e 'ave #e ne#nameGKTest 9)ne [email protected])neK &'ere Test 9)ne ac@ #escribespac@age name an# Class)ne #escribes class name.

T'is &ay" above given testng.7ml le &ill e7ecute only Class)ne class!rom Test 9)ne ac@ pac@age.

87ecuting testng.7ml File To 0un0ig't clic@ on testng.7ml le -O 0un s -O Select Test 9 Suite as s'o&n inbello& given image.

Page 244: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 244/292

It &ill start e7ecution o! #e ne# test class Class)ne !romTest 9)ne ac@ pac@age.

W'en e7ecution complete#" ou can vie& test e7ecution 6T+5 report as#escribe# in my 083I):S )ST . Test e7ecution 6T+5 report &ill loo@s li@e

bello&.

T'is is t'e one simple e7ample o! creating an# running testng.7ml le ineclipse !or &eb#river. We &ill see #i=erent e7amples o! testng.7ml le tocon gure our test in my !uture post.--2 .1 CreatingSingle)r+ultipleTestsFor+ultipleClasses

testng.7ml Creating Single )r +ultiple Tests For +ultiple Classes InWebDriver

Page 245: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 245/292

Page 246: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 246/292

QQ

bove class &ill be use# as base class to initiali?e an# close &eb#riverinstance.

2. Class)ne.4ava

pac@age Test 9)ne ac@H

import org.testng.annotations.TestH

public class Class)ne e7ten#s Test 9)ne [email protected])neL <<;Test annotation #escribes t'is met'o# as a test met'o#

;Test public voi# testmet'o#oneA L String title G #river.getTitleA H System.out.printAKZnCurrent page title is KPtitle H String Wor@#ir G System.get ropertyAKuser.#irK H String Classpac@name G t'is.getClassA .get ameA H System.out.printAKZn KPWor@#irPK -O KPClasspac@namePK -Otestmet'o#one 'as been e7ecute# success!ullyK H QQ

bove Class)ne is in'erite# !rom BaseClass)ne.

. ClassT&o.4ava

pac@age Test 9)ne ac@H

import org.testng.annotations.TestH

public class ClassT&o e7ten#s Test 9)ne [email protected])neL <<;Test annotation #escribes t'is met'o# as a test met'o#

;Test public voi# testmet'o#oneA L #river.navigateA .toAK'ttp <<only-testing-blog.blogspot.in<2 1$< 1<te7tbo7.'tmlK H String title G #river.getTitleA H System.out.printAKZnCurrent page title is KPtitle H String Wor@#ir G System.get ropertyAKuser.#irK H String Classpac@name G t'is.getClassA .get ameA H System.out.printAKZn KPWor@#irPK -O KPClasspac@namePK -Otestmet'o#one 'as been e7ecute# success!ullyK H QQ

Page 247: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 247/292

bove ClassT&o is also in'erite# !rom BaseClass)ne.

o& your class structure &ill be loo@s li@e bello& in eclipse.

Con gure testng.7ml to run t&o classes in one test

o& let me s'o& you 'o& to con gure testng.7ml to run bot' classes insingle test. Copy-paste bello& given lines in your testng.7ml le an# t'en0un it as Test g Suite. 6ere" bot' classes are inclu#e# in single test ATest)ne .

suite nameGKSuite )neK O test nameGKTest )neK O classesO class nameGKTest 9)ne [email protected])neK <O class nameGKTest 9)ne [email protected]&oK <O

<classesO <testO

<suiteO

W'en e7ecution complete#" 3ie& e7ecution result reports. It &ill loo@s li@ebello&.

Con gure testng.7ml to run t&o classes in t&o tests

Page 248: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 248/292

o& i! you &ants to run bot' classes as separate test t'en you 'ave tocon gure your testng.7ml le as bello&.

suite nameGKSuite )neK O test nameGKTest )neK O classesO class nameGKTest 9)ne [email protected])neK <O

<classesO <testO

test nameGKTest T&oK O classesO class nameGKTest 9)ne [email protected]&oK <O

<classesO <testO

<suiteO

6ere" KClass)neK is inclu#e# in Test )neK test an# KClassT&oK is inclu#e#in Test T&oK test. o& run bello& given testng.7ml le an# veri!y result.

o& compare bot' t'e results. In 1st e7ample" Bot' classes are e7ecute#un#er same test but in 2n# e7ample bot' classes are e7ecute# un#erseparate tests. T'is &ay you can con gure testng.7ml le as per yourre uirement.

--2 .2 CreatingTestSuite:singClassFromDi=erent ac@ages

testng.7ml Creating WebDriver Test Suite :sing Classes From Di=erentac@ages

o& you are alrea#y a&are about 6)W T) C08 T8 testng.7ml FI58 to

con gure an# run your &eb#river test. T'e main reason be'in# popularityo! Test 9 !rame&or@ !or &eb#river is &e can con gure our test as per our

Page 249: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 249/292

re uirements. I 'ave liste# some Similarities<Di=erences bet&een Junitan# Test 9!rame&or@ In T6IS )ST . In my previous post" We 'ave alrea#y seene7ample o! 'o& to con gure testng.7ml le to run single<multiple&eb#river test classes o! same pac@age. o& let me s'o& you ane7ample o! 'o& to create test suite using classes !rom #i=erent pac@ages.

First o! all let &e create ne& pac@ages an# classes as #escribe# in bello&given steps.

Step 1. Create pac@age G KTest 9)ne ac@K &it' classesG BaseClass)ne.4ava" Class)ne.4ava an# ClassT&o.4ava e7actly as#escribe# in my 083I):S )ST .

Step 2. Create pac@age G KTest 9T&o ac@K un#er same pro4ect an#a## Class)ne.4ava an# ClassT&o.4ava les &it' bello& given co#e lines.

Class)ne.4avapac@age Test 9T&o ac@H

import org.testng.annotations.TestH

public class Class)ne e7ten#s Test 9)ne [email protected])neL ;Test

public voi# testmet'o#oneA L

String title G #river.getTitleA H System.out.printAKZnCurrent page title is KPtitle H String Wor@#ir G System.get ropertyAKuser.#irK H String Classpac@name G t'is.getClassA .get ameA H System.out.printAKZn KPWor@#irPK -O KPClasspac@namePK -Otestmet'o#one 'as been e7ecute# success!ullyK H QQ

ClassT&o.4avapac@age Test 9T&o ac@H

import org.testng.annotations.TestH

public class ClassT&o e7ten#s Test 9)ne [email protected])neL ;Test

public voi# testmet'o#oneA L #river.navigateA .toAK'ttp <<only-testing-blog.blogspot.in<2 1$< 1<te7tbo7.'tmlK H String title G #river.getTitleA H

System.out.printAKZnCurrent page title is KPtitle H String Wor@#ir G System.get ropertyAKuser.#irK H

Page 250: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 250/292

String Classpac@name G t'is.getClassA .get ameA H System.out.printAKZn KPWor@#irPK -O KPClasspac@namePK -Otestmet'o#one 'as been e7ecute# success!ullyK H QQ

bove given bot' classes are in'erite#!rom Test 9)ne [email protected])ne to use &eb#river instance"initiali?ation an# closing t'e &eb#river.

Step . Create pac@age G KTest 9T'ree ac@K un#er same pro4ect an#a## Class)ne.4ava an# ClassT&o.4ava les &it' above given co#e lines.

ou nee# to c'ange pac@age name in bot' classATest 9T&o ac@ to Test 9T'ree ac@ to use t'em in KTest 9T'ree ac@K pac@age.

So no&" my pac@age an# class 'ierarc'y is as bello& !or pro4ect Test g)ne.

o& supposing I #o not &ant to e7ecute all class o! all pac@ages an# I&ants to e7ecute only !e& o! t'emli@e Test 9)ne [email protected])ne" Test 9T&o [email protected]&o" Test 9T'ree [email protected])ne an# Test 9T'ree [email protected]&o. 6o& can &e #o t'atR ou nee#to con gure your testng.7ml le as bello&.

suite nameGKSuite )neKO test nameGKTest )neK O classesO class nameGKTest 9)ne [email protected])neK <O class nameGKTest 9T&o [email protected]&oK <O class nameGKTest 9T'ree [email protected])neK <O class nameGKTest 9T'ree [email protected]&oK <O

<classesO <testO

<suiteO

Page 251: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 251/292

bove given le &ill run only #escribe# $ classes out o! total ( classesun#er single test ATest )ne . ou can #ivi#e t'em in multiple tests too as#escribe# in my previous post.

o& e7ecute above given testng.7ml le an# veri!y result. It &ill loo@s li@eas s'o&n in bello& given image.

T'is &ay &e can con gure our test suite using only speci c class !rom#i=erent pac@ages.

--2 . CreatingTestSuite:singSelecte#)r ll ac@ages

Con gure testng.7ml In 8clipse For Selenium WebDriver To 0un ll )rSpeci c ac@age

Test 9 is very use!ul an# po&er!ul !rame&or@ !or selenium &eb#river. Wenee# to con gure our tests base# on our re uirements or test scenarios.We are going very slo&ly &it' Test 9 !rame&or@ to un#erstan# eac' an#everyt'ing clearly &it' e7amples so t'at it can 'elp you to con gure yourtest in any@ing o! situations. In my previous posts" &e 'ave learnt about 6)W T)

0: S 8CIFIC C5 SS F0)+ DIFF808 T CY 98 :SI 9 testng.7ml an#6)W T) 0: SI 958 )0 +:5TI 58 C5 SS8S F0)+ ) 8 CY 98 :SI 9testng.7ml . So no& you can con gure your testng.7ml le to run anyspeci c class !rom any pac@age i! you 'ave rea# bot' previous posts.

Con gure Test 9)ne ro4ect In 8clipseIn testng.7ml le" ou can also con gure to run speci c pac@age or allpac@ages o! pro4ect. 5et &e see bot' e7amples. First o! all" Create bello&given t'ree pac@ages &it' re uire# classes" met'o#s an# co#e same as#escribe# in my 083I):S )ST .

Page 252: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 252/292

Page 253: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 253/292

speci c pac@age in our test suite. So &'en you run above testng.7ml le"It &ill run only targete# t&o pac@agesATest 9T&o ac@" Test 9T'ree ac@ .)t'er ATest 9)ne ac@ pac@ageAs &ill be e7clu#e# !rom e7ecution.87ecution report &ill loo@s li@e bello&.

s you see" KTest 9)ne ac@K pac@age is not e7ecute# as s'o&n in abovereport image.

Con gure testng.7ml to run all pac@ages o! pro4ect in single test suiteI! you &ants to run all pac@ages o! your pro4ect" you can con gure yourtestng.7ml using &il#car#A.> &it' pac@age tag as bello&.

suite nameGKSuite )neKO test nameGKTest )neK O pac@agesO pac@age nameGK.>K <O

<pac@agesO

<testO<suiteO

bove given testng.7ml le &ill e7ecute all pac@ages o! your selenium&eb#river pro4ect an# test result report &ill loo@s li@e bello&.

Page 254: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 254/292

s you see in report" o& all t'e pac@ages are e7ecute#. T'is &ay" &e canuse &il# car# to inclu#e all &eb#river test pac@ages in our test.--2 .$ Inclu#ing )nly Selecte# Test +et'o#s In Selenium WebDriver-Test g

Test

Inclu#e<87clu#e )nly Selecte# Test +et'o#s In Selenium WebDriver-Test g

Test Suite :sing testng.7mlI! you are using selenium &eb#river &it' Test g !rame&or@ t'en you caneasily run your selecte# test met'o#s !rom selecte# classes. Supposingyou 'ave a t&o classes in your pac@age an# rst class 'ave t'ree testmet'o#s an# secon# class 'ave ve test met'o#s. o& you &ants to runonly one test met'o# !rom rstclass an# t&o test met'o#s !rom secon# class t'en you can con gure itvery easily in testng.7ml le. 5et me s'o& you 'o& to #o it &it' simple&eb#river test con gure# by testng.7ml le.

Con gure Test 9)ne ro4ect In 8clipse

1. First o! all" Con gure Test 9)ne pro4ect in eclipse as #escribe# in083I):S )ST .

2. ## testmet'o#t&oA bello& t'e testmet'o#oneA as s'o&n bello&in Class)ne.4ava an# ClassT&o.4ava le o! all t'ree pac@ages.

public class ClassT&o e7ten#s Test 9)ne [email protected])neL ;Test

public voi# testmet'o#oneA L

Page 255: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 255/292

#river.navigateA .toAK'ttp <<only-testing-blog.blogspot.in<2 1$< 1<te7tbo7.'tmlK H String title G #river.getTitleA H System.out.printAKZnCurrent page title is KPtitle H String Wor@#ir G System.get ropertyAKuser.#irK H String Classpac@name G t'is.getClassA .get ameA H System.out.printAKZn KPWor@#irPK -O KPClasspac@namePK -Otestmet'o#one 'as been e7ecute# success!ullyK H Q

<< ## testmet'o#t&oA 'ere ;Test public voi# testmet'o#t&oA L #river. n#8lementABy.7pat'AK<<inputM;valueG !emale NK H String Wor@#ir G System.get ropertyAKuser.#irK H String Classpac@name G t'is.getClassA .get ameA H System.out.printAKZn KPWor@#irPK -O KPClasspac@namePK -Otestmet'o#t&o 'as been e7ecute# success!ullyK H QQ

o& your Test 9)ne pro4ect s structure &ill loo@s li@e bello&.

Page 256: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 256/292

Con gure testng.7ml le to Inclu#e an# run selecte# &eb#river testmet'o#s !rom !e& classes

o& supposing" I &ants to run only

• testmet'o#oneA met'o# !rom Test 9)ne ac@ -O Class)ne.4ava

• testmet'o#oneA an# testmet'o#t&oA met'o#s!rom Test 9T&o ac@ -O ClassT&o.4ava

• testmet'o#oneA an# testmet'o#t&oA met'o#s!rom Test 9T'ree ac@ -O Class)ne.4ava

Page 257: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 257/292

• testmet'o#oneA an# testmet'o#t&oA met'o#s!rom Test 9T'ree ac@ -O ClassT&o.4ava

To per!orm above action" I 'ave to con gure my testng.7ml le as bello&.

suite nameGKSuite )neKO test nameGKTest )neK O classesO class nameGKTest 9)ne [email protected])neK <O met'o#sO inclu#e nameGKtestmet'o#oneK <O <met'o#sO class nameGKTest 9T&o [email protected]&oK <O

<classesO pac@agesO pac@age nameGKTest 9T'ree ac@K <O <pac@agesO <testO

<suiteO

In above given testng.7ml le" met'o#s an# inclu#e tags are ne& to learn. ou can rea# about Test g Frame&or@ s suiteO" testO" classesO an#

classO tags in T6IS )ST an# pac@agesO" pac@ageO tags in T6IS)ST . met'o#sO tag #e nes t'e group o! met'o#s an# inclu#eO tag

#e nes &'ic' met'o# you &ants to inclu#e in e7ecution. o& e7ecute t'is

testng.7ml le an# veri!y t'e result report.

Page 258: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 258/292

I! you see in above result" )nly test testmet'o#oneA is e7ecute#!rom Test 9)ne ac@ -O Class)ne.4ava. T'is &ay &e can inclu#e anyspeci c met'o# in our test suite to e7ecute !rom any class.

Con gure testng.7ml le to e7clu#e speci c test met'o# !rom classSame&ay" To e7clu#e speci c test met'o# !rom class" We can use

e7clu#eO tag insi#e met'o#sO tag as bello&.

suite nameGKSuite )neKO test nameGKTest )neK O classesO class nameGKTest 9)ne [email protected])neK <O met'o#sO inclu#e nameGKtestmet'o#oneK <O <met'o#sO class nameGKTest 9T&o [email protected]&oK <O met'o#sO e7clu#e nameGKtestmet'o#oneK <O <met'o#sO

<classesO pac@agesO pac@age nameGKTest 9T'ree ac@K <O <pac@agesO <testO

<suiteO

W'en you run above given testng.7ml le" it &ill e7clu#e testmet'o#oneAtest met'o# o! Test 9T&o ac@ -O ClassT&o.4av !rom e7ecution an# &ille7ecute only ClassT&o.4ava le as s'o&n in bello& given test result report.

Page 259: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 259/292

T'is &ay &e can con gure our testng.7ml le to inclu#e or e7clu#e anyspeci c test met'o# !rom e7ecution.--2 .% testng.7ml - Inclu#e<87clu#e Selenium WebDriver Test ac@age

Inclu#e<87clu#e Selenium WebDriver Test ac@age From Test Suite :singtestng.7ml

o& you are alrea#y a&are about 6)W T) I C5:D8 )0 8VC5:D8S858CT8D T8ST +8T6)DS I T8ST S:IT8 . o& our ne7t tutorial is about'o& to inclu#e or e7clu#e selecte# pac@age !rom e7ecution o! test suite.Supposing you 'ave multiple pac@ages in your &eb#river test suite an#you&ants to run only speci c selecte# pac@age t'en 'o& &ill you #o itR 5et&e loo@ at simple e7ample !or t'e same.

First o! all con gure pro4ect KTest 9)neK &it' pac@ages as #escribe# inmy 083I):S )ST .

Con guring testng.7ml le to inclu#e only speci c pac@age in test suite!rom multiple pac@ages

s #escribe# in my T6IS )ST " &e can use &il#car#A.> &it' pac@age tagto run all pac@ages but t'en imme#iately &e &ill use inclu#e to runspeci c pac@age !rom all t'e pac@ages as bello&.

suite nameGKSuite )neKO test nameGKTest )neK O pac@agesO pac@age nameGK.>KO inclu#e nameGKTest 9)ne ac@K <O <pac@ageO <pac@agesO <testO

<suiteO

W'en you run above given testng.7ml le" it &ill run onlyKTest 9)ne ac@K pac@age !rom all pac@ages o! KTest 9)neK pro4ect. 5oo@at bello& given Test g result report.

Page 260: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 260/292

I! you see in above test result o! &eb#river testng suite" only classes an#met'o#s o! KTest 9)ne ac@K pac@age are e7ecute#. remaining 2pac@agesATest 9T&o ac@ an# Test 9T'ree ac@ are not e7ecute#.

Con guring testng.7ml le to e7clu#e speci c pac@age !rom e7ecution To e7clu#e speci c pac@age !rom e7ecution" ou nee# to con gure yourtestng.7ml le as bello&. Supposing I &ants to e7clu#e KTest 9T'ree ac@Kpac@age !rom e7ecution.

suite nameGKSuite )neKO test nameGKTest )neK O pac@agesO pac@age nameGK.>KO e7clu#e nameGKTest 9T'ree ac@K <O <pac@ageO <pac@agesO <testO

<suiteO

bove given testng.7ml le &it' e7clu#e KTest 9T'ree ac@K pac@age !rome7ecution an# &ill e7ecute remaining t&o pac@ages as s'o&n in bello&given report image.

Page 261: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 261/292

T'is &ay &e can inclu#e or e7clu#e !ull pac@age !rom e7ecution.

--2 .( testng.7ml - :sing 0egular 87pression To Inclu#e<87clu#e Test+et'o#

Test g Wit' Selenium WebDriver :sing 0egular 87pression ToInclu#e<87clu#e Test +et'o# From 87ecution

It is very important !or us to @no& t'e eac' an# every &ay o! testng.7mlcon guration to inclu#e<e7clu#e selecte# test met'o#s or pac@ages ine7ecution. ou can vie& my posts to @no& 'o& to inclu#e<e7clu#eS858CT8D CY 98 or S858CT8D T8ST +8T6)DS !rom selenium&eb#river test suitee7ecution. T'ere is also one anot'er &ay o! inclu#ing or e7clu#ingselecte# test met'o# using regular e7pression. 5et me #escribe it &it' onesimple e7ample.

For t'at you nee# to con gure KTest 9)neK pro4ect using pac@ages ass'o&n in T6IS )ST . o& let &e con gure testng.7ml le.

Inclu#ing<87clu#ing test met'o#s using regular e7pression

Supposing" I &ants to Inclu#e testmet'o#t&oA !rom Test 9)ne ac@-O Class)ne.4ava le an# 87clu#e testmet'o#t&oA !rom Test 9T&o ac@-O ClassT&o.4ava le using regular e7pression. For t'at my testng.7ml lecon guration &ill be loo@s li@e bello&.

suite nameGKSuite )neKO

Page 262: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 262/292

test nameGKTest )neK O classesO class nameGKTest 9)ne [email protected])neKO met'o#sO inclu#e nameGK.>t&o.>K<O <met'o#sO <classO class nameGKTest 9T&o [email protected]&oKO met'o#sO e7clu#e nameGK.>t&o.>K<O <met'o#sO <classO <classesO <testO

<suiteO

I! you see in above testng.7ml le" K.>t&o.>K is regular e7pression use# !ortest met'o#s. +eans test met'o#s containing Kt&oK &or# &ill beconsi#ere# !or inclusion<e7clusion in test suite e7ecution.. o& &'en I &illrun above testng.7ml le" Selenium &eb#river test e7ecution report &illloo@s li@e bello&.

I! you see in above given test e7ecution report" only t&o met'o#s 'asbeen e7ecute# as per our e7pectation. T'is &ay you can use regulare7pressions !or met'o# s inclusion or e7clusion.

--2 .* testng.7ml - S@ip Test Intentionally :sing S@ip87ceptionA

6o& To S@ip WebDriver Test In Test 9

Page 263: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 263/292

I! you remember" I 'ave poste# many posts on Test 9 Frame&or@ an# you&ill n# all t'ose posts on T6IS 5I Y or you can 3I8W T6IS 98 !or allrelate# posts listing. Stu#y all o! t'em one by one on poste# #ate or#er !oryour better un#erstan#ing. Till no& &e 'ave learnt about 'o& to Inclu#eor e7clu#e

T8ST +8T6)D " CY 98 !rom e7ecution. We 'ave also learnt 089:5 08V 08SSI) :S 98 !or Inclu#ing or 87clu#ing Test met'o#s !rome7ecution. o& supposing you &ants to s@ip speci c test Intentionally!rom e7ecution t'en 'o& can you #o ItR

S@ipping WebDriver Test Intentionally)ne &ay o! s@ipping selenium &eb#river test met'o# Is using t'ro& ne&S@ip87ceptionA e7ception - Test 9 87ception. Sometimes you nee# toc'ec@ some con#ition li@e I! some con#ition matc' t'en s@ip test elseper!orm some action In your &eb#river test. In t'is @in# o! situation" youcan use S@ip87ceptionA e7ception.

5et us loo@ at simple &eb#river test case e7ample &'ere I 'ave place#S@ip87ceptionA Insi#e i! con#ition to Intentionally s@ip t'at test. leasenote one t'ing 'ere once S@ip87ceptionA t'ro&n" remaining part o! t'attest met'o# &ill be not e7ecute# an# control &ill goes #irectly to ne7t testmet'o# e7ecution.

In bello& given e7ample" I! con#ition &ill matc' so t'ro&ne& S@ip87ceptionA part &ill be e7ecute# an# #ue to t'ate7ception" K !ter I! 8lseK statement &ill be not printe#. Intensional S@ipA

met'o# &ill be #isplaye# as s@ippe# in Testng report.

0un bello& given e7ample In your eclipse an# observe result.

pac@age Testng ac@H

import 4ava.util.concurrent.Time:nitHimport org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.S@ip87ceptionHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

public class Test 9 L

public static WebDriver #river G ne& Fire!o7DriverA H

;Be!oreTest

public voi# setupA t'ro&s 87ception L#river.manageA .&in#o&A .ma7imi?eA H

Page 264: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 264/292

#river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1 <11<ne&-test.'tmlK H

Q

;Test public voi# Intensional S@ipA L System.out.printlnAKIn 3eri!y TitleK H String titl G #river.getTitleA H

i!Atitl.e ualsAK)nly Testing e& TestK L <<To S@ip Test t'ro& ne& S@ip87ceptionAKTest C'ec@ C'ec@bo7 Is S@ippe#K H

QelseL System.out.printlnAKC'ec@ t'e C'ec@bo7K H #river. n#8lementABy.7pat'AK<<inputM;valueG Bi@e NK .clic@A H Q System.out.printlnAK !ter I! 8lseK H Q

;Test public voi# 0a#io c'ec@A L System.out.printlnAKIn C'ec@ 0a#ioK H #river. n#8lementABy.7pat'AK<<inputM;valueG male NK .clic@A H Q

; !terTest public voi# tearDo&nA t'ro&s 87ception L

#river. uitA HQ

Q

testng.7ml C5ICY 6808 to vie& 'o& to create testng.7ml

suite nameGKSimple SuiteKO test nameGKSimple S@ip TestKO

classesO class name G KTestng [email protected] 9K<O <classesO <testO

<suiteO

0esult &ill be as bello&

Page 265: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 265/292

Page 266: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 266/292

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.Data rovi#erHimport org.testng.annotations.TestH

public class Sample 5ogin L WebDriver #river G ne& Fire!o7DriverA H

;Be!oreTest

public voi# setupA t'ro&s 87ception L#river.manageA .&in#o&A .ma7imi?eA H

#river.manageA .timeoutsA .implicitlyWaitA1%" Time:nit.S8C) DS H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< %<login.'tmlK H

Q

; !terTest public voi# tearDo&nA t'ro&s 87ception L

#river. uitA H Q

<<T'is met'o# &ill return t&o #imensional array. <<T'is met'o# be'aves as #ata provi#er !or 5ogIn Test met'o#.

;Data rovi#er public )b4ectMNMN 5oginCre#entialsA L <<Create# t&o #imensional array &it' $ ro&s an# 2 columns. <<$ ro&s represents test 'as to run $ times. <<2 columns represents 2 #ata parameters. )b4ectMNMN Cre# G ne& )b4ectM$NM2NH

Cre#M NM N G K:serI#1KH Cre#M NM1N G K ass1KH

Cre#M1NM N G K:serI#2KH

Cre#M1NM1N G K ass2KH Cre#M2NM N G K:serI# KH

Cre#M2NM1N G K ass KH

Cre#M NM N G K:serI#$KH Cre#M NM1N G K ass$KH return Cre#H <<0eturne# Cre# Q <<9ive #ata provi#er met'o# name as #ata provi#er.

<< asse# 2 string parameters as 5oginCre#entialsA returns 2 parametersIn ob4ect.

Page 267: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 267/292

;Test A#ata rovi#erGK 5oginCre#entials K public voi# 5ogIn TestA String :se#i#" String ass L #river. n#8lementABy.7pat'AK<<inputM;nameG useri# NK .clearA H #river. n#8lementABy.7pat'AK<<inputM;nameG ps&r# NK .clearA H #river. n#8lementABy.7pat'AK<<inputM;nameG useri# NK .sen#YeysA:se#i#H #river. n#8lementABy.7pat'AK<<inputM;nameG ps&r# NK .sen#YeysA ass H #river. n#8lementABy.7pat'AK<<inputM;valueG 5ogin NK .clic@A H String alrt G #river.s&itc'ToA .alertA .getTe7tA H #river.s&itc'ToA .alertA .acceptA H System.out.printlnAalrt H QQ

testng.7ml le to run t'is e7ample Is as bello&.

suite nameGKSimple SuiteKO test nameGKSimple S@ip TestKO classesO class name G KTestng [email protected] 5oginK<O <classesO <testO

<suiteO

W'en you &ill run above e7ample In eclipse" It &ill enter :serID an#

pass&or#s one by one In :serI# an# pass&or# test bo7 o! &eb page. Test 9 result report &ill loo@s li@e bello&.

oste# by 8mail T'is BlogT'is[ S'are to T&itter S'are to Faceboo@ S'are to interest0elate# rticles Data Driven Testing in &eb#r

Page 268: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 268/292

--2 ./ arallel test e7ecution In multiple bro&sers using ; arametersannotation

Selenium WebDriver arallel Tests 87ecution :sing Test 9 - ; arameters

Bro&ser compatibility testing Is most Important t'ing !or any &ebapplication an# generally you 'ave to per!orm bro&ser compatibilitytesting be!ore 1 or 2 #ays o! nal release o! application. In suc' a sorttime perio#" you 'ave to veri!y eac' Important !unctionality In everybro&sers suggeste# by client. I! you &ill go!or running your all &eb#river tests In eac' bro&sers one by one t'en It&ill ta@e too muc' time to complete your test an# you may not complete

It be!ore release. In suc' situation" 0unning your tests In all re uire#bro&sers at same time &ill 'elps you to save your time e=orts. Souestion Is - Can &e run our tests parallel In all re uire# bro&sers using

&eb#riverR ns&er Is yes.

Be!ore learning 'o& to run &eb#river test parallel in multiple bro&sers" ou must 'ave @no&le#ge about 'o& to run test In &eb#river using Test g!rame&or@. ou &ill n# lin@s o! Test 9 tutorial post &it' e7amples on

T6IS 98 . I 'ave #escribe# testng con guration &it' #etaile#e7planation on t'ose lin@s so rea# t'em care!ully one by one. ou canrea# &eb#river relate# more tutorials on T6IS 5I Y .

arallelism In Test 9 ou can con gure your testng.7ml le In suc' a &ay to run your test suiteor tests or met'o#s In seperate bro&sers Is @no&n as parallelism In

Test 9. Intervie&er can as@ you t'is uestion. o& let us loo@ at e7ampleo! parellel test e7ecution In &eb#river using testng. Bello& given e7ample&ill run t'e test In +o?illa Fire!o7 an# 9oogle c'rome bro&ser parallel.

Create# Test arallel.4ava class le !or testing application an# con gure#testng.7ml le to run tests parallel as s'o&n In bello& given e7ample. Intestng.7ml le" parallelGKtestsK Insi#e suiteO tag &ill Instruct Test 9 toconsi#er bunc' o! met'o#s o! eac' testO tag as separate t'rea#. +eansI! you &ants to run your test In 2 #i=erent bro&sers t'en you nee# tocreate t&o testO bloc@s !or eac' bro&ser Insi#e testng.7ml le an#Insi#e eac' testO tag bloc@" #e ne one more tag KparameterK &it' samenameAIn bot' testO bloc@ but &it' #i=erent values. In bello& giventestng.7ml le" eac' testO bloc@ 'as KparameterK tag &it' same nameG bro&ser but #i=erent valuesAFFV an# C0+ .

In testcase" I 'ave use# ; arameters annotation to pass parameter Inmet'o#. 3alues o! t'is parameter &ill be !ee# by testng.7ml le. an# t'en

I! con#ition &ill c'ec@ t'at value to #eci#e &'ic' #river to use !or test. T'is &ay" e7ample testng.7ml le &ill !ee# t&o valuesAFFV an# C0+ In

Page 269: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 269/292

parameter so It &ill open Fire!o7 an# 9oogle c'rome bro&sers an# run testIn bot' bro&sers. AFor running test In google c'rome" ou nee# to#o&nloa# c'rome#river.e7e. 3I8W T6IS )ST to @no& more 'o& to run&eb#river test In google c'rome bro&ser .

0un bello& given test In your eclipse &it' testng to see 'o& It runs yourtest In 2 bro&sers at same time.

Test arallel.4ava

pac@age Testng ac@Himport org.4unit. ssertH

public class Test arallel L

private WebDriver #riverGnullH ;Be!oreClass<<parameter value &ill retrieve# !rom testng.7ml le s parameterO tag. ; arameters ALKbro&serKQ public voi# setupAString bro&ser L <<+et'o# &ill pass value o! parameter. i! Abro&ser.e ualsAKFFVK L <<I! value Is FFV t'en &eb#river &ill openFire!o7 Bro&ser. System.out.printlnAKTest Starts 0unning In Fire!o7 Bro&ser.K H #river G ne& Fire!o7DriverA H

Qelse i! Abro&ser.e ualsAKC0+K L <<I! value Is C0+ t'en &eb#river &illopen c'rome Bro&ser.

System.out.printlnAKTest Starts 0unning In 9oogle c'rome.K H System.set ropertyAK&eb#river.c'rome.#riverK"KD ZZc'rome#river &in 2 2. ZZc'rome#river.e7eK H

#river G ne& C'romeDriverA HQ

#river.manageA .&in#o&A .ma7imi?eA H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< %<login.'tmlK HQ<<Bot' bello& given tests &ill be e7ecute# In bot' bro&sers.

;Test public voi# veri!y titleA L

String title G #river.getTitleA H ssert.assert8 ualsAK)nly Testing 5ogInK" title H System.out.printlnAKTitle Is Fine.K HQ;Test

public voi# veri!y messageA L

#river. n#8lementABy.7pat'AK<<inputM;nameG useri# NK .sen#YeysAK:ID1KH #river. n#8lementABy.7pat'AK<<inputM;typeG pass&or# NK .sen#YeysAKpass1K H #river. n#8lementABy.7pat'AK<<inputM;valueG 5ogin NK .clic@A H

Page 270: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 270/292

String alert G #river.s&itc'ToA .alertA .getTe7tA H #river.s&itc'ToA .alertA .acceptA H ssert.assert8 ualsAK:serI# Is :ID1 ass&or# Is pass1K" alert H System.out.printlnAK lert Is Fine.K HQ; !terClasspublic voi# closebro&serA L

#river. uitA HQ

Q

testng.7ml

suite nameGK&ebDriverK parallelGKtestsKO test nameGKTest In FireFo7K O parameter nameGKbro&serK valueGKFFVK <O classesO class nameGKTestng [email protected] arallelK <O <classesO <testO test nameGKTest In 9oogle C'romeK O parameter nameGKbro&serK valueGKC0+KO <parameterO classesO class nameGKTestng [email protected] arallelKO <classO <classesO <testO

<suiteO

I! you &ants to run your test In one more bro&ser t'en you nee# to createne& testO tag bloc@ in testng.7ml le an# set anot'er parameter valuean# t'en put one more i! else con#ition in class le to c'ec@ an# run yourtest.

T'is &ay you can your test parallel In multiple bro&sers at same time tore#uce your time e=orts.

Day / - WebDriver ssertions Wit' Test 9

6ar# ssertions2 .1 assert8 uals ssertion Wit' 87ample

ssert.assert8 uals Test 9 Wit' Selenium WebDriver 87ample

T'ere are many assertions available in Selenium WebDriver &it' Test 9!rame&or@ an# &e &ill loo@ at all o! t'en one by one. ssertions areimportant an# very use!ul in any automation tools to assert somet'ing

#uring your test e7ecution. I! you @no&" Selenium ID8 'as many built inassertion comman#s an# i! you are

Page 271: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 271/292

selenium ID8 user t'en you can vie& set o! selenium ID8 assertioncomman# e7amples on T6IS 5I Y .

Di=erence bet&een ssertion an# veri cation3eri cation an# assertion is little #i=erent so #on t be con!use#.3eri cation &ill 4ust veri!y but assertion &ill rst veri!y an# i! result is notas per e7pectation t'en it &ill stop e7ecution o! t'at speci c test met'o#.

To#ay let &e loo@ at assert8 uals assertion in selenium &eb#river.

ssert.assert8 ualsAactual" e7pecte# assertion T'is assertion is use!ul to compare e7pecte# an# actual values inselenium &eb#river. I! bot' values matc' t'en its ne an# &ill continuee7ecution. But i! !ails t'en imme#iately it &ill mar@ t'at speci c testmet'o# as !ail an# e7it !rom t'at test met'o#.

ou can use #i=erent types o! values in actual an# e7pecte#li@e boolean" byteMN" c'ar" #ouble" Eoat" int" etc.. but !unction is same !orall. 5et &e loo@ at simple e7ample to un#erstan# it better.

Steps

• First o! all" create classes ABaseClass)ne.4ava an# ClassT&o.4avaas #escribe in T6IS )ST i! you #on t 'ave create#.

• o& copy paste bello& given lines in your ClassT&o.4ava le.

ssertion an# 3eri cation 87ample )! Selenium WebDriver Wit' Test g

pac@age Test 9)ne ac@H

import org.open a.selenium.ByHimport org.testng. ssertHimport org.testng.annotations.Be!oreClassHimport org.testng.annotations.TestH

public class ClassT&o e7ten#s Test 9)ne [email protected])neL String ctualte7tH

;Be!oreClass public voi# loa# urlA L #river.navigateA .toAK'ttp <<only-testing-blog.blogspot.in<2 1$< 1<te7tbo7.'tmlK H Q<<+et'o# 87ample For ssertion

;Test public voi# assertion met'o# 1A L ctualte7t G #river. n#8lementABy.7pat'AK<<'2<spanK .getTe7tA H

ssert.assert8 ualsA ctualte7t" KTues#ay" 2, January 2 1$K H System.out.printAKZn assertion met'o# 1A -O art e7ecute#K H

Page 272: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 272/292

Q<<+et'o# 87ample For ssertion

;Test public voi# assertion met'o# 2A L

ssert.assert8 ualsA ctualte7t" KTues#ay" 2/ January 2 1$K H System.out.printAKZn assertion met'o# 2A -O art e7ecute#K H Q <<+et'o# 87ample For 3eri cation

;Test public voi# veri cation met'o#A L

String time G#river. n#8lementABy.7pat'AK<<#ivM;i#G time5e!t NK .getTe7tA H

i! Atime GG KTues#ay" 2, January 2 1$K L System.out.printAKZnTe7t +atc'K H Q else L System.out.printAKZnTe7t #oes +atc'K H Q QQ

testng.7ml e7ample to run single class To 0un above e7ample using testng.7ml le" you &ill 'ave to con gure itas bello&.

suite nameGKSuite )neK con g!ailurepolicyGKcontinueKO test nameGKTest )neKO classesO class nameGKTest 9)ne [email protected]&oK <O

<classesO <testO

<suiteO

I! you run above e7ample" assertion met'o# 1A &ill #isplay pass in resultbut assertion met'o# 2A &ill #isplay !ail because 'ere e7pecte# an#actual values &ill not matc' &it' eac' ot'er.

Page 273: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 273/292

I! you &ill loo@ in console" )nly Kassertion met'o# 1A -O art e7ecute#K&ill be printe# over t'ere. Kassertion met'o# 2A -O art e7ecute#K &ill benot t'ere because e7ecution o! current met'o# &as s@ippe# a!terassertion !ailure. 5ast veri cation met'o#A is 4ust simple veri cationmet'o# &'ic' veri es te7t an# print message base# on result.

2 .2 assert ot8 uals ssertion Wit' 87ample

87ample )! assert ot8 uals In Selenium WebDriver Wit' Test 9

s #escribe# in my 083I):S )ST " assert8 uals assertion is use!ul tocompare t&o string" boolean" byteMN" c'ar" #ouble" Eoat" int" etc.. an#base# on assertion result" our test met'o# &ill pass or !ail. T'is pass or!ail in#ication &ill 'elps you in your testing process. So t'is is t'e verygoo# !unction in seleniumWebDriver &'ic' 'elps us to n# an# log issue. lease note one t'ingonce more is - :se 'ar# assertions in selenium &eb#river only &'en you&ants to s@ip remaining test e7ecution on assertion !ail. I! you &ants tocontinue e7ecution on assertion !ailure" you can use Test g so!tassertions. We &ill #iscuss about it in my upcoming post.

ou can 3I8W S858 I:+ ID8 SS80TI) 8V + 58S !or your re!erence.

ssert.assert ot8 ualsAactual" e7pecte#" message assertion)t'er one assertion in selenium WebDriver is assert ot8 uals assertion.It s !unction is opposite to assert8 uals assertion. +eans i! bot' si#esvalues &ill not matc' t'en t'is assertion &ill pass else it &ill !ail. 6ere you

can &rite your o&n message !or !ailure con#ition. Simplest e7ampleo! ssert.assert ot8 uals in selenium &eb#river is as bello&.

Page 274: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 274/292

0eplace bello& given test met'o#s Aassertion met'o# 1A an#assertion met'o# 2A &it' e7ample given in my 083I):S )ST an#t'en run it using testng.7ml le.

<< ssertion +et'o# ;Test public voi# assertion met'o# 1A L ctualte7t G #river. n#8lementABy.7pat'AK<<'2<spanK .getTe7tA H ssert.assert ot8 ualsA ctualte7t" KTues#ay" 2, January 2 1$K"K87pecte# an# actual matc' in assertion met'o# 1K H System.out.printAKZn assertion met'o# 1A -O art e7ecute#K H Q << ssertion +et'o#

;Test public voi# assertion met'o# 2A L

ssert.assert ot8 ualsA ctualte7t" KTues#ay" 2/ January 2 1$K"K87pecte# an# actual matc' in assertion met'o# 2K H System.out.printAKZn assertion met'o# 2A -O art e7ecute#K H Q

ou test e7ecution result &ill loo@s li@e bello&..

6ere" test met'o# assertion met'o# 1A &ill !ail because actual string te7tan# e7pecte# string te7t &ill matc'. ssertion message &ill be printe# intest result report as mar@e# &it' green color in above image.

2 . assertTrue ssertion Wit' 87ample

Page 275: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 275/292

Selenium WebDriver assertTrue assertion e7ample &it' Test 9

reviously &e 'ave learnt t&o assertions o! selenium &eb#river. ou canvie& practical e7ample pages o! selenium &eb#river assertions- assert8 uals an# assert ot8 uals be!ore learning assertTrueAcon#itionassertion. T'ere is also one anot'er relate# assertionis assertFalseAcon#ition assertion but &e &ill loo@about it in my ne7t post.

assertTrueAcon#ition ssertionassertTrue assertion is generally use# !or boolean con#ition true. It &illpass i! con#ition returns KtrueK. I! it &ill return !alse t'en it &ill !ail an# s@iptest e7ecution !rom t'at speci c met'o#. Synta7 !or assertTrue assertionis as bello&.

ssert.assertTrueAcon#ition H

Supposing you 'ave one c'ec@ bo7 on page. ou &ants to c'ec@ its statusli@e it is c'ec@e# or not. n# i! it is not c'ec@e# t'en you &ants to s@ipe7ecution !rom t'at met'o# an# &ants to !ail t'at met'o# in testngreport. T'is t'ing can be #one very easily using assertTrue assertion. 5et&e loo@ at practical e7ample o! assertTrue assertion.

0eplace bello& given variables an# met'o#s &it' e7ample given on my T6IS )ST an# t'en run it using testng.7ml le.

Web8lement c'@1" c'@2H ;Be!oreClass public voi# loa# urlA L #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< 2<attributes.'tmlK H c'@1 G #river. n#8lementABy.7pat'AK<<inputM;nameG option1 NK H c'@2 G #river. n#8lementABy.7pat'AK<<inputM;nameG option2 NK HQ<< ssertion +et'o# - &ill pass

;Test public voi# asserttrue1A L

System.out.printAKZnKPc'@1.isSelecte#A H ssert.assertTrueAc'@1.isSelecte#A H

System.out.printAKZn asserttrue1 - O 87ecute# - means assertion ispassK H Q<< ssertion +et'o# - &ill !ail

;Test public voi# asserttrue2A L

System.out.printAKZnKPc'@2.isSelecte#A H

ssert.assertTrueAc'@2.isSelecte#A H

Page 276: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 276/292

Page 277: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 277/292

e7plaine# in my previous posts. o& let &e try to un#erstan# assertFalseassertion !or selenium &eb#river.

ssert.assertFalseAcon#ition ssertionIt &ill c'ec@ boolean value returne# by con#ition an# &ill pass i! returne#value is KFalseK. I! returne# value is pass t'en t'is assertion &ill !ail an#s@ip e7ecution !rom current test met'o#. Synta7 !or assertFalse assertionis as bello&.

ssert.assertFalseAcon#ition H

In sort" assertFalse an# assertTrue assertions are opposite to eac' ot'er.W'en you &ants to assert KTrueK value - you can use assertTrue assertion.

n# &'en you &ants to assert !alse value - you can use assertFalseassertion in your selenium &eb#river test.

5et &e try to use assertFalse assertion &it' simple e7ample. Supposing &e'ave t&o c'ec@bo7s on page one !rom t'em is c'ec@e# an# anot'er one isnot c'ec@e#. 5et &e apply assertFalse assertion on bot' o! t'ese c'ec@bo7es !or its c'ec@e# status.

0eplace bello& given e7ample met'o#s &it' e7ample o! T6IS 98 . an#run it &it' testng.7ml as #escribe# on t'at post.

Web8lement c'@1" c'@2H ;Be!oreClass

public voi# loa# urlA L #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< 2<attributes.'tmlK H c'@1 G #river. n#8lementABy.7pat'AK<<inputM;nameG option1 NK H c'@2 G #river. n#8lementABy.7pat'AK<<inputM;nameG option2 NK HQ<< ssertion +et'o# - &ill Fail

;Test public voi# assert!alse1A L System.out.printAKZnKPc'@1.isSelecte#A H ssert.assertFalseAc'@1.isSelecte#A H Q << ssertion +et'o# - &ill ass ;Test public voi# assert!alse2A L System.out.printAKZnKPc'@1.isSelecte#A H ssert.assertFalseAc'@2.isSelecte#A H Q

In t'is case" assert!alse1A met'o# &ill !ail because it is alrea#y c'ec@e#so c'@1.isSelecte#A &ill return true value. assert!alse2A met'o# &ill passbecause 2n# c'ec@bo7 is not c'ec@e# so it &ill return !alse value.

87ecution result &ill loo@s li@e bello&.

Page 278: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 278/292

2 .$ assert ull ssertion Wit' 87ample

Selenium WebDriver ssertion assert ull 87ample Wit' Test 9

ssertions are very use!ul to c'ec@ your e7pecte# result an# s@ipe7ecution i! assertion !ails on run time. I! you are selenium ID8 user" youcan rea# all t'ese selenium ID8 SS80TI) C)++ DS e7amples poststo use t'em in your test cases !or your &eb application. Be!ore #escribingabout assert ull comman#"I recommen# you all to 08 D )T680 W8BD0I380 SS80TI) +8T6)D e7amples !or your re!erence.

assert ullAob4ect WebDriver ssertionassert ullAob4ect assertion &ill c'ec@ an# veri!y t'at ob4ect is null. It &illpass i! ob4ect !oun# null. I! ob4ect !oun# not null t'en it &ill return errormessage li@e K4ava.lang. ssertion8rror e7pecte# MnullN but !oun# MtrueNK.W'enever your assertion !ails" it &ill mar@ t'at speci c test met'o# as !ailin Test 9 result.

5et me present 'ere one simple e7ample o! assert ull assertion. We 'avet&o te7t bo7es. 1st te7t bo7 is enable# an# 2n# is #isable#. o& let meuse assert ull assertion in my test script to assert t'at te7tbo71 isenable# an# te7tbo72 is #isable#. 6ere &e use te7tbo7 s #isable# attribute!or t'is assertion.

Copy an# replace bello& given test met'o#s &it' e7ample given on T6IS98 an# t'en run it &it' testng.7ml le

Web8lement t7t1" t7t2H

Page 279: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 279/292

;Be!oreClass public voi# loa# urlA L #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< 2<attributes.'tmlK H t7t1 G #river. n#8lementABy.7pat'AK<<inputM;i#G te7t1 NK H t7t2 G #river. n#8lementABy.7pat'AK<<inputM;i#G te7t2 NK HQ<<87ample )! ssertion +et'o# - &ill ass

;Test public voi# null1A L System.out.printAKZnKPt7t1.get ttributeAK#isable#K H ssert.assert ullAt7t1.get ttributeAK#isable#K HQ<<87ample ssertion +et'o# - &ill Fail

;Test public voi# null2A L System.out.printAKZnKPt7t2.get ttributeAK#isable#K H ssert.assert ullAt7t2.get ttributeAK#isable#K H Q

In above given e7ample" t7t1.get ttributeAK#isable#K ob4ect &ill returnKnullK because t'ere is not any #isable# attribute &it' 1st te7tbo7. So t'atassertion &ill pass. n# met'o# null1A &ill pass too.t7t2.get ttributeAK#isable#K ob4ect &ill return KtrueK because #isable#attribute is available &it' te7tbo72. So in t'is case t'at assertion an#met'o# null2A &ill !ail as s'o&n in bello& given image.

2 .$ assert ot ull ssertion Wit' 87ample

Test 9 ssertion assert ot ull Wit' WebDriver 87ample

Page 280: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 280/292

s you @no&" t'ere are many assertions in Test 9 an# you &ill n# mosto! t'em on T6IS 98 . 8ac' o! t'ese Test 9 assertions are #esigne# to!ul ll #i=erent @in# o! con#itions. I 'ave #escribe# mostly use# allassertions &it' very clear e7amples &it' selenium &eb#river test. I! you&ill see on t'at lin@" you &ill n#e7ample o! assert ull assertion. assert ot ull assertion &or@s opposite toassert ull assertion means it &ill assert not null con#ition.

Test 9 ssertion assert ot ullAob4ect WebDriver ssertionassert ot ull assertion is #esigne# to c'ec@ an# veri!y t'at valuesreturne# by ob4ect is not null. +eans it &ill be pass i! returne# value is notnull. 8lse it &ill !ail.

Best e7ample to e7periment assert ot ull assertion is assertion o! enable# an# #isable# te7t el#s. 5et me give you simple e7ample &'ere&e &ill assert K#isable#K attribute o! te7t bo7 to veri!y is it #isable# or notusing assert ot ull assertion.

Copy paste bello& given co#e in e7ample given on T6IS 98 an# t'enrun it using testng.7ml le.

Web8lement t7t1" t7t2H ;Be!oreClass public voi# loa# urlA L #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< 2<attributes.'tmlK H t7t1 G #river. n#8lementABy.7pat'AK<<inputM;i#G te7t1 NK H

t7t2 G #river. n#8lementABy.7pat'AK<<inputM;i#G te7t2 NK HQ<<87ample )! ssertion +et'o# - &ill Fail

;Test public voi# notnull1A L System.out.printAKZnKPt7t1.get ttributeAK#isable#K H ssert.assert ot ullAt7t1.get ttributeAK#isable#K H Q<<87ample )! ssertion +et'o# - &ill ass

;Test public voi# notnull2A L System.out.printAKZnKPt7t2.get ttributeAK#isable#K H ssert.assert ot ullAt7t2.get ttributeAK#isable#K H Q

In above e7ample" assert ot ull assertion o! notnull1A met'o# &ill !ailbecause e7pecte# &as not null but te7tbo7 te7t1 is not #isable# soreturne# null. assert ot ull assertion o! notnull2A &ill pass becausee7pecte# &as not null an# te7tbo7 te7t2 is #isable# so it 'as return truevalue means not null.

Page 281: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 281/292

Same&ay" ou can use assert ot ull assertion to assert c'ec@bo7 isc'ec@e# or not" to assert te7tbo7 is visible or 'i##en" etc..

So!t ssertion2 .$ pplying Test 9 So!t ssertion Wit' 87ample

So!t ssertion For Selenium WebDriver Wit' Test 9I! you @no&" &e 'ave alrea#y learn about testng 'ar# assertions A87ampleassert8 uals " assert ot8 uals " etc.. &'ic' &e can use In our &eb#rivertest. 6ar# assertion e7amples lin@s are given on T6IS 98 . 3ie& all t'osee7amples one by one to learn 'ar# assertions. 5et me tell you one t'ingabout 'ar# assertions" W'en 'ar# assertion &ill !ail Insi#e any testmet'o#" remaining e7ecution o! t'at speci c test met'o# &ill be aborte#.

o& I! you &ants to continue remaining test part e7ecution even I! assertion !ails an# also you &ants to report assertion !ailure In testngresult report t'en you can use testng so!t assertion met'o#.

So!t ssertions In Selenium WebDriver To use testng so!t assertion" you 'ave to use testng So!t ssert class. T'isclass &ill 'elps to not t'ro& an e7ception on assertion !ailure an#recor#ing !ailure. I! you &ill use so!t assertion t'en your test e7ecution &illremain continue even I! any assertion !ails. not'er most Important t'ingIs your assertion !ailure &ill be reporte# In report so t'at you can vie& It aten# o! test. ou can use so!t assertion &'en you are using multipleassertions In same test met'o# an# you &ants to e7ecute all o! t'em evenI! any one In bet&een !ails.

5et us loo@ at very simple e7ample o! testng 'ar# assertion an# so!tassertion to see t'e #i=erence bet&een bot' o! t'em. In bello& give

Page 282: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 282/292

e7ample" 'ar# assert te7tA an# so!t assert te7tA eac' 'ave $ assertions.:se# 'ar# assertions In 'ar# assert te7tA met'o# an# so!t assertions Inso!t assert te7tA met'o# to #escribe #i=erence bet&een bot' o! t'em.

0un bello& given e7ample In your eclipse &it' testng !rame&or@.

pac@age Testng ac@H

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.open a.selenium.support.ui.87pecte#Con#itionsHimport org.open a.selenium.support.ui.WebDriverWaitHimport org.testng. ssertHimport org.testng.annotations. !terClassHimport org.testng.annotations.Be!oreClassHimport org.testng.annotations.TestHimport org.testng.asserts.So!t ssertH

public class So!t ssert L <<Create# ob4ect o! testng So!t ssert class to use It s roperties. So!t ssert s assert G ne& So!t ssertA H String ctualte7tH WebDriver #river G ne& Fire!o7DriverA H

;Be!oreClass public voi# loa# urlA L #river.manageA .&in#o&A .ma7imi?eA H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< 1<te7tbo7.'tmlK H Q

;Test <<In t'is met'o#" I! any assertion !ails t'en e7ecution &ill be aborte#. public voi# 'ar# assert te7tA L ctualte7t G #river. n#8lementABy.7pat'AK<<'2<spanK .getTe7tA H <<Te7t on e7pecte# si#e Is &ritten Incorrect intentionally to get !ail t'isassertion. ssert.assert8 ualsA ctualte7t" KTues#ay" 1 January 2 1$K" K1st assert!aile#.K H System.out.printlnAK6ar# ssertion -O 1st pagete7t assertione7ecute#.K H

ssert.assert8 ualsA ctualte7t" KTues#ay" 2, January 2 1$K" K2n# assert!aile#.K H System.out.printlnAK6ar# ssertion -O 2n# pagete7t assertione7ecute#.K H

#river. n#8lementABy.7pat'AK<<inputM;valueG S'o& +e lert NK .clic@A H String lert te7t G #river.s&itc'ToA .alertA .getTe7tA H

Page 283: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 283/292

#river.s&itc'ToA .alertA .acceptA H

ssert.assert8 ualsA lert te7t" K6i.. is alert message[K" K lert IsInCorrectK H System.out.printlnAK6ar# ssertion -O 1st alert assertion e7ecute#.K H ssert.assert8 ualsA lert te7t" K6i.. T'is is alert message[K" K lert IsCorrectK H System.out.printlnAK6ar# ssertion -O 2n# alert assertion e7ecute#.K H

Q

;Test <<In t'is met'o#" Test e7ecution &ill not abort even I! any assertion !ail.Full Test &ill be e7ecute#. public voi# so!t assert te7tA L ctualte7t G #river. n#8lementABy.7pat'AK<<'2<spanK .getTe7tA H <<Te7t on e7pecte# si#e Is &ritten Incorrect intentionally to get !ail t'isassertion. s assert.assert8 ualsA ctualte7t" KTues#ay" 1 January 2 1$K" K1stassert !aile#.K H System.out.printlnAKSo!t ssertion -O 1st pagete7t assertion e7ecute#.K H

s assert.assert8 ualsA ctualte7t" KTues#ay" 2, January 2 1$K" K2n#assert !aile#.K H System.out.printlnAKSo!t ssertion -O 2n# pagete7t assertione7ecute#.K H

#river. n#8lementABy.7pat'AK<<inputM;valueG S'o& +e lert NK .clic@A H String lert te7t G #river.s&itc'ToA .alertA .getTe7tA H #river.s&itc'ToA .alertA .acceptA H

<< lert e7pecte# te7t Is &ritten Incorrect intentionally to get !ail t'isassertion. s assert.assert8 ualsA lert te7t" K6i.. is alert message[K" K lert IsInCorrectK H System.out.printlnAKSo!t ssertion -O 1st alert assertion e7ecute#.K H

s assert.assert8 ualsA lert te7t" K6i.. T'is is alert message[K" K lert IsCorrectK H System.out.printlnAKSo!t ssertion -O 2n# alert assertion e7ecute#.K H s assert.assert llA H Q

;Test public voi# &ait an# clic@A L WebDriverWait &ait G ne& WebDriverWaitA#river" 1% H &ait.untilA87pecte#Con#[email protected]'AK<<inputM;i#

G submitButton NK H #river. n#8lementABy.7pat'AK<<inputM;i#G submitButton NK .clic@A H

Page 284: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 284/292

Q

; !terClasspublic voi# Closebro&serA L#river. uitA H

Q

Q

)n e7ecution completion" you &ill get bello& given result In console.

So!t ssertion -O 1st pagete7t assertion e7ecute#.So!t ssertion -O 2n# pagete7t assertion e7ecute#.So!t ssertion -O 1st alert assertion e7ecute#.So!t ssertion -O 2n# alert assertion e7ecute#.

SS8D &ait an# clic@F I58D 'ar# assert te7t

s per console result" so!t assert te7tA met'o# Is e7ecute# !ull an#printe# all statements even !aile# 2 assertions. )n ot'ersi#e" 'ar# assert te7tA met'o# 'as not printe# any statement In consolebecause e7ecution aborte# on rst assertion !ailure.

o& I! you loo@ at testng test result report" bot' !ailures o! so!t assertion'as been reporte# In testng report A ou can vie& t'is post to see 6)W T)3I8W T8ST 9 T8ST 08S:5T 08 )0T . T'at means your assertion !ailure

'as been reporte# &it'out test abortion. 5oo@ at bello& give our testngtest result report.

Day 1 Common Functions To :se In WebDriver Test

2 .1 Common Function To Compare T&o Strings

Page 285: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 285/292

9eneral Function To Comparing T&o Strings In Selenium WebDriver TestCase

W'en you &ill use selenium &eb#river to automate real &orl#applications" +any times you 'ave to compare t&o strings li@e page title"pro#uct name or any ot'er string. n# base# on comparison result you'ave to ta@e speci c action. Simple e7ample o! suc' scenario Is" ou arenavigating on any vie& pro#uctpage to a## pro#uct In to bas@et. But be!ore a##ing pro#uct In bas@et" ou&ants to veri!y pro#uct s name to c'ec@ It Is correct pro#uct or not.

+any time you 'ave to compare strings In automating any singleapplication. So W'at you &ill #oR ou &ill &rite same co#e multiple time Inyour #i=erent scriptsR 9eneral &ayAW'ic' I am using Is to create commonmet'o# In base class AW'ic' Is accessible !rom all test classes tocompare t&o strings. T'en &e can call t'at met'o# by passing actual an#e7pecte# string to compare strings &'enever re uire# In out tests.

5et me e7plain you 'o& to #o It.

3I8W +)08 W8BD0I380 T:T)0I 5S ST8 B ST8

Step 1 Create CommonFunctions Class In your pac@age &'ic' can beaccessible by any ot'er class.Step 2 Create met'o# compareStrings &'ic' can accept t&o stringvalues. lso Create ob4ect o! Test 9 So!t ssert class to assert assertion

so!tly as s'o&n In bello& given e7ample.

pac@age Testng ac@H

import org.testng. ssertHimport org.testng.asserts.So!t ssertH

public class CommonFunctions L So!t ssert So!t ssert G ne& So!t ssertA H

public boolean compareStringsAString actualStr" String e7pecte#Str L

tryL <<I! t'is assertion &ill !ail" It &ill t'ro& e7ception an# catc' bloc@ &ill bee7ecute#. ssert.assert8 ualsAactualStr" e7pecte#Str H Qcatc'AT'ro&able t L <<T'is &ill t'ro& so!t assertion to @eep continue your e7ecution evenassertion !ailure. <<:se 'ere 'ar# assertion K ssert.!ailAKFailure +essage StringK K I! you&ants to stop your test on assertion !ailure. So!t ssert.!ailAK ctual String KPactualStrPK n# 87pecte# String

KPe7pecte#Str PK Do ot +atc'.K H

Page 286: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 286/292

<<I! Strings &ill not matc'" return !alse. return !alseH Q <<I! Strings matc'" return true. return trueH QQ

Step o& &e can call compareStrings met'o# In our test class&'enever re uire# as s'o&n In bello& given e7ample. We 'ave In'erite#CommonFunctions class to use Its !unction In Common Functions Test.

lso call So!t ssert.assert llA met'o# at last o! your test to mar@ yourtest !ail I! bot' strings not matc'. 0ea# +ore about usage o! Test 9 So!t

ssertion In &eb#river.

pac@age Testng ac@H

import org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

<<:se# In'eritancepublic class Common Functions Test e7ten#s CommonFunctions L

WebDriver #riverH;Be!oreTest public voi# StartBro&ser av:05A L #river G ne& Fire!o7DriverA H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< %<login.'tmlK H Q ; !terTest

public voi# CloseBro&serA L#river. uitA H

Q ;Test

public voi# testToCompareStringsA LString actualTitle G #river.getTitleA H

<<Call compareStrings met'o# Insi#e I! con#ition to c'ec@ string matc' ornot. i!AcompareStringsAactualTitle" K)nly Testing 5ogInK L <<I! strings matc'" T'is bloc@ &ill be e7ecute#. System.out.printlnAKWrite ction ta@ing lines In t'is bloc@ &'en titlematc'.K H QelseL <<I! strings not matc'" T'is bloc@ &ill be e7ecute#.

Page 287: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 287/292

System.out.printlnAKWrite ction ta@ing lines In t'is bloc@ &'en title notmatc'.K H Q

<<To mar@ test !ail In report at t'e en# o! e7ecution I! strings not matc'. So!t ssert.assert llA H QQ

bove given e7ample &ill compare actual an# e7pecte# title strings an#mar@ test !ailure at t'e en# o! test e7ecution I! bot' strings not matc'&it'out any Interruption In e7ecution. o& you can call compareStringsmet'o# any time to compare strings. ou 4ust nee# to provi#e e7pecte#an# actual string values.

ote ou can use 'ar# assertion In your common !unction catc' bloc@ atplace o! so!t assertion I! you &ants to stop test on !ailure. See bello&. I! you &ill use bello& given synta7 In catc' bloc@ t'en It &ill stop teste7ecution on !ailure.

ssert.!ailAK ctual String KPactualStrPK n# 87pecte# StringKPe7pecte#StrPK Do ot +atc'.K H

2 .2 Common Function To Compare T&o Integers

+et'o# To Compare T&o Integer 3alues In Selenium WebDriver Test

We 'ave learnt 'o& to create common !unction to compare t&o strings Inmy 083I):S )ST . Same &ay" +any times you nee# to compare t&oInteger values. n# base# on comparison result" ou can per!orm yourne7t step o! test e7ecution. ou can also stop or continue your teste7ecution on assertion!ailure by using so!t or 'ar# Test 9 assertions. In t'is post" &e &ill see'o& to compare t&o Integer values using common !unction.

+ain Intention o! creating t'is @in# o! common !unctions Is to minimi?eyour co#e si?e. ou 'ave to create It only once an# t'en you can call Itagain an# again &'enever re uire# In your test. n# !or t'at" ou 'ave tocreate common class &'ic' s'oul# be accessible !rom all t'e test classes.

I! you are retrieving te7tA&'ic' Is Integer value !rom &eb page using&eb#river s .getTe7tA !unction t'en It &ill be string an# you 'ave toconvert It In Integer value using bello& given synta7 be!ore comparision.

String actual3alString G #river. n#8lementABy.7pat'AK<<>M;i#G post-bo#y-

,22,*1,,,/,$2,(1(, N<#ivM1N<table<tbo#y<trM1N<t#M2NK .getTe7tA H<<To convert actual value string to Integer value.

Page 288: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 288/292

int actual3al G Integer.parseIntAactual3alString H

Bello& given e7ample &ill e7plain you 'o& to create common !unction tocompare t&o Integers In your CommonFunctions class.

pac@age Testng ac@H

import org.testng. ssertHimport org.testng.asserts.So!t ssertH

public class CommonFunctions L So!t ssert So!t ssert G ne& So!t ssertA H

public boolean compareInteger3alsAint actualInteger3al" inte7pecte#Integer3al L tryL <<I! t'is assertion &ill !ail" It &ill t'ro& e7ception an# catc' bloc@ &ill bee7ecute#. ssert.assert8 ualsAactualInteger3al" e7pecte#Integer3al H Qcatc'AT'ro&able t L <<T'is &ill t'ro& so!t assertion to @eep continue your e7ecution evenassertion !ailure. <<:n-comment bello& given 'ar# assertion line an# commnet so!tassertion line I! you &ants to stop test e7ecution on assertion !ailure.

<< ssert.!ailAK ctual 3alue KPactualInteger3alPK n# 87pecte# 3alueKPe7pecte#Integer3alPK Do ot +atc'.K H So!t ssert.!ailAK ctual 3alue KPactualInteger3alPK n# 87pecte#3alue KPe7pecte#Integer3alPK Do ot +atc'.K H <<I! Integer values &ill not matc'" return !alse. return !alseH Q <<I! Integer values matc'" return true. return trueH QQ

o& you can call compareInteger3alsA !unction In your test to comparet&o Integer values as s'o&n In bello& given e7ample.

pac@age Testng ac@H

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestH

Page 289: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 289/292

import org.testng.annotations.TestH

public class Common Functions Test e7ten#s CommonFunctionsLWebDriver #riverH ;Be!oreTest

public voi# StartBro&ser av:05A L #river G ne& Fire!o7DriverA H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< %<!orm.'tmlK H Q ; !terTest

public voi# CloseBro&serA L#river. uitA H

Q ;Test

public voi# testToCompareIntegersA LString actual3alString G #river. n#8lementABy.7pat'AK<<>M;i#G post-

bo#y-,22,*1,,,/,$2,(1(, N<#ivM1N<table<tbo#y<trM1N<t#M2NK .getTe7tA H <<To convert actual value string to Integer value. int actual3al G Integer.parseIntAactual3alString H <<Call compareInteger3als met'o# Insi#e I! con#ition to c'ec@ Integervalues matc' or not.

i!AcompareInteger3alsAactual3al" % L <<I! values matc'" T'is bloc@ &ill be e7ecute#. System.out.printlnAKWrite ction ta@ing lines In t'is bloc@ &'en 3aluesmatc'.K H QelseL <<I! values not matc'" T'is bloc@ &ill be e7ecute#. System.out.printlnAKWrite ction ta@ing lines In t'is bloc@ &'en 3aluesnot matc'.K H Q

<<To mar@ test !ail In report at t'e en# o! e7ecution I! values not matc'. So!t ssert.assert llA H QQ

2 . Common Function To Compare T&o Doubles

Common Function To Compare T&o Double 3alues In Selenium WebDriver

Comparison o! #ouble values Is not muc' more #i=erent t'an comparisono! Integer values as #escribe# In my 083I):S )ST but let me give youe7ample o! same so t'at you can get It better. 5et me tell you one t'ingt'at comparison o! #i=erent #ata types A compare t&o strings " comparet&o Integers" etc.. Is

Page 290: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 290/292

pure 4ava concept an# It 'as not any relation &it' selenium &eb#river. +ymain Intention Is to s'o& you t'at 'o& to 'an#le rest o! test e7ecutionbase# on e7ecution result an# 'o& to mar@ your test !ail In &eb#river testreport &it'out any Interruption In e7ecution.

5et me try to #escribe you 'o& to compare t&o #ouble values &it'e7ample.

s e7plaine# In my previous post" I! you are retrieving value !rom &ebpage t'en It &ill be al&ays In string !ormat because &eb#river s .getTe7tA!unction Is returning It as a string. So my rst tas@ Is to convert It !romstring to #ouble using bello& given synta7.

String actual3alString G #river. n#8lementABy.7pat'AK<<>M;i#G post-bo#y-$2/2$1*,$* ,$/, ,/ N<#ivM1N<table<tbo#y<trM2N<t#M$NK .getTe7tA H<<To convert actual value string to Double value.#ouble actual3al G Double.parseDoubleAactual3alString H

Secon# tas@ Is to create general !unction In CommonFunctions class tocompare t&o #ouble values. 5atter on I &ill use t'is !unction &'eneverre uire# In my test. +ain &or@ o! t'is !unction Is to accept t&o #oublevalues t'en compare t'em an# return true or !alse base# on comparisonresult. See bello& given e7ample.

pac@age Testng ac@H

import org.testng. ssertHimport org.testng.asserts.So!t ssertH

public class CommonFunctions L So!t ssert So!t ssert G ne& So!t ssertA H

public boolean compareDouble3alsA#ouble actualDob3al" #oublee7pecte#Dob3al L

tryL <<I! t'is assertion &ill !ail" It &ill t'ro& e7ception an# catc' bloc@ &ill bee7ecute#. ssert.assert8 ualsAactualDob3al" e7pecte#Dob3al H Qcatc'AT'ro&able t L <<T'is &ill t'ro& so!t assertion to @eep continue your e7ecution evenassertion !ailure. <<:n-comment bello& given 'ar# assertion line an# commnet so!tassertion line I! you &ants to stop test e7ecution on assertion !ailure.

<< ssert.!ailAK ctual 3alue KPactualDob3alPK n# 87pecte# 3alueKPe7pecte#Dob3alPK Do ot +atc'.K H

So!t ssert.!ailAK ctual 3alue KPactualDob3alPK n# 87pecte# 3alueKPe7pecte#Dob3alPK Do ot +atc'.K H

Page 291: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 291/292

<<I! #ouble values &ill not matc'" return !alse. return !alseH Q <<I! #ouble values matc'" return true. return trueH QQ

o& I can use compareDouble3als !unction In my test &'enever re uire#as #escribe# In bello& given e7ample.

pac@age Testng ac@H

import org.open a.selenium.ByHimport org.open a.selenium.WebDriverHimport org.open a.selenium. re!o7.Fire!o7DriverHimport org.testng.annotations. !terTestHimport org.testng.annotations.Be!oreTestHimport org.testng.annotations.TestH

public class Common Functions Test e7ten#s CommonFunctionsLWebDriver #riverH

;Be!oreTest public voi# StartBro&ser av:05A L #river G ne& Fire!o7DriverA H #river.getAK'ttp <<only-testing-blog.blogspot.in<2 1$< 1<te7tbo7.'tmlK H Q ; !terTest

public voi# CloseBro&serA L#river. uitA H

Q ;Test

public voi# testToCompareDoublesA LString actual3alString G #river. n#8lementABy.7pat'AK<<>M;i#G post-

bo#y-$2/2$1*,$* ,$/, ,/ N<#ivM1N<table<tbo#y<trM2N<t#M$NK .getTe7tA H <<To convert actual value string to Double value. #ouble actual3al G Double.parseDoubleAactual3alString H <<Call compareDouble3als met'o# Insi#e I! con#ition to c'ec@ Doublevalues matc' or not. i!AcompareDouble3alsAactual3al" 2 .( L <<I! values matc'" T'is bloc@ &ill be e7ecute#. System.out.printlnAKWrite ction ta@ing lines In t'is bloc@ &'en 3aluesmatc'.K H QelseL

Page 292: Selenium Webdriver Tutorials

8/18/2019 Selenium Webdriver Tutorials

http://slidepdf.com/reader/full/selenium-webdriver-tutorials 292/292

<<I! l t t '" T'i bl @ &ill b 7 t #


Recommended