从C#组件内执行JavaScript [英] Execute JavaScript from within a C# assembly

查看:282
本文介绍了从C#组件内执行JavaScript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从C#组件内执行JavaScript code和有J​​avaScript的code中的结果返回给调用C#code。

I'd like to execute JavaScript code from within a C# assembly and have the results of the JavaScript code returned to the calling C# code.

这是比较容易确定的事情,我并不想这样做:

It's easier to define things that I'm not trying to do:

  • 我并不想从我的$ C $后面调用C网页上的JavaScript函数。

  • I'm not trying to call a JavaScript function on a web page from my code behind.

我并不想加载WebBrowser控件。

I'm not trying to load a WebBrowser control.

我不希望有JavaScript的执行AJAX调用到服务器。

I don't want to have the JavaScript perform an AJAX call to a server.

我想要做的就是用JavaScript编写单元测试,并随后单元测试输出JSON,甚至是纯文本就可以了。然后我想有一个通用的C#类/ executible可以加载包含JS文件,运行JS单元测试,废钢/加载结果,并返回一个通过/失败,在生成后任务的详细信息。

What I want to do is write unit tests in JavaScript and have then unit tests output JSON, even plain text would be fine. Then I want to have a generic C# class/executible that can load the file containing the JS, run the JS unit tests, scrap/load the results, and return a pass/fail with details during a post-build task.

我认为这是可能使用旧的ActiveX ScriptControl的,但似乎应该有一个.NET的方式来做到这一点,而无需使用Silverlight和DLR,或其他任何东西,还没有发货呢。任何人有什么想法?

I think it's possible using the old ActiveX ScriptControl, but it seems like there ought to be a .NET way to do this without using SilverLight, the DLR, or anything else that hasn't shipped yet. Anyone have any ideas?

更新:从布拉德·艾布拉姆斯博客

namespace Microsoft.JScript.Vsa
{
    [Obsolete("There is no replacement for this feature. " +
              "Please see the ICodeCompiler documentation for additional help. " +
              "http://go.microsoft.com/fwlink/?linkid=14202")]

澄清: 我们有单元测试正在使用JSUnit的框架,用JavaScript编写我们的JavaScript功能。现在在我们的构建过程中,我们必须手动加载网页并点击一个按钮,以确保所有的JavaScript单元测试通过。我希望能够在后构建过程来执行测试我们的自动C#单元测试运行时并报告成功/失败旁边的出C#单元测试,并把它们作为一个指标来与否的构建坏了。

Clarification: We have unit tests for our JavaScript functions that are written in JavaScript using the JSUnit framework. Right now during our build process, we have to manually load a web page and click a button to ensure that all of the JavaScript unit tests pass. I'd like to be able to execute the tests during the post-build process when our automated C# unit tests are run and report the success/failure alongside of out C# unit tests and use them as an indicator as to whether or not the build is broken.

推荐答案

您可以使用JSUnit的服务器南特内部运行JSUnit的,它是用Java编写的,并没有一个南特的任务,但你可以在命令提示符下运行,结果被记录为XML,你可以将它们它们与您构建报告过程集成。 这会不会是你NUnit的结果的一部分,但一个额外的报告。 如果任何这些测试失败,我们构建失败。 我们正在做的正是使用CC.Net。

You can run your JSUnit from inside Nant using the JSUnit server, it's written in java and there is not a Nant task but you can run it from the command prompt, the results are logged as XML and you can them integrate them with your build report process. This won't be part of your Nunit result but an extra report. We fail the build if any of those test fails. We are doing exactly that using CC.Net.

这篇关于从C#组件内执行JavaScript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆