﻿<?xml version="1.0" encoding="utf-8"?><rss version="2.0"><channel><title>Ayende @ Rahien</title><link>http://ayende.com</link><description>Ayende @ Rahien</description><copyright>Copyright (C) Ayende Rahien  2004 - 2021 (c) 2026</copyright><ttl>60</ttl><item><title>Jeff Brown commented on Eval with WatiN</title><description>@Stan: Neat trick!
  
  
This is what I put into our little WebTestFramework here.  It's similar to WatiN.
  
  
        /// &lt;summary&gt;
  
        /// Evaluates the specified JavaScript code within the scope of this
  
        /// document.  Returns the value computed by the last expression in the
  
        /// code block after implicit conversion to a string.
  
        /// &lt;/summary&gt;
  
        /// &lt;example&gt;
  
        /// The following example shows an alert dialog then returns the string "4".
  
        /// 
  
	        /// EvaluateJavaScript("window.alert('Hello world!'); 2 + 2");
  
	        /// 
  
        /// &lt;/example&gt;
  
        /// &lt;param name="javaScriptCode"&gt;The JavaScript code&lt;/param&gt;
  
        /// &lt;returns&gt;The result converted to a string&lt;/returns&gt;
  
        /// &lt;exception cref="JavaScriptException"&gt;Thrown when the JavaScript code cannot be evaluated
  
        /// or throws an exception during evaluation&lt;/exception&gt;
  
        public string EvaluateJavaScript(string javaScriptCode)
  
        {
  
            const string resultPropertyName = "___expressionResult___";
  
            const string errorPropertyName = "___expressionError___";
  
  
            string exprWithAssignment = "try {\n"
  
                + "document." + resultPropertyName + "= String(eval('" + javaScriptCode.Replace("'", "\\'") + "'))\n"
  
                + "} catch (error) {\n"
  
                + "document." + errorPropertyName + "= 'message' in error ? error.name + ': ' + error.message : String(error)\n"
  
                + "}";
  
  
            string result = null, error = null;
  
  
            Browser.Sync(delegate
  
            {
  
                // Prepare a property to acquire the result.
  
                IHTMLDocument2 domDocument = (IHTMLDocument2) DomReference;
  
                IHTMLWindow2 domWindow = domDocument.parentWindow;
  
                IExpando domDocumentExpando = (IExpando) domDocument;
  
  
                PropertyInfo resultProperty = InitializeExpandoProperty(domDocumentExpando, resultPropertyName);
  
                PropertyInfo errorProperty = InitializeExpandoProperty(domDocumentExpando, errorPropertyName);
  
  
                // Run the script.
  
                // The result value is not meaningful.
  
                domWindow.execScript(exprWithAssignment, "JavaScript");
  
  
                // Obtain the result and error.
  
                result = (string)resultProperty.GetValue(domDocument, null);
  
                error = (string)errorProperty.GetValue(domDocument, null);
  
            });
  
  
            if (error != null)
  
                throw new JavaScriptException(error);
  
  
            return result;
  
        }
  
  
        private static PropertyInfo InitializeExpandoProperty(IExpando expando, string propertyName)
  
        {
  
            PropertyInfo propertyInfo = expando.GetProperty(propertyName, BindingFlags.Instance);
  
            if (propertyInfo == null)
  
                propertyInfo = expando.AddProperty(propertyName);
  
  
            propertyInfo.SetValue(expando, null, null);
  
            return propertyInfo;
  
        }
</description><link>http://ayende.com/2285/eval-with-watin#comment5</link><guid>http://ayende.com/2285/eval-with-watin#comment5</guid><pubDate>Thu, 05 Apr 2007 18:57:56 GMT</pubDate></item><item><title>Ayende Rahien commented on Eval with WatiN</title><description>@Stan,
  
VERY cool!
  
I wasn't aware of this possiblity
</description><link>http://ayende.com/2285/eval-with-watin#comment4</link><guid>http://ayende.com/2285/eval-with-watin#comment4</guid><pubDate>Tue, 03 Apr 2007 22:15:36 GMT</pubDate></item><item><title>Stan Mitranic commented on Eval with WatiN</title><description>Ayende,
  
  
Here is a quick example:
  
  
Assuming you have already inserted the following into the DOM:
  
&lt;script&gt;
  
function myeval() {
  
    return 33 + 44;
  
}
  
&lt;/script&gt;
  
  
The following code:
  
Type scriptEngType = ie.HtmlDocument.Script.GetType();
  
int result = (int)scriptEngType.InvokeMember("myeval", BindingFlags.InvokeMethod, null, ie.HtmlDocument.Script, null);
  
Console.WriteLine("Result: {0}", result);
  
  
...will print out: Result: 77
  
  
Hope that helps.
</description><link>http://ayende.com/2285/eval-with-watin#comment3</link><guid>http://ayende.com/2285/eval-with-watin#comment3</guid><pubDate>Tue, 03 Apr 2007 21:58:04 GMT</pubDate></item><item><title>Ayende Rahien commented on Eval with WatiN</title><description>@Stan,
  
Can you give ore details about it?
  
document.execScript() has no return value.
</description><link>http://ayende.com/2285/eval-with-watin#comment2</link><guid>http://ayende.com/2285/eval-with-watin#comment2</guid><pubDate>Tue, 03 Apr 2007 21:35:34 GMT</pubDate></item><item><title>Stan Mitranic commented on Eval with WatiN</title><description>Another approach might be to insert the following into the DOM:
  
&lt;SCRIPT&gt;
  
    function myeval() { return ... code goes here... }
  
&lt;/SCRIPT&gt; 
  
  
Then just execute that function using the document.Script property (defined on IHTMLDocument), which would allow you to capture the return value.
  
  
It's a bit of a pain to use though because it only exposes the IDispatch interface so you either have to use reflection to make the appropriate call or use a loosely-typed language (Boo? ;)
  
</description><link>http://ayende.com/2285/eval-with-watin#comment1</link><guid>http://ayende.com/2285/eval-with-watin#comment1</guid><pubDate>Tue, 03 Apr 2007 21:22:53 GMT</pubDate></item></channel></rss>