Last active
June 26, 2017 07:53
-
-
Save pagebrooks/6466660 to your computer and use it in GitHub Desktop.
This helper class adds Eval so that you can obtain the result of a JavaScript
function. This functionality is not native to WatiN. Forked from: http://blog.ashmind.com/2007/09/05/evaluating-javascript-in-watin
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
// Forked from: http://blog.ashmind.com/2007/09/05/evaluating-javascript-in-watin/ | |
// This helper class adds Eval so that you can obtain the result of a JavaScript | |
// function. This functionality is not native to WatiN. | |
/* | |
* Usage: | |
* var browser = new IE(); | |
* var foo = browser.NativeDocument.Eval<bool>("isFoo()"); | |
*/ | |
public static class WatiNHelper | |
{ | |
public static T Eval<T>(this INativeDocument document, string code) | |
{ | |
IExpando window = GetWindow(document); | |
PropertyInfo property = GetOrCreateProperty(window, "__lastEvalResult"); | |
document.RunScript("window.__lastEvalResult = " + code + ";", "JavaScript"); | |
return (T)property.GetValue(window, null); | |
} | |
private static PropertyInfo GetOrCreateProperty(IExpando expando, string name) | |
{ | |
PropertyInfo property = expando.GetProperty(name, BindingFlags.Instance); | |
if (property == null) | |
{ | |
property = expando.AddProperty(name); | |
} | |
return property; | |
} | |
private static IExpando GetWindow(INativeDocument document) | |
{ | |
IEDocument ieDoc = document as IEDocument; | |
IHTMLDocument2 htmlDoc = ieDoc.HtmlDocument; | |
return htmlDoc.parentWindow as IExpando; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment