失敗したテストの最後にスクリーンショットを撮ろうとしています
私はうまくいくことをしましたが、問題があります:失敗したテストの後に発生するすべてのテストのスクリーンショットを取得します-例:1.テストに合格:スクリーンショットなし2.テストに失敗:スクリーンショット3.テストに合格:スクリーンショット-したがって最初のエラーの後、すべてのテストでスクリーンショットが取得されます
私はそれが _resultForDoCleanups メソッドの問題であることを知っています.私の場合は _outcomeForDoCleanups.success に置き換えることはできません.3ではなくpython 2.7を使用しているため.
私のコード:
def tearDown(self):
if self._test_has_failed():
if not os.path.exists(SCREEN_DUMP_LOCATION):
os.makedirs(SCREEN_DUMP_LOCATION)
for ix, handle in enumerate(self.driver.window_handles):
self._windowid = ix
self.driver.switch_to.window(handle)
self.take_screenshot()
self.driver.quit()
def _test_has_failed(self):
for method, error in self._resultForDoCleanups.errors:
if error:
return True
return False
def _get_filename(self):
timestamp = datetime.now().isoformat().replace(':', '.')[:19]
return "{folder}/{classname}.{method}-window{windowid}-{timestamp}".format(
folder=SCREEN_DUMP_LOCATION,
classname=self.__class__.__name__,
method=self._testMethodName,
windowid=self._windowid,
timestamp=timestamp
)
def take_screenshot(self):
filename = self._get_filename() + ".png"
print "\n{method} SCREENSHOT AND HTML:\n".format(
method=self._testMethodName)
print 'screenshot:', filename
self.driver.get_screenshot_as_file(filename)