40 likes | 40 Views
Know-How To Use DataProvider in TestNG u2013 TestNG Parameterization
E N D
Know-HowToUseDataProviderinTestNG– TestNGParameterization AutomationTestinghasevolvedasafixtureinthesoftwaredevelopmentprocess.Noonewantstodedicatehoursofhumaneffortandtimewhentheycanobtainaccurateresultswithsuitabletestscripts,tools,andframeworks. Thisiswhyitisessentialtolearnthedifferentcharacteristicsofautomatedtesting.TestNGcomesupwithDataProvidertoautomateprovidingtest-casesforimplementation. DataProvideraidswithdata-driventestcasesthatholdthesameprocessesbutcanberunmultipletimeswithdistinctdatasets.Italsohelpsinequippingcomplicatedparameterstothetestmethods. WhatisaData-DrivenFramework? Adata-drivenframeworkcachesthetestdatainatableorspreadsheetformat.Itpermitsautomationengineerstouseasingletestscriptforaccomplishingalltestdatapresentinthetable.Inthedata-drivenframework,inputvaluesarereadfromdatafilesandheldintoatestscriptvariable.Data-Driventestingallowsthecreationofbothpositiveandnegativetestcasesintoasingletest. Inthistestautomationnetwork,inputdatacanbestoredinasingleormultipledatasourceslikeXML,CSV,XLS,anddatabases. TestNGParameterization Parameterizationisanimplementationstrategythatrunsatestcaseautomatically,multipletimes,withvariousinputvalues.Thetestdesigncomeswithreadingdatafromafileordatabaseinsteadofthehard-codedvalues. InTestNG,therearetwomethodstoaccomplishparameterization:
WiththeaidofParametersannotationandTestNGXMLfile • @Parameters({“name,” “searchkey”}) • WiththeaidofDataProviderannotation • @DataProvider(name=“searchProvider”) • DataProviderinTestNG • First,toprovidethetestdata,declareamethodthatreturnsthedatasetintheformofatwo-dimensionalobjectarrayObject[][]. • Thefirstarraydefinesadataset,whereasthesecondarrayincludestheparametervalues.TheDataProvidermethodcanbeintheidenticaltestclassorsuperclasses.Itisalso • feasibletoequipaDataProviderinanothertype,butthemethodsearchkeyisfixed. Afteraddingthismethod,interpretitusing@DataProvidertoletTestNGknowitisaDataProvidermethod.Additionally,feedanameusingthenameattributeoftheDataProviderannotation.Ifonehasn’tprovidedthetitle,thenameoftheprocesswillbeusedforreferencebydefault. ParameterizationusingDataProvider Documentingthousandsofwebformsusingthetestingframeworkistiresome.Tolessenthisprocess,oneneedsadistinctprocessforconductinglargedatasetsinasingleimplementationflow.Thisdata-drivenconceptisaccomplishedthrough@DataProvider annotationintheTestNGframework.
Ithasonlyasingleattribute,‘name.’ Ifonedoesnotdefinethenameattribute,theDataProvider’snamewillbeidenticaltothecorrespondingprocessname.ThisishowDataProviderfacilitatesthetaskoftestingmultiplesetsofdata. Letustakeanexampletoexplainhowitworks. Inthisexample,thetesterdesirestoautomateenteringusernameandpasswordandlogintotwitter.com.Therefore,thistestcaseshouldruntwotimeswithdistinctsetsofdata(thedataprovidedinthe2Darray). Let’sexecutethesameandseehowitfunctions. importorg.openqa.selenium.By; importorg.openqa.selenium.WebDriver; importorg.openqa.selenium.Chrome.ChromeDriver;importorg.testng.annotations.DataProvider; importorg.testng.annotations.Test;publicclassDataProviderTest{ //thiswilltakedatafromdataproviderwhichwecreated @Test(dataProvider="testdata") publicvoidTestChrome(Stringuname,Stringpassword){System.setProperty("webdriver.chrome.driver","Pathofthedriver");WebDriverdriver=newChromeDriver(); //Maximizebrowserdriver.manage().window().maximize(); //Loadapplicationdriver.get("https://twitter.com/login"); //clearemailfield driver.findElement(By.name("session[username_or_email]")).clear(); //Enterusernamedriver.findElement(By.name("session[username_or_email]")).sendKeys(uname); //Clearpasswordfield driver.findElement(By.name("session[password]")).clear();
//Enterpassworddriver.findElement(By.name("session[password]")).sendKeys(password);//Enterpassworddriver.findElement(By.name("session[password]")).sendKeys(password); • } • @DataProvider(name="testdata")publicObject[][]TestDataFeed(){ • //Createobjectarraywithtworowsandtwocolumns-thefirstparameterisrowandsecondis//column • Object[][]twitterdata=newObject[2][2]; • //Enterdatatorow0column0twitterdata[0][0]="username1@gmail.com"; • //Enterdatatorow0column1 • twitterdata[0][1]="1password"; • //Enterdatatorow1column0twitterdata[1][0]="username2@gmail.com"; • //Enterdatatorow1column0twitterdata[1][1]="Password2"; • //returnarrayobjecttotestscriptreturntwitterdata; • } • } • ThedriverwilllaunchChromeonexecutingthisprogram,navigatetotheTwitterhomepage,andenterthedefinedusernameandpasswordcombinations. • DataProvidercanalsoenterdatacombinationsacrossmultiplewebsitessimultaneously. • Conclusion- • RunningSeleniumtestsemployingDataProviderandTestNGisidealforspeedinguptestcycles,demonstratingmorecomprehensiveautomatedtestingofwebsites,andmakingoutstandinguserexperienceswithminimaltime,effort,andresources.Therefore,itshouldfeatureintestingpipelines,makingtesters’ livesinfinitelymoreaccessible.But,ofcourse,thesetestsarealwaysbesttorunonrealbrowsersanddevices.