How to Select random value from a DropDownList using Selenium or UFT

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.
Application Link:– http://www.ufthelp.com/p/testpage.html

DropDownlist for random selection Selenium
DropDownlist + HTML code


Approach:-
1.We would take the count of existing options in the weblist/dropdownlist .
2.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)
3.Select the value based on the random value

Same logic would work whether we are using Selenium or UFT as our testing tool.

Solution:-
(Selenium)
1.How to take count of items:-
a.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.

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


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

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


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

Code

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.*;


public class RandomSelectionDrpDwnList {
WebDriver driver;
@BeforeTest
public void launch()
{
driver = new FirefoxDriver();
driver.get("http://www.ufthelp.com/p/testpage.html");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(20000, TimeUnit.MILLISECONDS);
}

@Test
public void selectRandom()
{
//Object of the Dropdownlist
WebElement drpDwnList = driver.findElement(By.xpath(".//*[@id='drpdwnTopics']"));
//Using FindElements to create a List object
//List <WebElement> weblist = driver.findElements(By.xpath(".//*[@id='drpdwnTopics']/option"));
//Using Select Class to fetch the count
Select objSel = new Select(drpDwnList);
List <WebElement> weblist = objSel.getOptions();
//Taking the count of items
int iCnt = weblist.size();
//Using Random class to generate random values
Random num = new Random();
int iSelect = num.nextInt(iCnt);
//Selecting value from DropDownList
objSel.selectByIndex(iSelect);
//Selected Value
System.out.println(drpDwnList.getAttribute("value"));
}

@AfterTest
public void close()
{
//Closing the browser
driver.quit();
}

}


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

Code

'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")
fnSelectRandom objList

'*****************Fynction***************************************************
'Function Name:- fnSelectRandom
'Function Description:- Function to Select Random Value from DropDown
'Input Parameters:- objList(WebList)
'Output Parameters:- None
'****************************************************************************
Sub fnSelectRandom(objList)
Dim iCnt,iRandom,sSelValue
'Getting the item count of dropdown
iCnt = objList.GetROProperty("items count")
'Taking the Random range between 0 and items of Dropdown
iRandom = RandomNumber(0,iCnt-1)
'Selecting the value
objList.Select "#" & iRandom
'Taking the Selected Random Value
Print "Selected Value = "&objList.Object.value
End Sub


More Examples
Learning Java 
Learn UFT

ufthelp

8 thoughts on “How to Select random value from a DropDownList using Selenium or UFT

  1. In UFT:- set the RandomNumber function range from 1 rather than 0.
    Selenium/UFT:-
    Simple approach is check that after randomization selected value is not 'select'
    like if(drpdownoption == 'select') than call random function again.

  2. Getting java.lang.NullPointerException
    java.lang.NullPointerException
    at RandomSelectionDrpDwnList.selectRandom(RandomSelectionDrpDwnList.java:53)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
    at java.lang.reflect.Method.invoke(Unknown Source)
    at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:104)
    at org.testng.internal.Invoker.invokeMethod(Invoker.java:645)
    at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:851)
    at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1177)
    at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129)
    at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112)
    at org.testng.TestRunner.privateRun(TestRunner.java:756)
    at org.testng.TestRunner.run(TestRunner.java:610)
    at org.testng.SuiteRunner.runTest(SuiteRunner.java:387)
    at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:382)
    at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
    at org.testng.SuiteRunner.run(SuiteRunner.java:289)
    at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
    at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
    at org.testng.TestNG.runSuitesSequentially(TestNG.java:1293)
    at org.testng.TestNG.runSuitesLocally(TestNG.java:1218)
    at org.testng.TestNG.runSuites(TestNG.java:1133)
    at org.testng.TestNG.run(TestNG.java:1104)
    at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
    at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:236)
    at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:81)

  3. I get why you want to select random options. You want to automate the testing a human conduct. But, as much as in might seem so from a computer's perspective, human's don't typically select random options from a select box. Real users select the option that applied to them. The point of automated testing is not to select for random cases, but to select for ALL cases. But again, kudos, for solving the puzzle.

Leave a Reply

Your email address will not be published. Required fields are marked *