请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

老师,请教2个问题

问题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)

正在回答 回答被采纳积分+3

2回答

Mushishi 2019-07-08 22:12:34

第二,当你有错误的时候把self._outcome.errors:这个打印出来,和没有错误对比,这就发现问题了

0 回复 有任何疑惑可以回复我~
Mushishi 2019-07-08 22:10:50

一,没有session,说明你的driver没有初始化,或者说你case执行完把driver关闭了,把你dricer放在setuoclass里面。

0 回复 有任何疑惑可以回复我~
  • 提问者 sunshine_mtt #1
    driver是放在setup(cls)下面,每次关闭driver也是在teardown(self)下面
    回复 有任何疑惑可以回复我~ 2019-07-08 22:13:49
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信