charliefortune
charliefortune

Reputation: 3188

How do I write a Python Selenium test script that contains more than one test case?

I am working on a Selenium Webdriver script in Python which only partially does what I want it to. I want it to run through a set of test cases, each in its own method in the class. So in the case of my script here, I want it to test the discount form (test_add_discount) then test the add unit form (test_add_unit_type).

Each time I run it, all I get is the first one, then it closes with the message;

Ran 1 test in 12.948s

If I run it verbose with -v parameter, I still don't see any reference to the second test case at all.

Here is my script;

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 AdminTestCase(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(30)
        self.base_url = "http://mysite.local"

    def test_discount_test_case(self):
        driver = self.driver
        driver.get(self.base_url + "/admin/login")
        driver.find_element_by_id("username").clear()
        driver.find_element_by_id("username").send_keys("admin")
        driver.find_element_by_id("password").clear()
        driver.find_element_by_id("password").send_keys("p@ssw0rd")
        driver.find_element_by_xpath("//button[@type='submit']").click()
        driver.find_element_by_xpath("//li[4]/a/span").click()
        driver.find_element_by_link_text("Add Discount").click()
        driver.find_element_by_name("title").clear()
        driver.find_element_by_name("title").send_keys("Selenium Test Discount")
        driver.find_element_by_name("body").clear()
        driver.find_element_by_name("body").send_keys("Test discount text")
        driver.find_element_by_name("start_date").clear()
        driver.find_element_by_name("start_date").send_keys("01/01/2014")
        driver.find_element_by_name("end_date").clear()
        driver.find_element_by_name("end_date").send_keys("01/03/2014")
        driver.find_element_by_name("discount_percentage").clear()
        driver.find_element_by_name("discount_percentage").send_keys("33")
        driver.find_element_by_xpath("//button[@type='submit']").click()

    def test_add_unit_type(self):
        driver = self.driver
        driver.get(self.base_url + "/maxsys/unit_types")
        driver.find_element_by_link_text("Add Unit type").click()
        driver.find_element_by_name("title").clear()
        driver.find_element_by_name("title").send_keys("Selenium Test Unit Type")
        driver.find_element_by_name("height").clear()
        driver.find_element_by_name("height").send_keys("22.5")
        driver.find_element_by_name("width").clear()
        driver.find_element_by_name("width").send_keys("Non-numeric")
        driver.find_element_by_name("depth").clear()
        driver.find_element_by_name("depth").send_keys("Test discount text")
        driver.find_element_by_name("body").clear()
        driver.find_element_by_name("body").send_keys("unit type description")
        driver.find_element_by_xpath("//button[@type='submit']").click()


    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

if __name__ == "__main__":
    unittest.main()

Upvotes: 2

Views: 1556

Answers (1)

charliefortune
charliefortune

Reputation: 3188

Ok, it turns out my code is fine. The problem was Python's indenting rules. The indents in the second test case were tabs not spaces. I have now set my editor to replace tab characters with 4 spaces and it all runs as expected.

Pretty infuriating and worse than that, invisible to the human eye. However, when I type swear words into the internet about Python's indenting, I am told that I will learn to love it eventually, so I am trying to keep an open mind.

To catch this, I have just learned about the -t parameter when invoking a python script from command line, which will give warnings about mixed space and tab characters in Python 2.

Using -tt will escalate them from warnings to errors.

Upvotes: 2

Related Questions