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

GD-655: Add ability to take screenshots during tests #655

Open
hatmix opened this issue Jan 30, 2025 · 6 comments
Open

GD-655: Add ability to take screenshots during tests #655

hatmix opened this issue Jan 30, 2025 · 6 comments
Assignees
Labels
enhancement New feature or request
Milestone

Comments

@hatmix
Copy link

hatmix commented Jan 30, 2025

The ability to take screenshots during tests is useful in a number of ways.

  • Verifying layout at various resolutions
  • Determining whether a given control is visible or clipped via code can be complicated, while manual confirmation of images is quick and easy if images exist (i.e. the time consuming part is navigating the app to the correct screen to verify)
  • Comparison to "known good" screenshot for when no changes are expected
  • Visual regression testing

Describe the solution you'd like
Trying the normal approach to capturing the viewport in gdUnit4 returns an empty texture. It would be ideal if Godot's standard get_viewport().get_texture() returned the expected rendered view. It would be very acceptable for scene runner to have a method like scene_runner.get_viewport_texture() that emulates the behavior of get_viewport().get_texture() when running scenes normally.

Describe alternatives you've considered
This is an activity that can be performed less frequently than automated unit or functional testing, so having a special scene or an editor script to gather screenshots is sufficient, if not ideal.

@hatmix hatmix added the enhancement New feature or request label Jan 30, 2025
@MikeSchulze MikeSchulze changed the title GD-XXX: screen captures from scene runner GD-655: Add ability to take screenshots during tests Jan 30, 2025
@MikeSchulze MikeSchulze added this to the 5.0.0 milestone Jan 30, 2025
@MikeSchulze
Copy link
Owner

We should provide two new functions to the SceneRunner

func capture() -> Texture2D
func store_capture(<Texture2D>, <path>) -> void

@MikeSchulze
Copy link
Owner

MikeSchulze commented Jan 30, 2025

And provide new assert to compare screenshots.
func assert_img(<Texture2D>).is_equal(<path to saved screenshot>)

A test could look like

func test_output_scene() -> void:

	var runner := scene_runner("res://scene.tscn")
	
	# wait the scene is fully rendered
	runner.simulate_frames(10)
	
	# capture the actual screen
	var screen_shot := runner.capture()
	# verify the image with a previous captured shot
	assert_image(screen_shot).is_equal("res://test/resources/img_0815.png")
  

@hatmix
Copy link
Author

hatmix commented Jan 30, 2025

An idea that just came to me, but is not necessary for my present use case... the ability to append screenshot(s?) to the report. Ideally, this would be attached to the test case where the screenshot was taken. If possible, this leads to a bunch of ideas about screenshots on failed assertions, etc.

@MikeSchulze
Copy link
Owner

MikeSchulze commented Jan 30, 2025

An idea that just came to me, but is not necessary for my present use case... the ability to append screenshot(s?) to the report. Ideally, this would be attached to the test case where the screenshot was taken. If possible, this leads to a bunch of ideas about screenshots on failed assertions, etc.

Sounds good.
We can add a function like
capture_screenshot_is_failed() to the end of a UI test, this will attach a screenshot to the report when the test is failing.

@hatmix
Copy link
Author

hatmix commented Jan 30, 2025

What runner.capture() does under headless is probably an important decision.

@MikeSchulze
Copy link
Owner

What runner.capture() does under headless is probably an important decision.

Only the image from the current view window will be grabbed.
var texture = scene.get_viewport().get_texture().get_image()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
Status: No status
Development

No branches or pull requests

2 participants