There are two simple ways to generate a value:
1. Get the system date time:
// Get the current date and time from system.
DateFormat dateFormat = new SimpleDateFormat(“yyyyMMddHHmmss”);
Calendar cal = Calendar.getInstance();
String GetDateTime = dateFormat.format(cal.getTime());
driver.findElement(By.id(“source”)).sendKeys(GetDateTime);
The Result: 20160528010203
2. Get a random number:
Random rd = new Random();
for (int idx = 1000; idx <= 100000; ++idx) {
randomInt = rd.nextInt(100000);
}
driver.findElement(By.id(“source”)).sendKeys(randomInt);
The Result: 95018
Example code in real:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
package basic_junit; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Random; import java.util.concurrent.TimeUnit; import org.junit.After; import org.junit.Before; import org.junit.Test; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; public class random_number { private WebDriver driver; private String baseUrl; private int randomInt; @Before public void setUp() throws Exception { driver = new FirefoxDriver(); // Open Firefox driver.manage().window().maximize(); // Maximize browser windows baseUrl = "https://translate.google.com"; // URL of testing site driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); // Wait } @Test public void testGoogleTrans() throws Exception { driver.get(baseUrl + "/#en/vi/"); driver.findElement(By.id("source")).clear(); // Get the current date and time from system. DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); Calendar cal = Calendar.getInstance(); String GetDateTime = dateFormat.format(cal.getTime()); driver.findElement(By.id("source")).clear(); driver.findElement(By.id("source")).sendKeys("I love this number: " + GetDateTime); // Wait 6s and input the new ramdom number. Thread.sleep(6000); Random rd = new Random(); for (int idx = 1000; idx <= 100000; ++idx) { randomInt = rd.nextInt(100000); } driver.findElement(By.id("source")).clear(); driver.findElement(By.id("source")).sendKeys("...And this number: " + randomInt); Thread.sleep(6000); } @After public void tearDown() throws Exception { //driver.quit(); } } |
Recent Comments