https://cypress.io logo
do i need to write text to a file and then access ...
# general-chat
c
do i need to write text to a file and then access it from there or there is any other more efficient way of doing this?
d
There are different ways to do this. For example, in the first file you can export the alias as a function:
Copy code
export const getMsgSubject = () => {
  cy.get(elements.MSGS.MSGS_TABLE_CELL)
    .first()
    .invoke('text')
    .as('custToEmpMessageSubject')
}
Then in the second file, you can import the function and use it to get the value of the alias:
Copy code
import { getMsgSubject } from './file1'
it('Test Case', () => {
getMsgSubject()
cy.get('@custToEmpMessageSubject').then((text) => {
// Use the value of the alias here
})
})
Another way is to use for example cy.window(): In the first file:
Copy code
getMsgSubject() {
  cy.get(elements.MSGS.MSGS_TABLE_CELL)
    .first()
    .then(($element) => {
      cy.window().its('messageSubject', $element.text())
    })
}
In the second file:
Copy code
it('Test Case', () => {
    cy.window().then((win) => {
      const messageSubject = win.messageSubject
      // Use the value of the variable here
    })
  })
In the same way, you can `setItem `in `localStorage `in the first file and use
win.localStorage.getItem
in the second. Or use
Cypress.env()
in the same way...
c
@dazzling-thailand-9844 thank you very much. Let me try these out. Thanks for taking time out to help!