Search code examples
javascripttypescripttestingautomated-testsplaywright

PLAYWRIGHT testing - How to skip to the next action if a click on a button is not possible (is not present)?


as anticipated in the question, I'd like to know if is possible to attempt a click action on a button, but if it's not in the page then the test skip that action (without being stucked or throwing an error) and continue to the next one. To be more specific, I have a modal where I can add rows to a table, but what I'd like to do is to add a new row only if a given button in the modal is not present, while if it's present the action would be to press first on that button and then proceeding to add a new row.

I ask you if it's possible because I'd like to avoid conditional statements during the test. Just let me know, thank you in advance :)


Solution

  • You can check if the element is present then do something if not then do nothing or something else.

     const modal = page.locator('modal-selector');
        
     if (await modal.isVisible()) {
       // do thing
     } else {
       // do another thing
     }