@@ -19,6 +19,15 @@ def assert_screenshot_file(database_path):
1919 assert screenshot_path .stat ().st_size > 0 , "Screenshot file is empty"
2020
2121
22+ def assert_no_screenshot_file (database_path ):
23+ run_dirs = [d for d in database_path .glob ("*.run" ) if d .is_dir ()]
24+ assert (
25+ len (run_dirs ) == 1
26+ ), f"Expected exactly one .run directory, found { len (run_dirs )} "
27+ screenshot_path = run_dirs [0 ] / "screenshot.png"
28+ assert not screenshot_path .exists (), "Unexpected screenshot file was captured"
29+
30+
2231def assert_screenshot_envelope (envelope ):
2332 found_screenshot = False
2433 for item in envelope .items :
@@ -30,7 +39,7 @@ def assert_screenshot_envelope(envelope):
3039 assert item .headers .get ("attachment_type" ) == "event.attachment"
3140 assert len (item .payload .bytes ) > 0
3241 found_screenshot = True
33- assert found_screenshot , "No screenshot item found in envelope"
42+ return found_screenshot
3443
3544
3645def assert_screenshot_upload (req ):
@@ -79,7 +88,9 @@ def test_capture_screenshot_native(cmake, httpserver):
7988
8089 assert len (httpserver .log ) == 1
8190 envelope = Envelope .deserialize (httpserver .log [0 ][0 ].get_data ())
82- assert_screenshot_envelope (envelope )
91+ assert (
92+ assert_screenshot_envelope (envelope ) == True
93+ ), "No screenshot item found in envelope"
8394
8495
8596@pytest .mark .skipif (
@@ -124,3 +135,55 @@ def test_capture_screenshot_crashpad(cmake, httpserver, run_args):
124135)
125136def test_capture_screenshot_crashpad_wer (cmake , httpserver , run_args ):
126137 test_capture_screenshot_crashpad (cmake , httpserver , run_args )
138+
139+
140+ @pytest .mark .skipif (
141+ sys .platform != "win32" ,
142+ reason = "Screenshots are only supported on Windows" ,
143+ )
144+ @pytest .mark .parametrize ("backend" , ["inproc" , "breakpad" ])
145+ def test_before_screenshot (cmake , httpserver , backend ):
146+ tmp_path = cmake (
147+ ["sentry_screenshot" ], {"SENTRY_BACKEND" : backend , "SENTRY_TRANSPORT" : "none" }
148+ )
149+
150+ env = dict (os .environ , SENTRY_DSN = make_dsn (httpserver ))
151+
152+ run (
153+ tmp_path ,
154+ "sentry_screenshot" ,
155+ ["crash" , "before-screenshot" ],
156+ expect_failure = True ,
157+ env = env ,
158+ )
159+
160+ assert_no_screenshot_file (tmp_path / ".sentry-native" )
161+
162+
163+ @pytest .mark .skipif (
164+ sys .platform != "win32" ,
165+ reason = "Screenshots are only supported on Windows" ,
166+ )
167+ def test_before_screenshot_native (cmake , httpserver ):
168+ tmp_path = cmake (["sentry_screenshot" ], {"SENTRY_BACKEND" : "native" })
169+
170+ env = dict (os .environ , SENTRY_DSN = make_dsn (httpserver ))
171+
172+ httpserver .expect_oneshot_request ("/api/123456/envelope/" ).respond_with_data ("OK" )
173+
174+ with httpserver .wait (timeout = 10 ) as waiting :
175+ run (
176+ tmp_path ,
177+ "sentry_screenshot" ,
178+ ["crash" , "before-screenshot" ],
179+ expect_failure = True ,
180+ env = env ,
181+ )
182+
183+ assert waiting .result
184+
185+ assert len (httpserver .log ) == 1
186+ envelope = Envelope .deserialize (httpserver .log [0 ][0 ].get_data ())
187+ assert (
188+ assert_screenshot_envelope (envelope ) == False
189+ ), "Screenshot item was unexpectedly found in envelope"
0 commit comments