Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Base64 encoded screenshot and additional screenshot creation from XPATH element #194

Merged
merged 36 commits into from
Aug 21, 2024

Conversation

HackXIt
Copy link
Contributor

@HackXIt HackXIt commented Jul 15, 2024

This PR adds another logging function, to directly embed screenshots as base64 encoded strings in the output log.

It also expands the screenshot functionality to allow for screenshot creation based on a provided element XPATH.
It will create a screenshot based on that element, so a screenshot of a window will contain the whole window, a screenshot of a button, will only contain that button.

Fixes #192 and fixes #193

@HackXIt HackXIt marked this pull request as draft July 15, 2024 10:45
HackXIt added 5 commits July 15, 2024 13:36
- Add xpath to ValueContainer
- Add additional actions for element and Base64 format
- add automation to init
- Add capture function for base64 using MemoryStream
- Add optional xpath to both capture functions
… by XPATh

- Adjust 'take screenshot' keyword with optional base64 boolean
- Add keyword 'take screenshot from element' for capturing element with xpath
- Fix initialization error due to missing automation parameter
- fix line too long
- Fix build errors, add ignore for ImageFormat
- Remove ScreenshotType
- Remove unused Optional import
@Nepitwin
Copy link
Member

Please update Changelog file to upcoming unreleased new keywords.

@Nepitwin
Copy link
Member

Is it possible to extend some Screenshots tests under ATests to see if log are resolved?

(Optional would be nice)

@Nepitwin
Copy link
Member

Nepitwin commented Jul 15, 2024

Probably a switch would be nice to decide default behavior from screenshots to create files or base64 like

Set Screenshot Log Mode (Base64 | File)

@HackXIt
Copy link
Contributor Author

HackXIt commented Jul 15, 2024

I'll get to address the changes right away, please keep in mind that I noticed that my commits made were using the wrong user configuration by mistake.
I have rebased the commits since then to overwrite the author with my correct information.

It should be visible in a moment.

if identifier:
module = self._container.create_or_get_module()
element = module.get_element(identifier, None, msg)
image_var = self._screenshots.execute_action(Screenshot.Action.CAPTURE_ELEMENT,
Copy link
Contributor Author

@HackXIt HackXIt Jul 15, 2024

Choose a reason for hiding this comment

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

This will always create a screenshot, even if the element could not be found. If the element is not found, the whole screen is captured like in default.
Might be better if the screenshot will not get created if the element was not found?

@HackXIt HackXIt changed the title Add Base64 encoded screenshot and additional screenshot creation from AutomationElement Add Base64 encoded screenshot and additional screenshot creation from XPATH element Jul 15, 2024
@HackXIt HackXIt marked this pull request as ready for review July 15, 2024 13:56
@HackXIt
Copy link
Contributor Author

HackXIt commented Jul 15, 2024

I'm done from my point of view, please have another look @Nepitwin

@HackXIt
Copy link
Contributor Author

HackXIt commented Aug 13, 2024

It triggers me hard that they are not working.

I should be better at this ;D

@Nepitwin
Copy link
Member

@HackXIt

Feel free this is the challenge of testing to execute them correctly on a testing system 📦

@HackXIt
Copy link
Contributor Author

HackXIt commented Aug 16, 2024

@Nepitwin I do need some help on this last test case. I do not know what runs differently on appveyor.

When I run the test locally, it creates the screenshot file and finds it correctly, but on appveyor it doesn't. I changed the format to use ${TEST_NAME} instead of hardcoding, but that didn't change results.

By the way, if you also have some suggestions on further necessary test cases, I can add those as well, but I believe with the feature being so small, the ones created should suffice.
Here's a list of the new ones:

  • Take Screenshot As Base64
  • Take Screenshot Of Window (this is the failing one)
  • Take Screenshot Of Window As Base64

Attached is my local log file of the successful execution of that test case. I did not run it via the keen.bat, since it executes quite a lot of test cases and I do not wish to run the full suite to verify a single one. It would be nice to have a mechanism in keen.bat to at least run suites seperately, but that's a change for another day.
result_take_screenshot_of_window.zip

Execution log C:; cd 'C:\Users\RINI\#Entwicklung_TechNICK\0_Git_Stash\robotframework-flaui\atests'; & 'C:\Users\RINI\AppData\Local\Temp\rf-ls-run\run_env_00_38e9sp6_.bat' '-u' 'c:\Users\RINI\.vscode\extensions\robocorp.robotframework-lsp-1.12.0\src\robotframework_debug_adapter\run_robot__main__.py' '--port' '7111' '--no-debug' '--listener=robotframework_debug_adapter.events_listener.EventsListenerV2' '--listener=robotframework_debug_adapter.events_listener.EventsListenerV3' '--outputdir' 'C:\Users\RINI\#Entwicklung_TechNICK\0_Git_Stash\robotframework-flaui/result/tmp' '--pythonpath' 'c:\Users\RINI\#Entwicklung_TechNICK\0_Git_Stash\robotframework-flaui/.venv/lib/python3.8/site-packages' '--pythonpath' 'c:\Users\RINI\#Entwicklung_TechNICK\0_Git_Stash\robotframework-flaui/atests' '--variable' 'UIA:UIA3' '--prerunmodifier=robotframework_debug_adapter.prerun_modifiers.FilteringTestsSuiteVisitor' 'c:\Users\RINI\#Entwicklung_TechNICK\0_Git_Stash\robotframework-flaui\atests\Screenshot.robot' s\x5cRINI\x5c#Entwicklung_TechNICK\x5c0_Git_Stash\x5crobotframework-flaui\x5catests\x5cScreenshot.robot' ============================================================================== Screenshot :: Test suite for screenshot keywords. XPath not found error han... ============================================================================== Take Screenshot Of Window | PASS | ------------------------------------------------------------------------------ Screenshot :: Test suite for screenshot keywords. XPath not found ... | PASS | 1 test, 1 passed, 0 failed ============================================================================== Output: C:\Users\RINI\#Entwicklung_TechNICK\0_Git_Stash\robotframework-flaui\result\tmp\output.xml Log: C:\Users\RINI\#Entwicklung_TechNICK\0_Git_Stash\robotframework-flaui\result\tmp\log.html Report: C:\Users\RINI\#Entwicklung_TechNICK\0_Git_Stash\robotframework-flaui\result\tmp\report.html

@HackXIt
Copy link
Contributor Author

HackXIt commented Aug 20, 2024

It would be better to always have it in the teardown, as currently it will not be executed on failure for the test that has to also stop the application. I'll add a keyword I think to combine the two.

@ghost
Copy link

ghost commented Aug 20, 2024

@Nepitwin I do need some help on this last test case. I do not know what runs differently on appveyor.

When I run the test locally, it creates the screenshot file and finds it correctly, but on appveyor it doesn't. I changed the format to use ${TEST_NAME} instead of hardcoding, but that didn't change results.

By the way, if you also have some suggestions on further necessary test cases, I can add those as well, but I believe with the feature being so small, the ones created should suffice. Here's a list of the new ones:

  • Take Screenshot As Base64
  • Take Screenshot Of Window (this is the failing one)
  • Take Screenshot Of Window As Base64

Attached is my local log file of the successful execution of that test case. I did not run it via the keen.bat, since it executes quite a lot of test cases and I do not wish to run the full suite to verify a single one. It would be nice to have a mechanism in keen.bat to at least run suites seperately, but that's a change for another day. result_take_screenshot_of_window.zip

Execution log

@HackXIt

Run a case in a robotfile, you can execute command like

.\keen.bat install

cd .\atests\

python -m robot --name "UIA3" --variable UIA:UIA3 --outputdir ../result/uia3 --test "Take Screenshot Of Window" .\Screenshot.robot

--test is the casename you edit in robotfile

@HackXIt
Copy link
Contributor Author

HackXIt commented Aug 21, 2024

There seems to be an unrelated problem on AppVeyor. The last commit had all tests successful, but the script had a failure somewhere. See console log for details on AppVeyor.

...
5 File(s) copied
Command exited with code 28
$wc = New-Object 'System.Net.WebClient'
$wc.UploadFile("https://ci.appveyor.com/api/testresults/junit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path .\result\xunit.xml))

@ghost
Copy link

ghost commented Aug 21, 2024

There seems to be an unrelated problem on AppVeyor. The last commit had all tests successful, but the script had a failure somewhere. See console log for details on AppVeyor.

...
5 File(s) copied
Command exited with code 28
$wc = New-Object 'System.Net.WebClient'
$wc.UploadFile("https://ci.appveyor.com/api/testresults/junit/$($env:APPVEYOR_JOB_ID)", (Resolve-Path .\result\xunit.xml))

@HackXIt

I debuged that, job "call:pylint" in keen.bat maybe retrurn the %ERRORLEVEL%=28, so the %result%=28 and it donnot change in left jobs, so keen.bat ended with EXIT /B %result%=28. EXIT code not be 0, it is marked as a Failure.

I add the attachment debug-flaui.zip with debugcode and EXIT code with 0
tip: use keen.bat in attachment and command ".\keen.bat test" will run only ./Screenshot.robot, no other robotfile

EXIT code with 28 in log, job "call:pylint" and job "tidy" print like
image

EXIT code with 0, should print like
image

@HackXIt
Copy link
Contributor Author

HackXIt commented Aug 21, 2024

@JimRevolutionist

Alright, applied the changes to keen.bat. (only the changes relating to errorlevel)

I also adjusted all screenshot test cases with a proper teardown keyword for resetting to default state, which should also run on error to not influence other test cases.

I also ran the screenshot suite beforehand, should all be green.

Fingers crossed, we should be good to go for merge after this.

Do you want me to create issues for the keen.bat so we can reference it in the PR?

I should also update Changelog to reflect the minor adjustments made.

@ghost
Copy link

ghost commented Aug 21, 2024

@JimRevolutionist

Alright, applied the changes to keen.bat. (only the changes relating to errorlevel)

I also adjusted all screenshot test cases with a proper teardown keyword for resetting to default state, which should also run on error to not influence other test cases.

I also ran the screenshot suite beforehand, should all be green.

Fingers crossed, we should be good to go for merge after this.

Do you want me to create issues for the keen.bat so we can reference it in the PR?

I should also update Changelog to reflect the minor adjustments made.

@HackXIt

optmize keen.bat should be an Independent issue,pr separate from screenshot issue,pr. We should not put keenbat commits into screenshot pr.

@HackXIt
Copy link
Contributor Author

HackXIt commented Aug 21, 2024

Oh. I'll revert the change then.

@Nepitwin
Copy link
Member

@HackXIt

Oh yes i forgot to update all related verifiction from pylint to see in error logs from appveyor. Sorry for this mistake. If you got already an solution you can create a new pr from it.

Because all issues can be seen afterwards in artifact files to guarantee python code style guides and robotframework has to be fixxed.

@JimRevolutionist

Sometimes api call failed from appveyor --> Command exited with code 28

It's not nice if a run was failed but for this case i would rerun the testcase from my perspective.

Sorry for late response.

@Nepitwin
Copy link
Member

@JimRevolutionist

By actuall solution error code is handled but appveyor does not upload zip file :(

ERRORLEVEL after call xcopy uia3 is: 0
ResultCode after call xcopy uia3: 1
ERRORLEVEL after call parsly is: 0
ResultCode after call parsly is: 1
Command exited with code 1
$wc = New-Object 'System.Net.WebClient'

In zip file from artifact the reasons from pylint is described what to fix.

@Nepitwin
Copy link
Member

But it can be seen in logs

************* Module src.FlaUILibrary.flaui.module.screenshot
src\FlaUILibrary\flaui\module\screenshot.py:115:8: R1705: Unnecessary "elif" after "return", remove the leading "el" from "elif" (no-else-return)
src\FlaUILibrary\flaui\module\screenshot.py:111:4: R1710: Either all return statements in a function should return an expression, or none of them should. (inconsistent-return-statements)
************* Module src.FlaUILibrary.keywords.screenshot
src\FlaUILibrary\keywords\screenshot.py:20:0: C0303: Trailing whitespace (trailing-whitespace)
src\FlaUILibrary\keywords\screenshot.py:47:0: C0301: Line too long (150/120) (line-too-long)
src\FlaUILibrary\keywords\screenshot.py:4:0: W0611: Unused robotlog imported from FlaUILibrary.robotframework (unused-import)
************* Module src.FlaUILibrary.robotframework.robotlog
src\FlaUILibrary\robotframework\robotlog.py:48:8: C0209: Formatting a regular string which could be an f-string (consider-using-f-string)

@Nepitwin
Copy link
Member

Verification locally can be done with

keen.bat pylint

But you should first install new package with

keen.bat install
keen.bat pylint

@HackXIt
Copy link
Contributor Author

HackXIt commented Aug 21, 2024

Alright, after all that… I think we finally have a valid one.

I fixed all the pylint errors, so appveyor should also give green light if my luck hasn't forsaken me.

Unfortunately, due to my bad behavior of pushing too much, it will take a bit until it gets to the last commit.

@Nepitwin Nepitwin merged commit 2673f21 into GDATASoftwareAG:main Aug 21, 2024
1 check passed
@HackXIt
Copy link
Contributor Author

HackXIt commented Aug 22, 2024

Yay.

Thank you @Nepitwin

I might do another PR soon regarding #199

noubar pushed a commit to noubar/robotframework-flaui that referenced this pull request Oct 3, 2024
… XPATH element (GDATASoftwareAG#194)

* Update .gitignore, add VSCode directory and .venv

* Add base64 logging function to robotlog

* Change screenshot module behaviour to accomodate base64 image conversion

- Add xpath to ValueContainer
- Add additional actions for element and Base64 format
- add automation to init
- Add capture function for base64 using MemoryStream
- Add optional xpath to both capture functions

* Change screenshot keywords to use optional base64 and allow capturing by XPATh

- Adjust 'take screenshot' keyword with optional base64 boolean
- Add keyword 'take screenshot from element' for capturing element with xpath

* Fix multiple build errors

- Fix initialization error due to missing automation parameter
- fix line too long
- Fix build errors, add ignore for ImageFormat
- Remove ScreenshotType
- Remove unused Optional import

* Add screenshot mode, Adjust parameter to retrieved element, execute capture based on mode

* Add AutomationInterfaceContainer, Combine capture into single keyword with optional XPATH, Add set mode keyword

* Remove unused imports

* Change example text

* Add simple testcases for new screenshot keyword behaviour

* Fix init to use container for screenshot keywords, remove container from Screenshot module

* Fix ScreenshotMode not defined (missing self)

* add getter for screenshot mode, Fix linting
errors/warnings, adjust docstrings

* Update changelog

* Fix missing default value for element in _capture_base64

* Improve screenshot testcases for base64 mode and xpath

* Fix argument name typo

* Tiny fix of take_screenshot docstring to explain identifier

* Fix AttributeError due to improper key access

* Add truthy checks in base64 screenshot tests before checking length

* Remove redundant None, explicit msg argument for get_element

* Add return statement to _capture function

* Fix check for None (wrong keyword used)

* Fix element assigned to wrong argument for screenshot action

* Remove redundant screenshot logging in keyword

* Fix filename for screenshot in test

* Fix Screenshot.robot (reorder test cases, deactivate Base64)

* Update Screenshot.robot (Minor format adjustment)

* Add getter keyword for screenshot log mode

* Add teardown mechanism with default state for all screenshot cases

* Add missing PID for reset to properly close applications

* Update .gitignore for custom keen.bat file in development

* Fix pylint errors to get them good grades

* Fix missing trailing blank line

* Fix via robotidy
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Create screenshots from AutomationElements Create screenshot as base64 encoded string instead of files
2 participants