Guest User

Untitled

a guest
Apr 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 39.93 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Net;
  4. using System.Text;
  5. using System.Linq;
  6. using System.Drawing;
  7. using System.Threading;
  8. using System.Diagnostics;
  9. using System.Drawing.Imaging;
  10. using System.Threading.Tasks;
  11. using System.Collections.Generic;
  12. using HtmlAgilityPack;
  13. using OpenQA.Selenium.Chrome;
  14. using OpenQA.Selenium;
  15. using Newtonsoft.Json.Linq;
  16. using System.Text.RegularExpressions;
  17. using AngleSharp;
  18. using AngleSharp.Parser.Html;
  19.  
  20. namespace AMRequests
  21. {
  22. public enum TestTypes
  23. {
  24. Favicon, Tagmanager, Lifeline, Opengraph, DesktopCheck, Gzip, Sitemap, Robots, Custom404, Ssl,
  25. PageTitles, LoremIpsum, ContentSize, LinkChecker, AltTags, NetworkRequests, ConsoleCheck, TakeScreenshot,
  26. Login
  27. }
  28.  
  29. public enum RequestFlags
  30. {
  31. HEAD, GZIP, STATUSCAKE, NONE
  32. }
  33.  
  34. public class ResponseData
  35. {
  36. public string StatusCode { get; set; }
  37. public string Content { get; set; }
  38. public string ContentEncoding { get; set; }
  39.  
  40. public ResponseData(HttpWebResponse response)
  41. {
  42. StatusCode = response.GetStatusCode();
  43. Content = response.GetContent();
  44. ContentEncoding = response.ContentEncoding;
  45. response.Close();
  46. }
  47. }
  48.  
  49. public class Request
  50. {
  51. string _dictKey;
  52. public string Key { get { return _dictKey; }}
  53. public string Url { get; }
  54. public string Content { get; }
  55. public string StatusCode { get; }
  56. public string AgentID { get; }
  57. public string Encoding { get; }
  58. public RequestFlags[] Flags { get; }
  59.  
  60. public Request(string url, HttpWebResponse response, string agentid = "win10chrome", RequestFlags[] flags = null)
  61. {
  62. Url = url;
  63. Content = response.GetContent();
  64. StatusCode = response.GetStatusCode();
  65. AgentID = agentid;
  66. Encoding = response.ContentEncoding;
  67. Flags = flags ?? new RequestFlags[]{RequestFlags.NONE};
  68. _dictKey = GenerateKey(Url, AgentID, Flags);
  69. response.Close();
  70. }
  71.  
  72. public Request(string url, ResponseData responseData, string agentid = "win10chrome", RequestFlags[] flags = null)
  73. {
  74. Url = url;
  75. Content = responseData.Content;
  76. StatusCode = responseData.StatusCode;
  77. Encoding = responseData.ContentEncoding;
  78. AgentID = agentid;
  79. Flags = flags ?? new RequestFlags[] { RequestFlags.NONE };
  80. _dictKey = GenerateKey(Url, AgentID, Flags);
  81. }
  82.  
  83. public static string GenerateKey(string url, string agentid = "win10chrome", RequestFlags[] flags = null)
  84. {
  85. flags = flags ?? new RequestFlags[] { RequestFlags.NONE };
  86. string key = "";
  87.  
  88. key = "";
  89. key += url.GetType() + "||";
  90. key += url + "||";
  91. key += agentid.GetType() + "||";
  92. key += agentid + "||";
  93.  
  94. foreach (RequestFlags flag in flags)
  95. {
  96. key += flag.GetType() + "||";
  97. key += flag + "||";
  98. }
  99.  
  100. key = key.TrimEnd('|');
  101. return key;
  102. }
  103.  
  104. public Object[] DecodeKey(string key)
  105. {
  106. string[] keyStrings = key.Split("||".ToCharArray());
  107. object[] keyData = new object[keyStrings.Length / 2];
  108. int index = 0;
  109.  
  110. for (int i = 0; i < keyStrings.Length; i += 2)
  111. {
  112. if (keyStrings[i] == "System.String")
  113. {
  114. keyData[index] = keyStrings[i + 1];
  115. }
  116. else if (keyStrings[i] == "Requests_Lib.RequestFlags")
  117. {
  118. keyData[index] = (RequestFlags)Enum.Parse(typeof(RequestFlags), keyStrings[i + 1]);
  119. }
  120.  
  121. index++;
  122. }
  123.  
  124. return keyData;
  125. }
  126. }
  127.  
  128. public class RequestFactory
  129. {
  130. string _domainUrl;
  131. string[] _domainAuth;
  132. bool _authRequired;
  133. bool _cookieRequired;
  134. Queue<string> _linksToCheck;
  135. HashSet<string> _uniqueLinksSet;
  136.  
  137. public Dictionary<string, UserAgent> Agents { get; set; }
  138. public Dictionary<string, Request> RequestsList { get; set; }
  139.  
  140. public RequestFactory(string domainUrl)
  141. {
  142. Agents = new Dictionary<string, UserAgent>();
  143. RequestsList = new Dictionary<string, Request>();
  144.  
  145. _domainUrl = domainUrl;
  146. _domainAuth = GetAuth(_domainUrl);
  147. _authRequired = _domainAuth[0] != "";
  148. _cookieRequired = _domainAuth[2] != "";
  149. _linksToCheck = new Queue<string>();
  150. _uniqueLinksSet = new HashSet<string>();
  151. }
  152.  
  153. public Request GetRequest(string url, string agentID = "win10chrome", RequestFlags[] flags = null)
  154. {
  155. string key = Request.GenerateKey(url, agentID, flags);
  156.  
  157. if (RequestsList.Keys.Contains(key))
  158. {
  159. return RequestsList[key];
  160. }
  161.  
  162. int remainingAttempts = 3;
  163. ResponseData responseData = null;
  164.  
  165. while (remainingAttempts > 0)
  166. {
  167. remainingAttempts--;
  168. responseData = GetUrlResponse(url, agentID, flags);
  169.  
  170. if (responseData.StatusCode != "200")
  171. continue;
  172. }
  173.  
  174. Request request = new Request(url, responseData, agentID, flags);
  175. RequestsList.Add(key, request);
  176. return request;
  177. }
  178.  
  179. HttpWebRequest GetUrlRequest(string url, string agentID = "win10chrome")
  180. {
  181. try
  182. {
  183. HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
  184. request.UserAgent = AgentHelpers.AgentIDToAgentString(agentID);
  185.  
  186. if (_cookieRequired && url.Contains(_domainUrl))
  187. {
  188. Debug.WriteLine("********** Domain Cookie Required **********");
  189. request.Headers[HttpRequestHeader.Cookie] = _domainAuth[2] + "=" + _domainAuth[3];
  190. }
  191. else
  192. {
  193. if (_authRequired && url.Contains(_domainUrl))
  194. {
  195. Debug.WriteLine("********** Domain Auth Required **********");
  196. string creds = "Basic " + Convert.ToBase64String(Encoding.UTF8.GetBytes(_domainAuth[0] + ":" + _domainAuth[1]));
  197. request.Headers[HttpRequestHeader.Authorization] = creds;
  198. }
  199. }
  200. return request;
  201. }
  202. catch(UriFormatException)
  203. {
  204. Debug.WriteLine($"XXXXXXXXXX {url} XXXXXXXXXX");
  205. return null;
  206. }
  207. }
  208.  
  209. public ResponseData GetUrlResponse(string url, string agentID = "win10chrome", RequestFlags[] flags = null)
  210. {
  211. flags = flags ?? new RequestFlags[] { RequestFlags.NONE };
  212. HttpWebRequest req = null;
  213.  
  214. try
  215. {
  216. req = GetUrlRequest(url, agentID);
  217.  
  218. if (req == null)
  219. {
  220. Debug.WriteLine($"XXXXXXXXXX {url} XXXXXXXXXX");
  221. return null;
  222. }
  223.  
  224. req.Proxy = null;
  225.  
  226. foreach (RequestFlags flag in flags)
  227. {
  228. switch (flag)
  229. {
  230. case RequestFlags.HEAD:
  231. req.Method = "HEAD";
  232. Debug.WriteLine("********** HEAD Request Only **********");
  233. break;
  234. case RequestFlags.GZIP:
  235. req.Headers.Add(HttpRequestHeader.AcceptEncoding, "gzip, deflate");
  236. Debug.WriteLine("********** Decompression Method GZip **********");
  237. break;
  238. case RequestFlags.STATUSCAKE:
  239. req.Headers.Add("API", Config.STATUSCAKE_API_KEY);
  240. req.Headers.Add("Username", Config.STATUSCAKE_USERNAME);
  241. break;
  242. }
  243. }
  244.  
  245. HttpWebResponse response = (HttpWebResponse)req.GetResponse();
  246. return new ResponseData(response);
  247. }
  248. catch (WebException ex)
  249. {
  250. HttpWebResponse response = (HttpWebResponse)ex.Response;
  251. return new ResponseData(response);
  252. }
  253. }
  254.  
  255. List<string> GetImagesFromUrl(string url)
  256. {
  257. //var timer = Stopwatch.StartNew();
  258. List<string> imageList = new List<string>();
  259.  
  260. if (string.IsNullOrEmpty(url))
  261. {
  262. //Debug.WriteLine($"GetImagesFromUrl() execution time: {timer.Elapsed.Milliseconds.ToString()}");
  263. return imageList;
  264. }
  265.  
  266. Request request = GetRequest(url);
  267.  
  268. if (request == null)
  269. {
  270. //Debug.WriteLine($"GetImagesFromUrl() execution time: {timer.Elapsed.Milliseconds.ToString()}");
  271. return imageList;
  272. }
  273.  
  274. string content = request.Content;
  275. var doc = new HtmlDocument();
  276. doc.LoadHtml(content);
  277.  
  278. var images = doc.DocumentNode.Descendants("img")
  279. .Select(e => e.GetAttributeValue("src", null))
  280. .Where(s => !String.IsNullOrEmpty(s));
  281.  
  282. foreach (var img in images)
  283. {
  284. string src = img.FormatAsImageUrl(url);
  285.  
  286. if (String.IsNullOrEmpty(src))
  287. continue;
  288.  
  289. imageList.Add(src);
  290. }
  291.  
  292. //Debug.WriteLine($"GetImagesFromUrl() execution time: {timer.Elapsed.Milliseconds.ToString()}");
  293. return imageList;
  294. }
  295.  
  296. List<string> GetLinksFromUrl(string url)
  297. {
  298. var timer = Stopwatch.StartNew();
  299. List<string> linkList = new List<string>();
  300.  
  301. if (string.IsNullOrEmpty(url))
  302. {
  303. Debug.WriteLine($"GetLinksFromUrl() execution time: {timer.Elapsed.Milliseconds.ToString()}, for 0 links.");
  304. return linkList;
  305. }
  306.  
  307. if (Path.HasExtension(url))
  308. {
  309. if (!Path.GetExtension(url).Contains("htm") && !Path.GetExtension(url).Contains("com"))
  310. {
  311. return linkList;
  312. }
  313. }
  314.  
  315. Request request = GetRequest(url);
  316.  
  317. string content = request.Content;
  318. var doc = new HtmlDocument();
  319. doc.LoadHtml(content);
  320.  
  321. foreach (HtmlNode link in doc.DocumentNode.SelectNodes("//a[@href]"))
  322. {
  323. string src = link.GetAttributeValue("href", null);
  324.  
  325. if (String.IsNullOrEmpty(src))
  326. continue;
  327.  
  328. if (Path.HasExtension(src))
  329. {
  330. if (!Path.GetExtension(src).Contains(".htm"))
  331. linkList.Add(src.FormatAsImageUrl(url));
  332. else
  333. linkList.Add(src.FormatAsUrl(_domainUrl));
  334. }
  335. else
  336. linkList.Add(src.FormatAsUrl(_domainUrl));
  337. }
  338.  
  339. Debug.WriteLine($"GetLinksFromUrl() execution time: {timer.Elapsed.Milliseconds.ToString()}, from {url}");
  340. return linkList;
  341. }
  342.  
  343. public List<string[]> GetAltTagsFromUrl(string url)
  344. {
  345. List<string[]> results = new List<string[]>();
  346. string content = GetRequest(url).Content;
  347. var doc = new HtmlDocument();
  348. doc.LoadHtml(content);
  349. var images = doc.DocumentNode.Descendants("img");
  350.  
  351. foreach (var img in images)
  352. {
  353. string src = img.GetAttributeValue("src", null);
  354. src = src.FormatAsImageUrl(url);
  355.  
  356. if (string.IsNullOrEmpty(src))
  357. continue;
  358.  
  359. string alt = "";
  360.  
  361. if (src.Contains("tracking.gif"))
  362. alt = "(ignore) TRACKING GIF";
  363. else if (src.Contains("shim.png"))
  364. alt = "(ignore) SHIM PNG";
  365. else
  366. alt = System.Web.HttpUtility.HtmlDecode(img.GetAttributeValue("alt", null));
  367.  
  368. if (alt == null)
  369. alt = "MISSING";
  370. else if (alt == "")
  371. alt = "BLANK";
  372.  
  373. results.Add(new string[] { src, alt });
  374. }
  375.  
  376. return results;
  377. }
  378.  
  379. List<string[]> GetElementsWithText(string url, string text)
  380. {
  381. List<string[]> elements = new List<string[]>();
  382.  
  383. if (string.IsNullOrEmpty(url))
  384. return elements;
  385.  
  386. Request request = GetRequest(url);
  387.  
  388. if (request == null)
  389. return elements;
  390.  
  391. string content = request.Content;
  392. var doc = new HtmlDocument();
  393. doc.LoadHtml(content);
  394.  
  395. var links = doc.DocumentNode.Descendants()
  396. .Where(n => n.Attributes.Any(a => a.Value.Contains(text)));
  397.  
  398. foreach (HtmlNode link in links)
  399. {
  400. elements.Add(new string[] { url, link.InnerHtml });
  401. }
  402.  
  403. return elements;
  404. }
  405.  
  406. List<string> GetLinksFromRequest(Request request)
  407. {
  408. List<string> linkList = new List<string>();
  409. string content = GetRequest(request.Url).Content;
  410. var doc = new HtmlDocument();
  411. doc.LoadHtml(content);
  412.  
  413. var links = doc.DocumentNode.Descendants("a")
  414. .Select(e => e.GetAttributeValue("href", null))
  415. .Where(s => !String.IsNullOrEmpty(s));
  416.  
  417. foreach (var lnk in links)
  418. {
  419. string src = lnk.FormatAsUrl(_domainUrl);
  420.  
  421. if (String.IsNullOrEmpty(src))
  422. continue;
  423.  
  424. linkList.Add(src);
  425. }
  426.  
  427. return linkList;
  428. }
  429.  
  430. public void SetDefaultConnectionLimit(int minLimit)
  431. {
  432. if (ServicePointManager.DefaultConnectionLimit < minLimit)
  433. ServicePointManager.DefaultConnectionLimit = minLimit;
  434.  
  435. ServicePointManager.Expect100Continue = false;
  436. }
  437.  
  438. public Dictionary<string, List<string>> CrawlSite(string domainURL)
  439. {
  440. Stopwatch timer = Stopwatch.StartNew();
  441. SetDefaultConnectionLimit(100);
  442.  
  443. Dictionary<string, List<string>> _toReturn = new Dictionary<string, List<string>>();
  444. _linksToCheck.Enqueue(domainURL);
  445. _uniqueLinksSet.Add(domainURL);
  446. int requestsMade = 0;
  447. var options = new ParallelOptions { MaxDegreeOfParallelism = 10 };
  448.  
  449. while (_linksToCheck.Any())
  450. {
  451. Debug.WriteLine($"{_linksToCheck.Count} links to check, {requestsMade} requests made, {_uniqueLinksSet.Count} unique links found.");
  452.  
  453. var p = _linksToCheck.Dequeue();
  454.  
  455. List<string> urls = GetLinksFromUrl(p);
  456. requestsMade++;
  457.  
  458. string code = GetRequest(p).StatusCode;
  459.  
  460. if (!_toReturn.ContainsKey(code))
  461. {
  462. _toReturn.Add(code, new List<string>());
  463. }
  464.  
  465. _toReturn[code].Add(p);
  466.  
  467. Debug.WriteLine($"{_linksToCheck.Count} links to check, {requestsMade} requests made, {_uniqueLinksSet.Count} unique links found.");
  468.  
  469. if (urls != null && urls.Count != 0)
  470. foreach (string url in urls)
  471. if (_uniqueLinksSet.Add(url))
  472. if (url.Contains(domainURL))
  473. _linksToCheck.Enqueue(url);
  474.  
  475. var images = GetImagesFromUrl(p);
  476.  
  477. if (images != null && images.Count > 0)
  478. foreach (string image in images)
  479. _uniqueLinksSet.Add(image);
  480. }
  481. //while (_linksToCheck.Any())
  482. //{
  483. // //Thread.Sleep(50);
  484.  
  485. // Debug.WriteLine($"{_linksToCheck.Count} links to check, {requestsMade} requests made, {_uniqueLinksSet.Count} unique links found.");
  486.  
  487. // var pages = _linksToCheck.ToList();
  488. // _linksToCheck.Clear();
  489.  
  490. // Parallel.ForEach(pages, options, p =>
  491. // {
  492. // List<string> urls = new List<string>();
  493.  
  494. // urls = GetLinksFromUrl(p);
  495. // requestsMade++;
  496.  
  497. // string code = GetRequest(p).StatusCode;
  498.  
  499. // if (!_toReturn.ContainsKey(code))
  500. // {
  501. // _toReturn.Add(code, new List<string>());
  502. // }
  503.  
  504. // _toReturn[code].Add(p);
  505.  
  506. // Debug.WriteLine($"{_linksToCheck.Count} links to check, {requestsMade} requests made, {_uniqueLinksSet.Count} unique links found.");
  507.  
  508. // if (urls != null && urls.Count != 0)
  509. // foreach (string url in urls)
  510. // if (_uniqueLinksSet.Add(url))
  511. // if (url.Contains(domainURL))
  512. // _linksToCheck.Add(url);
  513.  
  514. // var images = GetImagesFromUrl(p);
  515.  
  516. // if (images != null && images.Count > 0)
  517. // foreach (string image in images)
  518. // _uniqueLinksSet.Add(image);
  519. // });
  520. //}
  521.  
  522. Debug.WriteLine($"{_linksToCheck.Count} links to check, {requestsMade} requests made, {_uniqueLinksSet.Count} unique links found.");
  523. Debug.WriteLine($"CrawlSite() execution time: {timer.Elapsed.Milliseconds.ToString()}");
  524. return _toReturn;
  525. }
  526.  
  527. public Tuple<string, string> TakeScreenshot(string url = "", bool clearScreenshotDir = true, bool quitAfterSS = true, bool appendUrl=false)
  528. {
  529. BrowserDriver driver = BrowserDriver.NewDriver();
  530. return driver.TakeScreenshot(url, clearScreenshotDir, quitAfterSS, appendUrl);
  531. }
  532.  
  533. public string GetPageTitle(string url, bool appendUrl=false)
  534. {
  535. string content = GetRequest(url).Content;
  536. int index = content.GetLastCharIndex("<title>");
  537. int length = content.GetIndexOf("</title>") - index;
  538. string substring = content.Substring(index, length);
  539.  
  540. if (appendUrl)
  541. return $"{substring} ({url})";
  542.  
  543. return substring;
  544. }
  545.  
  546. public static string[] GetAuth(string url)
  547. {
  548. string username = "";
  549. string password = "";
  550. string cookieName = "";
  551. string cookieValue = "";
  552.  
  553. foreach (var key in Config.AUTH.Keys)
  554. {
  555. if (url.Contains(key))
  556. {
  557. username = Config.AUTH[key][0];
  558. password = Config.AUTH[key][1];
  559.  
  560. foreach (var ckey in Config.COOKIES.Keys)
  561. {
  562. if (url.Contains(ckey))
  563. {
  564. cookieName = Config.COOKIES[ckey][0];
  565. cookieValue = Config.COOKIES[ckey][1];
  566. break;
  567. }
  568. }
  569.  
  570. break;
  571. }
  572. }
  573.  
  574. return new string[] { username, password, cookieName, cookieValue };
  575. }
  576. }
  577.  
  578. public class BrowserDriver : ChromeDriver
  579. {
  580.  
  581. public string ScreenshotPath { get; private set; }
  582.  
  583. public static BrowserDriver NewDriver(bool headless = true, bool startMaximized = true, bool logPerformanceAll = false, bool logBrowserAll = false)
  584. {
  585. ChromeOptions _options;
  586. ChromePerformanceLoggingPreferences _logPrefs;
  587. BrowserDriver driver;
  588.  
  589. string chromeDriverPath = Environment.CurrentDirectory;
  590. chromeDriverPath.TrimEnd('/');
  591.  
  592. if (!chromeDriverPath.EndsWith("Requests"))
  593. {
  594. int lastIndex = chromeDriverPath.LastIndexOf('/');
  595. chromeDriverPath = chromeDriverPath.Substring(0, lastIndex);
  596. chromeDriverPath += "/Requests";
  597. }
  598.  
  599. chromeDriverPath += "/bin/Debug/netstandard2.0/";
  600.  
  601.  
  602. if (headless || startMaximized || logPerformanceAll || logBrowserAll)
  603. {
  604. _options = new ChromeOptions();
  605. _options.AddArgument("--user-agent=" + AgentHelpers.AgentIDToAgentString("win10chrome"));
  606. _options.AddArgument("--window-size=1920,1080");
  607.  
  608. if (headless)
  609. _options.AddArgument("--headless");
  610. if (startMaximized)
  611. _options.AddArgument("--start-maximized");
  612. if (logPerformanceAll)
  613. {
  614. _logPrefs = new ChromePerformanceLoggingPreferences();
  615. _logPrefs.AddTracingCategories(new string[] { "devtools.timeline" });
  616. _options.PerformanceLoggingPreferences = _logPrefs;
  617. _options.SetLoggingPreference("performance", LogLevel.All);
  618. }
  619. if (logBrowserAll)
  620. {
  621. _options.SetLoggingPreference(LogType.Browser, LogLevel.All);
  622. }
  623. driver = new BrowserDriver(chromeDriverPath, _options);
  624. }
  625. else
  626. {
  627. driver = new BrowserDriver();
  628. }
  629.  
  630. return driver;
  631. }
  632.  
  633. public BrowserDriver() { }
  634. public BrowserDriver(string chromeDriverDirectory, ChromeOptions options) : base(chromeDriverDirectory, options) { }
  635.  
  636. ~BrowserDriver()
  637. {
  638. Quit();
  639. }
  640.  
  641. public void GoToUrl(string url)
  642. {
  643. var auth = RequestFactory.GetAuth(url);
  644.  
  645. if (auth[0] == "" && auth[2] == "")
  646. {
  647. Navigate().GoToUrl(url);
  648. return;
  649. }
  650.  
  651. if (auth[2] != "")
  652. {
  653. // cookie auth
  654. DateTime time = DateTime.Now;
  655. time.AddDays(10);
  656. OpenQA.Selenium.Cookie ck = new OpenQA.Selenium.Cookie(auth[2], auth[3], url.GetDomain(), "/", time);
  657. Navigate().GoToUrl(url);
  658. Manage().Cookies.AddCookie(ck);
  659. Navigate().GoToUrl(url);
  660. return;
  661. }
  662. if (auth[0] != "")
  663. {
  664. // username/password auth
  665. Navigate().GoToUrl(url.InsertCredentials(auth[0], auth[1]));
  666. return;
  667. }
  668. }
  669.  
  670. public void ClearScreenshots()
  671. {
  672. DirectoryInfo di = new DirectoryInfo(Directory.GetCurrentDirectory() + "/wwwroot/screenshots");
  673.  
  674. foreach (FileInfo file in di.GetFiles())
  675. file.Delete();
  676. }
  677.  
  678. public Tuple<string, string> TakeScreenshot(string url = "", bool clearScreenshotDir = true, bool quitAfterSS = true, bool appendUrl = false)
  679. {
  680. bool overrideMarqueeHeight = url.Contains("www.am.com") || url.Contains("am-stage") || url.Contains("ogemec-dev") || url.Contains("ogetogether");
  681.  
  682. if (url != "")
  683. {
  684. GoToUrl(url);
  685. Thread.Sleep(2000);
  686. }
  687.  
  688. if (clearScreenshotDir)
  689. ClearScreenshots();
  690.  
  691. string FileName = "screenshot_" + Guid.NewGuid().ToString() + ".png";
  692. ScreenshotPath = Directory.GetCurrentDirectory() + "/wwwroot/screenshots/" + FileName;
  693. int oldHeight = Manage().Window.Size.Height;
  694.  
  695. // HACK: If we're on am.com, we need to stop the header from dynamically resizing
  696. if (overrideMarqueeHeight)
  697. {
  698. try
  699. {
  700. IWebElement element = FindElementByClassName("MarqueePartial");
  701. string pixelHeight = element.GetCssValue("height");
  702. IJavaScriptExecutor js = this as IJavaScriptExecutor;
  703. js.ExecuteScript("$('head').append('<style> .MarqueePartial { height: " + pixelHeight + " !important; }</style>');");
  704. }
  705. catch (NoSuchElementException) { }
  706.  
  707. try
  708. {
  709. IWebElement bgImage = FindElementByClassName("fixedBackgroundImage");
  710. string pixelHeight = bgImage.GetCssValue("height");
  711. IJavaScriptExecutor js = this as IJavaScriptExecutor;
  712. js.ExecuteScript("$('head').append('<style> .fixedBackgroundImage { height: " + pixelHeight + " !important; }</style>');");
  713. }
  714. catch (NoSuchElementException) { }
  715. }
  716.  
  717. // Get the full page height of the website.
  718. int scrollHeight = (int)(long)ExecuteScript("return document.body.scrollHeight");
  719. int siteWidth = Manage().Window.Size.Width;
  720.  
  721. // Set the new browser dimensions and maximizethe window.
  722. Manage().Window.Size = new Size(siteWidth, scrollHeight);
  723. Manage().Window.Maximize();
  724. Console.WriteLine(Manage().Window.Size.Height.ToString());
  725.  
  726. byte[] bitmap = ((ITakesScreenshot)this).GetScreenshot().AsByteArray;
  727.  
  728. using (Image image = Image.FromStream(new MemoryStream(bitmap)))
  729. {
  730. image.Save(ScreenshotPath, ImageFormat.Png);
  731. }
  732.  
  733. string pageTitle = Title;
  734.  
  735. if (quitAfterSS)
  736. Quit();
  737. else
  738. {
  739. // Return it back to normal after the screenshot.
  740. Manage().Window.Size = new Size(siteWidth, oldHeight);
  741. Manage().Window.Maximize();
  742. }
  743.  
  744. if (appendUrl)
  745. return Tuple.Create($"{pageTitle} ({url})", "/screenshots/" + FileName);
  746.  
  747. return Tuple.Create(pageTitle, "/screenshots/" + FileName);
  748. }
  749.  
  750. public HashSet<LogEntry> GetPerformanceLogs() => new HashSet<LogEntry>(Manage().Logs.GetLog("performance"));
  751. }
  752.  
  753. public class UserAgent
  754. {
  755. public string AgentID { get; set; }
  756. public string AgentString { get; set; }
  757. public int ScreenWidth { get; set; }
  758. public int ScreenHeight { get; set; }
  759.  
  760. public UserAgent(string agentID = "win10chrome", int width = 1920, int height = 1080)
  761. {
  762. AgentID = agentID;
  763. AgentString = AgentHelpers.AgentIDToAgentString(agentID);
  764. ScreenWidth = width;
  765. ScreenHeight = height;
  766. }
  767. }
  768.  
  769. public static class Extensions
  770. {
  771. public static string StripTrailingSlashes(this string url) => url.TrimEnd('/');
  772.  
  773. public static string StripExtension(this string url)
  774. {
  775. if (Path.HasExtension(url))
  776. {
  777. return url.Substring(0, url.LastIndexOf('/') + 1);
  778. }
  779.  
  780. return url;
  781. }
  782.  
  783. public static string GetDomain(this string url)
  784. {
  785. url = url.StripTrailingSlashes();
  786. url = url.TrimStart("https://www.".ToCharArray());
  787. url = url.TrimStart("http://www.".ToCharArray());
  788. url = url.TrimStart("https://".ToCharArray());
  789. url = url.TrimStart("http://".ToCharArray());
  790.  
  791. if (url.IndexOf('.') != url.LastIndexOf('.'))
  792. {
  793. url = url.Substring(url.IndexOf('.') + 1);
  794. }
  795.  
  796. return url;
  797. }
  798.  
  799. public static int GetIndexOf(this string content, string substring)
  800. {
  801. return content.IndexOf(substring, StringComparison.CurrentCulture);
  802. }
  803.  
  804. public static int GetIndexOf(this string content, string substring, int index)
  805. {
  806. return content.IndexOf(substring, index, StringComparison.CurrentCulture);
  807. }
  808.  
  809. public static int GetLastCharIndex(this string content, string substring)
  810. {
  811. return content.IndexOf(substring, StringComparison.CurrentCulture) + substring.Length;
  812. }
  813.  
  814. public static string InsertCredentials(this string data, string username, string password)
  815. {
  816. string temp = data;
  817.  
  818. if (temp.StartsWith("https://"))
  819. {
  820. return temp.Insert("https://".Length, username + ":" + password + "@");
  821. }
  822. if (temp.StartsWith("http://"))
  823. {
  824. return temp.Insert("http://".Length, username + ":" + password + "@");
  825. }
  826.  
  827. return data;
  828. }
  829.  
  830. public static string FormatAsImageUrl(this string src, string url)
  831. {
  832. if (src.StartsWith("http"))
  833. {
  834. return src;
  835. }
  836.  
  837. string imgSrc = src;
  838. string addy = url.StripExtension();
  839.  
  840. if (!addy.EndsWith("/") && !imgSrc.StartsWith("/"))
  841. imgSrc = "/" + imgSrc;
  842.  
  843. string imgUrl = addy + imgSrc;
  844.  
  845. return imgUrl;
  846. }
  847.  
  848. public static string FormatAsUrl(this string url, string domainUrl)
  849. {
  850. if (url.StartsWith("javascript"))
  851. return "";
  852. if (url.StartsWith("tel:"))
  853. return "";
  854. if (url.StartsWith("mailto:"))
  855. return "";
  856. if (url.StartsWith("#"))
  857. return "";
  858. if (url.Contains("sitecore"))
  859. return "";
  860. if (url.StartsWith("http"))
  861. return url.StripTrailingSlashes();
  862.  
  863. url = url.TrimEnd(new char[] { '/' });
  864.  
  865. if (url.StartsWith("//"))
  866. {
  867. return "https:" + url;
  868. }
  869. if (url.StartsWith("/"))
  870. {
  871. return domainUrl + url;
  872. }
  873.  
  874. return url;
  875. }
  876.  
  877. //public static (string key, string value) FormatUrl(this string link, string url)
  878. //{
  879. // link = link.StripTrailingSlashes();
  880.  
  881. // if (Path.HasExtension(link))
  882. // {
  883.  
  884. // }
  885. // else
  886. // {
  887. // if (url.StartsWith("javascript"))
  888. // return ("", "");
  889. // if (url.StartsWith("tel:"))
  890. // return ("", "");
  891. // if (url.StartsWith("mailto:"))
  892. // return ("", "");
  893. // if (url.StartsWith("#"))
  894. // return ("", "");
  895. // if (url.Contains("sitecore"))
  896. // return ("", "");
  897. // if (url.StartsWith("//"))
  898. // {
  899. // return ("link", "https:" + url);
  900. // }
  901. // if (url.StartsWith("/"))
  902. // {
  903. // return ("link", url + link);
  904. // }
  905.  
  906. // return ("link", link);
  907. // }
  908. //}
  909.  
  910. public static string GetContent(this HttpWebResponse response)
  911. {
  912. Stream data = response.GetResponseStream();
  913. string content = String.Empty;
  914.  
  915. using (StreamReader sr = new StreamReader(data))
  916. {
  917. content = sr.ReadToEnd();
  918. }
  919.  
  920. return content;
  921. }
  922.  
  923. public static string GetStatusCode(this HttpWebResponse response)
  924. {
  925. int statusCode = 0;
  926.  
  927. if (response != null)
  928. {
  929. statusCode = (int)response.StatusCode;
  930. }
  931. else
  932. {
  933. statusCode = (int)HttpStatusCode.NotFound;
  934. }
  935.  
  936. return statusCode.ToString();
  937. }
  938.  
  939. public static string ToStringList(this List<string> list, string separator)
  940. {
  941. var str = string.Join(separator, list.Select(s => AgentHelpers.FormatAgentID(s)));
  942. return "(" + str + ")";
  943. }
  944.  
  945. public static List<string> GetTwitterCardInfo(this string content)
  946. {
  947. List<string> results = new List<string>
  948. {
  949. "Card: ", "Title: ", "Description: ", "Image: ", "Site: ", "Creator: "
  950. };
  951.  
  952. var doc = new HtmlDocument();
  953. doc.LoadHtml(content);
  954.  
  955. var elements = doc.DocumentNode.Descendants("meta")
  956. .Where(s => !String.IsNullOrEmpty(s.GetAttributeValue("name", null))
  957. && s.GetAttributeValue("name", null).ToLower().StartsWith(""));
  958.  
  959. foreach (var item in elements)
  960. {
  961. string text = "";
  962. string temp = "";
  963.  
  964. if (item.GetAttributeValue("name", null).Contains("card"))
  965. {
  966. temp = item.GetAttributeValue("content", null);
  967. text = temp == "" ? text = "missing" : text = temp;
  968. results[0] += System.Web.HttpUtility.HtmlDecode(text);
  969. continue;
  970. }
  971. if (item.GetAttributeValue("name", null).Contains("title"))
  972. {
  973. temp = item.GetAttributeValue("content", null);
  974. text = temp == "" ? text = "missing" : text = temp;
  975. results[1] += System.Web.HttpUtility.HtmlDecode(text);
  976. continue;
  977. }
  978. if (item.GetAttributeValue("name", null).Contains("description"))
  979. {
  980. temp = item.GetAttributeValue("content", null);
  981. text = temp == "" ? text = "missing" : text = temp;
  982. results[2] += System.Web.HttpUtility.HtmlDecode(text);
  983. continue;
  984. }
  985. if (item.GetAttributeValue("name", null).Contains("image"))
  986. {
  987. temp = item.GetAttributeValue("content", null);
  988. text = temp == "" ? text = "missing" : text = temp;
  989. results[3] += System.Web.HttpUtility.HtmlDecode(text);
  990. continue;
  991. }
  992. if (item.GetAttributeValue("name", null).Contains("site"))
  993. {
  994. temp = item.GetAttributeValue("content", null);
  995. text = temp == "" ? text = "missing" : text = temp;
  996. results[4] += System.Web.HttpUtility.HtmlDecode(text);
  997. continue;
  998. }
  999. if (item.GetAttributeValue("name", null).Contains("creator"))
  1000. {
  1001. temp = item.GetAttributeValue("content", null);
  1002. text = temp == "" ? text = "missing" : text = temp;
  1003. results[5] += System.Web.HttpUtility.HtmlDecode(text);
  1004. continue;
  1005. }
  1006. }
  1007.  
  1008. return results;
  1009. }
  1010. }
  1011.  
  1012. public static class AgentHelpers
  1013. {
  1014. public static string FormatAgentID(string agent)
  1015. {
  1016. if (agent == "osxChrome")
  1017. return "OSX Chrome";
  1018. if (agent == "osxFirefox")
  1019. return "OSX Firefox";
  1020. if (agent == "osxSafari")
  1021. return "OSX Safari";
  1022. if (agent == "win7ie9")
  1023. return "Win7 IE9";
  1024. if (agent == "win7ie10")
  1025. return "Win7 IE10";
  1026. if (agent == "win10ie11")
  1027. return "Win10 IE10";
  1028. if (agent == "win10edge")
  1029. return "Win10 Edge";
  1030. if (agent == "win10chrome")
  1031. return "Win10 Chrome";
  1032. if (agent == "win10ff")
  1033. return "Win10 Firefox";
  1034. if (agent == "iphone")
  1035. return "iPhone 6";
  1036. if (agent == "ipad")
  1037. return "iPad Mini 2";
  1038. if (agent == "android")
  1039. return "Galaxy Tab 4";
  1040.  
  1041. return "Undefined Agent";
  1042. }
  1043.  
  1044. public static string AgentIDToAgentString(string agentID)
  1045. {
  1046. switch (agentID)
  1047. {
  1048. case "osxChrome":
  1049. return "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36";
  1050. case "osxFirefox":
  1051. return "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:49.0) Gecko/20100101 Firefox/49.0";
  1052. case "osxSafari":
  1053. return "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0.1 Safari/602.2.14";
  1054. case "win10chrome":
  1055. return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36";
  1056. case "win10ff":
  1057. return "Mozilla/5.0 (Windows NT 10.0; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0";
  1058. case "win10edge":
  1059. return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2486.0 Safari/537.36 Edge/13.10586";
  1060. case "win10ie11":
  1061. return "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko";
  1062. case "win7ie10":
  1063. return "Mozilla / 5.0(compatible; MSIE 10.0; Windows NT 6.1; Trident / 6.0)";
  1064. case "win7ie9":
  1065. return "Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; Trident/5.0)";
  1066. case "iphone":
  1067. return "Mozilla/5.0 (iPhone; CPU iPhone OS 10_1_1 like Mac OS X) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0 Mobile/14B100 Safari/602.1";
  1068. case "ipad":
  1069. return "Mozilla/5.0 (iPad; CPU OS 10_1_1 like Mac OS X) AppleWebKit/602.2.14 (KHTML, like Gecko) Version/10.0 Mobile/14B100 Safari/602.1";
  1070. case "android":
  1071. return "Mozilla/5.0 (Linux; Android 5.1.1; SM-T330NU Build/LMY47X) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.85 Safari/537.36";
  1072. default:
  1073. return "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36";
  1074. }
  1075. }
  1076.  
  1077. public class UserAgent
  1078. {
  1079. public string AgentString { get; set; }
  1080. public int ScreenWidth { get; set; }
  1081. public int ScreenHeight { get; set; }
  1082.  
  1083. public UserAgent(string agentString, int width, int height)
  1084. {
  1085. AgentString = agentString;
  1086. ScreenWidth = width;
  1087. ScreenHeight = height;
  1088. }
  1089. }
  1090.  
  1091. public static string GetElementFromJson(this string data, string element)
  1092. {
  1093. var item = JToken.Parse(data)[element];
  1094.  
  1095. if (item == null)
  1096. return null;
  1097.  
  1098. return JToken.Parse(data)[element].Value<string>();
  1099. }
  1100. }
  1101.  
  1102. public class MissingCaseException : Exception
  1103. {
  1104. public MissingCaseException(){}
  1105. public MissingCaseException(string message) : base(message){}
  1106. public MissingCaseException(string message, Exception inner) : base(message, inner){}
  1107. }
  1108. }
Add Comment
Please, Sign In to add comment