1

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.

Narendra Chandratre
2,8347 gold badges31 silver badges60 bronze badges
asked Sep 20, 2016 at 9:59
3
  • Move over to what? What have you tried? What programming language are u using? Commented 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. Commented 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. Commented Sep 20, 2016 at 10:33

4 Answers 4

1

There are three things you should get grasps of:

  1. Learn basic WebDriver programming and running a test: https://www.built.io/blog/getting-started-with-selenium-webdriver
  2. How to test forms with WebDriver: http://www.guru99.com/accessing-forms-in-webdriver.html
  3. 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.

answered Sep 20, 2016 at 10:14
2
  • 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. Commented 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/… Commented Sep 20, 2016 at 11:32
0

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();
}
alecxe
11.4k11 gold badges52 silver badges107 bronze badges
answered Sep 20, 2016 at 12:09
1
  • pleasure, If I could help better Commented Oct 14, 2016 at 11:25
0

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));
}
alecxe
11.4k11 gold badges52 silver badges107 bronze badges
answered Jan 16, 2017 at 16:50
0

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);
alecxe
11.4k11 gold badges52 silver badges107 bronze badges
answered Sep 24, 2017 at 9:06

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.