0

I have a problem similar to link, I think in that link guys are using typescript files. I have defined selectors in the js files, I tried to do it this way:

class Page {
   constructor(sampleText){
       this.optionsButton = Selector('#options').withText(sampleText);
   }
}
export default new Page();

but when I try to call selector from test file

test('sampleTest', async t=> {
    await t.click(Page.optionsButton('sometext');
)}

I got an error:

ERROR Cannot prepare tests due to the following error: The "text" argument (undefined) is not of expected type (string or a regular expression).

How to solve this issue ?

Alex Skorkin
  • 4,264
  • 3
  • 25
  • 47
larry
  • 1
  • 1
  • 2

1 Answers1

2

optionsButton is a property and its value is defined using the constructor so the parameter can only be passed at the moment you create the object like this:

new Page('sometext');

It would be better if you write a method in the Page class that returns the selector based on a parameter:

class Page {
  optionsButton(opt) {
    return Selector('#options').withText(opt);
  }
}

export default new Page();
R0_0107
  • 53
  • 5
  • thanks a lot, this is what I needed. Currently I cannot test it because test environment is not working – larry Jan 10 '22 at 16:39
  • '@Romer' thank you, I would like to confirm that your solution works perfectly – larry Jan 23 '22 at 21:43