隔离操作和测试

Isolation of actions and tests

我尝试重构我的代码。我知道如果我有几个期望,他们应该在 'it' 中被隔离。我试图理解如何改为这样写:

describe('my scenario should make', function () {

  var config = browser.params;
  var url = config.listOfReferencesUrl,
      grid,
      numberField;

  it('test1', function () {

    browser.get(url);
    browser.executeScript("icms.go('WEB_INQ_PROC', 'InquiryList', null, 0)");

    grid = psGrid(by.css("table[class='n-grid']"));
    numberField = grid.getQuickFilter(1);
    numberField.click().sendKeys("Hello!");

    since('fail1').expect(numberField.getInputText()).toEqual("");
  });

  it('test2', function () {
    since('fail2').expect(numberField.getInputText()).toEqual("Hello!");
  });

});

像这样:

    describe('my scenario should make', function () {

      var config = browser.params;
      var url = config.listOfReferencesUrl,
          grid,
          numberField;

 *********Make this part of code ONES before all tests in spec ****
    browser.get(url);
    browser.executeScript("icms.go('WEB_INQ_PROC', 'InquiryList', null, 0)");

    grid = psGrid(by.css("table[class='n-grid']"));
    numberField = grid.getQuickFilter(1);
    numberField.click().sendKeys("Hello!"); 
*******************************************************************   
      it('test1', function () {
        since('fail1').expect(numberField.getInputText()).toEqual("");
      });

      it('test2', function () {
        since('fail2').expect(numberField.getInputText()).toEqual("Hello!");
      });

    });

也许有人知道我该怎么做?

为了回答你的问题,如果你想在所有测试之前 运行 你的代码一次,那么使用 Jasmine 2 中可用的 beforeAll() 函数。这是一个示例 -

beforeAll(function(){
    //Write your code here that you need to run once before all specs
});

您可以使用 Jasmine 中可用的 beforeEach() 函数来 运行 它每次在测试规范之前。这是一个示例 -

beforeEach(function(){
    //Write your code here that you need to run everytime before each spec
});

如果您在使用这些功能时遇到问题,请将您的插件更新到最新版本,然后尝试 运行安装它。还可以在 conf.js 文件

中使用 framework: 'jasmine2'

希望这对您有所帮助。