Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 测试套件中web测试失败时的自动屏幕截图_Python_Selenium_Robotframework - Fatal编程技术网

Python 测试套件中web测试失败时的自动屏幕截图

Python 测试套件中web测试失败时的自动屏幕截图,python,selenium,robotframework,Python,Selenium,Robotframework,对不起,我的英语不好。 我的任务是增强源代码,以便在复杂测试套件中仅在web上测试失败时捕获web图像屏幕截图(我们在设备上测试,有时需要将设备的web登录到设置) 如果测试失败,我知道关键字Run。。。捕获页面截图并将其放入拆卸中,但我有许多测试套件要添加它们 这就是我必须增强源代码的原因。我在SeleniumLibrary中看到一个名为“Register_keyword_to_run_on_failure”的函数,但它似乎并没有像预期的那样工作。当我把它放在“启动浏览器”功能中时,它总是捕获

对不起,我的英语不好。 我的任务是增强源代码,以便在复杂测试套件中仅在web上测试失败时捕获web图像屏幕截图(我们在设备上测试,有时需要将设备的web登录到设置)

如果测试失败,我知道关键字Run。。。捕获页面截图并将其放入拆卸中,但我有许多测试套件要添加它们

这就是我必须增强源代码的原因。我在SeleniumLibrary中看到一个名为“Register_keyword_to_run_on_failure”的函数,但它似乎并没有像预期的那样工作。当我把它放在“启动浏览器”功能中时,它总是捕获屏幕截图

我们的测试套件在基于另一类python(selenium的子类…)的RobotFramework上运行,我们编写了自己的“capture_page_screenshot”函数来将报告推送到Defido

你对这个案子有什么想法吗

首先谢谢你

import os
import traceback

import robot.utils.robotenv as env

from SeleniumLibrary import SeleniumLibrary
from selenium.webdriver.common.desired_capabilities import DesiredCapabilities
from robot.libraries.BuiltIn import BuiltIn

from Browser import Browser
from Utils import Utils
from WebConfirmingAction import WebConfirmingAction

class WebSetup(object):
    def __init__(self):
        SeleniumLibrary.register_keyword_to_run_on_failure(WebInterface.capture_page_screenshot(self))
    "I want to code to capture web screenshot when test fail here"    

class WebInterface(object): 
    
    implicitly_timeout = 15
    page_load_timeout = 30
    set_driver_path = False

    def __init__(self, name = None):
        if not self.set_driver_path:
            env_path = env.get_env_var("PATH")
            env_path = ";".join([env_path, Utils.client_driver_path])
            env.set_env_var("PATH", env_path)
            self.set_driver_path = True
        self.screenshot_dir = None
        self.driver = SeleniumLibrary(self.page_load_timeout, self.implicitly_timeout, "")

    def start_browser(self, url, browser = None, port = None, protocol = None, ipv6_mode = False):
       pass
    
    def close_browser(self):
        try:
            self.driver.close_browser()
        except:
            traceback.print_exc()
            raise Exception(traceback._context_message)

    def capture_page_screenshot(self, file_name = None):
        file_name = str(file_name)
        if not self.screenshot_dir:
            self.screenshot_dir = os.path.join(Utils.get_output_dir(), "page_screenshot")
        Utils.create_directory_if_not_exist(self.screenshot_dir)
        _output = self.screenshot_dir
        if not file_name:
            file_name = self.driver.__hash__()
        _output = os.path.join(_output, file_name)
        if not _output.endswith(".png"):
            _output += ".png"
        image_path = self.driver.capture_page_screenshot(_output)
        Utils.add_file_to_report(image_path, file_name)
        return image_path
        

欢迎来到SO。请包括任何相关代码,以说明为什么事情不起作用。@ewong:谢谢,我编辑了内容并添加了脚本。