Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 89
Expand file tree
/
Copy pathProgram.cs
More file actions
Latest commit
97 lines (84 loc) · 4.33 KB
/
Copy pathProgram.cs
File metadata and controls
97 lines (84 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
usingMasterDevs.ChromeDevTools.Protocol.Chrome.Page;
usingSystem;
usingSystem.IO;
usingSystem.Linq;
usingSystem.Threading;
usingMasterDevs.ChromeDevTools.Protocol.Chrome.DOM;
usingTask=System.Threading.Tasks.Task;
namespaceMasterDevs.ChromeDevTools.Sample
{
internalclassProgram
{
constintViewPortWidth=1440;
constintViewPortHeight=900;
privatestaticvoidMain(string[]args)
{
Task.Run(async()=>
{
// synchronization
varscreenshotDone=newManualResetEventSlim();
// STEP 1 - Run Chrome
varchromeProcessFactory=newChromeProcessFactory(newStubbornDirectoryCleaner());
using(varchromeProcess=chromeProcessFactory.Create(9222,true))
{
// STEP 2 - Create a debugging session
varsessionInfo=(awaitchromeProcess.GetSessionInfo()).LastOrDefault();
varchromeSessionFactory=newChromeSessionFactory();
varchromeSession=chromeSessionFactory.Create(sessionInfo.WebSocketDebuggerUrl);
// STEP 3 - Send a command
//
// Here we are sending a commands to tell chrome to set the viewport size
// and navigate to the specified URL
awaitchromeSession.SendAsync(newSetDeviceMetricsOverrideCommand
{
Width=ViewPortWidth,
Height=ViewPortHeight,
Scale=1
});
varnavigateResponse=awaitchromeSession.SendAsync(newNavigateCommand
{
Url="http://www.google.com"
});
Console.WriteLine("NavigateResponse: "+navigateResponse.Id);
// STEP 4 - Register for events (in this case, "Page" domain events)
// send an command to tell chrome to send us all Page events
// but we only subscribe to certain events in this session
varpageEnableResult=awaitchromeSession.SendAsync<Protocol.Chrome.Page.EnableCommand>();
Console.WriteLine("PageEnable: "+pageEnableResult.Id);
chromeSession.Subscribe<LoadEventFiredEvent>(loadEventFired =>
{
// we cannot block in event handler, hence the task
Task.Run(async()=>
{
Console.WriteLine("LoadEventFiredEvent: "+loadEventFired.Timestamp);
vardocumentNodeId=(awaitchromeSession.SendAsync(newGetDocumentCommand())).Result.Root.NodeId;
varbodyNodeId=
(awaitchromeSession.SendAsync(newQuerySelectorCommand
{
NodeId=documentNodeId,
Selector="body"
})).Result.NodeId;
varheight=(awaitchromeSession.SendAsync(newGetBoxModelCommand{NodeId=bodyNodeId})).Result.Model.Height;
awaitchromeSession.SendAsync(newSetDeviceMetricsOverrideCommand
{
Width=ViewPortWidth,
Height=height,
Scale=1
});
Console.WriteLine("Taking screenshot");
varscreenshot=awaitchromeSession.SendAsync(newCaptureScreenshotCommand{Format="png"});
vardata=Convert.FromBase64String(screenshot.Result.Data);
File.WriteAllBytes("output.png",data);
Console.WriteLine("Screenshot stored");
// tell the main thread we are done
screenshotDone.Set();
});
});
// wait for screenshoting thread to (start and) finish
screenshotDone.Wait();
Console.WriteLine("Exiting ..");
}
}).Wait();
}
}
}