Quick test automation using Pexpect and Selenium Martin Jansson

I’ve found a few handy tools for automating tests as well as setting up a test environment in a scripted way very quickly. One is using python as language with pexpect as platform/module. The second one is for testing web pages quickly using Selenium.

Python is a powerful scripting language and in combination with Pexpect it is very powerful in getting many tests quickly. You use pexpect preferably by CLI-like interfaces such as telnet, ftp and so on. It is also very handy to use when setting up test environments and using the pexpect to validate that the configuration took place. You can use regexp when you validate the result of a command, very nice!

Selenium can be run in Mozilla Firefox as a plugin. You do some small tests, preferably a smoke pass just to see that something is up and running. All you do is recorded and can be rerun easily. You can then export the recorded tests to various program languages such as perl, C# or python.

Usually automated testing or test environments takes some time to setup, but if you just want to use it quick and dirty getting some kind of repeatable result fairly quickly, I recommend the two above.

3 Comments
Henrik Emilsson April 18th, 2008

Martin, could you provide some examples on how you use Pexpect?
/Henrik

Martin Jansson April 21st, 2008

Below is an example of two tests. One that tries incorrect password 3 times and the other that makes a correct login. As you can see it is fairly little code that does the trick.

———-

import unittest
import pexpect

class SanityCheck(unittest.TestCase):

def testFailedPassword(self):
child = pexpect.spawn (‘telnet 10.10.0.104 23’)
child.expect (‘Password: ‘)
child.sendline (‘a’)
child.expect (‘Password: ‘)
child.sendline (‘a’)
child.expect (‘Password: ‘)
child.sendline (‘a’)
child.expect (‘Invalid password, access refused’)

def testSuccessfulLogin(self):
child = pexpect.spawn (‘telnet 10.10.0.104 23’)
child.expect (‘Password: ‘)
child.sendline (‘xxxxx’)
child.expect (‘UNIT1>’)
child.sendline (‘exit’)