1. 程式人生 > >python selenium-5根據unittest組織測試用例

python selenium-5根據unittest組織測試用例

  • driver:瀏覽器driver存放地址
  • testcase:測試用例目錄
  • report:測試結果儲存目錄
  • runtest.py:執行檔案
    test_search1.py搜尋selenium,test_search2搜尋jenkins
import unittest,sys
from selenium import webdriver
from time import sleep
class TestBaidu(unittest.TestCase):
    def setUp(self):
        driver_path='/'.join(sys.path[0].split('/')[:-2])+"/driver/chromedriver"
        self.driver=webdriver.Chrome(executable_path=driver_path)
        self.driver.implicitly_wait(30)
        self.base_url="http://www.baidu.com"
    def test(self):
        driver=self.driver
        driver.get(self.base_url+"/")
        driver.find_element_by_xpath("//input[@id='kw' and @class='s_ipt']").clear()  
        driver.find_element_by_xpath("//input[@id='kw' and @class='s_ipt']").send_keys("selenium")
        driver.find_element_by_xpath("//input[@value='百度一下' and @id='su']").click()
        result_text = driver.find_element_by_xpath("//span[@class='nums_text']").text
        assert "百度為您找到相關結果約" in result_text
        sleep(3)
    def tearDown(self):
        self.driver.quit()
if __name__=="__main__":
    unittest.main()

runtest.py

import unittest
test_dir="./testcase/testsearch"
discover=unittest.defaultTestLoader.discover(test_dir,pattern="test*.py")
if __name__=="__main__":
    runner=unittest.TextTestRunner()
    runner.run(discover)

log.txt