How to pick set of 5 random values from property f

2019-08-07 01:52发布

问题:

I have a Property file called test.properties in C:\Test\ Directory.

In the Property file i have property ID and Property values as follows:

TestID=Test1,Test2,Test3,Tes4 upto 10

By using Java code how can i pick any 5 random values from the property file and then need to verify the those values are available in the FE.

I'm beginner of Java please help me with some sample code

The code which i have tried as follows:

@Test()
public void test() throws Exception{

Properties APPTEXT = new Properties();
Log.info("Getting Input from the Property file");
FileInputStream fs = new FileInputStream("C:\\FilterSection\\dropdown.properties");
APPTEXT.load(fs);
System.out.println ("" +APPTEXT);
Log.info("1. Verifying the Test drop down values are matching in both property file and UI");
String[] expectedDropDownItemsInArray = APPTEXT.getProperty("TestId").trim().split(",");
Random r = new Random();
//System.out.println(expectedDropDownItemsInArray.length);

 ArrayList<String> expectedDropDownItems = new ArrayList<String>();
for(int i=0; i<expectedDropDownItemsInArray.length; i++)
    expectedDropDownItems.add(expectedDropDownItemsInArray[i]);

System.out.println(expectedDropDownItems+"" );


Thread.sleep(6000);

回答1:

You read the values and store them in a List<String>. Then you shuffle the list using

Collections.shuffle(list);

Then you take the 5 first elements from the list.



回答2:

USE RANDOM FUNCTION Random rand = new Random();

int idx = rand.nextInt(Array.length); String random = (Array[idx]);