Selenium2+python Automation 60-screenshot after exception (screenshot)

Source: Internet
Author: User
Tags throw exception

Objective

During the execution of the use case because it is unattended, when the use case runs out of error, we want to leave evidence on the current screen.

When writing a use case, the final step is to assert that the action can be placed on the assertion here, so how can the assertion fail?

First, the method

1.get_screenshot_as_file (self, filename)

-This method obtains the current window, returns False when IOError occurs, and returns true successfully.

The filename parameter is the path to the saved file.
Usage:
Driver.get_screenshot_as_file ('/screenshots/foo.png ')

2.get_screenshot_as_base64 (self)

-This method is also to get the screen, save the base64 encoding format, when the HTML interface output, will be used.

For example, want to put in the HTML test report.
Usage:
Driver.get_screenshot_as_base64 ()

3.get_screenshot_as_png (self)
-This is to get the screen, save the binary data, rarely used.
Usage:
Driver.get_screenshot_as_png ()

Second, after the exception

1. In order to throw an exception, the ID of the location login button is changed to a wrong ID.

2. Add a timestamp to the name of the image to avoid overwriting the same filename.

3. File path, the file name directly written here, is the same path as the current script. If the picture is exported to a different file path, the absolute path to the file is required.

4. The result is that if the graph is not truncated to return false, success returns TRUE.

Iii. Examples of Selenium

1. When writing use cases in the UnitTest framework, we want to be on the current screen when the assertion fails.

2. If the try...except catches an exception, all the test cases are passed and the test results are affected. The solution is actually very simple, and then throw the exception to the line.

3. Reference code:

# Coding:utf-8
From selenium import Webdriver
Import Time,unittest
From Selenium.webdriver.support import expected_conditions as EC
Class Login (UnitTest. TestCase):
def setUp (self):
Url_login = "Https://passport.cnblogs.com/user/signin"
Self.driver = Webdriver. Firefox ()
Self.driver.get (Url_login)

def test_01 (self):
"Before entering the account password, let the correct operation to assert this step, the assertion is deliberately set to false unsuccessful"
Try
self.driver.find_element_by_id ("Input1"). Send_keys (U "Shanghai-leisurely")
self.driver.find_element_by_id ("Input2"). Send_keys ("xxx")
# login ID is wrong, positioning will throw exception
self.driver.find_element_by_id ("Signin"). Click ()
# Determine if the Login Success page has an account: "Shanghai-leisurely"
Time.sleep (3)
Locator = ("id", "lnk_current_user")
result = Ec.text_to_be_present_in_element (locator,u "Shanghai-Yo") (self.driver)
Self.assertfalse (Result)
Except Exception as msg:
Print (U "Exception reason%s"%msg)
# The picture name can be added a timestamp
Nowtime = Time.strftime ("%y%m%d.%h.%m.%s")
Self.driver.get_screenshot_as_file ('%s.jpg '% nowtime)
Raise

def tearDown (self):
Pass

if __name__ = = "__main__":
Unittest.main ()

4. Operation Result:

Exception reason true is not false

Failure
Traceback (most recent):
File "D:\test\yoyot\ketang\test01.py", line A, in test_01
Self.assertfalse (Result)
Assertionerror:true is not false

The study process has encountered the question, may add selenium (Python+java) QQ Group Exchange: 232607095

Feel that you have help, in the lower right corner of a praise it, thank you support!

Selenium2+python Automation 60-After exception (screenshot)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.