robotframework-browser icon indicating copy to clipboard operation
robotframework-browser copied to clipboard

Error while importing typescript ES Module with page objects to use in the keyword

Open testgitdl opened this issue 3 years ago • 0 comments

Describe the bug We have a page objects typescript library (inhouse developed) that we want to reuse also for robot playwright tests but when in the keyword we get an error that one of the resources is not available. So we need to import from an ES Module to commonjs via dynamic import and get the following error: "Error [ERR_MODULE_NOT_FOUND]: Cannot find module 'C:_AutomationProjects\CTA\Playwright\dist\page-objects\esm2020\lib\login.page' imported from C:_AutomationProjects\CTA\Playwright\dist\page-objects\esm2020\lib\main.page.mjs"

To Reproduce

Contents of the inhouse developed module:

login.page.ts

export class LoginPage {
  public page: Page;
  constructor(page: Page) {
    this.page = page;
  }
}

main.page.ts

import { LoginPage } from "./login.page";

export class MainPage {
  public page: Page;
  public login: LoginPage;
  constructor(page: Page) {
    this.page = page;
    this.login = new LoginPage(page);
  }
}

Then in the keyword.cjs that we use in the robot test we get the above mentioned error. keyword.cjs

var mainPage;
//function to load all needed page objects before starting the execution
async function loadMainModules(page, args) {
   mainPage = new (await import('../../dist/page-objects/esm2020/lib/main.page.mjs')).MainPage(page);
}

exports.__esModule = true;
exports.LoadMainPageObjects = loadMainModules;

test.robot

  • Settings * Library Browser jsextension=${CURDIR}/../keywords/keyword.cjs

  • Test Cases * MY_TEST New Browser chromium headless=false channel=chrome Browser.Set Browser Timeout 150 seconds New Context viewport={'width': 1850, 'height': 900} New Page https://angular.com Load Main page objects


**Expected behavior**
We need to be able to reuse this kind of typescript page-objects also in the robot test cases


testgitdl avatar Aug 31 '22 14:08 testgitdl