Newer
Older
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
98
99
100
101
102
103
104
using AutoWebPerf.Chrome;
using AutoWebPerf.Chrome.Messages;
using AutoWebPerf.Chrome.Messages.Network;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using WebSocket4Net;
namespace AutoWebPerf
{
class Program
{
static void Main(string[] args)
{
IChromeProxy _chromeProxy = null;
ChromeProcessInfo chromeProcessInfo = null;
try
{
chromeProcessInfo = StartChrome();
var c = new ChromeWrapper("http://localhost:" + ChromeRemoteDebuggingPort);
var sessions = c.GetAvailableSessions();
foreach (var s in sessions)
Console.WriteLine(s.url);
if (sessions.Count == 0)
throw new Exception("All debugging sessions are taken.");
// Will drive first tab session
var sessionWSEndpoint = sessions.Last().webSocketDebuggerUrl; // new tab window
c.SetActiveSession(sessionWSEndpoint);
c.NavigateTo("http://www.google.com");
var requestFactory = new RequestFactory();
_chromeProxy = new ChromeProxy(sessionWSEndpoint, new ResponseFactory(requestFactory));
_chromeProxy.Init().Wait();
var result = _chromeProxy.PublishAsync(requestFactory.CreateNetworkEnableRequest()).Result;
_chromeProxy.Subscribe<RequestWillBeSentResponse>((o, e) =>
{
Console.WriteLine("We got it!");
});
_chromeProxy.PublishAsync(requestFactory.CreateNavigateRequest("http://www.google.com")).Wait();
Console.ReadLine();
}
finally
{
if(null != _chromeProxy)
{
_chromeProxy.Dispose();
}
KillChrome(chromeProcessInfo);
}
}
private static void KillChrome(ChromeProcessInfo chromeProcessInfo)
{
try
{
chromeProcessInfo.ChromeProcess.Kill();
} catch
{
// right now, the process has been killed ... so i'm swallowing this during setup/testing
}
try
{
chromeProcessInfo.ChromeUserDirectory.Delete(true);
} catch
{
Thread.Sleep(500);
chromeProcessInfo.ChromeUserDirectory.Delete(true);
}
}
private static ChromeProcessInfo StartChrome()
{
string path = Path.GetRandomFileName();
var directoryInfo = Directory.CreateDirectory(Path.Combine(Path.GetTempPath(), path));
var remoteDebuggingArg = "--remote-debugging-port=" + ChromeRemoteDebuggingPort;
var userDirectoryArg = "--user-data-dir=\"" + directoryInfo.FullName + "\"";
var chromeProcessArgs = remoteDebuggingArg + " " + userDirectoryArg + " --bwsi --no-first-run";
var processStartInfo = new ProcessStartInfo(@"C:\Program Files (x86)\Google\Chrome\Application\chrome.exe", chromeProcessArgs);
var chromeProcess = Process.Start(processStartInfo);
return new ChromeProcessInfo
{
ChromeProcess = chromeProcess,
ChromeUserDirectory = directoryInfo
};
}
public const int ChromeRemoteDebuggingPort = 9222;
}
}