1 / 4

Know-How To Use DataProvider in TestNG – TestNG Parameterization

Know-How To Use DataProvider in TestNG u2013 TestNG Parameterization

Download Presentation

Know-How To Use DataProvider in TestNG – TestNG Parameterization

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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:

  2. 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.

  3. 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();

  4. //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.

More Related