Cypress
Cypress
When considering Java versus JavaScript, you’ll notice a few key differences. Java
is a compiled language, meaning that you write code, then run it through a compiler
and create bytecode. The bytecode is then run in a Java Virtual Machine (JVM),
which is likely the software you have on your computer. JavaScript is an
interpreted language. It doesn’t get compiled but is interpreted as the script
runs. It's commonly used to create interactive websites. You’re reading this right
now on a page running JavaScript.
github Frameworks:
https://github1s.com/prashanthkrishnashyam/cypress-bdd-pom-test/
https://github1s.com/rameshbaskar/cypress_test/
https://github1s.com/Ranjit005/CypressTest/
https://github1s.com/chaitanyaDBS/CypressFramework
-----------------------------------------------------------------------------------
----------
Cypress is a javascript-based front-end testing tool written in node js. Cypress
executes tests within the browser and makes the testing process easier and more
reliable.
Behind Cypress is a Node server process. Cypress and the Node process constantly
communicate, synchronize, and perform tasks on behalf of each other. Cypress also
operates at the network layer by reading and altering web traffic on the fly. This
enables Cypress to not only modify everything coming in and out of the browser but
also to change code that may interfere with its ability to automate the browser.
Features:
1)Cypress can run tests and also execute commands on the browser. so tests are less
flaky and much fast.
2)Automatic wait
3)Cypress can take snapshots of tests after executing each step. We don’t need to
configure extra plugins like selenium.
4)Built-in assertions available.
5)Network traffic control.
Components:
Test Runner: It tests in an interactive runner, which further helps by letting you
see the command and execute the same while viewing the application that is under
the test.
App Preview: It helps in seeing the tests while executing the commands.
Test Status: It assists in displaying a summary of what tests are in Progress and
have Failed or Passed.
Command Log: It showcases the command logs while executing the tests.
URL Preview: It displays the URL of the test and assists in tracking the entire URL
Route.
Viewport Sizing: It helps in setting the app viewport size for testing varying
layouts.
Testing Framework
We cannot use the Junit or TestNG in Cypress, Cypress comes with Mocha and Chai
assertion libraries.
-----------------------------------------------------------------------------------
----------
Locators
Cypress only supports CSS Selector. However, we can use the Cypress-Xpath plugin to
work with Xpath.
npm install -D @cypress/xpath and add require('@cypress/xpath'); it in
support>e2e.js
How will you get the first as well as the last child of a chosen element?
The first and the last child of a chosen element can be acquired through .first()
and .last() command. For example:
cy.get('input[name='rows'])
Let’s suppose these selectors return several elements, for instance, say six of
them. So:
cy.get('input[name='rows']).first()
The last child can be obtained through:
cy.get('input[name='rows']).last()
-----------------------------------------------------------------------------------
----------
Can we use BDD with Cypress?
Cypress does not offer official BDD built-in support, however, the NPM Cypress-
Cucumber-Preprocessor plugin allows you to write your tests in BDD Cucumber Syntax,
which will automatically translate them into Cypress.
-----------------------------------------------------------------------------------
----------
Hooks
Cypress hooks are used to define or set preconditions that we want to execute
before a set of tests or before each test. The available hooks are
before(),beforeEach(),after(),afterEach().
-----------------------------------------------------------------------------------
----------
How to read values from the Configuration file in Cypress.
We can read the cypress.config values from cypress using Cypress.config();
For example, if we have defined the env in the config.json file like this:
{“env”: stage}
same can be accessed in scripts like
let timeout = Cypress.config(‘env’)
Scroll
cy.get('footer').scrollIntoView()
or
cy.get('#nav').scrollIntoView({ offset: { top: 150, left: 0 } }) //scroll using
offset
Executing
npm cypress run - - spec="my spec.ts"" // to run only specific file
cy.get('#locator').shadow().find('.nb-btn').click()
cy.visit(Cypress.env('key'));
This environment variable value can be changed dynamically through command line
arguments:
{
“env”: {
token: “ueidkskslsdfjsdlf”
}
}
This environment variable’s value can be accessed via:
How will you define preserve cookies? Why are they important?
By default, Cypress clears cookies after each test. To stop clearing these cookies,
we use preserve cookies in Cypress. For instance:
Here is a code:
Let’s suppose you’ve logged into your app in test1. After executing the test1, when
it goes to test2, it again asks to login as login sessions and cookies were cleared
after test1.
All of this happens by default in Cypress. This problem can be handled by adding a
code in the test as mentioned below: