'Cypress: Is there a way to write a command that can check a checkbox using UI

Currently, I am following cypress's example below and it works perfectly for the type commands. However, I have too many commands and I am trying to condense whichever ones I can. In this case, I only need to be able to check a box in certain tests but not sure how I would go about it. Any hints/ tips/ advice would be greatly appreciated. :)

Cypress.Commands.add('typeLogin', (user) => {

  cy.get('input[name=email]').type(user.email)
  cy.get('input[name=password]').type(user.password)
  cy.get('input[name=checkbox]').check(user.checkbox)?

})

In the test:

    const user = { email: '[email protected]', password: 'Secret1' }';

    cy.typeLogin(user ) => {...


Solution 1:[1]


<script type="text/javascript">
var newWin;
function openPopup()
{
 newWin=window.open('https://www.somesite/url.php','window','width=400,height=600,scrollbars=0,resizable=1,top=300,left=300');

 document.onmousedown=()=>{
   setTimeout(focusPopup,2000)

};
 document.onkeyup=focusPopup;
 document.onmousemove=focusPopup;
}
function focusPopup(){
  if(!newWin.closed){
    newWin.focus();
  }
}
</script>

Solution 2:[2]

Just use setTimeout to add the delay.

The global setTimeout() method sets a timer which executes a function or specified piece of code once the timer expires.

var newWin;

function openPopup() {
  newWin = window.open('https://www.somesite/url.php', 'window', 'width=400,height=600,scrollbars=0,resizable=1,top=300,left=300');

  document.onmousedown =  function() {
    setTimeout(focusPopup, 2000);
  };
  document.onkeyup = focusPopup;
  document.onmousemove = focusPopup;
}

function focusPopup() {
    if (!newWin.closed) {
      newWin.focus();
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Musafiroon
Solution 2