-1
username = "robertredrain@gmail.com"
password = ""
tomailid = "robertredrain@yahoo.com"
emailsubject = "robertredrain@yahoo.com"
mailbody = "Great! you sent email:-)" + "\n" + "Regards," + "\n" + "Robert"

class send_email(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.baseUrl = "http://mail.google.com/intl/en/mail/help/about.html"

    def tearDown(self):
        self.driver.close();

    def testLoginEmail(self):
        self.driver.get(self.baseUrl)
        self.driver.maximize_window()
        self.driver.find_element_by_id("gmail-sign-in").click()
        self.driver.find_element_by_id("Email").clear()
        self.driver.find_element_by_id("Email").send_keys(username)
        self.driver.find_element_by_id("next").click()
        time.sleep(5)
        self.driver.find_element_by_id("Passwd").clear()
        self.driver.find_element_by_id("Passwd").send_keys(password)
        self.driver.find_element_by_id("signIn").click()

        #Verify login
        if "Gmail" in self.driver.title:
            print("Logged in sucessfully !!!" + self.driver.title)
        else:
            print("Unable to loggin :-( " + self.driver.title)

        time.sleep(5)


    def testComposeEmail(self):
        self.driver.find_element_by_xpath("//div[text()='COMPOSE']").click()
        time.sleep(5)
        self.driver.find_element_by_class_name("vO").send_keys(tomailid)
        self.driver.find_element_by_class_name("aoT").send_keys(emailsubject)

        self.driver.find_element_by_class_name("Am").clear()
        self.driver.find_element_by_class_name("Am").send_keys(mailbody)
        self.driver.find_element_by_xpath("//div[text()='Send']").click()



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

I am running this send gmail test by Python Unittest with Selenium. The process is after running on SetUp, it runs the 1st function testLoginEmail, which can successfully log in to my gmail account. Then I want to continue to run the 2nd function testComposeEmail, which should be run after the 1st function, because it needs to click the “Compose” button. But it cannot be run.

Could someone help revise the codes that how to run the 2nd function? Thanks a lot!

robertredrain
  • 59
  • 1
  • 1
  • 10
  • what error do you have? – midori Jan 25 '16 at 21:00
  • 1
    Usually the requirement to order tests screams bad test design, but provided you know what you are doing, you can either name them in alphabetical order, or use the same feature as [described in this answer](http://stackoverflow.com/questions/4005695/changing-order-of-unit-tests-in-python) – timbre timbre Jan 25 '16 at 22:29

1 Answers1

0

Note: I haven't tried your code, however there may be two issues.

Issue 1: I believe the unittest functions (other than setUp and tearDown) are run in alphabetical order, not necessarily the order they are in. testComposeEmail would run before testLoginEmail. Should be easy enough to test with print statements. This could be fixed with a judicious rename, e.g. test_1_LoginEmail and test_2_ComposeEmail.

HOWEVER

Issue 2: setUp and tearDown run before and after EACH test, not the whole suite of tests, so fixing Issue 1 will probably not help. I gather the tests should be written to be completely independent of each other.

You could combine the two test into just one monolithic test and see if that works.

MTset
  • 90
  • 5