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.

  • Move over to what? What have you tried? What programming language are u using? – Niels van Reijmersdal Sep 20 '16 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 Sep 20 '16 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 Sep 20 '16 at 10:33
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.

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();
}
  • pleasure, If I could help better – NarendraC Oct 14 '16 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));
}
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);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.