john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

selenium ide firefox plugin python unittest

Selenium (webclient) Test

Used the Selenium Firefox Plugin http://docs.seleniumhq.org/projects/ide/  in order to generate a quick login "test" (http://docs.seleniumhq.org/docs/02_selenium_ide.jsp#introduction)


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head profile="http://selenium-ide.openqa.org/profiles/test-case">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<link rel="selenium.base" href="https://mysite.com/" />
<title>selenium-login</title>
</head>
<body>
<table cellpadding="1" cellspacing="1" border="1">
<thead>
<tr><td rowspan="1" colspan="3">selenium-login</td></tr>
</thead><tbody>
<tr>
        <td>open</td>
        <td>/</td>
        <td></td>
</tr>
<tr>
        <td>type</td>
        <td>id=email</td>
        <td>admin@mysite.com</td>
</tr>
<tr>
        <td>type</td>
        <td>id=password</td>
        <td>examplepassword</td>
</tr>
<tr>
        <td>clickAndWait</td>
        <td>id=signin</td>
        <td></td>
</tr>
<tr>
        <td>assertLocation</td>
        <td>https://mysite.com/home</td>
        <td></td>
</tr>
<tr>
        <td>assertElementPresent</td>
        <td>link=Launch the web app</td>
        <td></td>
</tr>
</tbody></table>
</body>
</html>

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
The IDE helpfully not only can save a Test but can File -> Export Test Case As -> python / unittest / webdriver


from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
class SeleniumLogin(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "https://mysite.com/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_selenium_login(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("email").clear()
        driver.find_element_by_id("email").send_keys("admin@mysite.com")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("examplepassword")
        driver.find_element_by_id("signin").click()
        self.assertEqual("https://mysite.com/home", driver.current_url)
        self.assertTrue(self.is_element_present(By.LINK_TEXT, "Launch the web app"))

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def is_alert_present(self):
        try: self.driver.switch_to_alert()
        except NoAlertPresentException, e: return False
        return True

    def close_alert_and_get_its_text(self):
        try:
            alert = self.driver.switch_to_alert()
            alert_text = alert.text
            if self.accept_next_alert:
                alert.accept()
            else:
                alert.dismiss()
            return alert_text
        finally: self.accept_next_alert = True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)
if __name__ == "__main__":
    unittest.main()



python selenium-login.py runs the tests (opens Firefox, executes all of the commands)

  • « Amazon s3 bucket permissions policy ip address example
  • Opscode knife aws ec2 »

Published

Sep 17, 2013

Category

python

~229 words

Tags

  • firefox 6
  • ide 4
  • plugin 3
  • python 180
  • selenium 5
  • unittest 12