java - 如何使用 Java 从属性文件中选择一组 5 个随机值?

标签 java selenium random selenium-webdriver

我在 C:\Test\ 目录中有一个名为 test.properties 的属性文件。

在 Property 文件中,我有 property IDProperty 值,如下所示:

TestID=Test1,Test2,Test3,Tes4 upto 10

通过使用 Java 代码,我如何从属性文件中选取任意 5 个随机值,然后需要验证这些值在 FE 中可用。

我是 Java 的初学者,请帮我写一些示例代码

我试过的代码如下:

@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);

最佳答案

您读取值并将它们存储在 List<String> 中.然后你使用

打乱列表
Collections.shuffle(list);

然后从列表中取出前 5 个元素。

关于java - 如何使用 Java 从属性文件中选择一组 5 个随机值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22854719/

相关文章:

java - 使用 Saxon 处理 JIRA 模块中的 XPath

javascript - SyntaxError : Unexpected identifier in selenium-webdriver/lib/http. js:454 异步执行(命令)

python - Python 中来自值列表的加权随机数

c++ - 生成随机 64 位整数

c++ - STL::vector 无法分配内存 'randomly'

java - 当我单击打开另一个框架的按钮时,如何将此框架与另一个框架链接?- Java

java - 程序终止时遇到问题

c# - 我怎样才能知道元素是否不可见

java - 手动编写的 XSD 和生成的 java 类会导致使用 JAXB 出现 UnmarshallException

java - Selenium 与 Jbehave 和 Chromedriver 挂起或崩溃