如何通过JavaScript回调在Q#中运行QUnit测试并获取测试结果? [英] How to run QUnit test and get back test result in C# via JavaScript callback?

查看:103
本文介绍了如何通过JavaScript回调在Q#中运行QUnit测试并获取测试结果?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的几个项目中,我使用MVC模式将(关注的)代码分为3层.模型层和控制层都在C#上运行,因此我使用测试框架(例如MSTest或NUnit)来验证这些层的功能需求.对于View层,我使用QUnit来测试JavaScript文件.

In my several projects, I use MVC pattern for separating code (of concerns) into 3 tiers. Both of Model and Control tiers run on C# so I use testing framework like MSTest or NUnit to validate functional requirement for these tiers. For View tiers, I use QUnit to test JavaScript files.

但是,我不能将QUnit作为自动化测试执行,因为MSTest不直接支持测试网页.我需要像下面的逻辑一样在MSTest中运行它.

However, I cannot execute QUnit as automated test because MSTest doesn't directly support for testing web page. I need to run it in MSTest like the following logic.

[TestMethod]
public void JavaScriptTest()
{
    var result = QUnit.Test('~/QUnit/test1.htm');

    Assert.IsTrue(result.Failed <= 0)
}

解决方案必须在QUnit中使用回调函数(,而不是while循环检查),以确保测试方法在测试完成后立即执行.

Solution must use callback function in QUnit (not while-loop checking) to ensure that test method will execute immediately after test done.

推荐答案

对于跨浏览器测试, Selenium WebDriver 是解决此问题的最佳选择,因为我们只需更改一行代码就可以轻松地在浏览器之间切换.

For cross browser testing, Selenium WebDriver is the best option for solving this problem because we can easily switch between browser by changing only one line of code.

  1. 安装 Selenium WebDriver软件包通过

    2.下载首选驱动程序到您的项目中,添加到您的项目中 并将复制到输出目录"设置为如果较新则复制".为了这 例如,我使用Chrome驱动程序在Google Chrome上运行Selenium 我的机器.

    2.Download preferred driver to your project, add to your project and set "Copy to Output Directory" equals "Copy if newer". For this example, I use Chrome driver to run Selenium with Google Chrome on my machine.

    3.在测试方法中,创建驱动程序并设置最大执行时间,然后再运行QUnit.

    3.In test method, create driver and set max execution time before running QUnit.

    var browser = new ChromeDriver();
    var navigator = browser.Navigate();
    
    // Currently, max execution time is one minute.
    browser.Manage().Timeouts()
           .SetScriptTimeout(new TimeSpan(0, 1, 0));
    

    4.请确保已将QUnit的自动启动设置为false.

    4.Please make sure that you already set autostart of QUnit to false.

    QUnit.config.autostart = false;
    

    5.导航到QUnit页面.在这种情况下,我在解决方案文件夹中使用本地网页.

    5.Navigate to QUnit page. In this case, I use local webpage in solution folder.

     browser.Manage().Timeouts().SetScriptTimeout(new TimeSpan(0, 1, 0));
    navigator.GoToUrl(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"../../../QUnit example/qunit-demo.htm"));
    

    6.在浏览器对象中使用ExecuteAsyncScript方法注册 QUnit.done 函数的回调并手动开始QUnit测试.

    6.Use ExecuteAsyncScript method in browser object to register callback for QUnit.done function and manually start QUnit testing.

    var response = browser.ExecuteAsyncScript
    (
        "var callback = arguments[arguments.length - 1];" +
        "QUnit.done(callback); QUnit.start();"
    );
    

    7.QUnit测试完成后,它将返回一个响应.我们需要将其转换为合适的类型并获得测试结果.

    7.When QUnit test done, it will return a response. We need to convert it as suitable type and get the test result.

    var testResult = response as Dictionary<string, object>;
    
    if(testResult == null) throw new Exception("Unhandle error occur while running QUnit."); 
    
    Console.WriteLine("Test complete in " + (long)testResult["runtime"] + " ms.");
    Console.WriteLine("---------------------------");
    Console.WriteLine("total: " + (long)testResult["total"]);
    Console.WriteLine("passed: " + (long)testResult["passed"]);
    Console.WriteLine("failed: " + (long)testResult["failed"]);
    

    8.每次使用完测试时,别忘了关闭浏览器.

    8.Don't forget to close browser every time use finish testing.

    browser.Close();
    

    PS.我还为此答案提供了Visual Studio 2012解决方案(源代码).

    PS. I also provide Visual Studio 2012 solution (source code) for this answer.

    单击此处下载

    更新1

    • 修复了QUnit有时在系统注册回调完成功能之前开始测试的错误.
    • 包括本地QUnit页面.
    • 包括IEDriver以下载解决方案.但是,此版本在Windows 8上不支持IE10.但是在IE8上可以正常工作.

    这篇关于如何通过JavaScript回调在Q#中运行QUnit测试并获取测试结果?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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