Here are a bunch of useful methods to assist in Automation:
Wait for Page Load
/// <summary>
/// Waits for a 200 Response
/// </summary>
/// <param name="driver"></param>
public static void WaitForPageLoad(IWebDriver driver)
{
IWait wait = new OpenQA.Selenium.Support.UI.WebDriverWait(driver, TimeSpan.FromSeconds(30.00));
wait.Until(
driver =>
((IJavaScriptExecutor)driver).ExecuteScript("return document.readyState").Equals("complete"));
}
Wait for Elment Appear
///
/// waitForElement:
/// Waits for an element to Enable or to Display
///
/// ID, XPath, Class, CSS, or Tag
/// Text specific to element
/// 'Enable' or 'Display'
/// Time to wait
/// Every function needs one
///
public static bool WaitForElement(string TypeOfElement, string ElementName, string EnableOrDisplay, int SecondsToWait, IWebDriver driver)
{
IWebElement webElement = driver.FindElement(By.XPath("/html/body")); //Just putting a false element in to compile
int counter = 0;
// We'll wait in 100ms chunks, instead of full seconds
SecondsToWait *= 10;
switch (TypeOfElement.ToLower())
{
case "id":
webElement = driver.FindElement(By.Id(ElementName));
break;
case "xpath":
webElement = driver.FindElement(By.XPath(ElementName));
break;
case "class":
webElement = driver.FindElement(By.ClassName(ElementName));
break;
case "css":
webElement = driver.FindElement(By.CssSelector(ElementName));
break;
case "tag":
webElement = driver.FindElement(By.TagName(ElementName));
break;
}
// Now, wait for the element to either display or enable for up to secondsToWait seconds
switch (EnableOrDisplay.ToLower())
{
case "enable":
while ((!webElement.Enabled) && (counter <= SecondsToWait))
{
Thread.Sleep(100);
counter++;
}
break;
case "display":
while ((!webElement.Displayed) && (counter <= SecondsToWait))
{
Thread.Sleep(100);
counter++;
}
break;
default:
Console.WriteLine("\n\nERROR! Neither 'Enable' nor 'Display' was sent to function waitForElement");
counter = SecondsToWait + 1;
break;
}
return (counter <= SecondsToWait);
}
Print JSON Human Readable
/// <summary>
/// This will print in console json results in human readible format
/// </summary>
/// <param name="json"></param>
/// <returns></returns>
public static string FormatJsonString(string json)
{
if (string.IsNullOrEmpty(json)) return string.Empty;
json = json.Replace(Environment.NewLine, string.Empty).Replace("\t", string.Empty);
var offset = 0;
var output = new StringBuilder();
Action tabs = (sb, pos) => { for (var i = 0; i < pos; i++) { sb.Append("\t"); } };
Func previousNotEmpty = (s, i) =>
{
if (string.IsNullOrEmpty(s) || i <= 0) return null;
char? prev = null;
while (i > 0 && prev == null)
{
prev = s[i - 1];
if (prev.ToString() == " ") prev = null;
i--;
}
return prev;
};
Func nextNotEmpty = (s, i) =>
{
if (string.IsNullOrEmpty(s) || i >= (s.Length - 1)) return null;
char? next = null;
i++;
while (i < (s.Length - 1) && next == null)
{
next = s[i++];
if (next.ToString() == " ") next = null;
}
return next;
};
for (var i = 0; i < json.Length; i++)
{
var chr = json[i];
switch (chr.ToString())
{
case "{":
offset++;
output.Append(chr);
output.Append(Environment.NewLine);
tabs(output, offset);
break;
case "}":
offset--;
output.Append(Environment.NewLine);
tabs(output, offset);
output.Append(chr);
break;
case ",":
output.Append(chr);
output.Append(Environment.NewLine);
tabs(output, offset);
break;
case "[":
output.Append(chr);
var next = nextNotEmpty(json, i);
if (next != null && next.ToString() != "]")
{
offset++;
output.Append(Environment.NewLine);
tabs(output, offset);
}
break;
case "]":
var prev = previousNotEmpty(json, i);
if (prev != null && prev.ToString() != "[")
{
offset--;
output.Append(Environment.NewLine);
tabs(output, offset);
}
output.Append(chr);
break;
default:
output.Append(chr);
break;
}
}
return output.ToString().Trim();
}