public static Boolean IsWebSiteAvailable(String Url) { StatusExtension.Initialize(); HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(Url); request.Credentials = System.Net.CredentialCache.DefaultCredentials; request.Method = "GET"; HttpWebResponse response = null; HttpStatusCode code; try { using (response = (HttpWebResponse)request.GetResponse()) { code = response.StatusCode; } } catch (WebException ex) { response = ex.Response as HttpWebResponse; code = response.StatusCode; StatusExtension.ErrorMessage = ex.Message; } return(code == HttpStatusCode.OK); }
public static Boolean Click(this ChromeDriver driver, String xPath, String key = "", String keyToFind = "") { StatusExtension.Initialize(); Boolean success = false; if (String.IsNullOrEmpty(key) || driver.PageSource.ToLower().Contains(key)) { try { String currentUrl = driver.Url; IReadOnlyCollection <IWebElement> elements = driver.FindElements(By.XPath(xPath)); foreach (IWebElement element in elements) { if (element.Enabled && element.Displayed) { element.Click(); } else { ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", element); } if (String.IsNullOrEmpty(keyToFind) || driver.PageSource.ToLower().Contains(keyToFind)) { success = true; break; } } } catch (Exception) { } } return(success); }
public static List <T> Serialize <T>(this DataTable dataTable) { StatusExtension.Initialize(); List <T> list = new List <T>(); foreach (DataRow row in dataTable.Rows) { list.Add(row.Serialize <T>(true)); } return(list); }
public static Int32 GetGoogleSearchCurrenPage(this ChromeDriver driver) { Int32 page = 1; IWebElement element = null; StatusExtension.Initialize(); if (driver.GetElement("//td[@class='cur']", ref element)) { page = element.Text.Replace("\"", "").ConvertToInt32(); } return(page); }
public static IWebElement GetElement(this ChromeDriver driver, String xPath) { StatusExtension.Initialize(); IWebElement element = null; try { element = driver.FindElement(By.XPath(xPath)); } catch (Exception) { } return(element); }
public static Boolean IsWebSiteAvailable(this String url) { StatusExtension.Initialize(); Boolean success = false; if (url.IsValidUrl()) { try { HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); request.Credentials = System.Net.CredentialCache.DefaultCredentials; request.Method = "GET"; request.Timeout = 30 * 1000; HttpWebResponse response = null; HttpStatusCode code; try { using (response = (HttpWebResponse)request.GetResponse()) { code = response.StatusCode; success = true; //var stream = response.GetResponseStream(); //var sr = new StreamReader(stream); //var content = sr.ReadToEnd(); //System.IO.File.WriteAllText(@"C:\AndrewHuang\Development\Visual Studio 2015\Projects\QwayInc\TestConsoleApplication\TestFiles\Contact.html", content); } } catch (WebException ex) { if (ex.Response != null) { response = ex.Response as HttpWebResponse; code = response.StatusCode; StatusExtension.ErrorMessage = String.Format("[{0}] {1}", code, ex.Message); success = code == HttpStatusCode.OK; } else { StatusExtension.ErrorMessage = ex.Message; } } } catch (Exception ex) { StatusExtension.ErrorMessage = ex.Message; } } else { StatusExtension.ErrorMessage = $"Url is not valid."; } return(success); }
public static Boolean FindElement(this ChromeDriver driver, String xPath, ref IWebElement element) { Boolean success = false; StatusExtension.Initialize(); try { element = driver.FindElement(By.XPath(xPath)); success = element != null; } catch (Exception ex) { StatusExtension.ErrorMessage = ex.Message; } return(success); }
public static Boolean GetGoogleNextPage(this ChromeDriver driver) { Boolean success = false; StatusExtension.Initialize(); try { IWebElement element = driver.FindElementById("pnnext"); element.Click(); success = true; } catch (Exception ex) { StatusExtension.ErrorMessage = ex.Message; } return(success); }
public static String DownloadString(this String url, ChromeDriver driver = null, KeyValuePair <String, String> xPathToCheck = new KeyValuePair <String, String>(), Int32 secondsWait = 2) { StatusExtension.Initialize(); String html = ""; if (url.IsValidUrl()) { try { if (driver == null) { using (WebClient client = new WebClient()) html = client.DownloadString(url); } else if (url.IsWebSiteAvailable()) { driver.Navigate().GoToUrl(url); IJavaScriptExecutor js = (IJavaScriptExecutor)driver; js.ExecuteScript("window.scrollTo(0, 100)"); System.Threading.Thread.Sleep(secondsWait * 1000); html = driver.PageSource; } else { StatusExtension.ErrorMessage = $"Url not available.\n{StatusExtension.ErrorMessage}"; } if (!String.IsNullOrEmpty(html) && !String.IsNullOrEmpty(xPathToCheck.Key)) { HtmlAgilityPack.HtmlDocument htmlDocument = new HtmlAgilityPack.HtmlDocument(); htmlDocument.LoadHtml(html); if (htmlDocument.DocumentNode.SelectSingleNode(xPathToCheck.Key) != null) { StatusExtension.ErrorMessage = String.IsNullOrEmpty(xPathToCheck.Value) ? "Not Available" : htmlDocument.GetValue(xPathToCheck.Value); html = String.Empty; } } } catch (Exception ex) { StatusExtension.ErrorMessage = ex.Message; } } else { StatusExtension.ErrorMessage = $"Url not valid."; } return(html); }
public static Boolean GetGoogleSearches(this ChromeDriver driver, String keyword) { Boolean success = false; StatusExtension.Initialize(); IWebElement element = null; try { driver.Navigate().GoToUrl("https://www.google.ca"); element = driver.FindElement(By.Id("lst-ib")); element.SendKeys(keyword); element.SendKeys(Keys.Return); success = !driver.GetElement("//form[@id='captcha-form']", ref element, "captcha-form"); } catch (Exception) { } return(success); }
public static Dictionary <String, String> GetValues(this HtmlDocument htmlDocument, String xPath, String xPathKey, String xPathValue, String attribute = "") { StatusExtension.Initialize(); Dictionary <String, String> dic = new Dictionary <String, String>(); try { HtmlNodeCollection nodes = htmlDocument.DocumentNode.SelectNodes(xPath); if (nodes != null) { foreach (HtmlNode node in nodes) { dic[node.GetValue(xPathKey, attribute)] = node.GetValue(xPathValue, attribute); } } } catch (Exception) { } return(dic); }
public static System.Reflection.MethodBase GetMethodBase(Int32 stackIndex) { StatusExtension.Initialize(); System.Reflection.MethodBase method = null; try { StackTrace stackTrace = new StackTrace(); StackFrame stackFrame = stackTrace.GetFrame(stackIndex); if (stackFrame == null && stackIndex > 0) { stackFrame = stackTrace.GetFrame(stackIndex - 1); } if (stackFrame != null) { method = stackFrame.GetMethod(); } } catch (Exception ex) { StatusExtension.ErrorMessage = ex.Message; } return(method); }
public static Dictionary <String, String> GetValues2(this HtmlDocument htmlDocument, String xPath, String xPathKey, String xPathValue, String attribute = "") { StatusExtension.Initialize(); Dictionary <String, String> dic = new Dictionary <String, String>(); try { HtmlNode node = htmlDocument.DocumentNode.SelectSingleNode(xPath); if (node != null) { HtmlNodeCollection nodesKey = htmlDocument.DocumentNode.SelectNodes(xPathKey); HtmlNodeCollection nodesValue = htmlDocument.DocumentNode.SelectNodes(xPathValue); for (Int32 index = 0; index < Math.Min(nodesKey.Count, nodesValue.Count); ++index) { dic[nodesKey[index].getValue(attribute)] = nodesValue[index].getValue(attribute); } } } catch (Exception) { } return(dic); }
public static T Serialize <T>(this DataRow row, Boolean isFromDataTable = false) { if (!isFromDataTable) { StatusExtension.Initialize(); } Type type = typeof(T); PropertyInfo[] properties = type.GetProperties(); T item = (T)Activator.CreateInstance(typeof(T), new object[] { }); foreach (DataColumn col in row.Table.Columns) { PropertyInfo propertyInfo = properties.Where(p => p.Name.ToLower() == col.ColumnName.ToLower()).FirstOrDefault(); if (propertyInfo != null) { setProperty <T>(ref item, propertyInfo, row[col]); } } return(item); }
public static Dictionary <String, String> GetValuesFromJson(this String jsonString) { StatusExtension.Initialize(); Dictionary <String, String> dic = new Dictionary <String, String>(); try { jsonString = convertToJsonString(jsonString); Dictionary <String, String> dicJson = JsonConvert.DeserializeObject <Dictionary <String, String> >(jsonString); foreach (String key in dicJson.Keys) { dic[key] = dicJson[key]; foreach (String k in _ValueMappingReverse.Keys) { dic[key] = dic[key].Replace(k, _ValueMappingReverse[k]); } } } catch (Exception ex) { StatusExtension.ErrorMessage = ex.Message; } return(dic); }
public static DataTable GetDataTable(String connString, String storedProcedureName, String command, String tableName = "", List <KeyValuePair <String, Object> > parameters = null) { StatusExtension.Initialize(); String commandText = String.IsNullOrEmpty(storedProcedureName) ? command : storedProcedureName; CommandType commandType = String.IsNullOrEmpty(storedProcedureName) ? CommandType.Text : CommandType.StoredProcedure; tableName = tableName ?? "Data"; DataTable dt = new DataTable(tableName); using (SqlConnection conn = new SqlConnection(connString)) { try { SqlCommand cmd = conn.CreateCommand(); cmd.CommandType = commandType; cmd.CommandText = commandText; cmd.CommandTimeout = SQL_TIMEOUT; cmd.Parameters.Clear(); if (parameters != null) { cmd.Parameters.AddRange(GetSqlParameters(parameters)); } if (conn.State != ConnectionState.Open) { conn.Open(); } SqlDataAdapter da = new SqlDataAdapter(cmd); da.Fill(dt); } catch (Exception ex) { StatusExtension.ErrorMessage = String.Format("[{0}]: {1}\n{2}", commandType, commandText, ex.Message); if (StatusExtension.IsErrorDisplayed) { Console.WriteLine("\n[*SQL*] {0}", StatusExtension.ErrorMessage); } } } return(dt); }
public static Boolean Click(this IWebElement element, ChromeDriver driver) { Boolean success = false; StatusExtension.Initialize(); try { if (element.Displayed) { element.Click(); } else { ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].click();", element); } success = true; } catch (Exception ex) { StatusExtension.ErrorMessage = ex.Message; } return(success); }
public static Boolean ClosePopup(this ChromeDriver driver) { StatusExtension.Initialize(); Boolean success = false; String currentHandle = driver.CurrentWindowHandle; foreach (String handle in driver.WindowHandles) { if (handle != currentHandle) { driver.SwitchTo().Window(handle); driver.Close(); success = true; } } if (success) { driver.SwitchTo().Window(currentHandle); } //else // success = driver.ClickByText("x"); return(success); }
public static Boolean SendKeys(this IWebElement element, ChromeDriver driver, String message) { Boolean success = false; StatusExtension.Initialize(); try { if (element.Displayed) { element.Clear(); element.SendKeys(message); } else { ((IJavaScriptExecutor)driver).ExecuteScript("arguments[0].value=arguments[1];", element, message); } success = true; } catch (Exception ex) { StatusExtension.ErrorMessage = ex.Message; } return(success); }