What is the Cypress equivalent of Protractor's browser.executeScript() function?

Viewed 221

I'm current trying to convert my Protractor tests to Cypress. Trying to convert Protractor's browser.executeScript() function to Cypress has always made me hit a wall.

For instance, I have one Protractor function that looks something like this below. This function clicks the button element just in case this.button.click() doesn't work.:

this.button = element(by.id(button));

clickExactButton(): promise.Promise<void> {
  return browser.executeScript(
    "arguments[0].click();",
    this.button
  );
}

How do I make a Cypress equivalent of this clickExactButton() function?

I have another Protractor function that returns browser.executeScript(). This creates a session storage item called http_responses and then it adds all JSON data from jsonResponse[] into it.

inject(jsonResponses: jsonResponse[]): promise.Promise<unknown> {

  return browser.executeScript((res) => {
    let currentRes = sessionStorage.getItem('http_responses') || '[]';

    currentRes = JSON.parse(currentRes);

    currentRes = res.concat(currentRes);

    sessionStorage.setItem('http_responses', JSON.stringify(currentRes));
  }, jsonResponses);
}

A jsonResponse[] object looks something like this:

export const ExampleData: jsonResponse[] = [
  {
    id: '1',
    name: 'Bob',
    department: 'IT'  
  },
  {
    id: '2',
    name: 'Alice',
    department: 'Sales' 
  },
  {
    id: '3',
    name: 'David',
    department: 'InfoSec' 
  }
]

How do I make a Cypress equivalent of this inject() function?

0 Answers
Related