0% found this document useful (0 votes)
18 views5 pages

Cypress

Java is a compiled language that runs on the Java Virtual Machine, while JavaScript is an interpreted language commonly used for interactive web pages. Cypress is a JavaScript-based testing tool that offers features like automatic waiting, built-in assertions, and network traffic control, but only supports JavaScript and specific browsers. Key differences between Cypress and Selenium include Cypress's easier configuration and focus on JavaScript, while Selenium supports multiple languages and browsers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views5 pages

Cypress

Java is a compiled language that runs on the Java Virtual Machine, while JavaScript is an interpreted language commonly used for interactive web pages. Cypress is a JavaScript-based testing tool that offers features like automatic waiting, built-in assertions, and network traffic control, but only supports JavaScript and specific browsers. Key differences between Cypress and Selenium include Cypress's easier configuration and focus on JavaScript, while Selenium supports multiple languages and browsers.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 5

What is the difference between Java and JavaScript?

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.

Browsers : Chrome, Chromium, Edge, Firefox, Electron

How Cypress is different then Selenium.


Cypress Supports only JavaScript/Typescript languages while Selenium supports all
major languages like C#, Java, Python, JavaScript, Ruby, etc.
Selenium supports all major browsers Chrome, Edge, Internet Explorer, Safari, and
Firefox while Cypress support only chrome, firefox, and edge.
Configuration is difficult in selenium however Cypress comes with ready to use
framework.
Cypress is built on top of the mocha framework.
-----------------------------------------------------------------------------------
----------
Test Suit Creation
describe('Test Suite', () => {
it('Tc01', () => {
//code
})
})

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:

The first child can be obtained through:

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’)

How is the test data maintained in Cypress?


The fixtures folder keeps all the necessary data in the cypress project. Basically,
it helps us to get the data input from external files. All the test data can be
utilized by more than one test. All fixture data has to be declared within the
before hook block.
cy.fixture(path of test data)
-----------------------------------------------------------------------------------
----------
Assertions
How many types of assertions are available in Cypress?
There are 2 types of assertions available in Cypress
1)Implicit Assertions: We can have positive as well as negative implicit
assertions.
1.1)Positive Assertion
cy.get('li.todo').should('have.length', 3)
1.2)Negative Assertion
cy.get('li.todo').should('not.have.length', 2)
2)Explicit Assertions: Explicit assertions are used to perform some custom logic
before applying assertion.
const employee =
{ name: 'QA', age: 30, }
assert.isObject(employee, 'value is object')
-----------------------------------------------------------------------------------
----------
API Automation
How to perform API testing in Cypress?
describe('API Testing in cypress', function () {
it('Hit Get Request validate its response status code and body', () => {
cy.request({
method: 'GET',
url: 'https://randomuser.me/api/',
qs: 'results=1'
}).then((response) => {
expect(response.status).to.eq(200)
expect(response.body).to.have.property('info')
})
})
})
-----------------------------------------------------------------------------------
----------
Test Execution
How to execute tests in order in Cypress?
To execute Cypress tests in order put all the file names in the desired order in
the Cypress.JSON file under the test files property section.
-----------------------------------------------------------------------------------
----------
Reusability
How to handle reusability in the Cypress framework?
By default Cypress includes the index.js and commands.js in the Support folder
Index.js run before every test file. We can put reusable behavior such as custom
commands and global overrides in this folder.
-----------------------------------------------------------------------------------
----------
What are the drawbacks of the Cypress testing tool?
Supports only JavaScript.
No support for multiple tabs.
Iframes are not supported.
Only available for Web Testing.
-----------------------------------------------------------------------------------
----------
How to click a hidden element in Cypress?
cy.get('.checkbox').check({ force: true })
Upload File
cy.get('#upload').selectFile('cypress/images/logo.png')

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

How will you access shadow DOM in Cypress?


Shadow DOM generally helps in letting hidden DOM Trees that has to be attached to
the elements in a regular DOM Tree. Shadow DOM can be comprehended through this
command:

cy.get('#locator').shadow().find('.nb-btn').click()

How will you press keyboard keys in Cypress?


cy.get('#locator').type('{shift}{alt}'));

How can you create custom commands in Cypress?


Cypress.Commands.add("login", (username, password) => {
cy.get("#username").type(username);
cy.get("#password").type(password);
cy.get("#login").click();
});
-----------------------------------------------------------------------------------
----------
How will you change the baseUrl dynamically?
npx cypress run --config baseUrl="
-----------------------------------------------------------------------------------
----------
Environment Variables
Can you define an environment variable in Cypress?
The environment variables are referred to such variables whose value is set at a
level of the operating system. It is set outside the content of a framework or a
program. To use an environment variable, we have to define it in cypress.json.
{
"env": {
key: value
}
}
Once we have defined, we can access them in the script just the way mentioned
below:

cy.visit(Cypress.env('key'));
This environment variable value can be changed dynamically through command line
arguments:

cypress run --env "Key"="Value"


Let’s consider an example. Here, you can find an environment variable created in
cypress.json.

{
“env”: {
token: “ueidkskslsdfjsdlf”
}
}
This environment variable’s value can be accessed via:

let tokenvalue = Cypress.env(‘token’);


If you wish to change the value dynamically, it can be done using this command
line:

cypress run --env "token"="ieoeeoeieoeie";

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:

describe('my test', () => {


it('test1', () => {
//Some code to test
});
})
In the code mentioned above, we’ve got three tests, such as test1, test2, and
test3.

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:

describe('my test', () => {


beforeEach(() => {
Cypress.Cookies.preserveOnce('session_id', 'remember_token')
});
it('test1', () => {
//Some code to test
});
})
-----------------------------------------------------------------------------------
----------

You might also like