2016-01-25 87 views
-1
username = "[email protected]" 
password = "" 
tomailid = "[email protected]" 
emailsubject = "[email protected]" 
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() 

我正在使用Selenium運行此Python發送gmail測試。該過程在SetUp上運行後,運行第一個函數testLoginEmail,它可以成功登錄到我的Gmail帳戶。然後我想繼續運行第二個函數testComposeEmail,它應該在第一個函數之後運行,因爲它需要點擊「Compose」按鈕。但它不能運行。如何使用Unittest中的第一個函數運行第二個函數Python

有人可以幫助修改如何運行第二功能的代碼?非常感謝!

+0

你有什麼錯誤? – tinySandy

+1

通常訂購測試的需求尖叫不好的測試設計,但是如果您知道自己在做什麼,可以按字母順序對其進行命名,或者使用與[在此答案中所述]相同的功能(http://stackoverflow.com /問題/ 4005695 /改變階的單元的測試功能於蟒) –

回答

0

注:我沒有試過你的代碼,但可能有兩個問題。

問題1:我相信unittest函數(setUp和tearDown除外)按字母順序運行,不一定是它們的順序。testComposeEmail將在testLoginEmail之前運行。應該很容易用打印語句進行測試。這可以通過明智的重命名來修復,例如test_1_LoginEmail和test_2_ComposeEmail。

無論其

問題2:安裝和拆卸運行前和每個測試,而不是整個套件的測試,因此,解決問題1可能沒有用了。我收集的測試應該寫成完全獨立於對方。

你可以將兩個測試合併成一個單片測試,看看是否有效。

相關問題