Open In App

Cypress - origin() Method

Last Updated : 13 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Cypress is an open-source website testing tool that tests for JavaScript web applications. It is designed for end-to-end testing and it can be used for unit tests and integration tests as well. It is fast, reliable, and can run in real-time directly in the browser.

It's built to work with any front-end framework or website, including React, Angular, and Vue. In this article, we will learn about origin() Method in Cypress.

Cypress - origin() Method

origin() method in cypress allows you to interact with elements and perform actions on a different origin (domain) within your tests. It is particularly useful when you are testing applications that involve multiple domains such as authentication on third-party websites like Google, Facebook etc..

By default, URL tests are restricted to a single origin due to its security policies but with the origin() method, you can easily interact with another domain without compromising the security of your tests.

Syntax:

cy.origin(url, callbackFunction)

In this syntax:

  • URL: It is a string representing the origin (domain) to which you want to switch.
  • callbackFunction: A function containing Cypress commands to execute in the context of the specified origin.

Installation Steps

Before we begin make sure node.js is installed in your system.

Step 1: Create a Project Folder and move to it by using the following command.

mkdir cypress
cd cypress

Step 2: Initialize a project and Install Cypress.

npm init -y
npm install cypress --save-dev

Step 3: After Creating a Project, Run this command to start cypress.

npx cypress open

Step 4: Testing type, Configuration and creating a specialization.

  • Choose a E2E Testing or Component Testing, then after quick configuration choose browser.
  • After that create a new spec then click on your spec name to open your spec in any IDE.

Setting up localhost server

Follow the below steps to create two different project. One is running on port 3000 and second one is running on port 3001

  • Step 1: Creating a Node.js App
 npm init -y
  • Step 2: Installing a required dependencies.
npm i express
  • Step 3: Code for the First Application
HTML
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Main Page - Example Domain</title>
</head>
<body>
  <h1>Main Domain - Example.com</h1>
  <button id="redirect-button">Go to Another Domain</button>
  
  <script>
    document.getElementById('redirect-button').addEventListener('click', function () {
      window.location.href = 'http://localhost:3001'; // Redirect to another domain (another port)
    });
  </script>
</body>
</html>
JavaScript
//index.js
const express = require('express');
const app = express();
const path = require('path');

app.get('/', (req,res)=>{
    res.sendFile(path.join(__dirname,'/index.html'))
})

app.listen(3000,()=>{
    console.log("Server Started")
})
  • Step 4: Code for the Second Application
HTML
<!-- index.html -->
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Another Domain</title>
</head>
<body>
  <h1>Another Domain - Another.com</h1>
  <form id="login-form">
    <label for="username">Username:</label>
    <input type="text" id="username" name="username">
    <label for="password">Password:</label>
    <input type="password" id="password" name="password">
    <button type="submit">Login</button>
  </form>
</body>
</html>
JavaScript
//index.js
const express = require('express');
const path = require('path')

const app = express();

app.get('/', (req,res)=>{
    res.sendFile(path.join(__dirname, "/index.html"))
})

app.listen(3001, ()=>{
    console.log("Server Started")
})
  • Step 5: Running Both Application.

Make sure, you have created a two different folder to run the application & defined two different port. To run the application, use the below command

node index.js

Example

The below example demonstrate the use of origin() Method.

JavaScript
// cypress/e2e/origin_spec.cy.js
describe('Cypress origin() Method Example', () => {
  it('should interact with elements on a different origin', () => {
    // Visit the main page on the initial origin
    cy.visit('http://localhost:3000/');

    // Click on the button that redirects to another domain
    cy.get('#redirect-button').click();

    // Switch context to another origin
    cy.origin('http://localhost:3001/', () => {
      // Interact with elements on the cross-domain page
      cy.get('#username').type('testuser');
      cy.get('#password').type('password123');
      cy.get('button[type="submit"]').click();
    });

  });
});
JavaScript
//cypress.config.js

const { defineConfig } = require("cypress");

module.exports = defineConfig({
  e2e: {
    "chromeWebSecurity": false
  },
});

Output

Conclusion

In conclusion, Cypress is an end-to-end automated testing tool that enables efficient and reliable testing of web applications. It includes origin() method By which you can continue your test to work on third party website. By default cypress doesn't allow us to continue testing on third party website but you can achieve this functionality by using origin() method.


Next Article
Article Tags :

Similar Reads