Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
616 views
in Technique[技术] by (71.8m points)

Cypress: cy.window(): Unable to get property values

Goal

Hello, I wish to gather custom property values for a window object of a page using cy.window().

Issue

When using cy.log() jointly with JSON.stringify(), it presents that it does have properties with values; however, when using lodash _.has(), does not have these properties and thereby no value because these properties are not found.

Code

The following Cypress custom command using cy.window() gathers custom window's property

export function cmdCypressWindow($propName: string) {
  cy.window()
    .its($propName)
    .then(($propValue: Cypress.AUTWindow) => {
      cy.log('props names:', JSON.stringify(Object.getOwnPropertyNames($propValue), null, 2));
      cy.log('props values:', JSON.stringify($propValue, null, 2));
      cy.log('VERSION prop:', _.has($propValue, 'VERSION'));
      cy.log('HOST prop:', _.has($propValue, 'HOST'));
      cy.log('VERSION value:', _.get($propValue, 'VERSION'));
      cy.log('HOST value:', _.get($propValue, 'HOST'));
    });
}

Passed in for parameter $propName value 'ACT', because I am expecting the page's window object to contain window.ACT["VERSION"].

Using the example code, the log output shows that the page's window does contain property ACT["VERSION"].

However, when accessing this window object, listed properties are unavailable and undefined:

window
- its   .ACT
log     props names:, [ "__esModule", "VERSION", "HOST", "RulesList", "RulesAddEdit", "AppsList", "AppsOAuth", "AppsAdd" ]
log     props values:, { "VERSION": "0.2.11", "HOST": "radmin" }
log     VERSION prop:, false
log     HOST prop:, false
log     VERSION value:
log     HOST value:

How do I resolve this? Thank you, all feedback is very much appreciated.

question from:https://stackoverflow.com/questions/65837448/cypress-cy-window-unable-to-get-property-values

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Found part of the solution here: TypeScript: Find Key / Value in Object (list comprehension?)

Modified the function:

export function cmdCypressWindow($propName: string) {

  cy.window()
    .its($propName)
    .then(($propValue: Cypress.AUTWindow) => {
      const actValues: Record<string, string> = {};

      Object.keys($propValue).forEach(key => {
        // @ts-ignore
        if (typeof $propValue[key] !== 'function') {
          // @ts-ignore
          actValues[key as string] = $propValue[key];
        }
      });

      cy.log(`window.${$propName}`, JSON.stringify(actValues, null, 2));

      cy.wrap(actValues);
    });
}

Results show that I was able to acquire values from window object:

log   props names:, [ "__esModule", "VERSION", "HOST", "RulesList", "RulesAddEdit", "AppsList", "AppsOAuth", "AppsAdd" ]
log   window.ACT, { "VERSION": "0.2.11", "HOST": "radmin" }
wrap  {version: 0.2.11, host: radmin}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...