2017-08-09 25 views
0

我正在測試網站live.guru99.com。我正在測試三個功能,命名爲測試A,B和C.我正在使用geckodriver測試代碼,它的exe位於根文件夾中。使用python在硒中錯過的測試

的代碼如下:

from selenium import webdriver 
import time 
import unittest 

class Guru99BankTest(unittest.TestCase): 

    @classmethod 
    def setUp(self): 
     self.driver = webdriver.Firefox() 

    def test_C(self): 
     driver = self.driver 
     driver.get("http://live.guru99.com/") 

     driver.find_element_by_link_text('Mobile').click() 

     listed_prd_el = driver.find_element_by_xpath(
      "//a[contains(text(), 'Sony Xperia')]/ancestor::div[@class = 'product-info']") 
     listed_prd_el.find_element_by_tag_name('button').click() 

     qty = driver.find_element_by_xpath("//input[@title='Qty']") 

     qty.clear() 
     qty.send_keys('1000') 

     self.assertEqual(qty.get_attribute('value'), '1000') 

     driver.find_element_by_xpath("//button[@title='Update']").click() 

     time.sleep(3) 
     self.assertIn('products cannot be ordered in requested quantity', 
         driver.find_element_by_class_name('error-msg').text) 

     driver.find_element_by_xpath("//button[@title='Empty Cart']").click() 

     time.sleep(3) 

     self.assertIn('no items in your shopping cart', 
         driver.find_element_by_class_name('cart-empty').text) 

    def test_A(self): 
     driver = self.driver 

     driver.get("http://live.guru99.com/") 

     self.assertIn("This is demo site for", driver.page_source) 

     driver.find_element_by_link_text('Mobile').click() 

     self.assertEqual("Mobile", driver.title) 

    def test_B(self): 
     driver = self.driver 

     driver.get("http://live.guru99.com/") 

     driver.find_element_by_link_text('Mobile').click() 

     driver.find_element_by_xpath("//select[@title='Sort By']/option[contains(text(), 'Name')]").click() 

     product_names = ([e.text for e in driver.find_elements_by_xpath("//h2[@class='product-name']")]) 

     self.assertEqual(product_names, sorted(product_names)) 

     listed_prd_el = driver.find_element_by_xpath(
      "//a[contains(text(), 'Sony Xperia')]/ancestor::div[@class = 'product-info']") 
     listed_price = listed_prd_el.find_element_by_class_name("price").text 

     listed_prd_el.find_element_by_tag_name('a').click() 

     prod_price = driver.find_element_by_xpath('//span[@class="price"]').text 

     self.assertEqual(listed_price, prod_price) 



    @classmethod 
    def tearDown(self): 
     self.driver.close() 

def custom_suite(): 
    suite = unittest.TestSuite() 
    suite.addTest(Guru99BankTest('test_A')) 
    suite.addTest(Guru99BankTest('test_B')) 
    suite.addTest(Guru99BankTest('test_C')) 
    return suite 

if __name__ == "__main__": 
    runner = unittest.TextTestRunner() 
    runner.run(custom_suite()) 

然而,我發現,瀏覽器打開一次,並且不是三times.What我做錯了什麼?我在單元測試方面很新,所以請耐心等待。

+0

有兩個測試同名test_B,是否有意? – Satish

+0

@michaelsatish我很抱歉,這是一個錯字 –

回答

1

它爲我工作。由於test_b被複制,我將最後一次測試更改爲test_C。但是,如果說我不確定這是否是實際情況

+0

我剛剛在我的控制檯上運行1測試 –

+0

嘗試在chrome中運行它,不確定它會有什麼區別,我會讓chrome驅動程序說謊,所以解​​僱了你的代碼在Chrome中工作。但我不認爲瀏覽器類型和實例決定了流量 – Satish