-1

I have used page objects in Ruby. How can I do them in JavaScript?

I want the ability for simple references but also be able to pass parameters for cases where several page objects are similar, for example home street address lines vs billing street address lines.

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497

1 Answers1

-1

The following code allows you to use both simple and dynamic identifiers

const po = new WeakMap;

//Basic Page Object
po['email'] = `div#email`,

//Page Objects with 1 parameter
po['firstName'] = (personType) => `div#${personType}-first-name`,
po['lastName'] = (personType) => `div#${personType}-last-name`,
po['middleInitial'] = (personType) => `div#${personType}-middle-initial`;

//Page Objects with 2 parameters
po['street'] = (homeOrBilling, streetNumber) => `div#${homeOrBilling}-street-${streetNumber}`;

/* Usage */
const p = console.log;
p(po.email);
p(po.firstName('mother'));
p(po.lastName('mother'));
p(po.middleInitial('mother'));
p(po.street('home','1'));
p(po.street('home','2'));

module.exports = po;

/* Sample output */
/*
   div#email
   div#mother-first-name
   div#mother-last-name
   div#mother-middle-initial
   div#home-street-1
   div#home-street-2

*/
Michael Durrant
  • 93,410
  • 97
  • 333
  • 497
  • 2
    Surely a WeakMap is exactly what you *don't* want, because then the functions you've written can get garbage collected. Also doesn't this just become "how do I write a function that returns a string?" – jonrsharpe Aug 03 '20 at 11:06