Cypress Third Class - Ranjan Dhakal
Cypress Third Class - Ranjan Dhakal
Cypress InfoDev
Cypress Within
Cypress InfoDev
Cypress JS constant
const is a keyword in JavaScript that is used to declare a constant variable. A
constant variable cannot be reassigned once it is defined.
it.only(‘Constant', () => {
cy.visit(myURL)
});
Cypress InfoDev
Cypress DropDown Select dropdown
Custom Dropdown
it.only('DropDown Concept', () => {
cy.visit("https://www.ebay.com/")
cy.get('#gh-shop-a')
.should('be.visible')
.and('have.attr','aria-controls','gh-sbc-o').click()
.get('.scnd').should('be.visible').contains("Collectibles").click();
});
Cypress InfoDev
Cypress Photo Upload
it.only('Photo Upload', () => {
cy.visit("https://the-internet.herokuapp.com/upload")
cy.get("#file-upload").selectFile("cypress/fixtures/test.png")
cy.get(".button").click()
});
Cypress InfoDev
Cypress File Drag and Drop
1. Install the File upload Plugin
npm install --save-dev cypress-file-upload
import 'cypress-file-upload';
});
Cypress InfoDev
Cypress Photo Upload by attachFile
it.only('File Upload', () => {
cy.visit("https://the-internet.herokuapp.com/upload")
cy.get("#file-upload").attachFile("/test.pdf")
});
Cypress InfoDev
Cypress Environment Variable
• Benefits: Great for values that need to be checked into source control
and remain the same on all machines.
• Downsides: Only works for values that should be the same on across
all machines.
Cypress InfoDev
Cypress.env In config file
const { defineConfig } = require("cypress");
module.exports = defineConfig({
e2e: {
setupNodeEvents(on, config) {
// implement node event listeners here
},
env: {
username: '',
password: '',
},
},
});
cy.get('[placeholder="Username or Email"]').type(Cypress.env('username'))
Cypress InfoDev
Cypress.env
const username = Cypress.env('username')
const password = Cypress.env('password')
cy.get('[placeholder="Username or Email"]').type(username)
cy.get('[placeholder="Password"]').type(password)
Cypress InfoDev
Using cypress.env.json
Create a new file name cypress.env.json
{
"stag_url": "https://example.com",
"live_url": "https://docs.cypress.io/api/commands/within"
}
it('json.evn', () => {
cy.visit(Cypress.env('live_url'));
});
Cypress InfoDev
Implicit Assertion and Explicit Assertion
Cypress InfoDev
Implicit Wait and Explicit Wait
Explicit waits can be useful in certain scenarios, such as waiting for animations to complete or giving time
for elements to load. While explicit waits can be effective in some cases, it's generally recommended to
use Cypress commands that automatically wait for elements to be available or for conditions to be met.
While Cypress doesn't have a dedicated "implicit wait" command like Selenium, it has built-
in mechanisms that achieve similar outcomes:
Cypress's design emphasizes explicit
coordination and assertions over
cy.wait(2000) this is explicit wait implicit waits.
Instead of giving time to wait, we can use assertion method to wait for the element
Cypress InfoDev
Cypress exception Waiting TimeOut
it.only('DropDown Concept', () => {
cy.visit("https://www.saucedemo.com/v1/inventory.html")
cy.get('.product_label')
});
defaultCommandTimeout: 10000,
Cypress InfoDev
Cypress Wait
Cypress InfoDev
Cypress Network Intercept and Request
Cypress InfoDev
Cypress Network Intercept
it.only('Network Intercept Wait', () => {
cy.visit('https://hris.infodev.com.np/')
cy.get('[placeholder="Username or Email"]').type(username)
cy.get('[placeholder="Password"]').type(password)
cy.get('[type="submit"]').click()
cy.intercept("GET",'/Core/Home/DashboardChartResult').as("waitPage")
cy.wait("@waitPage")
});
cy.intercept( 'https://hris.infodev.com.np/').as("waitingPage")
cy.wait("@waitingPage")
Cypress InfoDev
Cypress Network Intercept
it.only('Network Intercept Wait', () => {
cy.visit('https://hris.infodev.com.np/')
cy.get('[placeholder="Username or Email"]').type(username)
cy.get('[placeholder="Password"]').type(password)
cy.get('[type="submit"]').click()
cy.intercept("GET",'**/NoticeInfo/**').as("waitPages")
cy.wait("@waitPages")
});
Cypress InfoDev
Cypress Network Intercept
cy.visit('https://www.nytimes.com/games/wordle/index.html')
cy.get('[data-testid="Play"]').should('have.text','Play').click()
cy.get('.Modal-module_closeIcon__TcEKb').should('be.visible').click()
cy.wait('@getJsonFile')
});
Cypress InfoDev
Any Questions??
Cypress InfoDev
Thank You!!
Cypress InfoDev