File tree Expand file tree Collapse file tree 3 files changed +72
-0
lines changed 
PuppeteerSharp.Tests/Page Expand file tree Collapse file tree 3 files changed +72
-0
lines changed Original file line number Diff line number Diff line change 1+ using  System . Threading . Tasks ; 
2+ using  Xunit ; 
3+ 
4+ namespace  PuppeteerSharp . Tests . Page 
5+ { 
6+     [ Collection ( "PuppeteerLoaderFixture collection" ) ] 
7+     public  class  SetContentTests  :  PuppeteerBaseTest 
8+     { 
9+         const  string  ExpectedOutput  =  "<html><head></head><body><div>hello</div></body></html>" ; 
10+ 
11+         [ Fact ] 
12+         public  async  Task  ShouldWork ( ) 
13+         { 
14+             var  page  =  await  Browser . NewPageAsync ( ) ; 
15+ 
16+             await  page . SetContentAsync ( "<div>hello</div>" ) ; 
17+             var  result  =  await  page . GetContentAsync ( ) ; 
18+ 
19+             Assert . Equal ( ExpectedOutput ,  result ) ; 
20+         } 
21+ 
22+         [ Fact ] 
23+         public  async  Task  ShouldWorkWithDoctype ( ) 
24+         { 
25+             var  page  =  await  Browser . NewPageAsync ( ) ; 
26+             const  string  doctype  =  "<!DOCTYPE html>" ; 
27+ 
28+             await  page . SetContentAsync ( $ "{ doctype } <div>hello</div>") ; 
29+             var  result  =  await  page . GetContentAsync ( ) ; 
30+ 
31+             Assert . Equal ( $ "{ doctype } { ExpectedOutput } ",  result ) ; 
32+         } 
33+ 
34+         [ Fact ] 
35+         public  async  Task  ShouldWorkWithHtml4Doctype ( ) 
36+         { 
37+             var  page  =  await  Browser . NewPageAsync ( ) ; 
38+             const  string  doctype  =  "<!DOCTYPE html PUBLIC \"  -//W3C//DTD HTML 4.01//EN\"  "  + 
39+                 "\" http://www.w3.org/TR/html4/strict.dtd\" >" ; 
40+ 
41+             await  page . SetContentAsync ( $ "{ doctype } <div>hello</div>") ; 
42+             var  result  =  await  page . GetContentAsync ( ) ; 
43+ 
44+             Assert . Equal ( $ "{ doctype } { ExpectedOutput } ",  result ) ; 
45+         } 
46+     } 
47+ } 
Original file line number Diff line number Diff line change @@ -116,6 +116,27 @@ internal Task<ElementHandle> AddScriptTag(dynamic options)
116116            throw  new  NotImplementedException ( ) ; 
117117        } 
118118
119+         internal  async  Task < string >  GetContentAsync ( ) 
120+         { 
121+             return  await  EvaluateFunctionAsync < string > ( @"() => { 
122+                 let retVal = ''; 
123+                 if (document.doctype) 
124+                     retVal = new XMLSerializer().serializeToString(document.doctype); 
125+                 if (document.documentElement) 
126+                     retVal += document.documentElement.outerHTML; 
127+                 return retVal; 
128+             }" ) ; 
129+         } 
130+ 
131+         internal  async  Task  SetContentAsync ( string  html ) 
132+         { 
133+             await  EvaluateFunctionAsync ( @"html => { 
134+                 document.open(); 
135+                 document.write(html); 
136+                 document.close(); 
137+             }" ,  html ) ; 
138+         } 
139+         
119140        internal  async  Task < string >  GetTitleAsync ( )  =>  await  EvaluateExpressionAsync < string > ( "document.title" ) ; 
120141
121142        internal  void  OnLifecycleEvent ( string  loaderId ,  string  name ) 
Original file line number Diff line number Diff line change @@ -262,6 +262,10 @@ await page.SetViewport(new ViewPortOptions
262262            return  page ; 
263263        } 
264264
265+         public  async  Task < string >  GetContentAsync ( )  =>  await  _frameManager . MainFrame . GetContentAsync ( ) ; 
266+ 
267+         public  async  Task  SetContentAsync ( string  html )  =>  await  _frameManager . MainFrame . SetContentAsync ( html ) ; 
268+ 
265269        public  async  Task < dynamic >  GoToAsync ( string  url ,  Dictionary < string ,  string >  options  =  null ) 
266270        { 
267271            var  referrer  =  _networkManager . ExtraHTTPHeaders ? . GetValueOrDefault ( "referer" ) ; 
    
 
   
 
     
   
   
          
     
  
    
     
 
    
      
     
 
     
    You can’t perform that action at this time.
  
 
    
  
     
    
      
        
     
 
       
      
     
   
 
    
    
  
 
  
 
     
    
0 commit comments