Last active
December 21, 2015 20:58
-
-
Save hadoan/6364725 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Server server = null; | |
Client client = null; | |
FirefoxDriver driver = null; | |
try | |
{ | |
// Supply the path to the Browsermob Proxy batch file | |
server = new Server(ConfigurationManager.AppSettings["BrowserMobBatFile"], 3333); | |
server.Start(); | |
client = server.CreateProxy(); | |
client.RemapHost("host", "ip address");//Remap host file in Windows Hosts | |
var seleniumProxy = new Proxy { HttpProxy = client.SeleniumProxy }; | |
var profile = new FirefoxProfile(); | |
profile.SetProxyPreferences(seleniumProxy); | |
driver = new FirefoxDriver(profile); | |
HarResult harData = null; | |
var siteUrls = | |
new List<string>( | |
ConfigurationManager.AppSettings["SiteUrls"].Split(';') | |
.Select( | |
x => | |
x.Replace("\r", "").Replace("\n", "").Trim())); | |
var reports = new HashSet<ReportInfo>(); | |
foreach (var siteUrl in siteUrls) | |
{ | |
harData = null; | |
client.NewHar("siteUrl" + Guid.NewGuid()); | |
DateTime date = DateTime.Now; | |
driver.Navigate().GoToUrl(siteUrl); | |
harData = client.GetHar(); | |
AutomatedTester.BrowserMob.HAR.Log log = harData.Log; | |
AutomatedTester.BrowserMob.HAR.Entry[] entries = log.Entries; | |
//int totalTime = 0; | |
if (entries == null || entries.Length == 0) | |
continue; | |
int i = 1; | |
foreach (var entry in entries) | |
{ | |
//if (entry.Request.Url.EndsWith(".aspx") || entry.Request.Url.EndsWith(".ico")) continue; | |
AutomatedTester.BrowserMob.HAR.Request request = entry.Request; | |
var url = request.Url; | |
var time = entry.Time; | |
Debug.WriteLine(i + " Url: " + url + " - Time: " + entry.Timings.Blocked + " " + | |
entry.Timings.Dns + | |
" " + entry.Timings.Connect + " " + entry.Timings.Send + " " + | |
entry.Timings.Wait + | |
" " + entry.Timings.Receive); | |
i++; | |
//totalTime += entry.Time; | |
} | |
var minStartDate = entries.Min(x => x.StartedDateTime); | |
var maxEndDate = entries.Max(x => x.StartedDateTime.AddMilliseconds(x.Time)); | |
var totalTime = (maxEndDate - minStartDate).TotalMilliseconds; | |
Debug.WriteLine("Total time: " + totalTime); | |
var report = new ReportInfo | |
{ | |
SiteName = siteUrl, | |
LoadTime = (int)totalTime, | |
}; | |
reports.Add(report); | |
} | |
Utility.ExportCsv(reports); | |
Console.ReadKey(); | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex); | |
} | |
finally | |
{ | |
if (driver != null) driver.Quit(); | |
if (client != null) client.Close(); | |
if (server != null) server.Stop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment