Example One: The main difference is the inclusion of the WebDriver API into Selenium. I ve put to!

ether a small example belo" that uses the ne" API to lo! into t"o "eb based e#mail clients and send an e#mail. WebDriverTestBase.java package tests; import import import import import import import org.openqa.selenium.WebDriver; org.openqa.selenium.firefox.FirefoxDriver; org.openqa.selenium.support.ui.Wait; org.openqa.selenium.support.ui.WebDriverWait; org.testng.Assert; org.testng.annotations.AfterClass; org.testng.annotations.BeforeClass;

public class WebDriverTestBase { public static FirefoxDriver driver; public static Wait wait; @BeforeClass(alwaysRun = true) protected void startWebDriver() { driver = new FirefoxDriver(); wait = new WebDriverWait(driver, 120); } @AfterClass(alwaysRun = true) protected void closeSession() { driver.close(); } public static void assert !uals(Object actual, Object ex"ected) { Assert.assertEquals(actual, ex"ected); }

} VisibilityOfElementLocated.java package tests; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.support.ui.ExpectedCondition; public class #isibility$f le%ent&ocated implements x"ectedCondition { By findCondition; #isibility$f le%ent&ocated(By by) { this.findCondition = by; } public Boolean a""ly(WebDriver driver) { driver.findElement(this.findCondition); return Boolean.valueOf(true); } } WebmailTest.java package tests; import org.openqa.selenium.By; import org.testng.annotations.Test; public class Web%ailTest extends WebDriverTestBase { //variables public static final String 'A($$) *A+& = "example@yahoo.co.uk"; public static final String ($T*A+&) *A+& = "example@hotmail.co.uk";

@Test(descri"tion = "Sends an e-mail from Yahoo account") public void sendFro%'a,oo() { //new message variables String to = ($T*A+&) *A+&; String sub-ect = "Test Sending Email Message From Yahoo"; String %essa.e = "This is a test e-mail from Yahoo"; //login to yahoo driver.get("http://mail.yahoo.com/"); driver.findElement(By.id("username")).sendKeys('A($$) *A+&); driver.findElement(By.id("passwd")).sendKeys("mytestpw"); driver.findElement(By.id(".save")).click(); //create new message driver.findElement(By.id("compose_button_label")).click(); wait.until(new #isibility$f le%ent&ocated(By.xpath("id('_testTo_label')/ancestor::tr[1]//textare a"))); //send test message

driver.findElement(By.xpath("id('_testTo_label')/ancestor::tr[1]//textarea")).sendKeys( to);

driver.findElement(By.xpath("id('_testSubject_label')/ancestor::tr[1]//input")).sendKeys (sub-ect); driver.switchTo().frame("compArea_test_"); driver.findElement(By.xpath("//div")).sendKeys(%essa.e); driver.switchTo().defaultContent(); driver.findElement(By.id("SendMessageButton_label")).click(); //WARNING! sometimes a captcha is displayed here wait.until(new #isibility$f le%ent&ocated(By.xpath("//nobr[contains(text(), 'Message Sent')]")));

} @Test(descri"tion = "Sends an e-mail from Hotmail account") public void sendFro%(ot%ail() { //new message variables String to = 'A($$) *A+&; String sub-ect = "Test Sending Email Message From Hotmail"; String %essa.e = "This is a test e-mail from Hotmail"; //login to hotmail driver.get("http://mail.live.com/"); driver.findElement(By.name("login")).sendKeys(($T*A+&) *A+&); driver.findElement(By.name("passwd")).sendKeys("mytestpw"); if (driver.findElement(By.name("remMe")).isSelected()) { driver.findElement(By.name("remMe")).click(); } driver.findElement(By.name("SI")).click(); //create new message driver.switchTo().frame("UIFrame"); driver.findElement(By.id("NewMessage")).click(); //send test message driver.findElement(By.id("AutoCompleteTo$InputBox")).sendKeys(to); driver.findElement(By.id("fSubject")).sendKeys(sub-ect); driver.switchTo().frame("UIFrame.1"); driver.findElement(By.xpath("//body")).sendKeys(%essa.e); driver.switchTo().frame("UIFrame"); driver.findElement(By.id("SendMessage")).click();

assert !uals(driver.findElement(By.cssSelector("h1.SmcHeaderColor")).getTex t(), "Your message has been sent"); } }

selenium. **4eturns an Iterator obje+t for t e +olle+tion & i+ may be use! to retrieve an obje+t . String /pen%a%popup%&in!o&.testng.testng. org. org. Set &in!o&i!s . String 1avaS+ript%0opup%Win!o&s."fterMet o!.Iterator. org. import java. **iterator. public class Test#$%Win!o&'an!le(xample%by%jasmine ) ** De+lare global test varialbles WebDriver jasmine!river .selenium.util. String 0ositione!%0opup.WebDriver. import java. It mo!els t e mat emati+al set abstra+tion. import import import import import import org.openqa. new FirefoxDriver-.annotations.BeforeMet o!. 2Test public void f-.By. org..selenium. jasmine!river.Test.firefox.testng.util. org.annotations.openqa. throws Interrupte!(x+eption ) ** " Set is a 3olle+tion t at +annot +ontain !upli+ate elements...FirefoxDriver.Example Two: Window Handle example wit seleni!m webdriver " !sin# $ava % T e &et 'nterface for seleni!m a!tomation testin# package testngtest.annotations.openqa.getWin!o&'an!les-.Set.

System.. T rea!. jasmine!river.iterator-.s&it+ To-. ** s&it+ to main &in!o& to +li+. 0ositione!%0opup .out. /pen%a%popup%&in!o&..println-1avaS+ript%0opup%Win!o&s.println-..+li+.out.-.. iter.. iter.iterator-. ** /pen first popup jasmine!river. jasmine!river... on ot er popup lin. System. System. . System.out...println-0ositione!%0opup.. System...iter. &in!o&i!s... &in!o&i!s .out.out. jasmine!river.next-.fin!(lement-By. &in!o&i!s..-.8+ontent89*! iv7>9*p*a5.getWin!o&'an!les-.out... &in!o&i!s .. iter.. ** /pen se+on! popup jasmine!river.next-.&in!o&-1avaS+ript%0opup%Win! o&s.8+ontent89*!iv7:9*a5.println-/pen%a%popup%&in!o&. iter. &in!o&i!s..next-.println-50ositione!%0opup5..Iterator iter.iter.xpath-5**672i!. System..iterator-. iter.+li+.out.println-1avaS+ript%0opup%Win!o&s.println-/pen%a%popup%&in!o&.out.getWin!o&'an!les-.xpath-5**672i!. System.sleep-:<<<=..next-.. . System.fin!(lement-By.next-. iter...println-. 1avaS+ript%0opup%Win!o&s..next-.

+fm5.&in!o&-/pen%a%popup%&in!o&.sleep-:<<<=.s&it+ To-. For eg. jasmine!river. ) jasmine!river.. ? ? Example Three: In Selenium 2(WebDriver). jasmine!river. System.s&it+ To-.qua+. ? 2"fterMet o! public void afterMet o!-.get-5 ttp@**&&&.&in!o&-0ositione!%0opup. ..T rea!.+lose-... jasmine!river..quit-.out.+om*javas+ript*popup%&in !o&s...it.. ) jasmine!river. Twist recorder records actions in popup windows as commented code.println-53entere!%0opup5..&in!o&-1avaS+ript%0opup%Win!o&s..s&it+ To-. ? 2BeforeMet o! public void beforeMet o!-. testing popup windows involve switching the driver to the popup window and then running the corresponding actions.. jasmine!river. jasmine!river..+lose-.

**Write your logi+ to lo+ate t e appropriate popup before using +ommente! a+tions. &in!o&Iterator.getWin!o&'an!le-.. popup. IteratorCStringD &in!o&Iterator . ) String &in!o&'an!le .fin!(lement-By. popup. bro&ser. (dd the code )ust be ore the irst popup action being commented. as#ext-. ** save t e +urrent &in!o& an!le.getWin!o&'an!les-..5 The above code is the result o recording in the popup window having google search page.submit-. **=oo..name-5q5.s5. bro&ser. . 'ote that the actions tells the title o the popup window.. **a+tion in popup 5$oogle5.next-.. null. To get the above code wor"ing# $) Identi % the popup. & ile-&in!o&Iterator. String parentWin!o&'an!le . WebDriver popup .fin!(lement-By..name-5btn$5. The ollowing code identi ies the popup window with title !&oogle!.. at A 8'o& !o I an!le popup in WebDriver8 se+tion for more !etails... **a+tion in popup 5$oogle5. searching or !Thoughtwor"s!.sen!Beys-5T oug t&or..

&in!o&-&in!o&'an!le. **a+tion in popup 5$oogle5..fin!(lement-By...submit-.s&it+ To-. **a+tion in popup 5$oogle5.equals-5$oogle5. ** +lose t e popup. ) brea.name-5btn$5.. if -popup.. ? ? 2) *ncomment code or the same popup.5 +) ( ter the popup actions..name-5q5...s5. switch the driver bac" to the parent window..s&it+ To-. ** S&it+ ba+. popup. to parent &in!o&...+lose-.getTitle-.&in!o&-parentWin!o&'an!le.fin!(lement-By. Example Four: . popup.. bro&ser..popup . bro&ser. bro&ser.sen!Beys-5T oug t&or.

< * Web!river tests5 4unning Selenium F.openqa.FirefoxDriver. >import org.ui.java &it t is +ontent@ Gpa+.s Nupport.allinurl GOR:"stringSjava.Wait.< * Web!river testsI so if you feel you !onJt un!erstan! & at I am sayingI please rea! t at post first.openqa.age +om.ing o& +oul! I ma.s G<upport. Mimport org.+om*F<GF*<F*runningAseleniumAFA&eb!riverA tests*5 Ho 54unning Selenium F.google.+om*sear+ GN P l.n+ona.selenium.openqa.+om*F<GF*<F*runningAseleniumAFA&eb!riverA tests*5 Ho 54unning Selenium F. F :import org.WebDriverWait.+omQbtnI .< * Web!river testsI I &as t in.fi Lrefox.Creating a test suite using Selenium 2 / Webdriver "fter &riting my post@ 'E0(4=I#B 5 ttp@**n+ona. T is post is ig ly base! on 'E0(4=I#B 5 ttp@**n+ona.openqa.enQq. Test Suite T e first t ing I figure! is t at &e nee! a main +lass t at &ill +all all t e ot er +lasses testsI so I +reate! a main test suite file +alle! #+onaTestSuite. KWebDriver. Oimport org. In t is post I am going to explain o& I use! 1ava +lasses to !o t is.selenium.selenium.selenium.< * Web!river tests5 4unning Selenium F.'E0( GL4=I#B GM5 ttp@**&&&.ui. GG GFpubli+ +lass #+onaTestSuite G:) GK G> publi+ stati+ voi! main.sun.e a test suite so I +oul! run all my fun+tional tests &it just one +omman!.

:< :G boolean result.F<. result .run-!riverI &ait. If any test fails result &ill be false.. ne& Mis+Test3lassT& K<o-.. QQ mt+:. . ne& Mis+Test3lass-.. ne& Mis+Test3lassT r K:ee-.IRFMmRF<Feeling FGRF<=u+.. for ea+ of t e +lasses KO ** above. KG Mis+Test3lassT ree KFmt+: . FF ) F: ** /bje+ts t at are going FKto be passe! to all test +lasses F> WebDriver FL!river . :N Mis+Test3lassT&o :Omt+F .y5 String79 args. ne& FOWebDriverWait-!riverI :<. QQ mt+F. :F try :: ) :K ** 'ere &e a!! all t e :>test +lasses &e &ant to run :L Mis+Test3lass :Mmt+G . ne& FirefoxDriver-.run-!riverI &ait. mt+G. KK K> ** We +all t e run KLmet o! -t at met o! runs all KM ** t e tests of t e KN+lass. FM FNWaitCWebDriverD&ait .run-!riverI &ait..

.+omQbt nI.google.allinurl R:"systemSjava.enQq.IRFMmRF<Feeling RF<=u+. ? finally ) !river. if -Tresult.5.out.printSta+.allinurl R:"ex+eptionSjava.sun.+om*sear+ P l.enQq.sun.sun. ? +at+ .IRFMmRF<Feeling RF<=u+...+om*sear+ P l.allinurl R:"systemSjava. result .5 @ 5faile! .+omQbt nI.println-5Test 5 S -result P 5passe!.enQq..+om*sear+ P l. ) e.+lose-.. ? 'E0(4=I#B 5 ttp@**&&&. false..google.y5 System. ) 'E0(4=I#B 5 ttp@**&&&.Tra+e-.IRFMmRF<Feeling RF<=u+.y5 (x+eption e.'E0(4=I#B 5 ttp@**&&&.exit-G.google.y5 System. ? ? ? Test Class .+om QbtnI.

MWebDriver. Limport org.ui. :: :K setUp-./ur test +lasses nee! to ave a run met o! t at &ill run all t e tests it +ontains. GLimport org.selenium.selenium.age +om.@ Gpa+.ui. :F &ait .s GMupport.fi GGrefox.openqa.s G:upport.openqa.=ist. OWeb(lement.openqa. Kimport org.n+ona. G<import org. Nimport org.ui. GFimport org. FF F: stati+ WaitCWebDriverD &ait FK.selenium.selenium. &ait"rg.openqa.selenium. GN GOpubli+ +lass Mis+Test3lass F<) FG stati+ WebDriver !river. 'ere is an example of o& it +oul! loo.selenium. F :import java. :< ) :G !river .util.openqa.openqa.(xpe+te!3on!ition.Wait.B >y.openqa. F> FL FM publi+ stati+ boolean run-We FNbDriver !river"rgI FOWaitCWebDriverD &ait"rg.. GKimport org.selenium.s G>upport.FirefoxDriver. :> .WebDriverWait. !river"rg.

KG .:L ** 4un all t e met o!s :Man! return false if any fails :N return :O mis+Met o!-.!ir5 va >lue.5 name. K< QQ mis+Met o!F-..5sr+.5. We just nee! to + ange t e target to use t e name of our ne& test suite file@ GCproje+t base!ir. ) ** 0ut your tests +o!e ere return result ? ? Build file T e buil!.5 FTest "utomation5D : K Cproperty name.5V)base!ir?*java*sr+5*D .xml file use! for t is test suite almost t e same as t e one in my previous post. KF ? K: KK private stati+ boolean mis+M et o!-. ) ** 0ut your tests +o!e ere return result ? private stati+ boolean mis+M et o!F-.

5buil!.!ir5 GLvalue.!ir?5D FO :< Cin+lu!e name.!ir !ir.5V FM)testautomation.5V O)base!ir?*java*+lasses*main5* G<D GG GF Cproperty name.!ir?5*D Cjava+ !ebug.5V )+lasses.jar FG5*D FF F: FK Cpat i!.5V F<)buil!.5V)lib.5V)base!ir?*lib5*D GK G> Cproperty name.5V )buil!.L M Cproperty name.5buil!5 !es+ri ption.jar?5*D FN Cfileset !ir.!i Nr5 value.!ir?5*D Cm.5V)base!ir?*buil!5*D GM GN Cproperty name.+l F>asspat 5D FL Cfile file.jar5 *D :G C*filesetD :F C*pat D Ctarget name.5testautom GOation.jar5 value.!ir !ir.56.5lib.5true5 .!ir5 val G:ue.5testautomation.!ir?*testautomation.5sets up t e environment for test exe+ution5D Cm.5+lasses.

5runA example5 !es+ription.5V )+lasses.#+onaTestSuite Example Five: .V)example? AAAAD Cjava +lassname.!ir?5 in+lu!e"nt4untime.jar?5*D C*targetD Ctarget name.n+ona.5testautomatio n. 3lassname is no& +om.5V )testautomation.n+on a.5false5 +lasspat ref.sr+!ir.+lasspat 5*D Cjar base!ir.!ir?5 !est!ir.+lasspat 5*D C*targetD C*proje+tD Run the suite First &e nee! to buil! t e proje+t@ Gant buil! Finally to run it &e &oul! use t is +omman!@ Gant runAexample ADexample.5+om.5V)sr+.5V )+lasses.5run +omman!Aline example5D CTAAAA T is is t e line I mo!ifie!.5testautomatio n.!ir?5 jarfile.5true5 +lasspat ref.V)example?5 failonerror.

Wor"boo"7 import org.bi .9i /6ception7 public class TestData< WebDriver driver = new :hromeDriver()7 public void 1ogin() throws Interrupted/6ception.read.-.has o!e.open8a.open8a.io./6ception < .I.Wor"boo"7 import )6l.selenium.has o!e. 9i /6ception.9%7 import org. import )6l.chrome.Sheet7 import )6l./6ception7 import )6l.FileInputStream7 import )ava. orumotion.selenium.:hromeDriver7 import )ava.WebDriver7 import org. om/"#$"/#%/sele&ium' we(!river'sele&ium'server'a&!'pa)eo(*e ts'(+'example/ Example .Selenium WebDriver. om/"#$"/#%/sele&ium'we(!river'sele&ium' server'a&!'pa)eo(*e ts'(+'example/" http://www.ix: .Sheet7 import )6l. Selenium Server and PageObjects by Example HYPERLINK "http://www.io. I.selenium.net3t$4+$5need5 selenium5webdriver5e6cel5sheet5e6amples! Selenium Webdriver + Excel sheet examples /6ample or read3write operations rom e6cel sheet using selenium webdriver./01I'2 !http#33selenium orum.open8a.

/6ception < S%stem.ropert%(!webdriver.send2e%s(s)7 driver. I thou)ht it was time to share some .e+ .FileInputStream i=new FileInputStream(!D#>>sreewor"space>>sree.id(!gb8 8!))... To!a+ we-re overi&) /e(0river-s &ative solutio& to a ver+ ommo& issue whe& !oi&) !istri(ute! ross (rowser testi&): uploading files in remote servers. I.ausing here seems to ma"e it wor" better.i& e it-s (ee& a while si& e m+ last .get:ell(2. !D#3sree"umar3Tutorial3Selenium3chromedriverBwinB$C.driver!.get(!https#33www. .43 chromedriver.println(s)7 driver.2).name(!btn2!)).1ogin()7 ? ? /6ample Seven# Uploading iles in Remote Web!river $arch %th& '()' b* Santia!o Suare+ Ordo.6ls!)7 Wor"boo" wor"boo" = Wor"boo".DDE. TestData login = new TestData()7 login.get:ontents()7 S%stem.ele&ium tips (lo) post.google.getSheet(!1ogin!)7 String s=sheet. 9i /6ception.e6e!)7 33.out.clic"()7 ? public static void main(String@A args) throws Interrupted/6ception. ind/lement(9%.set.chrome.ele&ium love a)ai&.driver.com!)7 login. ind/lement(9%.4.getWor"boo"( i)7 Sheet sheet = wor"boo". so.

set6apa(ilit+'"sele&ium'versio&"* "". o& urre&t.Time4&it" public class #esting$ploadSe2Sauce e%tends Test6ase & private Remote/e(0river !river" public void set$p'( throws Ex eptio& & 0esire!6apa(ilities apa(illities ) 0esire!6apa(ilities.=P(" apa(illities.ele&ium $ is to pla e +our 3iles i& a& a essi(le we( server a&! use the atta hFile omma&! that poi&ts to the orre t 4RL.o!e.ele&ium "?s File0ete tors"(" .set6apa(ilit+'"&ame"* "Remote File 4ploa! usi&) .1s +ou ma+ 2&ow. Here are some examples tests: 8ava import import import import import import *u&it." *ava. the 3ile will be base64 encoded and sent transparentlythrou)h the 8. our .4RL" *ava. /ith .ele&ium ".ope&:a.3ire3ox'(" apa(illities.set6apa(ilit+'"versio&"* "<"(" apa(illities. 1lmost ma)i all+.util.set6apa(ilit+'"plat3orm"* Plat3orm..3ramewor2.&et. 3or example. all +ou &ee! to !o is use the setFileDetector method to let /e(0river 2&ow that +ou-re uploa!i&) 3iles 3rom +our lo al omputer to a remote server i&stea! o3 *ust t+pi&) a path. *ust make sure Selenium 2.3ramewor2.sele&ium..remote. . For those o3 +ou !oi&) this lo all+.1ssert" *u&it.ele&ium " 6lou!7.#"(" apa(illities. /he& movi&) this test to a remote server 5su h as. This is a& ex elle&t solutio&. the .ope&:a." or). This 3eature is available in all the official Selenium 2 bindings.$>.ele&ium 0ev team has lu 2il+ ma!e this a lot more straightforward. as it lets +ou swit h +our tests 3rom a lo al to remote 0river without havi&) to worr+ a(out ha&)i&) +our tests.sele&ium. the wa+ to a!!ress this i& . all +ou &ee! to !o is use the sendKeys omma&! totype the local path o3 the 3ile i& a&+ 3ile 3iel!. This wor2s li2e a harm i& all !rivers.9N/ireProto ol 3or +ou (e3ore writi&) the 3ixe! remote path.! or newer is used as this 3eature has (ee& release! the&.Test6ase" or).

sau ela(s.ma&a)e'(.E69N0.3i&!Eleme&t'C+. 9ut with the commencement o Selenium5 23Webdriver.3i&!Eleme&t'C+.i!'"m+3ile"((" uploa!.*p) 5ima)e/*pe)7"* !river.assertE:uals'"!ar2(ul(.)etText'((" public void tearDown'( throws Ex eptio& & !river.(" public void testSauce'( throws Ex eptio& & !river.setFile0ete tor'new Lo alFile0ete tor'((" !river. Following things are re8uired# .)et'"http://sso.ta)Name'"p"((. li 2'(" !river. In the ollowing blog I will cover how to set5up %our grid and the changes that will be re8uired or eas% e6ecution o cases.3i&!Eleme&t'C+.i!'"su(mit"((. om:>#/w!/hu("(* apa(illities(" !river.ta)Name'"im)"((" 1ssert.impli itl+/ait'%#* Time4&it. om/test/)ui&ea'3ile'uploa!"(" /e(Eleme&t uploa! ) !river. grid setup has been changed.:uit'(" DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD DDDDDDDDDDDDDDDD Example Ei)ht: Selenium Grid with WebDriver I had earlier covered in m% blog on how to e6ecute %our 0: cases in selenium grid.3i&!Eleme&t'C+.timeouts'(.!river ) new Remote/e(0river' new $+.se&!Ke+s'"/4sers/sso/the/lo al/path/to/!ar2(ul(.'"http://@user&ameA:@api' 2e+ABo&!ema&!..!ev.*p)"(" !river.sau ela(s.

re%ot $% e.-.google. -ou can download it rom the lin"# ..o"en!a.seleniu%.annotations.google.testn.. The latest one can be downloaded rom the lin"# .o"en!a../01I'2 !http#33testng. Fava $.*alfor%ed/R& xce " "tion0 % i%"ort -ava. :hromeDriver.com3p3chromium3downloads3list! :hromeDriver The Test Code Following is an e6ample o test5class that have a test case to search google or !testing! and veri %ing a ter clic"ing it on a lin".seleniu%.html! Test'& H.net. /clipse with Test'& plugin installed(optional) E.$.-./01I'2 !http#33code. Selenium 2.66 version server )ar and Fava librar%. HYPERLINK "http://bl !"v#runin"$ m/%&''/'&/(elenium)!rid)with) webdriver"html" * i%"ort $ -ava./01I'2 !http#33code.WebDr $# iverBac1edSeleniu%0 $$ i%"ort $" or.re%ot $G e.o"en!a../R&0 E F i%"ort G or.Af $H terClass0 "# i%"ort "$ .seleniu%. Test'g )ar .net.com3p3selenium3downloads3list! Selenium52 2.seleniu%.o"en!a.Re%oteWebDriver0 $< i%"ort $> or.DesiredCa"abilities0 $E i%"ort $F or. :an be downloaded rom# .WebDr < iver0 > i%"ort H or..-..org3doc3download.G and above +.

twor1s.Te st0 i%"ort co%.concat 5"ort8.ou.concat56=wd=.ost<6.testn."" "% "E "F "G "< "> "H %# %$ %" %% %E %F %G %< %> %H E# E$ E" E% EE EF EG E< E> EH F# F$ F" F% FE FF FG F< F> FH G# G$ or.Be foreClass0 i%"ort or.2a ra%eters0 i%"ort or.tt"<==local.annotations..tt"<==local.. browser.Strin.Assert0 "ublic class 3oo. "ort84 DesiredCa"abilities ca"ability9 new DesiredCa"abilities580 ca"ability. ca"ability80 seleniu% 9 new WebDriverBac1edSeleniu%5d .annotations.6 "ort678 @BeforeClass "ublic void beforeClass5Strin.annotations.Seleniu%0 i%"ort -unit.ub688 .. 6.ost=6 ..fra%ewor1.testn.seleniu% ..setBrowser:a %e5browser80 try 4 WebDriver driver9 new Re%oteWebDriver5new /R&56 ('2 R&+:..le 4 "rivate Seleniu% seleniu%0 @2ara%eters546browser6.t.testn..

ty"e56id9lst> ib6.enerated catc.clic156==in"ut ?@value9@3oo. 6testin.e free encyclo"edia688 brea10 7 catc.le Searc.G" G% GE GF GG G< G> GH <# <$ <" <% <E <F <G river.read.le.o"en56=680 seleniu%.slee"5FBBB80 7 catc."rintStac1Trace580 .oo.tt"<==www.fail56ti%eout6 80 try 4 if 5seleniu% . > Wi1i"edia. 5 xce"tion e8 4 7 try 4 T.58 4 seleniu%.co%=6 . 5*alfor%ed/R& xce"tion e8 4 7 7 @Test "ublic void searc. t.oo.. 6 ('2 R&+:.tt"<==www.@A680 for 5int second 9 B00 secondCC8 4 if 5second D9 EB8 Assert.co%680 7 catc.. 6. 5+nterru"ted xce"tion e8 4 == T$D$ Auto> .le.680 seleniu%.is le%ent2resent56 lin19Software testin. bloc1 e.

e free encyclo"edia680 for 5int second 9 B00 secondCC8 4 if 5second D9 EB8 Assert."rintStac1Trace580 7 @AfterClass "ublic void afterClass584 seleniu%. The be ore:lass method accepts two properties !browser! and a !port!.clic156lin19So ftware testin.read.688 brea10 7 catc.isText2resent56 Software testin. 5+nterru"ted xce"tion e8 4 7 7 e. These values are used or initialiIation o driver and in5turn or . 5 xce"tion e8 4 7 try 4 T. > Wi1i"edia.sto"580 7 7 In the above class I am using the Test'& !.fail56ti%eout6 80 try 4 if 5seleniu%."rintStac1Trace580 7 seleniu%.slee"5FBBB80 7 catc. t.arameter! propert% to provide di erent data set to the !9e ore:lass! method !be ore:lass!.7 e.

I %ou want the code to be purel% WebDriver.tt"<==local. In case it is on di erent s%stem the !localhost! needs to be re5placed with the ip o the said s%stem.test.3oo.ost<6. In this e6ample I am assuming the server to be running locall%.6ml! ile or the above said test class.concat5"ort8. The main part in this test case is how the driver ob)ect is being created# DesiredCa"abilities ca"ability9 new DesiredCa"abilities580 ca"ability.read>count96H6D Gtest na%e96Seleniu% Test:3 > F6D G"ara%eter na%e96browser6 value96firefox6 =D G"ara%eter na%e96"ort6 value96IIII6 =D GclassesD Gclass na%e96co%. so that its eas% or gu%s who had wor"ed on Selenium5$ to understand the code.setBrowser:a%e5browser80 WebDriver driver9 new Re%oteWebDriver5new /R&56. This tells the selenium on which browser the test5case needs to run and where the server is located. The said con iguration e6ecutes the test5cases across di erent browser..le6 =D . ca"ability80 The above code creates an ob)ect o Desired:apabilit% and then set the browser value to it. %ou can directl% use the !driver! ob)ect or de ining %our test5cases. Following is an !testng. TestNG configuration For parallel e6ecution %ou need to use the Test'& con iguration.initialiIation o selenium ob)ect.ub688 .concat56=wd=.testn. In the above code I am using the !WebDriver9ac"edSelenium! class or creation o the selenium ob)ect. Gsuite na%e96Seleniu% Test:3 Suite6 "arallel96tests6 t. 'ow using this !capabilit%! ob)ect I am creating the webdriver ob)ect using the !0emoteWebDriver! class.

ub / r # +ire+ 1 b#(ed n de: -ava >-ar seleniu%>server>standalone>J.l( there #re tw di++erent te(t( in(ide # (uite" / r e#$h te(t # di++erent "br w(er" p#r#meter v#lue h#( been $ n+i!ured" Selenium-Grid server N w (t#rt 0 ur !rid u(in! the + ll win! $ mm#nd(" Run e#$h $ mm#nd in # (eper#te termin#l r $ mm#nd pr mpt b0 ! in! t the dire$t r0 $ nt#inin! 0 ur (elenium)(erver)(t#nd#l ne)%"1"1"2#r" In the + ll win! e1#mple I #m u(in! the %"3"& ver(i n + (elenium" / r Hub: -ava >-ar seleniu%>server>standalone>J.K.testn.-ar >role .ub .tt"<==local.B.G=classesD G=testD Gtest na%e96Seleniu% Test:3 > J6D G"ara%eter na%e96browser6 value96c.K.-ar >role webdriver >. I #m $ n+i!urin! -e(tNG t run "te(t(" in p#r#llel" .B.3oo.rid=re..ister >"ort HHHE >browser browser:a%e9firefox / r! !le)$hr me b#(ed n de: .le6 =D G=classesD G=testD G=suiteD In the #b ve $ n+i!ur#ti n.ost<IIII=.ro%e6 =D G"ara%eter na%e96"ort6 value96IIII6 =D GclassesD Gclass na%e96co%.test.

B.nce %ou run the above testng.tt"<==local.google. browser5version and all and then %ou can run %ou cases on particular t%pe o environment b% con iguring them accordingl%. using ant or maven.S (1inu6. Test'& will e6ecute the cases rom the &oogle class on grid across di erent browsers ire o6 and google5chrome as con igured.google. For e6.ro%e 9e ore running the above command %ou need to provide the chrome5driver path to the propert% !5Dwebdriver. .ro%edriver >-ar seleniu%>server>standalone>J.driver!.ister >"ort HHHH >browser browser:a%e9c.-ar >role webdriver >. -ou have a user detail orm where %ou need to .) . In this blog I will e6plain what is Data5Driven testing and will give an e6ample o Data5 driven testing using Funit and Test'& ramewor"s.c.-./01I'2 !http#33code.com3p3selenium3wi"i3&rid2! http#33code. 9ut the word will still be new or some resh aces in the ield o automation.com3p3selenium3wi"i3&rid2 Example Ni&e: D#t#)Driven te(tin! u(in! 4unit #nd -e(tNG Kost o the gu%s who are into automation ma% be "nowing the term Data5Driven testing.=to=c.ro%e. browser.K. Kore details can be ound at the ollowing *01# .rid=re. Data5Driven testing as the name suggests is a test driven b% the Data.chrome. 'ow run %our testng. -ou can do con igurations or environment li"e .Windows).6ml! li"e rom command prompt.6ml.r %ou can choose other wa%s to e6ecute !testng.ost<IIII=.driver9="at.6ml rom eclipse b% selecting it 5J 0ight clic" 5J 0un as 5J Test'& suite(this will wor" onl% i %ou have Test'& plugin installed in %our eclipse.ub .-ava >Dwebdriver.

L0 G i%"ort or..Assert.After0 < i%"ort or.runner.RunWit.Test0 H $# i%"ort $$ or.0 $" i%"ort $% or. Following I will mention a Data5Driven approach or searching on google with di erent data using Funit and Test'g ramewor"s# Using Junit HYPERLINK "http://bl !"v#runin"$ m/%&''/'&/d#t#)driven)te(tin!) u(in!)2unit)#nd"html" * i%"ort static $ or. . In case o additions or deletion o new3old entries .t.2ara%et $E eriMed0 $F i%"ort $G .ou. In this case it will be E and hence the test case will get e6ecuted E times with di erent data5set. The test case will get e6ecuted based on the data provided to it. that is illing the orm with user data and then submitting it.Before0 > i%"ort or..twor1s.seleniu% F .runners..fail0 " % i%"ort E co%.-unit.-unit. %ou )ust have to change the data and not %our actual test5case.-unit.ere i %ou have E di erent user data available and %ou have to write automation cases or these.-unit. %ou ma% end up writing E di erent automation test cases(one or each user data)..-unit..enter details o multiple users and save them. The advantage o using a Data5driven approach is that %ou reduce %our e ort in writing3maintaing test5cases or %our di erent t%pe o data.-unit.. I %ou appl% a Data5Driven approach %ou will end up with onl% one test5case..

67.67780 7 ..util.WebDr iverBac1edSeleniu%0 i%"ort or.cl ass8 "ublic class Nunit3oo.&ist0 @RunWit.2ara%eters0 i%"ort or.52ara%eteriMed. testData84 t.FirefoxDriver0 i%"ort -ava..o"en!a.as&ist5new $b-ect?A ?A446testin.runners.seleniu%.$< $> $H "# "$ "" "% "E "F "G "< "> "H %# %$ %" %% %E %F %G %< %> %H E# E$ E" E% EE EF EG E< E> EH F# F$ F" F% FE FF FG or.2ara%et eriMed.is.seleniu%.o"en!a.o"en!a.leBase 4 "ublic Seleniu% seleniu%0 WebDriver driver0 "rivate Strin.leBase5Strin.firef ox.util..Arrays0 i%"ort -ava. testData0 "ublic Nunit3oo.-unit..WebDr iver0 i%"ort or.testData9testData 0 7 @2ara%eters "ublic static &istG $b-ect?AD data58 4 return Arrays.seleniu%.46Software testin.

oo.tt"<==www.le Searc.tt"<==www..le.co%680 seleniu%. 6. 5 xce"tion e8 47 T. t. t.oo.clic156==in"u t?@value9@3oo.ty"e56id9lst> ib6..tt"<==www.slee"5FBBB80 7 seleniu%.co%=6 .tt"<==www. 6 ('2 R&+:.le.oo. 6.co%680 7 @Test "ublic void testSearc.F< F> FH G# G$ G" G% GE @Before "ublic void set/"58 t.le.e free . > Wi1i"edia.@A680 for 5int second 9 B00 secondCC8 4 if 5second D9 EB8 fail56ti%eout680 try 4 if 5seleniu%.le. > Wi1i"edia.o"en56 ('2 R&+:.rows xce"tion 4 driver9 new FirefoxDriver580 seleniu% 9 new WebDriverBac1edSeleniu%5d river.o"en56=680 seleniu%.read...oo.clic156lin19S oftware testin.e free encyclo"edia688 brea10 7 catc.co%=6 .58 t.rows xce"tion 4 seleniu%.is le%ent2resen t56lin19Software testin. testData80 seleniu%.

o"en!a.rows xce"tion 4 seleniu%.seleniu%.seleniu%.read.688 brea10 7 catc.firef $% .t.WebDr $# iverBac1edSeleniu%0 $$ i%"ort $" or.isText2resent56 Software testin.twor1s.encyclo"edia680 for 5int second 9 B00 secondCC8 4 if 5second D9 EB8 fail56ti%eout680 try 4 if 5seleniu%.seleniu%.sto"580 7 7 Using TestNg: HYPERLINK "http://bl !"v#runin"$ m/%&''/'&/d#t#)driven)te(tin!) u(in!)2unit)#nd"html" * i%"ort $ co%.L0 % E F i%"ort G or...o"en!a.o"en!a.WebDr < iver0 > i%"ort H or.seleniu% " ..ou..slee"5FBBB80 7 7 @After "ublic void tearDown58 t. 5 xce"tion e8 47 T.

FirefoxDriver0 i%"ort or.Be fore*et.od0 i%"ort or.Af ter*et.testn. 6 ('2 R&+:.testn..testn..rows xce"tion 4 driver9 new FirefoxDriver580 seleniu% 9 new WebDriverBac1edSeleniu%5d river.od "ublic void set/"58 t..annotations.annotations.46Software testin.leBase 4 "ublic Seleniu% seleniu%0 WebDriver driver0 @Data2rovider5na%e96"ar a%eter68 "ublic static $b-ect?A ?A data58 4 return new $b-ect?A?A 446testin.annotations.od0 i%"ort or..Assert0 i%"ort or. .testn..testn.Da ta2rovider0 i%"ort or.Te st0 "ublic class Test:33oo.6770 7 @Before*et..67....$E $F $G $< $> $H "# "$ "" "% "E "F "G "< "> "H %# %$ %" %% %E %F %G %< %> %H E# E$ E" E% EE EF EG E< E> EH F# F$ F" F% ox.annotations..

5 xce"tion e8 47 T.oo. > Wi1i"edia.@A680 for 5int second 9 B00 secondCC8 4 if 5second D9 EB8 Assert.5Strin.tt"<==www.tt"<==www.o"en56 ('2 R&+:.ty"e56id9lst> ib6.is le%ent2resen t56lin19Software testin.le.clic156==in"u t?@value9@3oo.fail56ti%eout680 try 4 if 5seleniu%.oo. t.co%=6 .clic156lin19S oftware testin.tt"<==www..le Searc.o"en56=680 seleniu%.. 6.le.oo.oo.co%=6 . testData80 seleniu%. testData8 t. t.slee"5FBBB80 7 seleniu%. > Wi1i"edia.FE FF FG 6.co%680 7 @Test5data2rovider96"ar a%eter68 "ublic void testSearc.co%680 seleniu%.tt"<==www.le.le.rows xce"tion 4 seleniu%.e free encyclo"edia688 brea10 7 catc...read.fail56ti%eout680 .e free encyclo"edia680 for 5int second 9 B00 secondCC8 4 if 5second D9 EB8 Assert.

read. but the same is not possible in Funit. .slee"5FBBB80 7 7 @After*et. 5 xce"tion e8 47 T.4 implementation. Its simple to do data5driven testing in Test'& ramewor" as %ou can provide a di erent data providing unction or each test5 method.sto"580 7 7Gs"an class96A""le> style>s"an6 style96font> fa%ily< #erdana.aramateriIed option to the class in Funit and suppl% data to the constructor o the said class.od "ublic void tearDown58 t. Example Te&: HYPERLINK "http://bl !"v#runin"$ m/%&'&/&5/!ener#tin!)(elenium) rep rt()u(in!"html" Gener#tin! (elenium rep rt( u(in! -e(tNG)1(lt thr u!h .try 4 if 5seleniu%.nt Test'&56slt generates user riendl% reports using the Test'& results output (testng5results. sans> serif06D G=s"anD The main di erence in the above two unctions is that %ou provide a . Its uses the pure LS1 or report generation and Sa6on as an LS12.isText2resent56 Software testin.688 brea10 7 catc.6ml).rows xce"tion 4 seleniu%. Where as in Test'& %ou do the same at the test5method level.

I %our current pro)ect does not use ant build then %ou can use ant onl% or the report generation purpose.Kost o the material is ta"en rom the original site. H. id96%aster> < class"at. For generating testng56slt report or %our pro)ect do the ollowing# $.org3. HYPERLINK "http://bl !"v#runin"$ m/(e#r$h/l#bel/Selenium 6%&Rep rt(" * G"ro-ect na%e96test6 $ basedir96.apache.6sl rom the testng56slt older(testng56slt5$.google.$>src>main>resources) to %our own pro)ect older. http#33code.M. Kodi % %our build.com3p3testng56slt3 I will tell in this blog how to implement this report or %our pro)ect.6ml o ant and add the ollowing target to it. This implementation will tell %ou how to generate the testng56slt report using ant.ele%ent H location96O4B+:76 =D $# Gfileset dir96O $$ 4&+B76D $" Ginclude $% . 'ow cop% the sa6on librar% rom (testng56slt5$. I %ou dont "now ant please chec" the (pache ant website http#33ant. Download the testng56slt 2.6D > G"at. *nIip and cop% the testng5results.$>lib>sa6on5 C.)ar)to %our pro)ect lib older.6D " G"ro"erty na%e96&+B6 % value96O4basedir7=libs6 =D E G"ro"erty na%e96B+:6 F value96O4basedir7=bin6 =D G G"at. +.

testDeta ilsFilter6 =D G"ara% ex"ression96true6 na%e96test:.Pslt.S.> xslt>re"ort6D Gdelete dir96O 4basedir7=testn.out"utDi r6 =D G"ara% ex"ression96true6 na%e96test:.-ar6 =D G=filesetD G="at.Pslt.> results.C$:F.sortTest Case&in1s6 =D G"ara% ex"ression96FA+&.2AS S..x%l6 style96O 4basedir7=testn.B')C&ASS6 na%e96test:.>xslt6D G=%1dirD Gxslt in96O 4basedir7=test> out"ut=testn.> xslt=index.s.owRunt .Pslt.D Gtar.> results.Pslt.>xslt6D G=deleteD G%1dir dir96O 4basedir7=testn.>xslt=6 na%e96test:.et na%e96testn.$E $F $G $< $> $H "# "$ "" "% "E "F "G "< "> "H na%e96LL=L.+2.t%l6D G"ara% ex"ression96O 4basedir7=testn.xsl6 out96O 4basedir7=testn.

TK1 content.etD G="ro-ectD The LS1 trans ormation can be con igured using the parameters described below. De aults to $.TK1 reports.i%eTotals6 =D Gclass"at. This parameter is not re8uired. The in ormation is displa%ed or each test case and aggregated or the whole suite. testNg!slt"sho%&untimeTotals 5 9oolean lag indicating i the report should displa% the aggregated in ormation about the methods durations. testNg!slt"css$ile 5 Speci ies and alternative st%le sheet ile overriding the de ault settings. testNg!slt"reportTitle 5 *se this setting to speci % a title or %our . testNg!slt"output#ir 5 Sets the target output director% or the . refid96%aster>class"at. 9% de ault the% are rendered in the order the% are generated b% Test'& so %ou should set this to true to change this behavior. 'on5mandator% parameter.6D G=class"at. de aults to alse. I %ou are using the Kaven plugin this is set automaticall% so %ou donNt have to provide it. :an be an% combination (comma5separated) o # . testNg!slt"sortTestCase'in(s 5 Indicates whether the test case lin"s (buttons) in the le t rame should be sorted alphabeticall%. This is mandator% and must be an absolute path. testNg!slt"chartScale$actor 5 ( scale actor or the SO& pie chart in case %ou want it larger or smaller.D G=xsltD G=tar. This is not a mandator% parameter and de aults to !Test'& 0esults!. testNg!slt"test#etails$ilter 5 Speci ied the de ault settings or the chec"bo6 ilters at the top o the test details page.

(lso dont orget to add the sa6on librar% to %our target classpath else %ou will get an error.)unit.-unit.. ( screenshot o the last state o the browser helps in this case..:. the path to the st%le sheet testng5 results.muhu".(SS.6sl and the output inde6../01I'2 !http#33seleniumh8. Step 0: Getting the name of the current test -ou probabl% want to include the name o the test class and the name o the current test method in the ilename.After0 i%"ort or.-.0K(TI'/T When %ou run .I:T*0/ !http#33www. I will be using .'F.-unit. .6ml) .png! >P K/0&/F.S2I.org3! )unit as the test ramewor". Example Eleve&: How to take a screenshot at the end of your Selenium WebDriver tests? I':1*D/.F(I1.-.ere is how %ou ind it out# i%"ort or. In m% case it is the master5classpath.9-B:1(SS -ou need to provide the testng56slt st%lesheet the Test'& results 6ml(testng5results..org3! Selenium headless on the server.html path./01I'2 !http#33www. debugging ailures with )ust the standard outputs can be challenging. 'oe run the ant target or report generation (in m% case !testng5 6slt5report!) and chec" the ouput older con igured b% %ou or testng56slt report.rules.Rule0 i%"ort or..-unit.com3wp5 content3uploads324$23423webdriverBscreenshot5+4462M4.Test:a%e0 . This little tutorial e6plains how to ta"e such a screenshot and save it as an arti act in Fen"ins.

-.etScreens.err.html! >l !ta"ing5a5screenshot! Selenium documentation suggests the ollowing# i%"ort or..$ut"utTy"e0 i%"ort or.o"en!a.Ta1esScreens.etClass58. class:a%e 9 t."rintln56Finis.ot0 "ublic abstract class Test 4 @After "ublic void tearDown58 4 == driver is your WebDriver File screens.is.After0 i%"ort -ava..od:a%e 9 t.seleniu%. %et.seleniu%.etSi%"le:a%e580 Strin."ublic abstract class Test 4 @Rule "ublic Test:a%e test:a%e 9 new Test:a%e580 @After "ublic void tearDown58 4 Strin.od:a%e580 Syste%.6 C %et.od:a%e C 658680 7 7 The code above is )ust a simple demonstration o how %ou use Test:a%e.is.org3docs34HBwebdriverBadvanced.test:a%e.ot8 driver8 ...-unit.owever it is a good idea to produce a simple output li"e above in the beginning and the end o each test.io.o"en!a.ed test 6 C class:a%e C 6.otAs5$ut"utTy"e../01I'2 !http#33seleniumh8. .File0 i%"ort or.et*et.F+& 80 .ot 9 55Ta1esScreens. Step : !aking the screenshot ...

"n.ot is t."rintStac1Trace580 7 7 7 To give a concrete e6ample..a"ac. 5+$ xce"tion e8 4 e..-. it will be copied to OW$R.otAs58 method saves the output in a temporar% location. i%"ort or.e na%e of t. new File5file:a%e880 7 catc.io. -our screenshots will appear in the Build Artifacts in build details page .S2AC =screens.etScreens.+$ xce"tion0 i%"ort or./01I'2 !http#33stac"over low.co"yFile58 to cop% this temporar% ile bac" inside m% wor"space.co%%ons.e file we .After0 i%"ort -ava. Step ": Saving the image .e.com3a3+H2++HM3H2$CC! the advice here and used File/tils.co"yFile5screens.7 7 /6ample code in the o icial documentation is a little more terse."n.ot> TestClass>test:a%e. i %our file:a%e is 6screens.6.ot>TestClass>test:a%e..ot. I le t out ever%thing but the essentials.-unit.e i%a. Step #: $rchiving the screenshot as an artifact In Post-build Actions section enable Archive the artifacts and enter the appropriate glob in the te6tbo6 below. I too" .e file try 4 File/tils.io.File/tils0 "ublic abstract class Test 4 @After "ublic void tearDown58 4 == screens.ave ac!uired == file:a%e is t.

Sign up to vote on this title
UsefulNot useful