Using Capture Screenshot with %d in filename by aaltat · Pull Request #504 · robotframework/SeleniumLibrary

@aaltat

This commit enhances the Capture Screenshot keyword to support
internal running counter also when filename is not None. The place
of the counter is indicated with %d character.

Also the absolute path of the screenshot filename is always returned
by the keyword.

Also made small drive by refactoring to follow pep8 better.

@just-be-dev

just-be-dev

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would an exception be raised because of there being an extra % in the name?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is because of Python older string formatting, which the code uses, see in here: https://github.com/robotframework/Selenium2Library/pull/504/files#diff-5346a546c6b37e3c016ad51e7155887dR122. Example this: python -c "'foo % bar %d' % 'string'" will also cause exception. There is bug in documentation, that exception is not always TypeError and it would be best to say only exception.

The Python 2.6 onward supports str.format(), which could solve the problem in different way but because we must support Python 2.5, I did not use it. I could also do simple str.replace() in the code, but I would like to keep some formatting options open for future enhancements.

@pekkaklarck

I initially proposed %d as a placeholder for the index. It's an interesting idea to possible support also other formatting options, but after thinking a bit I don't think that would be very useful. Such formatting would work well if there would be other automatically generated information, but I doubt the would be. Formatting wouldn't be too useful in hypothetical cases like

Capture Page Screenshot    foo-{bar}-{index}.png    bar=zap
Capture Page Screenshot    foo-{bar}-{index}.png    bar=${var}

because you could just use

Capture Page Screenshot    foo-zap-{index}.png
Capture Page Screenshot    foo-${var}-{index}.png

Because I think other formatting isn't likely very useful, I don't think we need to take that into account. That means we could easily use something more explicit than %d to specify an index is needed. We could, for example, allow <index> or :index: and just use .replace('<index>', index) in the code.

@pekkaklarck

Also, @aaltat mentioned that str.format cannot be used because Python 2.5 needs to be supported. I think Selenium has dropped 2.5 support years ago so that shouldn't be an issue. Is Python 2.5 support promised somewhere?

@aaltat

It is not promised directly. But we do claim to support quite old versions of Robot Framework (2.6 is the minimum version) and older versions do support Python 2.5. How realistic this is, ido not know.

But if we announced that we only support 2.6 onwards, it would allow to use str.format. I still keep the formatting option open because it would be really nice to get example 00001, 00002... and not 1, 2... Or perhaps adding epoch would be nice too.

@pekkaklarck

As I already wrote, Selenium itself hasn't supported Py2.5 for years. Do you think someone would be using new S2L and downgrade Se separately to an ancient version?

Supporting formatting like leading zeros is a very good point. Supporting {index} and using .format() sounds like a good idea.

@aaltat

Ah my mistake, in INSTALL.rst we say that 2.6 is the minimum version. Because of the trouble caused by %d, I will change the implementation so direction which was suggested by @pekkaklarck. The str.format feels also quite temping to use, because it opens quite nice formatting possibilities. Perhaps it would be best to do something like this: python -c "print 'file-name-{index}.png'.format(index=1)"

@aaltat

Refactored the pull request to use str.format

pekkaklarck

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This note should be more explicit about what's new in 1.8. I would also either move it at the end of the doc or embed it into the doc like Starting from version 1.8, if ....

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Went for Starting from version 1.8, if ...

@aaltat

@just-be-dev

I just read through it and everything looks good to me. You've got my 👍 to merge whenever you feel this is ready.

@aaltat

If @pekkaklarck is also happy, then I am also ready for merge

@aaltat

Squashed all to commits to one, because the middle commits where not meaningful alone.

@aaltat

Hmm some other test did fail, can I somehow push Travis to rerun the tests?

pekkaklarck

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these changes here to avoid lines longer than 80 chars? That is OK in general, but such changes should be submitted separately. Preferably so that the whole module is gone through in one go. In minor cases it's also a bit questionable is the slightly enhanced PEP-8 compatibility worth an extra change in the version history.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mostly agree with what @pekkaklarck said. We really should keep changes focused. With that being said, I'm not necessarily against this staying. I'll defer to you two on that decision.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, modifications where done do void too long lines. I can can remove it from the commit if it's needed.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to remove. Unrelated changes just add noise that makes the review a bit harder. Now we all already know why the changes are here and removing them wouldn't win much.

@pekkaklarck

I added few more line notes. Please take a look at them @aaltat and fix ones that you see worth fixing. Would you @zephraph like take a final look at this afterwards and merge?

@just-be-dev

@aaltat

just-be-dev

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's a typo here. Plus it's worded a little weird. Maybe it should be something like

Capturing a page screenshot with two indexes should not cause an error

Or something, idk. Regardless, it could definitely be reworded.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

True, wording is not correct and your example is better. But should I remove the hole test and what you mean: worked weird?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed to name

@just-be-dev

@aaltat, have you ran this locally to make sure that the screenshots really do show up?

@aaltat

I did manualy verify that screen captures are visible in the log.

just-be-dev

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo. whith -> with.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@just-be-dev

Found two more small typos. Otherwise, it looks good to me. Fix those and I'll merge it in.

Thanks for your patience!

@aaltat

This commit enhances the Capture Screenshot keyword to support
internal running counter also when filename is not None. The place
of the counter is indicated with {index} characters.

Also the absolute path of the screenshot filename is always returned
by the keyword.

@aaltat

Fixed based on @zephraph comments.

I think, this is normal iterative development process. I did have an idea, or actually problem to solve. Made a first attempt to fix it and honesty it was quite horrible attempt. Then there was new idea from the review process, which was better, but caused lot of problems/corner cases. And on last attempt, we did nail it down quite well.

Also I am used to review process, it is nice way to share the information between people. It also yields to better results (and this is fine example of one), like better functionality, cleaner code and so one.

@pekkaklarck

@just-be-dev

just-be-dev added a commit that referenced this pull request

Sep 15, 2015
Use Capture Screenshot with {index} in filename.

@aaltat aaltat deleted the screen_shot_with_prosent_index branch

August 8, 2016 15:22