0
votes

I am trying to extract a URL from a registration email in my end-to-end test for Protractor, but I am getting errors trying to parse the larger string.

The error I am getting is:

*

Failures: 1) MockMock Get verification link Message: Failed: regText.indexOf is not a function Stack: TypeError: regText.indexOf is not a function at getRegLink (D:\QA\Scripting\ProtractorHelloWorld\CCspecMockMock.js:19:27) at UserContext. (D:\QA\Scripting\ProtractorHelloWorld\CCspecMockMock.js:37:14) at C:\Users\dcoughler\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:112:25 at new ManagedPromise (C:\Users\dcoughler\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:1077:7) at ControlFlow.promise (C:\Users\dcoughler\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2505:12) at schedulerExecute (C:\Users\dcoughler\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:95:18) at TaskQueue.execute_ (C:\Users\dcoughler\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:3084:14) at TaskQueue.executeNext_ (C:\Users\dcoughler\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:3067:27) at asyncRun (C:\Users\dcoughler\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2974:25) at C:\Users\dcoughler\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:668:7 From: Task: Run it("Get verification link") in control flow at UserContext. (C:\Users\dcoughler\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:94:19) at C:\Users\dcoughler\AppData\Roaming\npm\node_modules\protractor\node_modules\jasminewd2\index.js:64:48 at ControlFlow.emit (C:\Users\dcoughler\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\events.js:62:21) at ControlFlow.shutdown_ (C:\Users\dcoughler\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2674:10) at shutdownTask_.MicroTask (C:\Users\dcoughler\AppData\Roaming\npm\node_modules\protractor\node_modules\selenium-webdriver\lib\promise.js:2599:53) From asynchronous test: Error at Suite. (D:\QA\Scripting\ProtractorHelloWorld\CCspecMockMock.js:35:1) at Object. (D:\QA\Scripting\ProtractorHelloWorld\CCspecMockMock.js:2:1) at Module._compile (internal/modules/cjs/loader.js:688:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:699:10) at Module.load (internal/modules/cjs/loader.js:598:32) at tryModuleLoad (internal/modules/cjs/loader.js:537:12) 1 spec, 1 failure Finished in 0.622 seconds

*

Here is the code:

// 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('[email protected]');
    browser.get(getRegLink(tRegMessage));
    browser.pause();
  });

  });

How should I be parsing strings in protractor? I'm too used to vbscript, it seems.

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

I've made changes based on the comments below, but I am still stuck:

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('[email protected]');
    var regURL=getRegLink(tRegMessage.getText());
    browser.get(regURL.toString());
    browser.pause();
  });

});

This gives a new error now: Failed: unknown error: unhandled inspector error: {"code":-32000,"message":"Cannot navigate to invalid URL"} (Session info: chrome=71.0.3578.98) (Driver info: chromedriver=2.46.628402 (536cd7adbad73a3783fdc2cab92ab2ba7ec361e1),platform=Windows NT 10.0.16299 x86_64)

2

2 Answers

0
votes

You got error on line:

browser.get(getRegLink(tRegMessage));

Because tRegMessage is not string. It is ElementFinder (you got it from this line element(by.className('well'));) So, you need take a text from this element. For example:

it('Get verification link', function() {
    const url = tRegMessage.getText();
    clickRegistration('[email protected]');
    browser.get(getRegLink(url));
    browser.pause();
  });

Also, your method getRegLink() does not return string. So, after first fix you will get an error that browser.get() should have argument string

0
votes

The problem is tRegMessage is not a string. It's an ElementFinder. You need to call getText() first.

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));
    }   
  }         
}