How to Select random value from a DropDownList using Selenium or UFT – User friendly Tech help

Requirement:- We need to select random value from a given dropdownlist, to test the functionality how our AUT would behave under varying selection of options.

n

n

n

n

n

n

n

n

DropDownlist + HTML code

n

nApproach:-
n1.We would take the count of existing options in the weblist/dropdownlist .
n2.Take a random value between 0(starting index of the dropdownlist, we can avoid in our case as we have –Select– at 0 index) and maximum value -1(total available options in the list)
n3.Select the value based on the random value
n
nSame logic would work whether we are using Selenium or UFT as our testing tool.
n
nSolution:-
n(Selenium)
n1.How to take count of items:-
na.We can use Findelements method to return a list of items and further we would use getSize method of list to fetch the count value.

n

//Creating the List object List  weblist = driver.findElements(By.xpath(".//*[@id='drpdwnTopics']/option")); //Taking the count of items int iCnt = weblist.size();

n

nb.We can use the Select Class and implement its getOptions method to get the count.

n

Select objSel = new Select(driver.findElement(By.xpath(".//*[@id='drpdwnTopics']"))); List  weblist = objSel.getOptions(); //Taking the count of items int iCnt = weblist.size();

n

n2.How to generate random number, we would implement “Random” class and use its nextInt method to output the random number between the given range.
nNote:- nextInt, method would generate number from 0 to upperlimit-1, example nextInt(10), would generate numbers between 0 and 9.
n
Code

n

import java.util.List; import java.util.Random; import java.util.concurrent.TimeUnit; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.firefox.FirefoxDriver; import org.openqa.selenium.support.ui.Select; import org.testng.annotations.*;

n

public class RandomSelectionDrpDwnList { WebDriver driver; @BeforeTest

n

public void launch()

n{n

driver = new FirefoxDriver();

n
ndriver.get(“http://www.ufthelp.com/p/testpage.html”);
ndriver.manage().window().maximize();
ndriver.manage().timeouts().implicitlyWait(20000, TimeUnit.MILLISECONDS);
n}n

@Test
npublic void selectRandom() {

n

//Object of the Dropdownlist

n
nWebElement drpDwnList = driver.findElement(By.xpath(“.//*[@id=’drpdwnTopics’]”));
n//Using FindElements to create a List object
n//List weblist = driver.findElements(By.xpath(“.//*[@id=’drpdwnTopics’]/option”));
n//Using Select Class to fetch the count
nSelect objSel = new Select(drpDwnList);
nList weblist = objSel.getOptions();
n//Taking the count of items
nint iCnt = weblist.size();
n//Using Random class to generate random values
nRandom num = new Random();
nint iSelect = num.nextInt(iCnt);
n//Selecting value from DropDownList
nobjSel.selectByIndex(iSelect);
n//Selected Value
nSystem.out.println(drpDwnList.getAttribute(“value”));
n}n

@AfterTest
npublic void close() {

n

//Closing the browser

n
ndriver.quit();
n}n

}

n


(UFT)
n1.For taking count of items, we would use GetRoProperty in UFT and pass “all items” to fetch the count 
n2. Random number is a Utility object , which generates random number between the given range
n
Code

n

'Random number is a Utility object , which generates random number between the given range Dim obj Set objList = Browser("creationtime:=0").Page("creationtime:=0").webList("html id:=drpdwnTopics")nfnSelectRandom objList

n

‘*****************Fynction***************************************************
n‘Function Name:- fnSelectRandom
n‘Function Description:- Function to Select Random Value from DropDown
n‘Input Parameters:- objList(WebList)
n‘Output Parameters:- None
n‘****************************************************************************
nSub fnSelectRandom(objList)
nDim iCnt,iRandom,sSelValue
n‘Getting the item count of dropdown
niCnt = objList.GetROProperty(“items count”)
n‘Taking the Random range between 0 and items of Dropdown
niRandom = RandomNumber(0,iCnt-1)
n‘Selecting the value
nobjList.Select “#” & iRandom
n‘Taking the Selected Random Value
nPrint “Selected Value = “&objList.Object.value
nEnd Sub

n

nMore Examples
nLearning Java 
nLearn UFT

Was this article helpful?
YesNo

Similar Posts