从量角器中较大的文本源中检索子字符串时出错

时间:2019-02-04 15:58:34

标签: jasmine protractor

我试图在针对量角器的端到端测试中从注册电子邮件中提取URL,但是在尝试解析较大的字符串时遇到错误。

我得到的错误是:

*

  

故障:1)MockMock获取验证链接消息:       失败:regText.indexOf不是函数堆栈:       TypeError:regText.indexOf不是一个函数           在getRegLink(D:\ QA \ Scripting \ ProtractorHelloWorld \ CCspecMockMock.js:19:27)           在UserContext上。 (D:\ QA \ Scripting \ ProtractorHelloWorld \ CCspecMockMock.js:37:14)           在C:\ Users \ dcoughler \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ jasminewd2 \ index.js:112:25           在新的ManagedPromise(C:\ Users \ dcoughler \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:1077:7)           在ControlFlow.promise(C:\ Users \ dcoughler \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:2505:12)           在schedulerExecute(C:\ Users \ dcoughler \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ jasminewd2 \ index.js:95:18)           在TaskQueue.execute_(C:\ Users \ dcoughler \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:3084:14)           在TaskQueue.executeNext_(C:\ Users \ dcoughler \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:3067:27)           在asyncRun(C:\ Users \ dcoughler \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:2974:25)           在C:\ Users \ dcoughler \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:668:7       来自:任务:在控制流中运行它(“获取验证链接”)           在UserContext上。 (C:\ Users \ dcoughler \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ jasminewd2 \ index.js:94:19)           在C:\ Users \ dcoughler \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ jasminewd2 \ index.js:64:48           在ControlFlow.emit(C:\ Users \ dcoughler \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ events.js:62:21)           在ControlFlow.shutdown_(C:\ Users \ dcoughler \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:2674:10)           在shutdownTask_.MicroTask上(C:\ Users \ dcoughler \ AppData \ Roaming \ npm \ node_modules \ protractor \ node_modules \ selenium-webdriver \ lib \ promise.js:2599:53)       通过异步测试:       错误           在套房。 (D:\ QA \ Scripting \ ProtractorHelloWorld \ CCspecMockMock.js:35:1)           在对象。 (D:\ QA \ Scripting \ ProtractorHelloWorld \ CCspecMockMock.js:2:1)           在Module._compile(内部/模块/cjs/loader.js:688:30)           在Object.Module._extensions..js(内部/模块/cjs/loader.js:699:10)           在Module.load(internal / modules / cjs / loader.js:598:32)           在tryModuleLoad(internal / modules / cjs / loader.js:537:12)处出现1个规范,1个故障在0.622秒内完成

*

代码如下:

// spec.js
describe('MockMock', function() {

  var tRegMessage = element(by.className('well'));
  var tabledata = $$('./table');
  // get rows 
  var rows = tabledata.all(by.tagName("tr"));
  // get cell values
  var cells = rows.all(by.tagName("td"));


  var commonfunctions = require('./CCCommonFunctions.js');

  function clickRegistration(email) {
    element(by.xpath("//td[. = '" + email + "']/following-sibling::td/a")).click();
}

  function getRegLink(regText) {
      var startUrl = regText.indexOf("http://");
      var endUrl = regText.indexOf("Thank you",startUrl);
      getRegLink = regText.substring(startUrl,endUrl);
  }

  function Login(username, password) {
    fUserName.sendKeys(username);
    fPassword.sendKeys(password);
    commonfunctions.ccClick(bLoginButton);
  }

  beforeEach(function() {
    browser.waitForAngularEnabled(false);
    browser.get('http://ns-rd-app-wi:2525/');
  });

it('Get verification link', function() {
    clickRegistration('PhillipPies@mock.com');
    browser.get(getRegLink(tRegMessage));
    browser.pause();
  });

  });

我应该如何在量角器中解析字符串?似乎我也很习惯vbscript。

================================================ ==================

我已根据以下评论进行了更改,但我仍然受阻:

describe('MockMock', function() {

  var tRegMessage = element(by.className('well'));
  var tabledata = $$('./table');
  // get rows 
  var rows = tabledata.all(by.tagName("tr"));
  // get cell values
  var cells = rows.all(by.tagName("td"));


  var commonfunctions = require('./CCCommonFunctions.js');

  function clickRegistration(email) {
    element(by.xpath("//td[. = '" + email + "']/following-sibling::td/a")).click();
}

  function getRegLink(regMessage) {
  return new Promise(resolve => {
    regMessage.getText().then(text => {
      var startUrl = text.indexOf("http://");
      var endUrl = text.indexOf("Thank you",startUrl);
      resolve(text.substring(startUrl,endUrl-2))
    }  ) 
  })         
}

  beforeEach(function() {
    browser.waitForAngularEnabled(false);
    browser.get('http://ns-rd-app-wi:2525/');
  });


it('Get verification link', function() {
    clickRegistration('PhillipPies@mock.com');
    var regURL=getRegLink(tRegMessage.getText());
    browser.get(regURL.toString());
    browser.pause();
  });

});

现在给出一个新错误: 失败:未知错误:未处理的检查器错误:{“代码”:-32000,“消息”:“无法导航到无效URL”}   (会议信息:chrome = 71.0.3578.98)   (驱动程序信息:chromedriver = 2.46.628402(536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform = Windows NT 10.0.16299 x86_64)

2 个答案:

答案 0 :(得分:0)

您在行上出错:

"@babel/plugin-transform-spread"

因为"@babel/plugin-proposal-object-rest-spread"不是browser.get(getRegLink(tRegMessage)); 。是tRegMessage(您是从string这一行得到的) 因此,您需要从此元素中获取文本。例如:

ElementFinder

此外,您的方法element(by.className('well'));不会返回it('Get verification link', function() { const url = tRegMessage.getText(); clickRegistration('PhillipPies@mock.com'); browser.get(getRegLink(url)); browser.pause(); }); 。因此,在第一次修复后,您将得到一个错误,提示getRegLink()应该具有参数string

答案 1 :(得分:0)

问题是tRegMessage不是字符串。这是ElementFinder。您需要先致电getText()

function getRegLink(regMessage) {
  return new Promise(resolve => {
    regMessage.getText().then(text => {
      var startUrl = text.indexOf("http://");
      var endUrl = text.indexOf("Thank you",startUrl);
      resolve(text.substring(startUrl,endUrl));
    }   
  }         
}