{"id":760,"date":"2021-07-21T18:42:35","date_gmt":"2021-07-21T18:42:35","guid":{"rendered":"https:\/\/rahulshettyacademy.com\/blog\/?p=760"},"modified":"2024-02-13T06:12:59","modified_gmt":"2024-02-13T06:12:59","slug":"common-cypress-interview-questions","status":"publish","type":"post","link":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/2021\/07\/21\/common-cypress-interview-questions\/","title":{"rendered":"Common Cypress Interview Questions"},"content":{"rendered":"<h3 id=\"t-1626888826259\"><a name=\"_Toc76233657\" style=\"outline: none;\">What is Cypress?<\/a><\/h3>\n<p>Cypress is a next-generation testing tool constructed for the modern web. This tool addresses the critical problems which we face every day like synchronization issues, flaky tests, element loading issues, issues with dynamic elements etc.<\/p>\n<h3 id=\"t-1626888826260\"><a name=\"_Toc76233658\" style=\"outline: none;\">How Cypress architecture is different from selenium?<\/a><\/h3>\n<p>Selenium relies on WebDriver to execute the commands that means selenium executes remote command across the network. But in the cypress, it directly operates inside the browser. So the browser executes the commands which we run as script.<\/p>\n<p><strong>Explanation: <\/strong>Real world scenario,<\/p>\n<p><strong>Consider Selenium,<\/strong><\/p>\n<p>If I have <strong>element.click()<\/strong> in my test script, Once I execute this script selenium sends this code to browser specific drivers like ChromeDriver, Ghecko Driver etc. The browser specific drivers then interact with specific browsers like Chrome, Firefox etc. and executes the click action.<\/p>\n<p><strong>Consider Cypress,<\/strong><\/p>\n<p>The &nbsp;same command <strong>element.click()<\/strong> Considering Cypress.<\/p>\n<p>Once you execute code in Cypress, The cypress directly sends the code to browser and click action gets executed.<\/p>\n<p>So with the above example we can understand that the middle layer that is <strong>webdriver<\/strong> which is present in the selenium but Cypress doesn\u2019t need any middle layer.<\/p>\n<h3 id=\"t-1626888826261\"><a name=\"_Toc76233659\" style=\"outline: none;\">Can I use Cypress framework with C# or Java just like Selenium?<\/a><\/h3>\n<p>No, Cypress is NodeJS based framework. Cypress supports only JavaScript and Typescript as a programming language.<\/p>\n<h3 id=\"t-1626888826262\"><a name=\"_Toc76233660\" style=\"outline: none;\">Could you tell me about some differences between Cypress and Selenium?<\/a><\/h3>\n<ul>\n<li>Selenium supports all major languages like C#, Java, Python, JavaScript, Ruby etc.<\/li>\n<li>Cypress Supports only JavaScript\/Typescript languages<\/li>\n<li>Selenium Commands are executed through web drivers<\/li>\n<li>Cypress Commands Executed directly on the browser<\/li>\n<li>&nbsp;Selenium Supports all major browsers chrome,Edge, Internet Explorer, Safari, Firefox<\/li>\n<li>Cypress supports only Chrome, Edge and Firefox<\/li>\n<li>With Selenium Configuration of drivers and language biding should be done by our own<\/li>\n<li>With Cypress we get ready framework available we just need to install.<\/li>\n<li>Selenium Appium can we used to test native mobile applications<\/li>\n<li>Cypress doesn\u2019t support any native mobile application testing<\/li>\n<\/ul>\n<h3 id=\"t-1626888826264\"><a name=\"_Toc76233662\" style=\"outline: none;\">What are the advantages of Cypress?<\/a><\/h3>\n<ul>\n<li>Cypress is NodeJS modern framework so it works seamlessly with Single Page applications and internal Ajax calls.<\/li>\n<li>Cypress provides Time Travel options, so It takes snapshots of each tests, after execution we can see what happened exactly in each step. We don\u2019t have to do any configuration for this this comes by default with cypress.<\/li>\n<li>Cypress Debuggability feature provides access to developer tools on the browser so we can debug directly in the browser.<\/li>\n<li>Cypress Automatically waits for commands and assertions or any animation to complete so most of the time we don\u2019t have to put additional sleep in our tests.<\/li>\n<li>Since Cypress directly executes commands on browser it is quite faster compared to Selenium based tools.<\/li>\n<li>Cypress runs tests and executes commands directly on browser so it is less flaky.<\/li>\n<li>Cypress provides Video Capture option we can record whole set of tests execution.<\/li>\n<\/ul>\n<h3 id=\"t-1626888826265\"><a name=\"_Toc76233663\" style=\"outline: none;\">Can I use Junit or TestNG in cypress?<\/a><\/h3>\n<p><strong>&nbsp; &nbsp; Or<\/strong><\/p>\n<p><strong>&nbsp; &nbsp; What is Testing Framework Cypress comes with?<\/strong><\/p>\n<p>We cannot use the Junit or TestNG in Cypress, Cypress comes with Mocha and Chai assertion libraries.<\/p>\n<h3 id=\"t-1626888826266\"><a name=\"_Toc76233664\" style=\"outline: none;\">How can I install cypress?<\/a><\/h3>\n<p>If you want to install cypress, we need to install Node first, once we install node we can install cypress with command &nbsp;<strong>npm install cypress<\/strong><\/p>\n<h3 id=\"t-1626888826267\"><a name=\"_Toc76233665\" style=\"outline: none;\">How can I open cypress window and execute tests?<\/a><\/h3>\n<p>Once you install the cypress, we can open cypress using <strong>npm cypress open<\/strong> command and execute the tests by clicking on test file name.<\/p>\n<p><strong><em>Note:<\/em><\/strong> First time when we execute npm cypress open command, cypress prepares the framework ready for you. This involves creating folder structure such as cypress, support, plugin, integration etc. This happens only when we execute npm cypress open command for the first time, subsequent execution of this command only launches the cypress window since set up is done already.<\/p>\n<h3 id=\"t-1626888826268\"><a name=\"_Toc76233666\" style=\"outline: none;\">Could you talk something about Cypress file and folder structures?<\/a><\/h3>\n<ol>\n<li>Cypress uses <strong>cypress.json<\/strong> file, if we want to specify any <strong>custom configuration<\/strong> which is located in the root of our project.<\/li>\n<li>The <strong>folder<\/strong> named <strong>cypress<\/strong> is located in the project root folder which is main folder for cypress automation framework.<\/li>\n<li><strong>By default Cypress folder contains 4 subfolders<\/strong> namely <strong>fixtures<\/strong>, <strong>integration<\/strong>, <strong>plugins<\/strong> and <strong>support<\/strong>.<\/li>\n<li><strong>Fixtures<\/strong> folder can be used to store our external Json data files and we can use these files in our tests using the command cy.fixture().<\/li>\n<li><strong>Integration<\/strong> folder mainly consists of our actual spec\/test files<\/li>\n<li><strong>Plugins<\/strong> folder contains special files that executes in Node before project is loaded, before the browser launches, and during your test execution. This is very&nbsp; helpful when we want to have pre processers and post processors. Files in this folder can be executed after all the test execution is complete as well.<\/li>\n<li>Example: Let us consider, If we want to generate HTML result after all the test is completed. The HTML report generation or collating those HTML reports can be done here in this file.<\/li>\n<li><strong>Support<\/strong> folder contains the special file index.js which will be run before each and every test. Support folder can also be used to create utility methods which will be helpful in through out our automation framework. This file is the perfect place to put all your reusable behavior such as Custom commands or global overrides that you want to be applied to all of your spec files.<\/li>\n<\/ol>\n<h3 id=\"t-1626888826269\"><a name=\"_Toc76233667\" style=\"outline: none;\">What is Cypress CLI?<\/a><\/h3>\n<p>Cypress CLI is unique feature in cypress it provides ability to run our cypress tests in command line. This feature is helpful when we run our cypress tests in pipelines. It provides many options and flags control the cypress test behavior.<\/p>\n<p><strong>Example:<\/strong> use <strong>npm cypress run<\/strong> to run your tests in command line.<\/p>\n<h3 id=\"t-1626888826270\"><a name=\"_Toc76233668\" style=\"outline: none;\">How can I run single specfile in command line?<\/a><\/h3>\n<p>We can run single spec file in command line using <strong>&#8211; &#8211; spec<\/strong> option and specifying test name.<\/p>\n<p>Example: &nbsp;<strong>npm cypress run &#8211; &#8211; spec=\u201dmyspec.ts\u201d<\/strong><\/p>\n<h3 id=\"t-1626888826271\"><a name=\"_Toc76233669\" style=\"outline: none;\">I am new to cypress, I wanted to execute hello world test script in cypress how can I do that could you explain?<\/a><\/h3>\n<p>Once you have installed cypress it will create folder called integration, inside integration folder you can create your first spec file using typescript or JavaScript.<\/p>\n<p>Your spec file should contain <em><strong>describe()<\/strong><\/em> block and or <em><strong>it()<\/strong><\/em> block. Use the cypress command inside this block and execute the test.<\/p>\n<h3 id=\"t-1626888826272\"><a name=\"_Toc76233670\" style=\"outline: none;\">Why should I create my tests inside integration folder, why can\u2019t I create test in other folders?<\/a><\/h3>\n<p>The default tests folder is integration in cypress, so when we execute the tests it will look for integration folder get all the test file.<\/p>\n<p>But you can create the tests in other folder in that case you need to change the configuration and specify where your integration folder is located using option called \u201c<em><strong>integrationFolder<\/strong><\/em>\u201d<\/p>\n<h3 id=\"t-1626888826273\"><a name=\"_Toc76233671\" style=\"outline: none;\">How can see the default configuration in Cypress?<\/a><\/h3>\n<p>You can see the default configuration in Cypress Under on Cypress window &nbsp;Test Runner &gt; Settings and Configurations.<\/p>\n<h3 id=\"t-1626888826274\"><a name=\"_Toc76233672\" style=\"outline: none;\">How can I create suites in cypress?<\/a><\/h3>\n<p>We can create a describe() block the describe block acts as suite, inside that each test can be created as single <em><strong>it()<\/strong><\/em> block.<\/p>\n<p><em><strong>Describe()<\/strong><\/em> is like a suite here.<\/p>\n<h3 id=\"t-1626888826275\"><a name=\"_Toc76233673\" style=\"outline: none;\">Tell me at least 5 Cypress commands?<\/a><\/h3>\n<p><strong>cy.visit():<\/strong> cy.visit() is used to navigate to the specific websiteEx: cy.visit(\u2018http:\/\/www.google.com\u2019);<strong>cy.log():<\/strong> cy.log is used for display cypress console logs during executionEx: cy.log(\u2018test 123\u2019);<strong>cy.get():<\/strong> cy.get is used for getting dom element in cypress, once we get dom element we can perform action on that like click, type etc.Example: let myElement = cy.get(\u2018#username\u2019)<strong>cy.url():<\/strong> cy.url() is used to Get the current URL of the page that is currently active.<em><strong>Example:<\/strong><\/em> Consider you have navigate to https:\/\/google.com using cy.visit();, now you can use the cy.url() to get the url back.<\/p>\n<p style=\"\">\/\/ Example Cypress Code:<\/p>\n<p style=\"\">cy.visit(&#8216;https:\/\/www.google.com&#8217;); \/\/Visit the webpage<\/p>\n<p style=\"\">let myurl= cy.url(); \/\/Get webpage url<\/p>\n<p style=\"\">cy.url().should(&#8216;include&#8217;, &#8216;google&#8217;); \/\/Assert URL<\/p>\n<h3 id=\"t-1626888826276\"><a name=\"_Toc76233674\" style=\"outline: none;\">How can I click on the button in Cypress?<\/a><\/h3>\n<p><strong>.click()<\/strong> function is used to click the element on cypress. The click function should be chained with element. I.e we need to get the desired element first and we need to click.<\/p>\n<p><strong>Example:<\/strong> If we want to click on Google Search button, First we need to get the element of search using selectors like below.<\/p>\n<p style=\"\">cy.get(&#8216;#search&#8217;).click()<\/p>\n<h3 id=\"t-1626888826277\"><a name=\"_Toc76233675\" style=\"outline: none;\">List out some commands which I can use to interact with DOM elements?<\/a><\/h3>\n<p><strong> .click() :<\/strong> is used to click on the element<\/p>\n<p><strong><em>Example:<\/em><\/strong><\/p>\n<p style=\"\">cy.get(&#8216;#search&#8217;).click()<\/p>\n<p><strong>.dblclick() : is used to double click on the element.<\/strong><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"\">cy.get(&#8216;input[name=&#8221;btnK&#8221;]&#8217;).dblclick();<\/p>\n<p><strong>.rightclick(): is used to right click on the elementExample:<\/strong><\/p>\n<p style=\"\">cy.get(&#8216;body&#8217;).rightclick();<\/p>\n<p><strong>.type()<\/strong> : is used to type on element or text boxes<strong>Example:<\/strong><\/p>\n<p style=\"\">cy.get(&#8216;input[name=&#8221;texbox&#8221;]&#8217;).type(&#8220;This is to test&#8221;);<\/p>\n<p><strong>.clear()<\/strong> : is used to clear the fields or text boxes.<strong>Example:<\/strong><\/p>\n<p style=\"\">cy.get(&#8216;input[name=&#8221;q&#8221;]&#8217;).clear();<\/p>\n<p><strong>.check() :<\/strong> is used to Check checkbox(es) or radio(s).<strong>Example:<\/strong><\/p>\n<p style=\"\">cy.get(&#8216;input[name=&#8221;chkbox&#8221;]&#8217;).check();<\/p>\n<p><strong>.uncheck() :<\/strong> is used to unCheck checkbox(es) or radio(s).<strong>Example:<\/strong><\/p>\n<p style=\"\">cy.get(&#8216;input[name=&#8221;chkbox&#8221;]&#8217;).uncheck();<\/p>\n<p><strong>.select()<\/strong>: Select an &lt;option&gt; within a &lt;select&gt;<strong>Example:<\/strong><\/p>\n<p style=\"\">cy.get(&#8216;select&#8217;).select(&#8216;user-1&#8217;)<\/p>\n<h3 id=\"t-1626888826278\"><a name=\"_Toc76233676\" style=\"outline: none;\">Consider a scenario, I have link on webpage and clicking this link opens new window I need to verify some text in that window. How can I verify?<\/a><\/h3>\n<p>Cypress by default doesn\u2019t support, working with new windows so we need to follow different approach here.<\/p>\n<p><span style=\"text-decoration: underline;\">Let\u2019s consider example:<\/span><\/p>\n<p><em><strong>&lt;a href=&#8221;http:\/\/google.com&#8221; target=&#8221;_blank&#8221;&gt; Take me to Google &lt;\/a&gt;<\/strong><\/em><\/p>\n<p>In the above code clicking on Take me to Google link opens the new window the reason is we have attribute <strong>target=&#8221;_blank&#8221;.<\/strong> So now if we remove attribute. <strong>target=&#8221;_blank&#8221;<\/strong> then it will open in the same window. That can be performed by below code<\/p>\n<p style=\"\">\/\/ We can remove the offending attribute &#8211; target=&#8217;_blank&#8217;<\/p>\n<p style=\"\">\/\/ that would normally open content in a new tab.<\/p>\n<p style=\"\">cy.get(&#8216;a[href*=&#8221;google&#8221;]&#8217;).invoke(&#8216;removeAttr&#8217;, &#8216;target&#8217;).click()<\/p>\n<p style=\"\">\/\/ after clicking the &lt;a&gt; we are now navigated to the new page and we can assert that the url is correct<\/p>\n<p style=\"\">cy.url().should(&#8216;include&#8217;, &#8216;google&#8217;)<\/p>\n<h3 id=\"t-1626888826279\"><a name=\"_Toc76233677\" style=\"outline: none;\">What are the selectors supported by Cypress?<\/a><\/h3>\n<p>By Default cypress supports only CSS selectors, However we can use third party plugin to use Xpath selectors.<\/p>\n<h3 id=\"t-1626888826280\"><a name=\"_Toc76233678\" style=\"outline: none;\">Can I use XPath in Cypress?<\/a><\/h3>\n<p>Cypress doesn\u2019t support xpath by default, but if we install third party plugin like <strong>cypress-xpath <\/strong>we can use xpath in your script.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>We need to install <strong>cypress-xpath <\/strong>in our project first using<\/p>\n<p style=\"\">&nbsp;npm install cypress-xpath<\/p>\n<p>Once we have installed the above plugin, we need to add entry into project&#8217;s cypress\/support\/index.js<\/p>\n<p style=\"\">require(&#8216;cypress-xpath&#8217;)<\/p>\n<p>After that we can use the xpath in out script like below<\/p>\n<p style=\"\">&nbsp;cy.xpath(&#8216;\/\/ul[@]\/\/li&#8217;)<\/p>\n<h3 id=\"t-1626888826281\"><a name=\"_Toc76233679\" style=\"outline: none;\">What is environment variable in Cypress?<\/a><\/h3>\n<p>The Environment Variables is a variable whose value sets at the level of the operating system, and it sets outside the context of the program or framework<\/p>\n<p>In order to use environment variable we need to define in <em><strong>cypress.json<\/strong><\/em><\/p>\n<p style=\"\">{<\/p>\n<p style=\"\">&nbsp; &#8220;env&#8221;: {<\/p>\n<p style=\"\">&nbsp;&nbsp;&nbsp; key: value<\/p>\n<p style=\"\">&nbsp; }<\/p>\n<p style=\"\">}<\/p>\n<p>Once we define we can access them in our script like below<\/p>\n<p style=\"\">cy.visit(Cypress.env(&#8216;key&#8217;));<\/p>\n<p>This environment variable value we can change dynamically using command line arguments<\/p>\n<p style=\"\">cypress run &#8211;env &#8220;Key&#8221;=&#8221;Value&#8221;<\/p>\n<p>Lets consider one example, let me create one evnrinment variable in <strong>cypress.json<\/strong><\/p>\n<p><strong>For example:<\/strong><\/p>\n<p><strong><img decoding=\"async\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAmgAAABgCAYAAACpI44NAAAP90lEQVR4Ae2dvY4byRVG9RbaFbCzEiDBUOTYoXI7dG5jggXsR1jZ0T6Aok02sB5AkbN5gXktGt9YZ3H3bpPdbDanm+QZYHC7\/qtuHVZ9LJLdL3bt7+7ubpf\/ly9fnvRPPdr\/+1M\/6AcZkAEZkAEZkIHOQJNhvwZf\/Hr19YKCCjQhggWtLMiADMiADMjAeRjoOozwZIH28PCwe3x83H3+\/HnSyZoTeZ6J1K\/6VQZkQAZkQAauhwEEWbeTBFpE2ZcvXyYJM07ehOd64HEunUsZkAEZkAEZOA8DXZgRniTQIs4+fvyoQPv6\/TwhPQ+k+lW\/yoAMyIAM3BoDCLJuFWiKrqcfhdzaC8LxugnIgAzIgAxsgYEuzAgr0BRoCjQZkAEZkAEZkIGVGECQdTtJoOXHAff3937EudLkbUHh2wffacqADMiADMjA8gx0YUb4oEDjl5t88f8Y6yQuP4n6VJ\/KgAzIgAzIwHUxgCDr9qBAQ5B5gnZdMPjidj5lQAZkQAZkYBsMdGFGeJJA81ec25hEX0zOgwzIgAzIgAxcFwMIsm4VaH6vzC+GyoAMyIAMyIAMrMRAF2aEFWgrTYjvgK7rHZDz6XzKgAzIgAzMYQBB1q0CTYHmuyYZkAEZkAEZkIGVGOjCjPAkgfbp0ycf9bTSxM1R45bxXZw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n72PmCZyiWwZkQAZkQAZkYGEGThJoHz58eDpFQ7BNsSr761D2zqPzKAMyIAMyIAPnY0CBtrDiFdbzwapv9a0MyIAMyMCtMKBAU6B5LC0DMiADMiADMrAxBk4SaPlIMz8UyKObpny8mTy3onwdp+\/yZEAGZEAGZEAG5jJwskCL6Pry5cvkX3TO7ajlhFwGZEAGZEAGZOBWGDhZoHmC5ovlVl4sjlPWZUAGZEAGnouBkwSav+IU1OcC1XZkTQZkQAZk4JYYUKBt7EuBtwSfY3WxlQEZkAEZkIFhBhRoCjR\/uCEDMiADMiADMrAxBhRoG5sQ30kMv5PQL\/pFBmRABmTglhg4SaDlUU\/5BefUW2x4mw1fXLf04nKs8i4DMiADMjCXgVkCzYelC9xc4CwnOzIgAzIgAzIwzsAsgXbMiVnP66SMT4o+0kcyIAMyIAMycNsMTBZo+zIarwf0gB7QA3pAD+gBPfA8HnjxPM3Yih7QA3pAD+gBPaAH9MBUDyjQpnrKfHpAD+gBPaAH9IAeeCYP\/A+OiKJunlZ8kgAAAABJRU5ErkJggg==\" alt=\"\"><\/strong><\/p>\n<p>The above value of environment variable can be accessed through<\/p>\n<p style=\"\">let tokenvalue = Cypress.env(\u2018token\u2019);<\/p>\n<p>If I want to change the value dynamically using command line I can do so.<\/p>\n<p style=\"\">cypress run &#8211;env &#8220;token&#8221;=&#8221;ieoeeoeieoeie&#8221;;<\/p>\n<h3 id=\"t-1626888826282\"><a name=\"_Toc76233680\" style=\"outline: none;\">How can I change the baseUrl in cypress dynamically?<\/a><\/h3>\n<p>When we say executing multiple environment that means we are executing scripts on Staging, QA, production, Usually website remains same only website url changes. That is called baseUrl, we can change baseUrl dynamically by passing baseUrl in command line.<\/p>\n<p>First we need to configure cypres.json with baseUrl option<\/p>\n<p><strong>Code:<\/strong> &lt;cypress.json&gt;<\/p>\n<p style=\"\">&#8220;baseUrl&#8221;: &#8220;https:\/\/qa.web.com&#8221;<\/p>\n<p>Then we can override this base url in command line while running our tests.<\/p>\n<p style=\"\">npx cypress run &#8211;config baseUrl=&#8221;https:\/\/www.staging-website.com\/&#8221;<\/p>\n<p>the above code runs the tests on staging website. Earlier configured qa.web.com will be overridden with command line argument <em><strong>baseUrl <\/strong><\/em>value.<\/p>\n<h3 id=\"t-1626888826283\"><a name=\"_Toc76233681\" style=\"outline: none;\">What is Preserve cookies in Cypress? Why do we need that?<\/a><\/h3>\n<p>Cypress by default clear the cookies after every test. In order to stop clearing cookies we need to use preserve cookies option in cypress<\/p>\n<p><b>Example<\/b><strong>:<\/strong><\/p>\n<p>I have code like below<\/p>\n<p style=\"\">describe(&#8216;my test&#8217;, () =&gt; {<\/p>\n<p style=\"\">it(&#8216;test1&#8217;, () =&gt; {<\/p>\n<p style=\"\">\/\/Some code to test<\/p>\n<p style=\"\">});<\/p>\n<p style=\"\">it(&#8216;test2&#8217;, () =&gt; {<\/p>\n<p style=\"\">\/\/Some code to test<\/p>\n<p style=\"\">});<\/p>\n<p style=\"\">it(&#8216;test3&#8217;, () =&gt; {<\/p>\n<p style=\"\">\/\/Some code to test<\/p>\n<p style=\"\">});<\/p>\n<p style=\"\">})<\/p>\n<p>In the above code we have three tests namely test1, test2, test3,Lets consider you have logged into your application in test1, after execution of test1 when it moves to test2, it again asks for login because login sessions and cookies are cleared after test1. <\/p>\n<p>This is default bevaior in cypress. We can overcome this problem by adding code in our tests like below.<\/p>\n<p style=\"\">describe(&#8216;my test&#8217;, () =&gt; {<\/p>\n<p style=\"\">beforeEach(() =&gt; {<\/p>\n<p style=\"\">Cypress.Cookies.preserveOnce(&#8216;session_id&#8217;, &#8216;remember_token&#8217;)<\/p>\n<p style=\"\">});<\/p>\n<p style=\"\">it(&#8216;test1&#8217;, () =&gt; {<\/p>\n<p style=\"\">\/\/Some code to test<\/p>\n<p style=\"\">});<\/p>\n<p style=\"\">it(&#8216;test2&#8217;, () =&gt; {<\/p>\n<p style=\"\">\/\/Some code to test<\/p>\n<p style=\"\">});<\/p>\n<p style=\"\">it(&#8216;test3&#8217;, () =&gt; {<\/p>\n<p style=\"\">\/\/Some code to test<\/p>\n<p style=\"\">});<\/p>\n<p style=\"\">})<\/p>\n<h3 id=\"t-1626888826284\"><a name=\"_Toc76233682\" style=\"outline: none;\">What is custom commands in Cypress?<\/a><\/h3>\n<p>Cypress comes with default set of commands like <strong>cy.visit<\/strong>, <strong>cy.relaod<\/strong> etc.. like this we can create our custom commands in cypress.<\/p>\n<p>For example: I can create some commands like <strong>cy.login()<\/strong>, which will enter the <strong>username<\/strong> and <strong>password<\/strong> and clicks on <strong>submit<\/strong>.<\/p>\n<p>We can accomplish the custom command with below lines of code<\/p>\n<p>All custom commands are first configured in <strong>cypress\/support\/command.js<\/strong> in this file we need to create a command like<\/p>\n<p style=\"\">Cypress.Commands.add(&#8220;login&#8221;, (username, password) =&gt; {<\/p>\n<p style=\"\">&nbsp; \/\/adding a new command named login<\/p>\n<p style=\"\">&nbsp; cy.get(&#8220;#username&#8221;).type(username);<\/p>\n<p style=\"\">&nbsp; cy.get(&#8220;#password&#8221;).type(password);<\/p>\n<p style=\"\">&nbsp; cy.get(&#8220;#login&#8221;).click();<\/p>\n<p style=\"\">});<\/p>\n<p>Once we add the above command now we can use in our script like below.<\/p>\n<p style=\"\">cy.visit(&#8220;http:\/\/mysite.com\/login.html&#8221;);<\/p>\n<p style=\"\">cy.login(&#8216;Myusername&#8217;,&#8217;My Passowrd&#8217;);<\/p>\n<h3 id=\"t-1707804713029\"><a name=\"_Toc76233683\" style=\"outline: none;\">How can I type Keypress in Cypress?<\/a><\/h3>\n<p>Keypress can be done in cypress using <strong>.type()<\/strong> command, If we need to use the keys like <strong>CTRL<\/strong>, <strong>SHIFT<\/strong>, <strong>ALT<\/strong> etc then we need to specify in braces like below<\/p>\n<p style=\"\">cy.get(\u2018login\u2019).type(&#8216;{shift}{alt}hello&#8217;));<\/p>\n<h3 id=\"t-1707804713030\"> <a name=\"_Toc76233684\" style=\"outline: none;\">How can I read the value from Cypress Configuration file?<\/a><\/h3>\n<p>We can read the cypress.config values from cypress using <strong>Cypress.config();<\/strong><\/p>\n<p><strong>Example:<\/strong><\/p>\n<p>If I have defined pageLoadTimeout in config file like blow.<\/p>\n<p>Below code is in <strong>cypress.json<\/strong><\/p>\n<p style=\"\">{<\/p>\n<p style=\"\">&nbsp; &#8220;pageLoadTimeout&#8221;: 60000<\/p>\n<p style=\"\">}<\/p>\n<p>I can access the same in my script like<\/p>\n<p style=\"\">let timeout = Cypress.config(&#8216;pageLoadTimeout&#8217;)<\/p>\n<h3 id=\"t-1707804713031\"><a name=\"_Toc76233685\" style=\"outline: none;\">How to get the title of the page?<\/a><\/h3>\n<p><strong>cy.title()<\/strong> : cy.title() can be used to get the title of the current active window<\/p>\n<p><strong>example:<\/strong><\/p>\n<p style=\"\">cy.visit(&#8216;http:\/\/www.facebook.com&#8217;)<\/p>\n<p style=\"\">cy.title().should(&#8216;eq&#8217;,&#8217;Facebook Login or Signup&#8217;)<\/p>\n<h3 id=\"t-1707804713032\"><a name=\"_Toc76233686\" style=\"outline: none;\">How can I use the sleep in Cypress?<\/a><\/h3>\n<p><strong>cy.wait() <\/strong>: cy.wait is used to wait for specific amount of time.<\/p>\n<p><strong>Example:<\/strong><\/p>\n<p style=\"\">cy.wait(1000)<\/p>\n<h3 id=\"t-1707804713033\"><a name=\"_Toc76233687\" style=\"outline: none;\">How can I get the first and last child of the selected element?<\/a><\/h3>\n<p>.first() and .last() is used for getting first and last child of the element<\/p>\n<p>Consider example,<\/p>\n<p style=\"\">cy.get(&#8216;input[name=&#8217;rows&#8217;])<\/p>\n<p>let\u2019s say this selectors returns array of elements say 6 elements.<\/p>\n<p>Then<\/p>\n<p>First child can be obtained using<\/p>\n<p style=\"\">cy.get(&#8216;input[name=&#8217;rows&#8217;]).first()<\/p>\n<p>Last child can be obtained using<\/p>\n<p style=\"\">cy.get(&#8216;input[name=&#8217;rows&#8217;]).last()<\/p>\n<h3 id=\"t-1707804713034\"><a name=\"_Toc76233688\" style=\"outline: none;\">What is shadow Dom? How can I access shadow DOM in cypress?<\/a><\/h3>\n<p>Shadow DOM allows hidden DOM trees to be attached to elements in the regular DOM tree \u2014 this shadow DOM tree starts with a shadow root.<\/p>\n<p><strong>Example: <\/strong>Consider we have shadow dom like below<\/p>\n<p style=\"\">&lt;div&gt;<\/p>\n<p style=\"\">&nbsp; #shadow-root<\/p>\n<p style=\"\">&nbsp; &lt;button&gt;Click me&lt;\/button&gt;<\/p>\n<p style=\"\">&lt;\/div&gt;<\/p>\n<p>We can access the above shadow dom using below code in cypress<\/p>\n<p style=\"\">cy.get(&#8216;.shadow-host&#8217;).shadow().find(&#8216;.my-button&#8217;).click()<\/p>\n","protected":false},"excerpt":{"rendered":"<p>What is Cypress? Cypress is a next-generation testing tool constructed for the modern web. This tool addresses the critical problems which we face every day like synchronization issues, flaky tests, element loading issues, issues with dynamic elements etc. How Cypress architecture is different from selenium? Selenium relies on WebDriver to execute the commands that means [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":761,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21],"tags":[],"class_list":["post-760","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-interview-questions","post-wrapper","thrv_wrapper"],"_links":{"self":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/760","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=760"}],"version-history":[{"count":14,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/760\/revisions"}],"predecessor-version":[{"id":2834,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/760\/revisions\/2834"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/761"}],"wp:attachment":[{"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=760"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=760"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rahulshettyacademy.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=760"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}