2

I've recently started using windmill and python to run automated tests of my web application. This is the python script that windmill auto-generated from recording my events:

# Generated by the windmill services transformer
from windmill.authoring import WindmillTestClient
import string
import random

    def test_recordingSuite0():
        client = WindmillTestClient(__name__)

        client.click(id=u'input-999052296848829736')
        client.type(text=u'btsr65ejdfgdjdfg', id=u'input-999052296848829736')
        client.click(id=u'input-999052296848829736-1')
        client.type(text=u'dfgdbdfgdfgjdfgjd', id=u'input-999052296848829736-1')
        client.click(name=u'_u911175390904082714')
        client.select(option=u'1', name=u'_u911175390904082714')
        client.click(value=u'1')
        client.click(id=u'input-497945674625883994')
        client.type(text=u'dfgbhdfbgxcvbz3@asdfvsevsdf54.com', id=u'input-497945674625883994')
        client.click(name=u'_u969737303932735624')
        client.radio(name=u'_u969737303932735624')
        client.type(text=u'asdg9a7e0g57wn4bgwsdfhsdfhsdfhssdhsd', id=u'input-542327653202413691')
        #client.click(name=u'submit')
        #client.waits.forPageLoad(timeout=u'20000')

I'm totally new to python and I'm working on learning some of the syntax right now. But can someone help me make the input-text random in the various fields?

For example: line 2: On one test I would like

client.type(text=u'LAKJSDOGUSDGSDGS', id=u'input-999052296848829736')

and on another:

client.type(text=u'908374098afsDGSGS', id=u'input-999052296848829736')

(random, different)

Thanks!

Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
Casey Flynn
  • 13,654
  • 23
  • 103
  • 194
  • If you do this, how can you be sure that the data your web application is seeing actually matches the random data generated for the test run? Why do you care about having random data, anyway? – Karl Knechtel Apr 07 '12 at 09:32

2 Answers2

6

At the top of your program, you import the necessary modules and you get the list of characters that you want to put in your random strings:

import string
import random

CANDIDATE_CHARS = string.ascii_letters+string.digits  # lowercase and uppercase letters, and digits

In the test function, you create a random string of alphanumeric characters, like so:

random_text = u''.join(random.choice(CANDIDATE_CHARS) for _ in range(16))  # 16 random characters
client.type(text=random_text, id=u'input-999052296848829736')
Eric O. Lebigot
  • 91,433
  • 48
  • 218
  • 260
-1

You are looking for the random package. It has a shuffle method which shuffles a list in-place.

import string, random

def get_random_string(length):
    chars = list(string.lowercase+string.digits)
    random.shuffle(chars)

    return "".join(chars[:length])

for i in range(12):
    print get_random_string(10)

The string module provides some convenient strings, which are string.uppercase, string.lowercase, string.digits, ... You can use these for convenience or write your own list of characters. "".join(L) will separate all the letters of the list L by nothing, so you get the sequence of characters in one string.

In your case, you could use:

client.type(text=unicode(get_random_string(20)), id=u'input-999052296848829736')
jadkik94
  • 7,000
  • 2
  • 30
  • 39
  • This does not work: the question is not about shuffling characters, but about getting a string of random characters. This solution cannot generate, say, a string of 1000 random characters. – Eric O. Lebigot Apr 07 '12 at 09:33
  • If he needs that much characters, he can change the `chars` variable to whatever suits him `chars*200` and shuffle it. But he won't need that in **his** case. – jadkik94 Apr 07 '12 at 09:40
  • 1
    @jadkik94 your function is wrong because a given character can only appear in the string once. While the string is still "random", this solution is not accurate. – Nolen Royalty Apr 07 '12 at 10:19
  • @NolenRoyalty True, but by changing the list of characters (like `chars*n`) it will be "random enough" for the use case... but you are still right – jadkik94 Apr 07 '12 at 10:39
  • @jadkik94 I think EOL is being a bit too harsh when he says that it "does not work" but especially given his more elegant and more random solution, I think it's fair to say that this isn't the solution that the OP wants. Especially with how expensive creating a string of that size would get. – Nolen Royalty Apr 07 '12 at 10:44
  • @NolenRoyalty: Alright, this solution kind of works: I was a little harsh. :) It's just that the `length` parameter is not the length of the resulting string, in the general case (for large enough lengths), and that, as you pointed out, characters are not independent of each other. – Eric O. Lebigot Apr 07 '12 at 10:48
  • @EOL it works for some value of "works". That doesn't mean that it is correct :p. I'm just grateful that the two of you introduced me to string.ascii_letters and string.digits. Not sure how I went this long without knowing about them. – Nolen Royalty Apr 07 '12 at 10:54
  • @EOL Let the OP decide, he would have learned two methods, especially that he is new to Python. – jadkik94 Apr 07 '12 at 11:04