如何使用实习生测试框架对动态生成的服务器内容进行功能测试


How to use the intern testing framework for functional testing on dynamically generated server content

我是InternetFramework的新手,正在努力了解如何对服务器生成的代码执行功能测试。Intern文档预先表示这是可能的,但没有提供更多的解释。我不知道如何设置项目,以便在实习生测试运行程序加载我的页面时执行我的php代码。

我有推荐的实习生文件结构,src目录中有两个文件:test.php和test.html。如果我用测试运行器在test.html上运行我的功能测试,它就可以通过;但是,如果我在test.php上运行它,浏览器只会下载该文件并使测试失败。

我的实习生配置文件:

// tests/intern.js
define({
  capabilities: {
    'browserstack.selenium_version': '2.45.0'
  },
  // Maximum number of simultaneous integration tests that should be executed on the remote WebDriver service
  maxConcurrency: 2,
  tunnel: 'NullTunnel',
  environments: [ { browserName: 'chrome' } ],
  loaderOptions: {
    // Packages that should be registered with the loader in each testing environment
    packages: [ { name: 'myPackage', location: '.' } ]
  },
  // Functional test suite(s) to execute against each browser once non-functional tests are completed
  functionalSuites: [  'tests/functional/index.js'  ],
  // A regular expression matching URLs to files that should not be included in code coverage analysis
  excludeInstrumentation: /^(?:tests|node_modules)'//
});

我的功能测试:

// tests/functional/index.js
define(function (require) {
var registerSuite = require('intern!object');
var assert = require('intern/chai!assert');
registerSuite({
name: 'index',
'get Header': function () {
  return this.remote
    .get(require.toUrl('src/index.php'))
    .setFindTimeout(5000)
    .findByTagName('h1')
    .setFindTimeout(5000)
    .getVisibleText()
    .then(function(text) {
      assert.equal(text, "Home",
            'calling getHeader for home page should return Home');
    });
}
});
});

我的PHP页面:

// src/test.php
<?php
   // do php stuff
   if (isset($_GET["returnJson"])) {
     // return php stuff
   } else {
?>
  <!DOCTYPE html>
  <!--[if lte IE 8]> <html class="lte-ie8"> <![endif]-->
  <!--[if IE 9]> <html class="lte-ie9"> <![endif]-->
  <!--[if gt IE 9]><!--> <html> <!--<![endif]-->
  <head></head>
  <body>
    <h1>Home</h1>
  </body>
  </html>
}

我的HTML页面:

// src/test.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <title>Intern And Server Generated Content</title> 
</head>
<body>
  <h1>Home</h1>
</body>
</html>

在远程上调用get只需加载提供的任何URL,就像web浏览器一样。这里的问题是使用require.toUrl来引用PHP文件。这将相对于测试文件本身加载src/index.php。测试文件和其他与Intern相关的资产是通过Intern的测试服务器(称为"代理")加载的,该服务器不处理PHP,因此浏览器最终只会显示index.php的原始内容。

要测试PHP应用程序,您需要为get调用提供支持PHP的服务器的URL。