Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cy.get() returns undefined when using global options object #5221

Closed
WilliamWaterworth opened this issue Sep 26, 2019 · 1 comment
Closed
Labels
type: duplicate This issue or pull request already exists

Comments

@WilliamWaterworth
Copy link

WilliamWaterworth commented Sep 26, 2019

Current behavior:

I used a global object for cy.get commands, which usually works for Cypress testing. It works here if all tests run in the same specify function. Otherwise, all tests except the first fail with a vague "Undefined does not have class <classname>" error.

Desired behavior:

Ideally, I shouldn't have to redefine my cy.get options parameter for each specify. At minimum, Cypress should provide a useful error message.

Steps to reproduce: (app code and test code)

This works only when inlining the options object, i.e. {timeout: 120000} instead of options

const options = { timeout: 120000 };
describe("Onboard Files", function() {
    beforeEach(function() {
        Cypress.Cookies.preserveOnce(".cookiename");
        const files = Cypress.env("fileSpecifications");
        createCypressFile(files.fileA).then(blob => createMockDrag(blob, files.fileA)).as("fileA");
        createCypressFile(files.fileB).then(blob => createMockDrag(blob, files.fileB)).as("fileB");
    });

    specify("A", function() {
        dragAndDropFile(this.fileA);
        cy.get("c-container-drag-drop", options).should("have.class", "success");
    });
    
    specify("B", function() {
        dragAndDropFile(this.fileB);
        cy.get("c-container-drag-drop", options).should("have.class", "success");
    });
});

const createCypressFile = function(fileObject) {
    return cy
        .fixture(fileObject.path, 'base64')
        .then(Cypress.Blob.base64StringToBlob);
};

const createMockDrag = function(blob, fileObject) {
    const file = new File([blob], fileObject.name, { type: fileObject.type });
    return {
        dataTransfer: {
            files: [file],
        },
        preventDefault: function() { },
    };
};

const dragAndDropFile = function(e) {
    cy.get("c-container-drag-drop").as("host");

    cy.get("c-container-drag-drop:not(.onboard):not(.dragging)", options)
        .then(function(host) {
            getShadow(host).querySelector("#content").ondragenter(e);
        });

    cy.get("c-container-drag-drop.dragging", options)
        .then(function(host) {
            getShadow(host).querySelector("#drop-indicator").ondrop(e);
        });

    cy.get("c-container-drag-drop", options)
        .should("not.have.class", "onboard");
};

const getShadow = host => host[0] === undefined
    ? host.shadowRoot
    : host[0].shadowRoot;

Versions

Cypress 3.4.1 running Chrome 77

@kuceb
Copy link
Contributor

kuceb commented Sep 26, 2019

@WilliamWaterworth thanks for the bug report! I'm closing this as a duplicate of #3171

@kuceb kuceb closed this as completed Sep 26, 2019
@kuceb kuceb added the type: duplicate This issue or pull request already exists label Sep 26, 2019
@cypress-io cypress-io locked and limited conversation to collaborators Jan 3, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
type: duplicate This issue or pull request already exists
Projects
None yet
Development

No branches or pull requests

2 participants