问题1:单条用例每一条都能执行,但是三条用例时,只执行第二条,其它用例报错:no such session。
问题2:在tearDown(self)下面,if error,保存截图,并没有生效。
ft1.3: Traceback (most recent call last):
File "/Users/mengtingting/PycharmProjects/review/jd/testcase/jd_test.py", line 31, in setUp
self.driver.get('https://www.jd.com')
File "/Users/mengtingting/workspace/python_env/env06/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 333, in get
self.execute(Command.GET, {'url': url})
File "/Users/mengtingting/workspace/python_env/env06/lib/python3.6/site-packages/selenium/webdriver/remote/webdriver.py", line 321, in execute
self.error_handler.check_response(response)
File "/Users/mengtingting/workspace/python_env/env06/lib/python3.6/site-packages/selenium/webdriver/remote/errorhandler.py", line 242, in check_response
raise exception_class(message, screen, stacktrace)
selenium.common.exceptions.WebDriverException: Message: no such session
(Driver info: chromedriver=2.41.578706 (5f725d1b4f0a4acbf5259df887244095596231db),platform=Mac OS X 10.11.6 x86_64)
import unittest
from jd.config.logging_setting import get_logger
from jd.utils.browser_engine import BrowserEngine
from jd.page.login_page import LoginPage
from jd.page.index_search import IndexSearch
from jd.page.left_nav import LeftNav
from jd.config import basic_config
import time
import os
import HTMLTestRunner
import ddt
from jd.utils.excel_util import ExcelUtil
ex = ExcelUtil()
data = ex.get_data()
@ddt.ddt
class JdTest(unittest.TestCase):
@classmethod
def setUpClass(cls) -> None:
cls.driver = BrowserEngine.init_local_driver('chrome')
cls.url = basic_config.START_URL
cls.logger = get_logger()
cls.logger.info('测试开始')
@classmethod
def tearDownClass(cls) -> None:
cls.logger.info('测试结束')
def setUp(self) -> None:
self.logger.info('单个测试开始')
self.driver.get('https://www.jd.com')
def tearDown(self) -> None:
time.sleep(2)
for method_name, error in self._outcome.errors:
if error:
case_name = self._testMethodName
file_path = os.path.join(os.getcwd() + "/report/" + case_name + ".png")
print(file_path)
self.driver.save_screenshot(file_path)
self.driver.close()
@ddt.data(*data)
def test_login(self,data):
linklogin, login, name, password, login_btn, name_value, password_value = data
login_page = LoginPage(self.driver,self.url)
login_page.login_function(linklogin, login, name, password, login_btn, name_value, password_value)
title = self.driver.title
self.assertNotIn('登录',title)
self.logger.info('登录用例执行--pass!')
def test_index_search(self):
index_search = IndexSearch(self.driver,self.url)
index_search.index_search_function()
title = self.driver.title
self.assertIn('笔记本',title)
self.logger.info('首页搜索用例执行--pass!')
def test_left_nav(self):
left_nav = LeftNav(self.driver,self.url)
left_nav.left_nav_function()
if __name__ == '__main__':
report_file = os.path.join(os.path.dirname(os.getcwd()),'report','login_report.html')
with open(report_file,'wb') as f:
suite = unittest.TestLoader().loadTestsFromTestCase(JdTest)
run = HTMLTestRunner.HTMLTestRunner(stream=f,verbosity=0,title='login_report')
run.run(suite)