void LoadUrl(ScheduledUrlData scheduledUrlData) { const string javaScriptWaitFunction = "function wait()" + "{" + "if(window.onLoad) { return; } else { setTimeout('wait()', 100); } " + "}" + "wait();"; string targetResource = Helpers.FixUrlProtocolIfNeeded(scheduledUrlData.host + scheduledUrlData.path); m_webDriver.Navigate().GoToUrl(targetResource); IJavaScriptExecutor javaScriptExecutor = m_webDriver as IJavaScriptExecutor; javaScriptExecutor.ExecuteScript(javaScriptWaitFunction); Console.WriteLine(targetResource + " loaded"); m_dataProvider.SavePageData(PreparePageData(scheduledUrlData)); }
PageData PreparePageData(ScheduledUrlData scheduledUrlData) { PageData pageData = new PageData(); pageData.webResourceId = scheduledUrlData.webResourceId; pageData.path = scheduledUrlData.path; pageData.htmlCode = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(m_webDriver.PageSource)); ReadOnlyCollection <LogEntry> performanceLogs = m_webDriver.Manage().Logs.GetLog("performance"); foreach (LogEntry logEntry in performanceLogs) { JObject jsonObject; try { jsonObject = JObject.Parse(logEntry.Message); string methodString = (string)jsonObject["message"]["method"]; if (!methodString.StartsWith("Network.responseReceived")) { continue; } JObject paramsObject = (JObject)jsonObject["message"]["params"]; JObject response = (JObject)paramsObject["response"]; string urlString = (string)response["url"]; int remotePort = ExtractRemotePort(response); Uri loadedUrl = new Uri(urlString); UriBuilder expectedUrlBuilder = new UriBuilder(loadedUrl.Scheme, scheduledUrlData.host, remotePort, scheduledUrlData.path); if (!Helpers.CompareUrls(loadedUrl, expectedUrlBuilder.Uri)) { continue; } // TODO: here we must not to continue loop execution by throwing an exception // otherwise we lose a valid target URL information JObject headers = (JObject)response["headers"]; pageData.protocol = loadedUrl.Scheme; pageData.statusCode = (int)response["status"]; pageData.dateTime = DateTime.Now; pageData.serverResponse = headers.ToString(); break; } catch (Exception e) { Console.WriteLine(e.Message); continue; } } return(pageData); }