如何在Selenium Web Driver中为文本框输入带连字符的随机6位数字。

huangapple go评论73阅读模式
英文:

How to enter a random 6 digit number with hyphen in between for a text box in selenium web driver

问题

我有一个场景,在文本框中我需要输入像这样的值:234-89。数字需要在每次运行测试时随机生成,“-”应该添加在前3个数字之后,其余的两个数字也应该是随机的。我该如何做?

英文:

I have scenario where in an text box i need to enter value like : 234-89. Numbers need to randomly generated each time i run the test, "-" should be added after 3 numbers and rest two numbers should also be random. How can i do it ?

答案1

得分: 1

生成两个随机数,第一个为三位数,第二个为两位数。然后用连字符将两个变量连接起来。

如果需要帮助生成随机数,可参考此链接:https://www.educative.io/edpresso/how-to-generate-random-numbers-in-java

英文:

Generate two random numbers, the first one 3 digits, the second one 2 digits. Then concatenate both variables with a hyphen in between them.

If you need help generating random numbers, see this link https://www.educative.io/edpresso/how-to-generate-random-numbers-in-java

答案2

得分: 0

有许多选项可以在Java中生成随机数。

> 选项-1

JavaFaker是Java中的伪造数据类。

或者选项2-

import java.util.Random;

Random rand = new Random();

// 生成范围在0到999之间的3位随机整数
int rand1 = rand.nextInt(1000);
// 生成范围在0到99之间的2位随机整数
int rand2 = rand.nextInt(100);
String textfortexbox = Integer.toString(rand1) + "-" + Integer.toString(rand2);

> 输出 999-99

英文:

there are many options to generate random numbers in Java

> Option -1

JavaFaker's fake data classes in java

or Option 2-

import java.util.Random; 


     Random rand = new Random(); 

    // Generate 3digit random integers in range 0 to 999 
    int rand1 = rand.nextInt(1000); 
    // Generate 2digit random integers in range 0 to 99 
    int rand2 = rand.nextInt(100); 
    String textfortexbox=Integer.toString(rand1)+"-"+Integer.toString(rand2);

> Output 999-99

huangapple
  • 本文由 发表于 2020年9月25日 04:57:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/64054298.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定