def take_screenshot():
    if config.get_option("disable_screenshot"):
        return

    from . import testexecutor
    web_drivers = testexecutor.current_executor().get_property("web_drivers")
    running_test_fixture = testexecutor.current_executor().get_property("running_test_fixture")

    if web_drivers:
        for index, web_driver in enumerate(web_drivers):
            screenshot = {
                "source": "Web Driver",
                "path": "%s-%s.png" % (running_test_fixture.full_name, index + 1)
            }
            running_test_fixture.screenshots.append(screenshot)

            try:
                screenshot["alert"] = web_driver.switch_to.alert.text
            except Exception:
                pass

            while True:
                try:
                    web_driver.switch_to.alert.dismiss()
                except Exception:
                    break

            try:
                screenshot["url"] = web_driver.current_url
            except Exception:
                pass

            try:
                screenshot["title"] = web_driver.title
            except Exception:
                pass

            try:
                with open(os.path.join(config.get_option("temp"), screenshot["path"]), mode="wb") as f:
                    f.write(web_driver.get_screenshot_as_png())
            except Exception:
                preporter.warn("Failed to take the screenshot.\n%s" % traceback.format_exc())
    else:
        if system() == 'Darwin' and not pyobjc_installed:
            preporter.warn("The package pyobjc is necessary for taking screenshot of desktop, please install it.")
            return

        screenshot = {
            "source": "Desktop",
            "path": "%s.png" % running_test_fixture.full_name
        }
        running_test_fixture.screenshots.append(screenshot)

        output = BytesIO()
        try:
            mss().save(output=output, screen=-1)  # -1 means all monitors
            with open(os.path.join(config.get_option("temp"), screenshot["path"]), mode="wb") as f:
                f.write(output.getvalue())
        except Exception:
            preporter.warn("Failed to take the screenshot.\n%s" % traceback.format_exc())
dataset/ETHPy150Open KarlGong/ptest/ptest/screencapturer.py/take_screenshot/original