DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

Related

  • Using Environment Variable With Angular
  • A Comprehensive Guide on JavaScript Array Map Method
  • Selenium vs Cypress: Does Cypress Replace Selenium?
  • How to Set up TestCafe and TypeScript JavaScript End-to-End Automation Testing Framework From Scratch

Trending

  • How the Go Runtime Preempts Goroutines for Efficient Concurrency
  • AI, ML, and Data Science: Shaping the Future of Automation
  • Evolution of Cloud Services for MCP/A2A Protocols in AI Agents
  • It’s Not About Control — It’s About Collaboration Between Architecture and Security
  1. DZone
  2. Coding
  3. JavaScript
  4. How To Execute Javascript Commands in Cypress

How To Execute Javascript Commands in Cypress

Execute JavaScript Commands in Cypress JavascriptExecutor

By 
Ganesh Hegde user avatar
Ganesh Hegde
DZone Core CORE ·
Jul. 07, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
33.8K Views

Join the DZone community and get the full member experience.

Join For Free

If you are from a selenium background like if you have used a tool like Selenium Java, Protractor then you should have used executeScript commands to execute the javascript commands. Usually, when you are struggling to simulate any action through in-built methods or in case it doesn’t work then we will use it. Also, there are many uses like if you want to execute custom javascript methods on the browser it will be useful.

Considering Cypress, These kinds of javascript commands are not much needed in cypress. The reason is it usually works directly within the browser. Still, if you are in need, Cypress provides a way to execute JavaScript.

This article explains:

  • How to execute Javascript Click in Cypress?
  • How to use Javascript Executor in Cypress
  • Running Javascript in Cypress
  • How to execute commands on the Native browser window?

With the help of the example in this article, you can modify and perform Javascript actions in Cypress like MovetoElement, Scroll, MouseMove, ScrollIntoView, Hover, MouseHover any click actions, Double click, Focus, etc.

Let’s consider one example. In Selenium, you write the below code to perform the javascript click action. 

Java
 
//Selenium Code to Execute Javscript for Click Actions
WebElement element = driver.findElement(By.id("pHiOh"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", element);

How To Execute Javascript Click Action in Cypress?

Considering the above example we can perform similarly in Cypress

Method 1: Execute Javascript in Cypress by Getting Native HTML DOM Element

TypeScript
 
//Method1: Cypress way of executing javascript click
cy.get("pHiOh").then(($el)=>{
 $el.get(0).click(); //This is the navite HTML DOM element
})

With the above Cypress gives you control over the native HTML DOM element you can perform any action on that, which is very similar to Javascript click event. 

Considering the above code $el.get(0).click()the $el holds the native HTML DOM Element and you can perform any action on this as shown above example.

Method 2: Execute Javascript in Cypress Using Window Object in Cypress

This way Cypress gives you control over the whole window, you can perform any action on that lets looks at the example

The Cypress cy.window() command gives direct access to the browser window, once you have this object you can perform any action directly on the browser window. below is the example

TypeScript
 
//Method 2: Cypress way of executing javascript click using window object
cy.window().then((win) => {
 win.eval('document.getElementById("login_submit").click()');
});

In the above, we are using a window.eval() function to execute javascript.

If you want to add or remove any HTML Elements in cypress you can do that so as well.

How To Change the Text in HTML Element Dynamically in Cypress?

TypeScript
 
cy.visit('https://www.google.com/');
cy.window().then((win) => { 
   win.document.getElementsByName('btnK')[1].value = "Ganesh" 
});

Updated text result

Using windows object in Cypress you can almost do any native browser operations.

How To Add an HTML Element Dynamically in Cypress?

TypeScript
 
cy.visit('https://www.google.com/');
  var el = window.document.createElement('button');
  el.innerText = "GANESH APPENDED"
  cy.get('#SIvCob').then(($el) => {
   $el.get(0).appendChild(el);
  })

After execution, it looks like this

Dynamic element added

In short, We can perform all javascript actions in cypress equivalent to Javascript execution in Selenium Java/C# or protractor.

Reference Links:

  1. Javascript Commands
  2. Native Dom Element

*****

Encourage me to write more articles

Buy me a coffee

If you are looking for any help, support, guidance contact me on LinkedIn|https://www.linkedin.com/in/ganeshsirsi/.

JavaScript Command (computing)

Opinions expressed by DZone contributors are their own.

Related

  • Using Environment Variable With Angular
  • A Comprehensive Guide on JavaScript Array Map Method
  • Selenium vs Cypress: Does Cypress Replace Selenium?
  • How to Set up TestCafe and TypeScript JavaScript End-to-End Automation Testing Framework From Scratch

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!