blackboard / protractor-sync

Wrapper around Protractor, providing synchronous test writing and lots of helper functions.
MIT License
17 stars 5 forks source link

Not able to use protractor-sync module with cucumber #82

Open Vardhan18 opened 5 years ago

Vardhan18 commented 5 years ago

I am using protractor-sync module with cucumber so that to make the calls synchronously But when I start using the functions of protractor-sync browserSync.get("https://www.google.com");

Error: Could not find the current asyncblock flow. Please make sure this method is called from an asyncblock context. Error console: × Given Navigate to Google Page # Protractor\node_modules\cucumber\lib\support_code_library_builder\build_helpers.js:173 Error: Could not find the current asyncblock flow. Please make sure this method is called from an asyncblock context. at Object.exec (C:\Cucumber Protractor\node_modules\protractor-sync\dist\exec.js:10:19) at BrowserSync.get (C:\Cucumber Protractor\node_modules\protractor-sync\dist\browser-sync.js:34:23) at World.<anonymous> (C:\Cucumber Protractor\steps\LoginSteps.ts:12:15)

Feature File ` Feature: Validation test

@CucumberScenario Scenario: Validate Google Page Given Navigate to Google Page `

Steps File: ` import { Given } from 'cucumber'; import { browserSync } from 'protractor-sync';

Given('Navigate to Google Page',{ timeout: 120 * 1000 }, ()=> {

browserSync.get("https://www.google.com");

}); protractor conf.js exports.config = { allScriptsTimeout: 240000, specs: [ "./features/*.feature", ], framework: 'custom', frameworkPath: require.resolve("protractor-cucumber-framework"), cucumberOpts: { strict: true, compiler: 'ts:ts-node/register'

}

}; ` Can we use protractor-sync module along with cucumber ?

mindywhitsitt commented 5 years ago

Hi,

The error you're receiving isn't related to the use of cucumber. Rather, you'll need to call protractor-sync methods within an asyncblock flow. Be sure to import asyncblock at the top:

import * as ab from 'asyncblock';

Then you'll need to create an asyncblock flow and put your code inside it. The example in our readme shows a somewhat more complex way of doing this (note that createTest is creating an asyncblock flow), but I believe you could also do something like:


ab(() => {
  // your code here
});
Vardhan18 commented 5 years ago

Hi @mindywhitsitt ,

As per your inputs, I have used async block as follows in the code

import { Given,setDefaultTimeout } from 'cucumber';
import { browserSync, configure, elementSync, polledExpect } from 'protractor-sync';
import * as ab from 'asyncblock';
configure({ implicitWaitMs: 500 });
function createTest(fn: Function, errorMsg?: string) {
  return (done: Function) => {
    ab(() => {
      fn();
    }, (err: any) => {
      if (errorMsg) {
        expect(err.message).toEqual(errorMsg);
      } else {
        expect(err && err.stack || err || undefined).toBeUndefined();
      }
      done();
    });
  };
}
setDefaultTimeout(20 * 1000); 
Given('Navigate to Google Page', createTest(() => {
  const googleTranslateUrl = 'https://translate.google.com/'; 
     browserSync.waitForAngularEnabled(false);
    browserSync.get(googleTranslateUrl);   

}));

Getting following error

× Given Navigate to Google Page # Protractor\node_modules\cucumber\lib\support_code_library_builder\build_helpers.js:173
       Error: function timed out, ensure the callback is executed within 20000 milliseconds
           at Timeout._time.default.setTimeout [as _onTimeout] (.\node_modules\cucumber\lib\user_code_runner.js:81:20)
           at ontimeout (timers.js:475:11)
           at tryOnTimeout (timers.js:310:5)
           at Timer.listOnTimeout (timers.js:270:5)

let me know for any information

mindywhitsitt commented 5 years ago

Hm. It appears that function timed out error is coming from cucumber rather than protractor_sync. I'm not familiar with cucumber, so I'm not sure how much help I'll be in getting the two to work together. Is it possible that cucumber is expecting something else to be returned? In that case, maybe something like https://stackoverflow.com/questions/50983943/function-timed-out-ensure-the-callback-is-executed-within-60000-milliseconds?noredirect=1&lq=1 would be helpful.

I'd also be interested to know whether it really does go to the URL as expected, as that might help us narrow down the problem.