shakacode / cypress-on-rails

Use cypress.io or playwright.dev with your rails application. This Ruby gem lets you use your regular Rails test setup and clean-up, such as FactoryBot.
MIT License
414 stars 61 forks source link

Error with running using_scenarios_spec.js #102

Closed VadymBoguslavsky closed 2 years ago

VadymBoguslavsky commented 2 years ago

Снимок экрана 2022-02-04 в 16 00 33

What can i do to fix this error? All i have done is installed cypress-on-rails and run it locally

It seems that something failed in the appCommands function

Cypress.Commands.add('appCommands', function (body) {
  Object.keys(body).forEach(key => body[key] === undefined ? delete body[key] : {});
  const log = Cypress.log({ name: "APP", message: body, autoEnd: false })
  return cy.request({
    method: 'POST',
    url: "/__cypress__/command",
    body: JSON.stringify(body),
    log: false,
    failOnStatusCode: false
  }).then((response) => {
    log.end();
    if (response.status !== 201) {
      expect(response.body.message).to.be.empty
      expect(response.status).to.be.equal(201)
    }
    return response.body
  });
});
grantspeelman commented 2 years ago

i see it's returning a 404 status ... maybe it can't find the clean.rb file. Is clean.rb still in the cypress/app_commands folder?

VadymBoguslavsky commented 2 years ago

Yes. clean.rbis still there. I have tried to remove if in cypress AppCommands

Cypress.Commands.add('appCommands', function (body) {
  Object.keys(body).forEach(key => body[key] === undefined ? delete body[key] : {});
  const log = Cypress.log({ name: "APP", message: body, autoEnd: false })
  return cy.request({
    method: 'POST',
    url: "/__cypress__/command",
    body: JSON.stringify(body),
    log: false,
    failOnStatusCode: false
  }).then((response) => {
    log.end();
    return response.body
  });
});

And logically there is no error anymore. but after that i discovered that clean.rb, and other commands that defined in app commands like appFactories or appScenarios not working and i cant understand why. Also i define and use it like :

  beforeEach(() => {
    cy.app('clean') // have a look at cypress/app_commands/clean.rb
    cy.app('basic');
    cy.appScenario('everything_active')
  })

  it('setup basic scenario', function() {
    cy.app('basic');
    cy.visit('/')
    cy.appFactories([
      ['create_list', 'ad', 10],
      ['create', 'ad', {title: 'erfreerfr'} ],
    ])
    cy.appFactories([['create', 'ad', {price: 234, featured: true}]])

And my scenario is

user = User.create!(email: 'user1@test.com',name: 'user1')
shop = Shop.create!(:shop, user: user)
invoice = Invoice.create!(:invoice, target: shop)

Ad, shop, invoice already defined in factorybot.

grantspeelman commented 2 years ago

hmmm, i guess the other possibility is that the middleware is not enabled and that is why you are getting a 404 error.

in you cypress_on_rails.rb initializer is use_middleware set to true?

VadymBoguslavsky commented 2 years ago

yes, this is my cypress_on_rails.rb file

if defined?(CypressOnRails)
  CypressOnRails.configure do |c|
    c.cypress_folder = File.expand_path("#{__dir__}/../../cypress")
    # WARNING!! CypressOnRails can execute arbitrary ruby code
    # please use with extra caution if enabling on hosted servers or starting your local server on 0.0.0.0
    c.use_middleware = true
    c.logger = Rails.logger
  end

  # # if you compile your asssets on CI
  # if ENV['CYPRESS'].present? && ENV['CI'].present?
  #  Rails.application.configure do
  #    config.assets.compile = false
  #    config.assets.unknown_asset_fallback = false
  #  end
  # end
end
grantspeelman commented 2 years ago

Can you share the output of the browser console when you click on "Print to console" please.

VadymBoguslavsky commented 2 years ago

Снимок экрана 2022-02-08 в 11 24 58

VadymBoguslavsky commented 2 years ago

Its an error with default test using_scenarios

grantspeelman commented 2 years ago

You should be able to expand the "Applied to" output in the printed console output. That is the body of the http request. Hopefully you can see something in it that can be a clue on why it's not working. Feel free to share it here as well.

VadymBoguslavsky commented 2 years ago

Снимок экрана 2022-02-09 в 15 27 26

grantspeelman commented 2 years ago

Can you share the full "Response Body" please