Today I Learned - Rocky Kev

TIL Scoping with Cypress

POSTED ON:

TAGS:

Today I learned about how to scope with Cypress:

cy.findByRole("main").within(() => {
// Everything in here is scoped to the "main" element only
cy.findByRole('heading', { name: "Dashboard" });
});

So you wanted to find a specific element called heading.

But you have a BUNCH of elements called heading.

By using within() helper, you can force it to only look inside main elements.

Cypress within

<form>
<input name="email" type="email" />
<input name="password" type="password" />
<button type="submit">Login</button>
</form>

<script>
cy.get('form').within(($form) => {
// you have access to the found form via
// the jQuery object $form if you need it

// cy.get() will only search for elements within form,
// not within the entire document
cy.get('input[name="email"]').type('john.doe@email.com')
cy.get('input[name="password"]').type('password')
cy.root().submit()
})
</script>

via https://dev.to/devteam/what-cypress-e2e-testing-has-taught-us-about-our-code-5aco


Related TILs

Tagged:

TIL what is npm Script

Despite their high usage they are not particularly well optimized and add about 400ms of overhead. In this article we were able to bring that down to ~22ms.

TIL fancy methods to transform Javascript Objects

You can use Object.entries(), Object.keys(), Object.fromEntries()...

TIL how to hide your JS code

ONE THING TO NOTE: Encrypting a script is stronger than obfuscation, both methods are still not adequate to protect secret content. Honestly, I don't think it's worth it on a production site, and instead just go with pure server-side if you want security. But it's fascinating.