1+ using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
4+ using System . Text ;
5+ using System . Threading . Tasks ;
6+ using Xunit ;
7+ using Xunit . Abstractions ;
8+
9+ namespace PuppeteerSharp . Tests . BrowserContextTests
10+ {
11+ [ Collection ( TestConstants . TestFixtureCollectionName ) ]
12+ public class DefaultBrowserContextTests : PuppeteerPageBaseTest
13+ {
14+ public DefaultBrowserContextTests ( ITestOutputHelper output ) : base ( output )
15+ {
16+ }
17+
18+ public override async Task InitializeAsync ( )
19+ {
20+ await base . InitializeAsync ( ) ;
21+
22+ Context = Browser . DefaultContext ;
23+ Page = await Context . NewPageAsync ( ) ;
24+ }
25+
26+ [ Fact ]
27+ public async Task PageGetCookiesAsyncShouldWork ( )
28+ {
29+ await Page . GoToAsync ( TestConstants . EmptyPage ) ;
30+
31+ await Page . EvaluateExpressionAsync ( "document.cookie = 'username=John Doe'" ) ;
32+ var cookie = ( await Page . GetCookiesAsync ( ) ) . FirstOrDefault ( ) ;
33+ Assert . Equal ( "username" , cookie . Name ) ;
34+ Assert . Equal ( "John Doe" , cookie . Value ) ;
35+ Assert . Equal ( "localhost" , cookie . Domain ) ;
36+ Assert . Equal ( "/" , cookie . Path ) ;
37+ Assert . Equal ( - 1 , cookie . Expires ) ;
38+ Assert . Equal ( 16 , cookie . Size ) ;
39+ Assert . False ( cookie . HttpOnly ) ;
40+ Assert . False ( cookie . Secure ) ;
41+ Assert . True ( cookie . Session ) ;
42+ }
43+
44+ [ Fact ]
45+ public async Task PageSetCookiesAsyncShouldWork ( )
46+ {
47+ await Page . GoToAsync ( TestConstants . EmptyPage ) ;
48+
49+ await Page . SetCookieAsync ( new CookieParam
50+ {
51+ Name = "username" ,
52+ Value = "John Doe"
53+ } ) ;
54+
55+ var cookie = ( await Page . GetCookiesAsync ( ) ) . FirstOrDefault ( ) ;
56+ Assert . Equal ( "username" , cookie . Name ) ;
57+ Assert . Equal ( "John Doe" , cookie . Value ) ;
58+ Assert . Equal ( "localhost" , cookie . Domain ) ;
59+ Assert . Equal ( "/" , cookie . Path ) ;
60+ Assert . Equal ( - 1 , cookie . Expires ) ;
61+ Assert . Equal ( 16 , cookie . Size ) ;
62+ Assert . False ( cookie . HttpOnly ) ;
63+ Assert . False ( cookie . Secure ) ;
64+ Assert . True ( cookie . Session ) ;
65+ }
66+
67+ [ Fact ]
68+ public async Task PageDeleteCookieAsyncShouldWork ( )
69+ {
70+ await Page . GoToAsync ( TestConstants . EmptyPage ) ;
71+
72+ await Page . SetCookieAsync (
73+ new CookieParam
74+ {
75+ Name = "cookie1" ,
76+ Value = "1"
77+ } ,
78+ new CookieParam
79+ {
80+ Name = "cookie2" ,
81+ Value = "2"
82+ } ) ;
83+
84+ Assert . Equal ( "cookie1=1; cookie2=2" , await Page . EvaluateExpressionAsync < string > ( "document.cookie" ) ) ;
85+ await Page . DeleteCookieAsync ( new CookieParam
86+ {
87+ Name = "cookie2"
88+ } ) ;
89+ Assert . Equal ( "cookie1=1" , await Page . EvaluateExpressionAsync < string > ( "document.cookie" ) ) ;
90+
91+ var cookie = ( await Page . GetCookiesAsync ( ) ) . FirstOrDefault ( ) ;
92+ Assert . Equal ( "cookie1" , cookie . Name ) ;
93+ Assert . Equal ( "1" , cookie . Value ) ;
94+ Assert . Equal ( "localhost" , cookie . Domain ) ;
95+ Assert . Equal ( "/" , cookie . Path ) ;
96+ Assert . Equal ( - 1 , cookie . Expires ) ;
97+ Assert . Equal ( 8 , cookie . Size ) ;
98+ Assert . False ( cookie . HttpOnly ) ;
99+ Assert . False ( cookie . Secure ) ;
100+ Assert . True ( cookie . Session ) ;
101+ }
102+ }
103+ }
0 commit comments