Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
ChromeDevTools
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
FrameIT
ChromeDevTools
Commits
973567ad
Commit
973567ad
authored
7 years ago
by
svatal
Browse files
Options
Downloads
Patches
Plain Diff
added example that is actually doing something (taking screenshot of the page)
parent
4f74d04e
No related branches found
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
source/ChromeDevTools/LocalChromeProcess.cs
+2
-0
2 additions, 0 deletions
source/ChromeDevTools/LocalChromeProcess.cs
source/Sample/Program.cs
+61
-15
61 additions, 15 deletions
source/Sample/Program.cs
with
63 additions
and
15 deletions
source/ChromeDevTools/LocalChromeProcess.cs
+
2
−
0
View file @
973567ad
...
...
@@ -20,6 +20,8 @@ namespace MasterDevs.ChromeDevTools
base
.
Dispose
();
Process
.
Kill
();
Process
.
WaitForExit
();
Process
.
Close
();
DisposeUserDirectory
();
}
}
...
...
This diff is collapsed.
Click to expand it.
source/Sample/Program.cs
+
61
−
15
View file @
973567ad
using
MasterDevs.ChromeDevTools.Protocol.Chrome.Page
;
using
System
;
using
System.IO
;
using
System.Linq
;
using
System.Threading
;
using
MasterDevs.ChromeDevTools.Protocol.Chrome.DOM
;
using
MasterDevs.ChromeDevTools.Protocol.Chrome.Emulation
;
using
Task
=
System
.
Threading
.
Tasks
.
Task
;
namespace
MasterDevs.ChromeDevTools.Sample
{
internal
class
Program
{
const
int
ViewPortWidth
=
1440
;
const
int
ViewPortHeight
=
900
;
private
static
void
Main
(
string
[]
args
)
{
Task
.
Run
(
async
()
=>
{
// synchronization
var
screenshotDone
=
new
ManualResetEventSlim
();
// STEP 1 - Run Chrome
var
chromeProcessFactory
=
new
ChromeProcessFactory
(
new
StubbornDirectoryCleaner
());
using
(
var
chromeProcess
=
chromeProcessFactory
.
Create
(
9222
))
...
...
@@ -22,31 +31,68 @@ namespace MasterDevs.ChromeDevTools.Sample
// STEP 3 - Send a command
//
// Here we are sending a command to tell chrome to navigate to
// the specified URL
var
navigateResponse
=
chromeSession
.
SendAsync
(
new
NavigateCommand
// Here we are sending a commands to tell chrome to set the viewport size
// and navigate to the specified URL
await
chromeSession
.
SendAsync
(
new
SetVisibleSizeCommand
{
Width
=
ViewPortWidth
,
Height
=
ViewPortHeight
});
var
navigateResponse
=
await
chromeSession
.
SendAsync
(
new
NavigateCommand
{
Url
=
"http://www.google.com"
})
.
Result
;
});
Console
.
WriteLine
(
"NavigateResponse: "
+
navigateResponse
.
Id
);
// STEP 4 - Register for events (in this case, "Page" domain events)
// send an
event
to tell chrome to send us all Page events
// send an
command
to tell chrome to send us all Page events
// but we only subscribe to certain events in this session
var
pageEnableResult
=
chromeSession
.
SendAsync
<
ChromeDevTools
.
Protocol
.
Chrome
.
Page
.
EnableCommand
>()
.
Result
;
var
pageEnableResult
=
await
chromeSession
.
SendAsync
<
Protocol
.
Chrome
.
Page
.
EnableCommand
>();
Console
.
WriteLine
(
"PageEnable: "
+
pageEnableResult
.
Id
);
chromeSession
.
Subscribe
<
Protocol
.
Chrome
.
Page
.
DomContentEventFiredEvent
>(
domContentEvent
=>
{
Console
.
WriteLine
(
"DomContentEvent: "
+
domContentEvent
.
Timestamp
);
});
// you might never see this, but that's what an event is ... right?
chromeSession
.
Subscribe
<
Protocol
.
Chrome
.
Page
.
FrameStartedLoadingEvent
>(
frameStartedLoadingEvent
=>
chromeSession
.
Subscribe
<
LoadEventFiredEvent
>(
loadEventFired
=>
{
Console
.
WriteLine
(
"FrameStartedLoading: "
+
frameStartedLoadingEvent
.
FrameId
);
// we cannot block in event handler, hence the task
Task
.
Run
(
async
()
=>
{
Console
.
WriteLine
(
"LoadEventFiredEvent: "
+
loadEventFired
.
Timestamp
);
var
documentNodeId
=
((
CommandResponse
<
GetDocumentCommandResponse
>)
await
chromeSession
.
SendAsync
(
new
GetDocumentCommand
())).
Result
.
Root
.
NodeId
;
var
bodyNodeId
=
((
CommandResponse
<
QuerySelectorCommandResponse
>)
await
chromeSession
.
SendAsync
(
new
QuerySelectorCommand
{
NodeId
=
documentNodeId
,
Selector
=
"body"
})).
Result
.
NodeId
;
var
height
=
((
CommandResponse
<
GetBoxModelCommandResponse
>)
await
chromeSession
.
SendAsync
(
new
GetBoxModelCommand
{
NodeId
=
bodyNodeId
})).
Result
.
Model
.
Height
;
await
chromeSession
.
SendAsync
(
new
SetVisibleSizeCommand
{
Width
=
ViewPortWidth
,
Height
=
height
});
Console
.
WriteLine
(
"Taking screenshot"
);
var
screenshot
=
(
CommandResponse
<
CaptureScreenshotCommandResponse
>)
await
chromeSession
.
SendAsync
(
new
CaptureScreenshotCommand
{
Format
=
"png"
});
var
data
=
Convert
.
FromBase64String
(
screenshot
.
Result
.
Data
);
File
.
WriteAllBytes
(
"output.png"
,
data
);
Console
.
WriteLine
(
"Screenshot stored"
);
// tell the main thread we are done
screenshotDone
.
Set
();
});
});
Console
.
ReadLine
();
// wait for screenshoting thread to (start and) finish
screenshotDone
.
Wait
();
Console
.
WriteLine
(
"Exiting .."
);
}
}).
Wait
();
}
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment