test_selenium.py 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. # coding:utf-8
  2. import unittest
  3. from flask import current_app, url_for, json
  4. from app import create_app, db
  5. from selenium import webdriver
  6. class SeleniumTestCase(unittest.TestCase):
  7. client = None
  8. @classmethod
  9. def setUpClass(cls):
  10. # 启动firefox
  11. try:
  12. cls.client = webdriver.Firefox()
  13. except:
  14. pass
  15. if cls.client:
  16. cls.app = create_app('testing')
  17. cls.app_context = cls.app.app_context()
  18. cls.app_context.push()
  19. db.create_all()
  20. threading.Thread(target=cls.app.run).start()
  21. @classmethod
  22. def tearDownClass(cls):
  23. if cls.client:
  24. cls.client.get('http://localhost:5000/shutdown')
  25. cls.client.close()
  26. db.drop_all()
  27. db.session.remove()
  28. cls.app_context.pop()
  29. def setUp(self):
  30. if not self.client:
  31. self.skipTest('Firefox is invailable')
  32. def tearDown(self):
  33. pass
  34. def test_home_page(self):
  35. self.client.get('http://localhost:5000/')
  36. self.assertTrue(re.search('Home', self.client.page_source))