top of page

Cypress - Part 2 - CSS & XPath Locators, Assertions, Folder Structure, Interacting with WebElements

Let's get started with second part of Cypress basics. The first part of the blog can be found here.

In this blog, we will be going through CSS and XPath Locators, Types of Assertions in Cypress, and Folder Structure, Interacting with WebElements like Radio Buttons, Checkboxes, Dropdowns, Alerts.

Cypress E2E Web Automation:

CSS & XPath Locators: get() & xpath() methods:

Locators: CSS Selector, XPath Locators (supported with cypress XPath plugin)

CSS Selector in Cypress:

  • Locate the element and then identify the element using cy.get(locator)

  • tag id, tag class, tag attribute, tag class attribute→ tag is optional in these 4 combinations.

  • Check selector: SelectorsHub plugin (OR) ctrl+F

  • tag#id, tag.class, tag[attribute=’value’], tag.class[attribute=”value”]

  • Spec file: optional: const{describe} = require(“mocha”);

  • Every file: const cypress = require(“cypress”) (OR)

  • /// <reference types=”cypress”/> //can put in support/commands.js

  • cy.visit(“”)

  • cy.get(“input#search_query_top”).type(“T-Shirts”) // tag input is optional here , id

  • cy.get(“[name=’submit_search’]”).click() // attribute

  • cy.get(“.lighter”).contains(“T-Shirts”) // class

  • Time travel can be seen switching between steps

  • When the spec file is modified, the test is run again automatically.

XPath Locators in Cypress:

  • To use XPath in Cypress: Search cypress plugins:

  • cypress-xpath plugin → npm install -D cypress --xpath

  • commands.js :add reference: /// <reference types=”cypress-xpath” />

  • e2e.js: require(‘cypress-xpath’) // applicable for all tests or individually in each spec file

  • cy.xpath(“//ul[@id=’homefeatured’]/li”).should(‘have.length’,7)

  • cy.xpath(“//ul[@id=’homefeatured’]”).xpath(“./li”).should(‘have.length’,7) // chained xpath

  • XPath: preceding, following, ancestor, sibling, child, parent, self, namespace, attribute …

Selenium WebDriver XPath axes: Child, Parent, Self, Ancestor, Ancestor-or-self, Descendant, Descendant-or-self, Following, Following-sibling, Preceding, Preceding-sibling, Attribute, Namespace axis.

Assertions: Types of Assertions: Implicit / Built-in assertions & Explicit Assertions

  • Keywords:: Implicit: should, and; explicit: expect (BDD Assertion), assert(TDD framework Assertion)

  • All assertions are derived from child library.

Implicit Assertions:

  • cy.url().should('include','') // .url() get url

  • cy.url().should(‘eq’,’<full_url>’) , ‘contain’ similar to ‘include’

  • cy.url().should(...).should(...).should(...) → cy.url().should(...).and(...).and(...)

  • Negative assertions: ‘not.contain’

  • cy.title().should(...) // verify title

  • cy.get('.orangehrm-login-branding > img').should('be.visible') // .should(‘exist’)

  • Capture number of links on web page: cy.xpath("//a").should(‘have.length’,’5’)

  • cy.get("input[placeholder='Username']").type("Admin") // provide value into input box

  • cy.get("input[placeholder='Username']").should('have.value','Admin') // value check

  • Assertions for should: eq, contain, include, not.eq, not.contain, not.include, exist, have.length, have.value

Explicit assertions:

  • expect: BDD Assertions:

let expName = "xyz";
cy.get(".oxd-userdropdown-name").then( (x) =>{  // x stores element 
	let actName = x.text()
	expect(actName).to.not.equal(expName) // BDD assertions use expect
  • assert: TDD assertions

assert.equal(actName, expName)  ; 
assert.notEqual(actName, expName);

Folder Structure - IntelliSense Auto Suggestions:

The new version of Cypress has folder structure different from previous versions.

commnands.js: The following type of commands help to enable IntelliSense - to get list of available commands automatically - Intelligence Automated Suggestions. Instead of adding the commands in every Spec file, we can add in commands.js file once.

/// <reference types="Cypress" /> // for cypress

/// <reference types="cypress-xpath" /> // for xpath

e2e.js: had plugin information, contains commands.


const {defineConfig} = require("cypress"); // for cypress commands to work
	module.exports = defineConfig({
	e2e: {
			setupNodeEvents(on, config){ 

similarly we can also add in cypress.config.js:

const {xpathConfig} = require('cypress-xpath'); // for XPath, define function xpathConfig

Interacting with Elements | Validating Elements | Assertions:

Radio Buttons & Checkboxes:

describe("Check UI Elements", ()=>{
	it("Checking Radio Buttons", ()=> {
		cy.get("input#male").should('be.visible')  // visibility
		// selecting radio buttons
		// check() selects element
		// only male or female should be selected, not both
	it("Checking Check Boxes", ()=>{
		// visibility of checkbox
		// select single checkbox

		// unselect checkbox

be.visible: Here, "be" indicates behavior of web element.

When the test case is run, we can time travel between steps hovering over on the left pane, on each step of the test case. We can see this from below screenshot.

  • //select all check boxes

  • cy.get("input.form-check-input[type='checkbox']").check().should('be.checked')

  • // deselect all checkboxes

  • cy.get("input.form-check-input[type='checkbox']").uncheck().should('')

  • //select first check box

  • cy.get("input.form-check-input[type='checkbox']").first().check().should('be.checked')

  • // select last checkbox

  • cy.get("input.form-check-input[type='checkbox']").last().check().should('be.checked')


// for those with select tag


//boostrap framework drop downs, without select tag in HTML


//enter in input box


//assertion on drop box where value is selected.

cy.get('#select2-billing_country-container').should('have.text', 'Italy')

// Autosuggested Dropdown automation (static in nature)

it('Auto Suggest Dropdown', function(){


// gets list of items and check for value in each of them

cy.get('.suggestion-title').contains('Delhi University').click()


// dynamic dropdown
it('Dynamic Dropdown', ()=>{
        cy.get("input[name='q']").type('cypress automation')
        cy.get('div.wM6W7d>span').each( ($el, index, $list)=>{
            // find the element
            if($el.text() == 'cypress automation tool'){  
        cy.get("input[name='q']").should('have.value', 'cypress automation tool') // check if selected in input box


Cypress will automatically close alerts. But to do validation, we have to do it programmatically using events.

it.only executes only that test case and not other test cases in the describe test suite.

Alerts windows are automatically closed by Cypress itself. But, if we want to do some validations on alert window, get control on it by raising certain Event, and then control the Alert. has more events to be raised to control Alerts, for Validation.

describe('Alerts', ()=>{
	// 1. JavaScript Alert: It will have some text and an 'OK' button.
	it('JS Alert', ()=>{
        // validating text on Alert window
        cy.on('window:alert', (t)=>{
            expect(t).to.contains('I am a JS Alert');
        // alert window automatically closed by cypress
        // after closing, validate alert window
        cy.get("#result").should('have.text','You successfully clicked an alert')
    // 2. JavaScript Confirm Alert: It will have some text with 'OK' and 'Cancel' buttons.
    it('JS Confirm', ()=>{
        // default cypress closes with OK automatically.
        // to close alert clicking Cancel button:
        cy.on('window:confirm', (t)=>{
            expect(t).to.contains('I am a JS Confirm');

        // true here means cypress closes using default ok
        // to click using cancel use false
        cy.on('window:confirm',()=> false); 
        // alert window automatically closed by cypress
        // after closing, validate alert window
        cy.get('#result').should('have.text','You clicked: Cancel')

	// 3. JavaScript Prompt Alert: It will have some text with a text box for user input along with 'OK' button.
    // provide text and OK(1event) (OR) provide text and click Cancel (2events)
    // before opening alert, we have to pass text in input box
    // cypress closes by default clicking ok button
    it('JS prompt alert', ()=>{
        cy.window().then( (win)=>{
        //validate for ok button automatically closed by cypress
        //cy.get("#result").should('have.text', 'You entered: welcome');
        // validate for cancel button
        cy.get("#result").should('have.text', 'You entered: welcome');

	// 4. Authenticated Alert
    it('Authenticated Alert', ()=>{
        // pass username password, as parameters in visit method, to skip alert window
        cy.visit('', {auth: {username:"admin", password:"admin"}});
        cy.get("div[class='example'] p").should('have.contain',"Congratulations")

        // pass username password,  along with URL
        cy.get("div[class='example'] p").should('have.contain',"Congratulations")

Hope you enjoyed learning Cypress, using CSS and XPath Locators, Types of Assertions in Cypress, and Folder Structure, Interacting with Web Elements like Radio Buttons, Checkboxes, Dropdowns, Alerts.

7,425 views2 comments


Rated 0 out of 5 stars.
No ratings yet

Add a rating
Apr 23

how can we get siblings locator

in cypress


Jun 24, 2023
Rated 5 out of 5 stars.

thank you!

bottom of page