测试人社区

测混合APP时,切换到H5页面后,封装的截图方法报错

用老师上课教的封装的截图方法,在APP原生页面可以正常运行,但是切换到H5页面的时候,截图方法会报错
报错信息:

self = <page.main.Main object at 0x000001F69F0FEDA0>, locator = 'xpath'
value = "//*[@class='cmsui-tabBar']//li[@class][2]"

    @handle_black
    def find(self, locator, value: str = None):
        element: WebElement
    
        # 截图
>       self.screenshot("a.png")

..\page\base_page.py:38: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <page.main.Main object at 0x000001F69F0FEDA0>, name = 'a.png'

    def screenshot(self, name):
>       self._driver.save_screenshot(name)

..\page\base_page.py:22: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <appium.webdriver.webdriver.WebDriver (session="61ee47c6-3cc2-4eba-b689-1590922a555a")>
filename = 'a.png'

    def save_screenshot(self, filename):
        """
        Saves a screenshot of the current window to a PNG image file. Returns
           False if there is any IOError, else returns True. Use full paths in
           your filename.
    
        :Args:
         - filename: The full path you wish to save your screenshot to. This
           should end with a `.png` extension.
    
        :Usage:
            driver.save_screenshot('/Screenshots/foo.png')
        """
>       return self.get_screenshot_as_file(filename)

..\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py:1055: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <appium.webdriver.webdriver.WebDriver (session="61ee47c6-3cc2-4eba-b689-1590922a555a")>
filename = 'a.png'

    def get_screenshot_as_file(self, filename):
        """
        Saves a screenshot of the current window to a PNG image file. Returns
           False if there is any IOError, else returns True. Use full paths in
           your filename.
    
        :Args:
         - filename: The full path you wish to save your screenshot to. This
           should end with a `.png` extension.
    
        :Usage:
            driver.get_screenshot_as_file('/Screenshots/foo.png')
        """
        if not filename.lower().endswith('.png'):
            warnings.warn("name used for saved screenshot does not match file "
                          "type. It should end with a `.png` extension", UserWarning)
>       png = self.get_screenshot_as_png()

..\venv\lib\site-packages\selenium\webdriver\remote\webdriver.py:1032: 
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _

self = <appium.webdriver.webdriver.WebDriver (session="61ee47c6-3cc2-4eba-b689-1590922a555a")>

    def get_screenshot_as_png(self):
        """
        Gets the screenshot of the current window as a binary data.
    
        :Usage:
            driver.get_screenshot_as_png()
        """
>       return base64.b64decode(self.get_screenshot_as_base64().encode('ascii'))
E       AttributeError: 'NoneType' object has no attribute 'encode'

同时下面还有另外一个报错信息:

>       raise exception_class(message, screen, stacktrace)
E       selenium.common.exceptions.WebDriverException: Message: An unknown server-side error occurred while processing the command. Original error: chrome not reachable
E         (Session info: chrome=52.0.2743.100)
E         (Driver info: chromedriver=2.23.409699 (49b0fa931cda1caad0ae15b7d1b68004acd05129),platform=Windows NT 10.0.17763 x86_64)

我开始一直以为我的浏览器驱动有问题,后面发现只要把截图的方法注释掉,脚本就会运行成功,后面那个关于chrome not reachable的错误也没有了。
截图方法(在bese_pege文件里):

    def screenshot(self, name):
        self._driver.save_screenshot(name)

截图方法的引用(在bese_pege文件里):

@handle_black
    def find(self, locator, value: str = None):
        element: WebElement

        # 截图
        self.screenshot("a.png")
        with open("a.png", "rb") as f:
            content = f.read()
        allure.attach(content, attachment_type=allure.attachment_type.PNG)

        if isinstance(locator, tuple):
            element = self._driver.find_element(*locator)
        else:
            element = self._driver.find_element(locator, value)

        return element

切换到H5页面后,只要触发到截图,就会直接报这个错误,想请教下老师,有什么方法可以让截图方法在原生和H5页面都可以正常截图吗?

本质还是你的webview自动化测试环境没配置好吧,应该不止是截图吧

目前就发现在H5页面不能截图,把截图注释掉就可以正常运行了。
运行H5页面需要另外配置什么样的webview自动化环境呢?可以在H5页面进行点击、断言等操作,所以驱动应该是没问题的吧