Voy a explicar, con un ejemplo sencillito, cómo realizar pruebas web.
Además, voy a terminar indicando cómo podemos hacerlo aún más chulo lanzándolo desde atheist.
Instalación
Necesitaremos bajarnos el servidor selenium. También necesitaremos el cliente python (he estado intentándolo con Java y se me ha hecho bastante complejo, debido a las dependencias).
Una vez nos hemos descargado el cliente, lo descomprimimos y ejecutamos:
$ ./setup build
$ su
# ./setup install
Programa de ejemplo
Como uso Debian y aún no han actualizado a Iceweasel 4, puedo utilizar Selenium IDE. Esperemos que ambos se actualicen pronto a Firefox 4.
Con este IDE he generado el siguiente programita de ejemplo (exportándolo a python):
from selenium import selenium
import unittest, time, re
class Untitled(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*chrome", "http://magmax.org/")
self.selenium.start()
def test_untitled(self):
sel = self.selenium
sel.open("/drupal/")
sel.click("link=About")
sel.wait_for_page_to_load("30000")
try: self.failUnless(sel.is_text_present(u"Miguel Ángel"))
except AssertionError, e: self.verificationErrors.append(str(e))
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
A este código le hago unas mínimas modificaciones: le añado una cabecera pythónica, otra para mi emacs y, finalmente y lo más importante, la ejecución de UnitTestCase al final del todo (por si quiero seguir usándolo desde python sin más, mantengo lo que hay):
#!/usr/bin/python
# -*- mode:python; coding:utf-8; tab-width:3 -*-
from selenium import selenium
import unittest, time, re
class Untitled(unittest.TestCase):
def setUp(self):
self.verificationErrors = []
self.selenium = selenium("localhost", 4444, "*chrome", "http://magmax.org/")
self.selenium.start()
def test_untitled(self):
sel = self.selenium
sel.open("/drupal/")
sel.click("link=About")
sel.wait_for_page_to_load("30000")
try: self.failUnless(sel.is_text_present(u"Miguel Ángel"))
except AssertionError, e: self.verificationErrors.append(str(e))
def tearDown(self):
self.selenium.stop()
self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
unittest.main()
else:
UnitTestCase()
Y... ya está.
Resultados
El resultado de la ejecución es el siguiente:
$ python test.py
.
----------------------------------------------------------------------
Ran 1 test in 12.455s
OK
$
Y si lo ejecutamos con Atheist:
$ atheist test.py
[ OK ] TaskCase: ./test.py
[ ALL OK! ] - 11.97s - 1 test - 1 task
$
0 comentarios:
Publicar un comentario en la entrada