0

I have the following HTML:

<input name="txtVFNAME" tabIndex="2" id="txtVFNAME" style="width: 150px;" onkeydown="CallRestricted("vfname")" type="text" maxLength="35"/>

I want to enter an increasing digit at the end of the Name that I set. e.g. I want to enter first name as "Christina1", 2nd time when my code runs,I want it to enter first name as Christina2 and so on.If not in an increasing manner then it can append any random digit at the end of the name.

Kindly help me. I am using Java , Selenium , IE 10, Windows 8.

Sully
  • 14,672
  • 5
  • 54
  • 79
newLearner
  • 184
  • 2
  • 15
  • possible duplicate http://stackoverflow.com/questions/363681/generating-random-numbers-in-a-range-with-java – lordkain Nov 20 '13 at 07:50
  • You want to write java code to extract this HTML name field, add a random/increasing number to it ?? – TheLostMind Nov 20 '13 at 07:55
  • lordkain : My question is a bit different as i am unable to understand the details mentioned in your given link. @X86: I am using selenium, I need java code to enter a different username everytime my code runs as my web app does not allow the same name to be entered again. Kindly help – newLearner Nov 20 '13 at 08:00

3 Answers3

1

Why cant you just do.

   static int num=0;
   String name="Peter";//  give your own name or read it from HTML doc
   String someFunc()
    {
    num=num+1; 
    return name+num;   // use this String wherever you want
    }

You want to get a different name each time the code/function runs (Not the entire program.. ) correct??

TheLostMind
  • 35,966
  • 12
  • 68
  • 104
0

If you want it in java code than for multithreading environment :

class Xyz{
private final AtomicLong atomicLong = new AtomicLong(0L);
public long getNext(){    
    long current=0L, next=0L;
    do{
        current= atomicLong.get();
        next=current+1;
    }while(!atomicLong.compareAndSet(current, next));
   return next;
}
}

This is give you increasing number.

Trying
  • 14,004
  • 9
  • 70
  • 110
  • Thank you for the above code but I am really new to Selenium. Just started a couple of days back. Can you please be very precise? Write a code that i can directly paste in my current code. Right now I am using the following code ` idriver.findElement(By.id("txtVFNAME")).sendKeys(new String[] {"Franddfiena"}); // Sets the First Name` – newLearner Nov 20 '13 at 08:07
  • @Haseeb this is a generic java code, i don't have any experience of selenium. – Trying Nov 20 '13 at 08:26
0

You can use a class in java called as RandomStringUtils. You have a method randomNumeric(count). Provide the count argument which generates numbers based on the count. If you give 3 then it will generate any three random numbers. You can give a bigger number as the max length of the string is 35. Also this method generates a string number meaning you can simply append the generated number to the existing name using + operator. Hope this helps. Cheers.

Vinay
  • 648
  • 4
  • 12