Python newbie generate random strings

1.9k Views Asked by At

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'[email protected]', 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!

2

There are 2 best solutions below

0
Eric O. Lebigot On BEST ANSWER

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')
8
jadkik94 On

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')