Selenium Pythonでスクリーンショットをキャプチャする方法はわかりましたが、キャプチャするタイミングはいつですか? コードに入れる場所。 私のシナリオ:My SeleniumスクリプトがWebページ上の要素(ボタン)をクリックし、エラーダイアログが表示されました。エラーダイアログのために私のテストメソッドを続行できませんでした。 スクリプトは、次のテストメソッドに移動します。これは問題ありません。Selenium Pythonエラーのスクリーンショットをキャプチャするときに、Webページがエラーをスローする要素
要素をクリックしたときにエラーがスローされた場合、スクリーンショットをキャプチャしたいと思います。エラーダイアログが発生する可能性がクリックイベントでは、404エラーページが表示される可能性
など
現在、私は要素を検索し、要素が見つからない場合は、スクリーンショットをキャプチャするカスタムメソッドを持っています。
マイfind_element方法は次のとおりです。
# returns the element if found
def get_element(self, how, what):
# params how: By locator type
# params what: locator value
try:
element = self.driver.find_element(by=how, value=what)
except NoSuchElementException, e:
print what
print "Element not found "
print e
screenshot_name = how + what + get_datetime_now() # create screenshot name of the name of the element + locator + todays date time. This way the screenshot name will be unique and be able to save
self.save_screenshot(screenshot_name)
raise
return element
def save_screenshot(self, name):
self.driver.save_screenshot(os.path.join(Globals.screenshots_folder, name + '.png'))
return self
エラーページやエラーダイアログが要素をクリックすることで表示された場合は、スクリーンショットをキャプチャする方法は?
ドライバーが終了したときに、引き裂き方法でスクリーンショットをキャプチャする必要がありますか? 破損中にエラーが発生した場合、どのようにキャプチャスクリーンショットを挿入するのか?
おかげで、 リアズ
私の解決策: 私は私の同僚に、アンドリュー・リーガンからのコメント答えを示し、彼はテストケースごとにテストメソッドの周りにtry/catchブロックを置くことを提案しました。 私は次のようにしました。試験法の下に私のコードスニペット:
def test_00001_add_all_records_report_CRM(self):
try:
print "*** Test add_all_records_report - CRM ***"
self.login_page.userLogin_valid(Globals.login_username, Globals.login_password)
tool_bar = ToolbarPage(self.driver)
tool_bar.select_project_from_drop_down2("Analysis")
menu_bar = MenuBarPage(self.driver)
reporting_page = menu_bar.select_menuBar_item("Reporting")
project_navigator = ProjectNavigatorPage(self.driver)
reports_page = project_navigator.select_projectNavigator_item("Reports")
reports_page.click_add_button_for_reports()
reports_manual_tab = reports_page.click_manual_tab()
reports_manual_tab.enter_name_and_description_for_the_report_from_details_section("all_records", "This is an all records report")
reports_manual_tab.click_type_dropdown_from_details_section("USN entities")
reports_manual_tab.click_USN_category_link()
reports_manual_tab.click_an_entity_from_usn_category_drop_down("All entities")
reports_manual_tab.click_datasets_link()
reports_manual_tab.uncheck_all_datasets_checkboxes() # untick all datasets checkboxes so we can start from a clean slate
reports_manual_tab.click_a_dataset_checkbox_from_datasets_section("CRM")
reports_manual_tab.click_variables_link()
reports_manual_tab.click_all_variables_checkboxes_from_variables_section()
reports_manual_tab.click_fields_link()
reports_manual_tab.click_a_field_checkbox_from_fields_section("SOURCE_FIELDS", "Name")
reports_manual_tab.click_a_field_checkbox_from_fields_section("SOURCE_FIELDS", "Address")
reports_manual_tab.click_a_field_checkbox_from_fields_section("SOURCE_FIELDS", "DOB")
reports_manual_tab.click_right_arrow_to_move_fields_to_selected_fields_listbox_from_fields_section()
reports_page.click_save()
project_navigator.select_projectNavigator_item("Reports")
self.assertTrue(reports_page.is_report_details_saved("all_records", "This is an all records report", "USN entities"), "Problem with saved report. Please see log for details")
except Exception:
base_page = BasePage(self.driver)
base_page.save_screenshot("test_00001_add_all_records_report_CRM")
raise
私のテストメソッドが失敗した場合は、スクリーンショットを作成しました。私はそれをこのように書いても大丈夫ですか?
あなたのヒントをありがとう。スクリーンショットをラッパーに置くと、スクリーンショットに失敗したテストメソッドが呼び出されます。私はラッパーを書かなかった。私はこれを見なければならないでしょう。 –
これで運がいい? –
私はラッパーについてあなたが言ったことを私の同僚に示し、私はすべてのテストメソッドにtry/catchブロックを置くことを提案しました。私は上記の質問の下にテスト方法を貼り付けました。これは大丈夫ですか? –