背景:どのpy.testアサーションエラーでもメソッドを呼び出す方法は?
pytestと一緒にpytest-seleniumを使用していますが、アサーションに失敗したときに、スクリーンショットを撮りたいと思います。
現在、私は私の基本ページオブジェクトクラスに小さなヘルパーメソッドを定義している:
class PageBase(object):
def __init__(self,driver):
self.driver = driver
self.fake = Factory.create()
def screenshot(self,name):
self.driver.save_screenshot(datetime.now().strftime('%Y-%m-%d %H:%M:%S') + 'scr_'+name+'.png')
@contextmanager
def wait_for_page_load(self, timeout=45):
old_page = self.driver.find_element_by_tag_name('html')
yield
WebDriverWait(self.driver, timeout).until(
EC.staleness_of(old_page)
)
問題は、私はそれ自動化メカニズムの代わりに、「取扱説明書」の使用したいと思いますということです。 (テストクラスの例) :
class TestLogin:
@allure.feature('Ability to login into admin panel')
def test_admin_login(self, prepare, page):
print URLMap.admin('test')
driver = prepare
driver.get(URLMap.admin(page))
login_page = LoginPage(driver)
assert login_page.is_page_correct(),'Login page not loaded correctly'
login_page.fill_login_data('testadmin','testadmin')
login_page.click_login_button()
assert login_page.is_user_logged_in(),'User cannot log in with provided credentials'
login_page.screenshot(page+'_logged_in')
アサーションの失敗ごとに特定の方法を実行するにはどうすればよいですか?
プラグインのために公開されているフック:https://pytest.org/latest/writing_plugins.html – jonrsharpe
必要なものは@AfterMethodです。この方法では、テストが失敗したかどうかを確認し、それに応じてスクリーンショットを作成できます。 – RemcoW