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

Fixtures aliasing not working as expected? #948

Closed
valentinogagliardi opened this issue Nov 21, 2017 · 12 comments
Closed

Fixtures aliasing not working as expected? #948

valentinogagliardi opened this issue Nov 21, 2017 · 12 comments

Comments

@valentinogagliardi
Copy link

  • Operating System: Fedora 26
  • Cypress Version: 1.1.1
  • Browser Version: Chrome 62

Is this a Feature or Bug?

Probably a bug

Current behavior:

When using cy.fixture("users/admin").as("adminData"); I can't access adminData

Desired behavior:

I should be able to access this.adminData

How to reproduce:

Call this.adminData.email after a fixture has been aliased

Test code:

describe("Knowledge Base Application", () => {

  it("Admin should be able to login", () => {
    cy.fixture("users/admin").as("adminData");

    cy.visit("/login");

    cy
      .get('input[name="email"]')
      .type(this.adminData.email)
      .should("have.value", this.adminData.email);

    cy
      .get('input[name="password"]')
      .type(this.adminData.password)
      .should("have.value", this.adminData.password);

    cy.get("form").submit();

    cy.location("pathname").should("eq", "/home");
  });
});

Fixture code

cypress/fixtures/users/admin.js

const admin = {
  name: "Valentino",
  email: "valentino@valentinog.com",
  password: "secret"
};
@brian-mann
Copy link
Member

Not a bug. Cypress commands are async. It's impossible for this.adminData to be defined at the moment you're using it.

This doc explains this exact scenario: https://docs.cypress.io/guides/core-concepts/variables-and-aliases.html#Aliases

@innerdaze
Copy link

innerdaze commented Apr 9, 2018

The reason OP probably though it was a bug is because the docs for fixture claim the following is valid on it's own:

cy.fixture('users.json').as('usersData')

@brian-mann
Copy link
Member

It is valid on its own, so long as by the time you access it with this.*, you've nested those cypress commands in a .then().

Alternatively you could use cy.get('@usersData').then((users) => ... )

@kud
Copy link

kud commented Jun 20, 2018

I agree, the doc isn't clear about that. I did the same mistake.

@jennifer-shehane
Copy link
Member

I created a new issue in our docs to document this better here: cypress-io/cypress-documentation#722. Our documentation is open source and contributions are welcome. 😄

@aaditya-kumar-harness
Copy link

aaditya-kumar-harness commented Sep 20, 2018

@brian-mann Any idea why this is not working (v3.1.0):

describe('Knowledge Base Application', () => {
  before(() => {
    cy.fixture('users/admin').as('adminData')
  })

  it('test1', () => {
    cy.get('@adminData')
      .then(adminData => { // able to read adminData
        cy.log(adminData)
      })
  })

  it('test2', () => {
    cy.get('@adminData')
      .then(adminData => { // **throws exception: CypressError: cy.get() could not find a registered alias for: '@adminData'. You have not aliased anything yet.**
        cy.log(adminData)
      })
  })
})

This works fine if I move the fixture setup from before to beforeEach.

@azollai
Copy link

azollai commented Sep 29, 2018

@aaditya-kumar-harness For some reason the fixtures you are initializing in before() method will only be accessable from the first test (Cypress implemented it like that.) If you want to access from all of it, you have to use beforeEach.

@jennifer-shehane
Copy link
Member

@aaditya-kumar-harness relevant thread: #665

@matteocollina
Copy link

matteocollina commented Aug 26, 2019

It works. Being asynchronous you have to wait for the fixture to be loaded

cy.fixture("users/admin.json").as("admin")
cy.get('@admin')
  .then((admin) => {
      cy.get("#selector1")
          .type(admin.username)
      cy.get("#selector2")
          .type(admin.password)
  })

@KamilWojtczyk
Copy link

KamilWojtczyk commented Jan 12, 2021

Can someone tell why i am getting "Cannot read property testadata of undefined?

describe('Testing system', function () {
    beforeEach(function () {
        cy.visit('/')
        cy.wait(3000)
    })

    beforeEach(function () {
        cy.fixture('testdata').then(function (testdata) {
            this.testdata = testdata
        })
    })

    it('Finding all dictionaries with an asterisk', function () {
        cy.get('.nav-link[title="Słowniki"]').click()
        cy.get('.sub-menu li').then(($divs) => {
            Cypress._.each(this.testdata.list, (tag) => {
                expect($divs).to.contain(tag)
            })
        })
        cy.log('List length', this.testdata.list.length)
    })

    it('Testing specific dictionary', function () {

        cy.get('.nav-link[title="Słowniki"]').click()
        for (var i = 0; i < this.testdata.list.length; i++) {
            cy.wait(3000)
            cy.get(`.sub-menu`).contains(this.testdata.list[i]).click()
            cy.url().should('include', this.testdata.listurl[i])

            if(this.testdata.listurl[i] === '/slowniki/bramy-wjzadowe' || this.testdata.listurl[i] === '/slowniki/gatunek-produktu'){
                cy.log("'Słownik nie posiada kolumn Aktywny', 'użytkownik modyfikujący' oraz 'data ostaniej modyfikacji'")
            }  else {
                cy.get('.dx-row td[aria-label="Column Aktywny?"]').should('have.text', 'Aktywny?')
             }
         }
    })
})

@jennifer-shehane
Copy link
Member

@padifu You need to alias the fixture using the .as() command to make it available with the this context.

cy.fixture('testdata').as('testdata')

You may want to try asking our community in our GitHub Discussions. As an open source project with a small maintainer team we have to focus our time on bugs and features in the product, which Issues are reserved for.

@KamilWojtczyk
Copy link

This solution is not working, how should i call data ? Just simple testdata.nameofdata ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

9 participants