Hacktoberfest 2026:维护者为十月标记出来的 issue,仍然开放、适合新手。 浏览 Hacktoberfest issue

Infinity Loop with `Capture ** Screenshot` when using `{{index}}` (escaped) while file exists

未关闭
#1,882 0 条评论 0 个 reaction 已指派 1 人 在 GitHub 查看

@emanlove 已经在做这个了。

开始于 2024年1月20日。

评估

这个 Issue 还没有评估数据。

描述

needs review

The following atest should be updated to this, so that the screenshot is taken twice.

Capture page screenshot with escaped braces
    ${file} =    Capture Page Screenshot    ${OUTPUTDIR}/screenshot-and-index/brackets-{{index}}-name.png
    File Should Exist    ${OUTPUTDIR}/screenshot-and-index/brackets-{index}-name.png
    ${file} =    Capture Page Screenshot    ${OUTPUTDIR}/screenshot-and-index/brackets-{{index}}-name.png    # this line is new!
    Should Be Equal    ${file}    ${OUTPUTDIR}${/}screenshot-and-index${/}brackets-{index}-name.png
    ${file} =    Capture Page Screenshot    ${OUTPUTDIR}/screenshot-and-index/brackets-{{index-name.png
    File Should Exist    ${OUTPUTDIR}/screenshot-and-index/brackets-{index-name.png

The problem is, that the code of _get_screenshot_path() does increment the index and then checks if either the (original) filename is equal to the formatted filename, which would mean that there was no {index} in it, or the file does not exists.

So that mean if the filename is before and after formatting the same, it does not matter if it exists, it will be overwritten.

BUT there is one special feature, that you can include {index} in the final file name, by doubling the braces.

So filename-{{index}}.png will be filename-{index}.png in the filesystem.
The problem now is, that the original filename (filename-{{index}}.png) is not equal with the formatted filename filename-{index}.png and therefore the index is increased. But there is no replaceable index in the original name, so the while loop is infinit.

The fix would be to compare not the original with the formatted, but two formatted with different index numbers with each other.

This is the fixed code:

    def _get_screenshot_path(self, filename):
        if self._screenshot_root_directory != EMBED:
            directory = self._screenshot_root_directory or self.log_dir
        else:
            directory = self.log_dir
        filename = filename.replace("/", os.sep)
        index = 0
        while True:
            index += 1
            formatted = _format_path(filename, index)
            formatted_control = _format_path(filename, sys.maxsize)   # control group with max int
            path = os.path.join(directory, formatted)
            # filename didn't contain {index} or unique path was found
            if formatted == formatted_control or not os.path.exists(path):    # two formatted texts are compared.
                return path

kind regards
René

主要语言
Python
星标
1.5k
派生
787
PR 合并指标
30 天内没有已合并 PR

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

robotframework/SeleniumLibrary 的其他 Issue

查看 robotframework/SeleniumLibrary 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。