プログラムの正確性の確認
アサーションステートメントを使用すると、スクリプトが予期される結果を受信していることを確認できます。セッションステータスは、ジョブが失敗した原因が不明な例外か、またはアサーションステートメントの失敗かを判断するために使用します。
このページでは、アサーションの例を示して、セッションステータスを使用して問題のあるスクリプトを特定する方法について説明します。
Use Assertions to Verify Expected Results
Assert statements use a simple syntax to verify an expected result. You can also provide a message to display if the assertion fails. In the example below, the message Title should contain AppDynamics will be displayed if the assertion fails.
# Get page and check the title
driver.get("http://https://ecommerce.com/view.html/ref=nav_cart")
assert "E-Commerce Shopping Cart" in driver.title, "Title should contain E-Commerce Shopping Cart"
# Click link and confirm URL is correct
driver.find_element_by_link_text("Place your order").click()
assert "ThankYouCart" in driver.current_urlYou can also verify that a string is not in the result or the truth of a mathematical expression. Assert statements that are false will result in a session status of FAILED.
# Confirm the page doesn't gives a 500 error.
assert "500 Internal Server Error" not in driver.page_source
# Confirm the footer is on the page.
footer = driver.find_elements_by_class_name('footer-body')
assert len(footer) > 0, "Footer couldn't be found."Use the Session Status to Find Script Issues
The two-session statuses BROKEN and FAILED indicate that the script had an issue. If the session status is BROKEN, it means that your script threw an uncaught exception. You'll most likely need to review your code and look for errors. See Exceptions for a list of common exceptions.
If the session state is FAILED, an assert statement failed or if you checked "Fail on page load error" and the page failed to load. You'll need to determine why your assertion statement failed or why the page couldn't be loaded.
You can view the results of assertion statements in the session status detail and script output. From the Session Details page, you can view failed assert statements in a red exception box at the top and from the Waterfall tab:
Failed assert statements will also appear in the Script Output dialog: