I am currently trying to write a test that will generate random users and businesses. This will be generated into text fields and used for a "sign up form"
I'm still relatively new to Webdriver, I was using IDE, but I've had to move over as I was limited by what I could do there.
any help with this would be greatly appreciated
Q : How to generate random characters as a string in text boxes using Java?
Thanks!
This is what I was using in IDE, however, I have not been able to get it to work with webdrrver, or even if it would.
Selenium.prototype.doRandomString = function( options, varName ) {
var length = 8;
var type = 'alphanumeric';
var o = options.split( '|' );
for ( var i = 0 ; i < 2 ; i ++ ) {
if ( o[i] && o[i].match( /^\d+$/ ) )
length = o[i];
if ( o[i] && o[i].match( /^(?:alpha)?(?:numeric)?$/ ) )
type = o[i];
}
switch( type ) {
case 'alpha' : storedVars[ varName ] = randomAlpha( length ); break;
case 'numeric' : storedVars[ varName ] = randomNumeric( length ); break;
case 'alphanumeric' : storedVars[ varName ] = randomAlphaNumeric( length ); break;
default : storedVars[ varName ] = randomAlphaNumeric( length );
};
};
function randomNumeric ( length ) {
return generateRandomString( length, '0123456789'.split( '' ) );
}
function randomAlpha ( length ) {
var alpha = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split ( '' );
return generateRandomString( length, alpha );
}
function randomAlphaNumeric ( length ) {
var alphanumeric = '01234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'.split( '' );
return generateRandomString( length, alphanumeric );
}
function generateRandomString( length, chars ) {
var string = '';
for ( var i = 0 ; i < length ; i++ )
string += chars[ Math.floor( Math.random() * chars.length ) ];
return string;
}
at the moment I am using this to input text to the form textboxes,
driver.findElement(By.id ("cphMainContent_cphMainContent_ContentControl_ctl00_content_core_administration_organisation_tabs_profile_ascx_tbVWId")).clear();
driver.findElement(By.id ("cphMainContent_cphMainContent_ContentControl_ctl00_content_core_administration_organisation_tabs_profile_ascx_tbVWId")).sendKeys("test");
however, I do need the text to be randomised. which is where I am getting stuck.
-
Move over to what? What have you tried? What programming language are u using?Niels van Reijmersdal– Niels van Reijmersdal2016年09月20日 10:02:46 +00:00Commented Sep 20, 2016 at 10:02
-
I think you are mixing programming languages, because you said you where using Java, but the .prototype suggests you are using JavaScript. Do you have developers close by? Maybe ask them to give you a head start.Niels van Reijmersdal– Niels van Reijmersdal2016年09月20日 10:29:15 +00:00Commented Sep 20, 2016 at 10:29
-
Sadly our devs are in another country. and getting in contact with them is rather difficult. That was what I was using on Selenium IDE, not on webdriver. as I say, I was unsure where to start with it on webdriver. I am currently reading through what you sent to me.JPrice– JPrice2016年09月20日 10:33:17 +00:00Commented Sep 20, 2016 at 10:33
4 Answers 4
There are three things you should get grasps of:
- Learn basic WebDriver programming and running a test: https://www.built.io/blog/getting-started-with-selenium-webdriver
- How to test forms with WebDriver: http://www.guru99.com/accessing-forms-in-webdriver.html
- Generating random data in the programming language of your choice: https://dzone.com/articles/generate-random-alpha-numeric (Java Example)
Google examples and try to get it working, with this knowledge you might have more clear questions we can answer for you.
-
I had, had a look, but I was finding it difficult to find what I needed. however I will not read through these, and hoipefully wil be able to inform better what is needed. or hopefully it will solve the issues I am having, cheers.JPrice– JPrice2016年09月20日 10:16:50 +00:00Commented Sep 20, 2016 at 10:16
-
Maybe you learn better from books, if so have a look at this: amazon.com/Selenium-WebDriver-Practical-Guide-Avasarala/dp/…Niels van Reijmersdal– Niels van Reijmersdal2016年09月20日 11:32:55 +00:00Commented Sep 20, 2016 at 11:32
Faced same before few days. After search I got below solution:
private static final String CHAR_LIST = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
private static final int RANDOM_STRING_LENGTH = 10;
//This method generates random string
public String generateRandomString(){
StringBuffer randStr = new StringBuffer();
for(int i=0; i<RANDOM_STRING_LENGTH; i++){
int number = getRandomNumber();
char ch = CHAR_LIST.charAt(number);
randStr.append(ch);
}
return randStr.toString();
}
-
pleasure, If I could help betterNarendra Chandratre– Narendra Chandratre2016年10月14日 11:25:51 +00:00Commented Oct 14, 2016 at 11:25
Here is a much easiest way to use the random generator:
//for letters
public String generateRandomString(int length){
return RandomStringUtils.randomAlphabetic(length);
}
//for numbers
public String generateRandomNumber(int length){
return RandomStringUtils.randomNumeric(length);
}
Then just use:
public void YourUniqueName(){
driver.findElement(locator).sendKeys("Name" +" - "+"Test" + " - " + RandomStringUtils.randomAlphabetic(3) + RandomStringUtils.randomNumeric(3));
}
I think you could try this way:
private int randomInt;
Random rd = new Random();
for (int idx = 1000; idx <= 100000; ++idx) {
randomInt = rd.nextInt(100000);
}
driver.findElement(By.id("source")).sendKeys(randomInt);