Skip to content

Commit

Permalink
fix: specs_e2e
Browse files Browse the repository at this point in the history
Signed-off-by: Khaled Emara <[email protected]>
  • Loading branch information
KhaledEmaraDev committed May 31, 2023
1 parent 09df3d5 commit d6ff6cd
Show file tree
Hide file tree
Showing 11 changed files with 134 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = defineConfig({
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: 'http://127.0.0.1:3000',
baseURL: 'http://127.0.0.1:5017',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { request, expect } from '@playwright/test'
import config from '../../playwright.config'

const contextPromise = request.newContext({ baseURL: config.use ? config.use.baseURL : 'http://localhost:3000' })
const contextPromise = request.newContext({ baseURL: config.use ? config.use.baseURL : 'http://localhost:5017' })

const appCommands = async (data) => {
const context = await contextPromise
Expand Down
4 changes: 2 additions & 2 deletions plugin/support/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Cypress.Commands.add("vcr_insert_cassette", (cassette_name, options) => {
const log = Cypress.log({ name: "VCR Insert", message: cassette_name, autoEnd: false })
return cy.request({
method: 'POST',
url: "/__cypress__/vcr/insert",
url: "/__e2e__/vcr/insert",
body: JSON.stringify([cassette_name,options]),
log: false,
failOnStatusCode: false
Expand All @@ -23,7 +23,7 @@ Cypress.Commands.add("vcr_eject_cassette", () => {
const log = Cypress.log({ name: "VCR Eject", autoEnd: false })
return cy.request({
method: 'POST',
url: "/__cypress__/vcr/eject",
url: "/__e2e__/vcr/eject",
log: false,
failOnStatusCode: false
}).then((response) => {
Expand Down
2 changes: 1 addition & 1 deletion spec/cypress_on_rails/command_executor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def executor_perform(*values)
end

before do
CypressOnRails.configuration.cypress_folder = folder
CypressOnRails.configuration.install_folder = folder
DummyTest.values.clear if defined?(DummyTest)
end

Expand Down
9 changes: 6 additions & 3 deletions spec/cypress_on_rails/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,25 @@
it 'has defaults' do
CypressOnRails.configure { |config| config.reset }

expect(CypressOnRails.configuration.cypress_folder).to eq('spec/cypress')
expect(CypressOnRails.configuration.api_prefix).to eq('')
expect(CypressOnRails.configuration.install_folder).to eq('spec/e2e')
expect(CypressOnRails.configuration.cypress_folder).to eq('spec/cypress')
expect(CypressOnRails.configuration.use_middleware?).to eq(true)
expect(CypressOnRails.configuration.logger).to_not be_nil
end

it 'can be configured' do
my_logger = Logger.new(STDOUT)
CypressOnRails.configure do |config|
config.cypress_folder = 'my/path'
config.api_prefix = '/api'
config.install_folder = 'my/path'
config.cypress_folder = 'my/path'
config.use_middleware = false
config.logger = my_logger
end
expect(CypressOnRails.configuration.cypress_folder).to eq('my/path')
expect(CypressOnRails.configuration.api_prefix).to eq('/api')
expect(CypressOnRails.configuration.cypress_folder).to eq('my/path')
expect(CypressOnRails.configuration.install_folder).to eq('my/path')
expect(CypressOnRails.configuration.use_middleware?).to eq(false)
expect(CypressOnRails.configuration.logger).to eq(my_logger)
end
Expand Down
40 changes: 20 additions & 20 deletions spec/cypress_on_rails/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,88 +14,88 @@ def rack_input(json_value)
StringIO.new(JSON.generate(json_value))
end

context '/__cypress__/command' do
context '/__e2e__/command' do
before do
allow(command_executor).to receive(:perform).and_return({ id: 1, title: 'some result' })
allow(file).to receive(:exist?)
env['PATH_INFO'] = '/__cypress__/command'
env['PATH_INFO'] = '/__e2e__/command'
end

it 'command file exist' do
allow(command_executor).to receive(:perform).and_return({ id: 1, title: 'some result' })
env['rack.input'] = rack_input(name: 'seed')
allow(file).to receive(:exist?).with('spec/cypress/app_commands/seed.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/e2e/app_commands/seed.rb').and_return(true)

aggregate_failures do
expect(response).to eq([201,
{"Content-Type"=>"application/json"},
["[{\"id\":1,\"title\":\"some result\"}]"]])
expect(command_executor).to have_received(:perform).with('spec/cypress/app_commands/seed.rb', nil)
expect(command_executor).to have_received(:perform).with('spec/e2e/app_commands/seed.rb', nil)
end
end

it 'command file exist with options' do
env['rack.input'] = rack_input(name: 'seed', options: ['my_options'])
allow(file).to receive(:exist?).with('spec/cypress/app_commands/seed.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/e2e/app_commands/seed.rb').and_return(true)

aggregate_failures do
expect(response).to eq([201,
{"Content-Type"=>"application/json"},
["[{\"id\":1,\"title\":\"some result\"}]"]])
expect(command_executor).to have_received(:perform).with('spec/cypress/app_commands/seed.rb', ['my_options'])
expect(command_executor).to have_received(:perform).with('spec/e2e/app_commands/seed.rb', ['my_options'])
end
end

it 'command file does not exist' do
object = BasicObject.new
allow(command_executor).to receive(:perform).and_return(object)
env['rack.input'] = rack_input(name: 'seed')
allow(file).to receive(:exist?).with('spec/cypress/app_commands/seed.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/e2e/app_commands/seed.rb').and_return(true)

aggregate_failures do
expect(response).to eq([201,
{"Content-Type"=>"application/json"},
["{\"message\":\"Cannot convert to json\"}"]])
expect(command_executor).to have_received(:perform).with('spec/cypress/app_commands/seed.rb', nil)
expect(command_executor).to have_received(:perform).with('spec/e2e/app_commands/seed.rb', nil)
end
end

it 'command result does not respond to to_json' do
env['rack.input'] = rack_input(name: 'seed')
allow(file).to receive(:exist?).with('spec/cypress/app_commands/seed.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/e2e/app_commands/seed.rb').and_return(true)

aggregate_failures do
expect(response).to eq([201,
{"Content-Type"=>"application/json"},
["[{\"id\":1,\"title\":\"some result\"}]"]])
expect(command_executor).to have_received(:perform).with('spec/cypress/app_commands/seed.rb', nil)
expect(command_executor).to have_received(:perform).with('spec/e2e/app_commands/seed.rb', nil)
end
end

it 'running multiple commands' do
env['rack.input'] = rack_input([{name: 'load_user'},
{name: 'load_sample', options: {'all' => 'true'}}])
allow(file).to receive(:exist?).with('spec/cypress/app_commands/load_user.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/cypress/app_commands/load_sample.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/e2e/app_commands/load_user.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/e2e/app_commands/load_sample.rb').and_return(true)

aggregate_failures do
expect(response).to eq([201,
{"Content-Type"=>"application/json"},
["[{\"id\":1,\"title\":\"some result\"},{\"id\":1,\"title\":\"some result\"}]"]])
expect(command_executor).to have_received(:perform).with('spec/cypress/app_commands/load_user.rb', nil)
expect(command_executor).to have_received(:perform).with('spec/cypress/app_commands/load_sample.rb', {'all' => 'true'})
expect(command_executor).to have_received(:perform).with('spec/e2e/app_commands/load_user.rb', nil)
expect(command_executor).to have_received(:perform).with('spec/e2e/app_commands/load_sample.rb', {'all' => 'true'})
end
end

it 'running multiple commands but one missing' do
env['rack.input'] = rack_input([{name: 'load_user'}, {name: 'load_sample'}])
allow(file).to receive(:exist?).with('spec/cypress/app_commands/load_user.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/cypress/app_commands/load_sample.rb').and_return(false)
allow(file).to receive(:exist?).with('spec/e2e/app_commands/load_user.rb').and_return(true)
allow(file).to receive(:exist?).with('spec/e2e/app_commands/load_sample.rb').and_return(false)

aggregate_failures do
expect(response).to eq([404,
{"Content-Type"=>"application/json"},
["{\"message\":\"could not find command file: spec/cypress/app_commands/load_sample.rb\"}"]])
expect(response).to eq([404,
{"Content-Type"=>"application/json"},
["{\"message\":\"could not find command file: spec/e2e/app_commands/load_sample.rb\"}"]])
expect(command_executor).to_not have_received(:perform)
end
end
Expand All @@ -104,7 +104,7 @@ def rack_input(json_value)
context '"Other paths"' do
it 'runs app' do
aggregate_failures do
%w(/ /__cypress__/login command /cypress_command /).each do |path|
%w(/ /__e2e__/login command /e2e_command /).each do |path|
env['PATH_INFO'] = path

response = subject.call(env)
Expand Down
10 changes: 5 additions & 5 deletions spec/cypress_on_rails/vcr_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ def rack_input(json_value)
StringIO.new(JSON.generate(json_value))
end

describe '/__cypress__/vcr/insert' do
describe '/__e2e__/vcr/insert' do
before do
env['PATH_INFO'] = '/__cypress__/vcr/insert'
env['PATH_INFO'] = '/__e2e__/vcr/insert'
end

it do
Expand Down Expand Up @@ -78,9 +78,9 @@ def rack_input(json_value)
end
end

describe '/__cypress__/vcr/eject' do
describe '/__e2e__/vcr/eject' do
before do
env['PATH_INFO'] = '/__cypress__/vcr/eject'
env['PATH_INFO'] = '/__e2e__/vcr/eject'
end

it do
Expand All @@ -104,7 +104,7 @@ def rack_input(json_value)

it 'runs app' do
aggregate_failures do
%w(/ /__cypress__/login command /cypress_command /).each do |path|
%w(/ /__e2e__/login command /e2e_command /).each do |path|
env['PATH_INFO'] = path

response = subject.call(env)
Expand Down
79 changes: 79 additions & 0 deletions specs_e2e/playwright.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// @ts-check
const { defineConfig, devices } = require('@playwright/test');

/**
* Read environment variables from file.
* https://github.com/motdotla/dotenv
*/
// require('dotenv').config();

/**
* @see https://playwright.dev/docs/test-configuration
*/
module.exports = defineConfig({
testDir: './playwright/e2e',
/* Run tests in files in parallel */
fullyParallel: true,
/* Fail the build on CI if you accidentally left test.only in the source code. */
forbidOnly: !!process.env.CI,
/* Retry on CI only */
retries: process.env.CI ? 2 : 0,
/* Opt out of parallel tests on CI. */
workers: process.env.CI ? 1 : undefined,
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
reporter: 'html',
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
use: {
/* Base URL to use in actions like `await page.goto('/')`. */
baseURL: 'http://127.0.0.1:5017',

/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
trace: 'on-first-retry',
},

/* Configure projects for major browsers */
projects: [
{
name: 'chromium',
use: { ...devices['Desktop Chrome'] },
},

{
name: 'firefox',
use: { ...devices['Desktop Firefox'] },
},

{
name: 'webkit',
use: { ...devices['Desktop Safari'] },
},

/* Test against mobile viewports. */
// {
// name: 'Mobile Chrome',
// use: { ...devices['Pixel 5'] },
// },
// {
// name: 'Mobile Safari',
// use: { ...devices['iPhone 12'] },
// },

/* Test against branded browsers. */
// {
// name: 'Microsoft Edge',
// use: { ...devices['Desktop Edge'], channel: 'msedge' },
// },
// {
// name: 'Google Chrome',
// use: { ..devices['Desktop Chrome'], channel: 'chrome' },
// },
],

/* Run your local dev server before starting the tests */
// webServer: {
// command: 'npm run start',
// url: 'http://127.0.0.1:3000',
// reuseExistingServer: !process.env.CI,
// },
});

6 changes: 5 additions & 1 deletion specs_e2e/rails_3_2/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ bundle --version
bundle install --quiet --gemfile="$DIR/Gemfile" --retry 2 --path vendor/bundle

echo '-- cypress install'
bundle exec ./bin/rails g cypress_on_rails:install --install_cypress_with=npm
bundle exec ./bin/rails g cypress_on_rails:install --install_folder=test/e2e --cypress_folder=test/cypress --playwright_folder=test/playwright --install_cypress --install_playwright --install_with=npm --skip
rm -vf cypress/e2e/rails_examples/advance_factory_bot.cy.js
rm -vf cypress/e2e/rails_examples/using_vcr.cy.js

Expand All @@ -34,5 +34,9 @@ cp -fv ../cypress.config.js .
node_modules/.bin/cypress run --record
# fi

echo '-- cypress run'
cp -fv ../cypress.config.js .
npx playwright test

echo '-- stop rails server'
kill -9 `cat tmp/pids/server.pid`
10 changes: 7 additions & 3 deletions specs_e2e/rails_4_2/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,8 @@ gem install bundler -v "~> 1.0" --conservative
bundle --version
bundle install --quiet --gemfile="$DIR/Gemfile" --retry 2 --path vendor/bundle

echo '-- cypress install'
yarn install
bundle exec ./bin/rails g cypress_on_rails:install --cypress_folder=spec/cypress --experimental --skip
echo '-- cypress and playwright install'
bundle exec ./bin/rails g cypress_on_rails:install --install_folder=spec/e2e --cypress_folder=spec/cypress --playwright_folder=spec/playwright --install_cypress --install_playwright --experimental --skip
rm -vf spec/cypress/e2e/rails_examples/advance_factory_bot.cy.js

echo '-- start rails server'
Expand All @@ -35,5 +34,10 @@ cp -fv ../cypress.config.js spec/
yarn run cypress run -P ./spec --record
# fi

echo '-- playwright run'
cp -fv ../playwright.config.js spec/
cd spec
yarn run playwright test

echo '-- stop rails server'
kill -9 `cat tmp/pids/server.pid`
9 changes: 7 additions & 2 deletions specs_e2e/rails_5_2/test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ echo '-- migration'
bundle exec ./bin/rails db:drop || true
bundle exec ./bin/rails db:create db:migrate

echo '-- cypress install'
bundle exec ./bin/rails g cypress_on_rails:install --cypress_folder=test/cypress --skip
echo '-- cypress and playwright install'
bundle exec ./bin/rails g cypress_on_rails:install --install_folder=test/e2e --cypress_folder=test/cypress --playwright_folder=test/playwright --install_cypress --install_playwright --skip
rm -vf test/cypress/e2e/rails_examples/using_vcr.cy.js

echo '-- start rails server'
Expand All @@ -39,5 +39,10 @@ cd test
yarn run cypress run --record
# fi

echo '-- playwright run'
cp -fv ../playwright.config.js test/
cd test
yarn run playwright test

echo '-- stop rails server'
kill -9 `cat ../tmp/pids/server.pid` || true

0 comments on commit d6ff6cd

Please sign in to comment.