Selenium Behind: Taking the Leap Into the Cypress World Part-II
Tags: cypress, javascript, testing, testautomatio
Published: 2023-02-02 00:32:37

Here is the [link of the Part-I](https://dev.to/atulkrsharma/selenium-behind-taking-the-leap-into-the-cypress-world-2637), if you've landed directly here.

Many are curious about Part-I of this dev blog that only features content (I agree but that's the bare minimum theoretical background one must have to start) and no code and it echoes in every Developer's mind, Isn't it? Mine too.

> “Talk is cheap. Show me the code.” ― Linus Torvalds

so, Let's continue the rest of the concepts needed to work with [Cypress](https://www.cypress.io/)

- **Subject Management in Cypress**

In Cypress, commands do not return their subjects, instead yield them. Cypress commands are asynchronous and get queued for execution at a later time. During execution, subjects are yielded from one command to the next and a lot of helpful Cypress code runs between each command to ensure everything is in order.
Okay, you've written code in [Selenium](https://www.selenium.dev/) like this below:-

![Subject Management in Cypress](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h81jvb6gcm7smmr9xfec.PNG)

~~It's going to work like charm~~. Sorry, _Did you forget??_ In Cypress, commands do not return their subjects, instead yield them.
Now, _a million dollar question- How can user interact with subject directly_? My only reply will be this:

> "Patience is not just about waiting for something; it's about how you wait, or your attitude while waiting." - Joyce Meyer

**_As advised in the above quote, talking about how or your attitude while waiting. Do you know that Cypress wait command can wait for a specific request to respond?_** 

Alrighty folks, let's park this subject for now, tackle this some other article & continue our thrilling ride through the fundamentals of Cypress.

- **Context switching b/w Cypress & jQuery [.then() & .wrap()]**

After going above Point, we know that commands do not return their subjects then, **_user can interact with subject directly with command -then()_**. Whatever is returned from the callback function becomes the new subject and will flow into the following command and Cypress will wait for that to resolve before continuing forward through the chain of commands.


![Cypress .then command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2smsessbt8vz2t6jkdo4.PNG)

Before using .then(), user is in Cypress context and can use the Cypress commands as its disposal. See below, find() command of Cypress is used & tooltip is confirming the same.


![ Before .then() command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qss5uhy7ydd6r0nohjx4.PNG)


After using .then() command, user comes into jQuery context leaving the Cypress context. See below, [find() command of jQuery](https://api.jquery.com/find/) is used, tooltip is showing the same and work with your Subject, as desired.


![After .then() command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lre7kapu6k0xvr6m8037.PNG)

Oh No!! We're stuck again in the jQuery context and someone tell me how to get out of this context switch to Cypress context??

_It's my pleasure to remind you the same quote by "Joyce Meyer" about Patience._

Now, Cypress has built .wrap command to switch to Cypress context, so that user can start leveraging the Cypress commands.  See below, jQuery object is wrapped with Cypress .wrap command, regular Cypress commands can be used again to carry on the tests. Observe the tooltip and it's showing up that Cypress find() command is being used now.

![After .wrap command](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zpntijx0583x8utedad2.PNG)

_Catch you in the next article coming next week_. Feel free to comment on this.

_Please refer to my online portfolios:_
[LinkedIn](https://www.linkedin.com/in/atulkmrsharma/)
[GitHub](https://github.com/AtulKrSharma)
[StackOverflow] (https://stackoverflow.com/users/10837620/atul-kumar-sharma)