Headless Cypress in docker container resolution always 1280x1024 - docker

When I run my tests in the official docker container (image cypress/included:7.5.0) the browser is always just 1280x1024.
When running the same config, same spec etc on host, I get desired 1920x1080.
My cypress.json:
{
"viewportHeight": 1080,
"viewportWidth": 1920,
"chromeWebSecurity": false,
"baseUrl": "http://phoenix:4000",
"watchForFileChanges": false,
"requestTimeout": 40000,
"defaultCommandTimeout": 40000,
"pageLoadTimeout": 80000,
"video": false,
"env": {
"codeCoverage": {
"url": "http://phoenix:4000/__coverage__",
"expectBackendCoverageOnly": false
}
}
In addition, I added some config like it you can find it explained here: https://docs.cypress.io/api/plugins/browser-launch-api#See-all-Chrome-browser-switches
My plugin/index.js:
module.exports = (on, config) => {
on('before:browser:launch', (browser, launchOptions) => {
if (browser.name === 'chrome' && browser.isHeadless) {
launchOptions.args.push('--window-size=1920,1080');
launchOptions.args.push('--force-device-scale-factor=1');
launchOptions.args.push('--start-fullscreen');
}
return launchOptions;
});
require('#cypress/code-coverage/task')(on, config);
return config;
};
Why do I still get 1280x1024 resolution when running from within docker container (./node_modules/.bin/cypress run --browser chrome)?

I got it, I need to start my own Xvfb server first:
Xvfb -screen 0 1920x1080x24 :99 & export DISPLAY=:99 && ./node_modules/.bin/cypress run --browser chrome && pkill Xvfb
Source: https://docs.cypress.io/guides/continuous-integration/introduction#Xvfb

Related

Docker image of cypress 12 is not working

I upgraded my cypress version from 8 to cypress 12.2.0.
When I run cypress suite at my local it works, But when run using docker image 12.2.0, it throw an error
`Your configFile threw an error from: cypress.config.js
We stopped running your tests because your config file crashed.
Error: ENOENT: no such file or directory, lstat '/support'
at Object.lstatSync (fs.js:1007:3)
at Object.<anonymous> (/root/.cache/Cypress/12.2.0/Cypress/resources/app/node_modules/#packages/server/node_modules/graceful-fs/polyfills.js:312:16)
at Object.lstatSync (/root/.cache/Cypress/12.2.0/Cypress/resources/app/node_modules/#packages/server/node_modules/graceful-fs/polyfills.js:312:16)
at Host._follow (/node_modules/tsify/lib/Host.js:278:19)
at Host.writeFile (/node_modules/tsify/lib/Host.js:127:29).............
My dockerFile is as below:
# FROM cypress/base:16.13.0
# FROM cypress/base:12
FROM cypress/base:12.2.0
COPY ./cypress ./cypress
COPY ./docker-entrypoint.sh ./docker-entrypoint.sh
COPY ./cypress.config.js ./cypress.config.js
COPY ./package.json ./package.json
COPY ./package-lock.json ./package-lock.json
COPY ./docker-entrypoint.sh ./docker-entrypoint.sh
COPY ./tsconfig.json ./tsconfig.json
RUN chmod +x docker-entrypoint.sh
RUN apt-get update && \
export DEBIAN_FRONTEND=noninteractive && \
apt-get -y install \
libgtk2.0-0 \
libgtk-3-0 \
libgbm-dev \
libnotify-dev \
libgconf-2-4 \
libnss3 \
libxss1 \
libasound2 \
libxtst6 xauth xvfb
RUN npm install
My cypress.config.js File as below:
const { defineConfig } = require( 'cypress');
const cucumber = require('cypress-cucumber-preprocessor').default
const browserify = require('#cypress/browserify-preprocessor');
export default defineConfig({
reporter: 'cypress-mochawesome-reporter',
defaultCommandTimeout: 5000,
numTestsKeptInMemory: 0,
viewportWidth: 1360,
viewportHeight: 768,
retries: 1,
env: {
username: 'sxxxxxxxxxx',
password: 'xxxxxxxx',
},
"video": false,
e2e: {
chromeWebSecurity: false,
// We've imported your old cypress plugins here.
// You may want to clean this up later by importing these.
setupNodeEvents(on, config) {
const options = browserify.defaultOptions;
options.browserifyOptions.plugin.unshift(['tsify']);
on("file:preprocessor", cucumber(options));
// on("file:preprocessor", cucumber());
// return require('./cypress/plugins/index.js')(on, config)
},
defaultCommandTimeout: 10000,
specPattern: 'cypress/integration/**/*.feature',
supportFile: 'cypress/support/e2e.js'
},
})
My local node --version is v16.3.0.
To execute the test suite in docker commands as below:
docker build -t btest:cypre . // to create docker image
docker run -it --rm --name bContainer -v $PWD/cypress:/cypress btest:cypre // to create container and start
docker exec bContainer npm run bokieLoc -- video=false //// to run project
My package.json file as below :
"scripts": {
"bokieLoc": "npx cypress-tags run --env TAGS=\"#home\" --config ",
},
"author": "",
"license": "ISC",
"dependencies": {
"ng": "^0.0.0"
},
"devDependencies": {
"#cypress/browserify-preprocessor": "^3.0.1",
"#cypress/webpack-preprocessor": "^5.9.1",
"add": "^2.0.6",
"cucumber-html-reporter": "^5.5.0",
"cypress": "^12.2.0",
"cypress-cucumber-attach-screenshots-to-failed-steps": "^1.0.0",
"cypress-cucumber-preprocessor": "^4.2.0",
"cypress-mochawesome-reporter": "^2.2.0",
"cypress-xpath": "^1.6.2",
"multiple-cucumber-html-reporter": "^1.18.0",
"tsify": "^5.0.4",
"typescript": "^4.5.5",
"webpack": "^5.61.0",
"yarn": "^1.22.10"
},
"cypress-cucumber-preprocessor": {
"nonGlobalStepDefinitions": true,
"cucumberJson": {
"generate": true,
"outputFolder": "cypress/cucumber_report",
"filePrefix": "",
"fileSuffix": ".cucumber"
}
}
}
I stuck here from last 3 days, please let know where I am making a mistake.
Need to add return config statement in cypress.config.js
So cypress.config.js file should be like this :
const { defineConfig } = require('cypress');
const createBundler = require('#bahmutov/cypress-esbuild-preprocessor');
const addCucumberPreprocessorPlugin =
require('#badeball/cypress-cucumber-preprocessor').addCucumberPreprocessorPlugin;
const createEsbuildPlugin =
require('#badeball/cypress-cucumber-preprocessor/esbuild').createEsbuildPlugin;
module.exports = defineConfig({
defaultCommandTimeout: 5000,
numTestsKeptInMemory: 0,
viewportWidth: 1360,
viewportHeight: 768,
env: {
username: 'xxxxxxxx',
password: 'xxxxxxxxx',
},
"retries": 1,
"video": false,
e2e: {
// Integrate #bahmutov/cypress-esbuild-preprocessor plugin.
async setupNodeEvents(on, config) {
const bundler = createBundler({
plugins: [createEsbuildPlugin(config)],
});
// This is required for the preprocessor to be able to generate JSON reports after each run, and more,
on('file:preprocessor', bundler);
await addCucumberPreprocessorPlugin(on, config);
return config;
},
specPattern: 'cypress/e2e/**/*.feature',
},
});

Jenkins timing out

I am working with Jenkins pipelines and I have this code:
stages {
stage('Stage1') {
options {
timeout(time: 1, unit: "MINUTES")
}
steps {
script {
sh'''
#!/bin/bash
set -eux pipefail
ssh user#server.com "
ssh -p 50 user#localhost'\
docker run --rm --name name\
-e user=...\
-e passwd=...\
-v /location:/location2\
-w location2\
server2.com:6000/my-x-y:1.1\
python script.py\
'\
"
'''
}
}
}
}
When the connection inside the script is not being made the job will timeout but it will still go on and will be still marked as succeeded.
I get this message:
17:10:53 Cancelling nested steps due to timeout
17:10:53 Sending interrupt signal to process
After that the jobs moves to the next stage and the status is success.
So even though I am getting timeout the job is being marked as success.
I'd like to send notifications when this stage is not properly executed (I already have a notification.sh script for it).
Anyway I can get this job to be aborted when it gets the timeout?
Or any other way to go around this in order to warn users that this stage was not properly executed?
Try something like below.
try {
timeout (time: 10, unit: 'SECONDS') {
sh'''
#!/bin/bash
set -eux pipefail
ssh user#server.com "
ssh -p 50 user#localhost'\
docker run --rm --name name\
-e user=...\
-e passwd=...\
-v /location:/location2\
-w location2\
server2.com:6000/my-x-y:1.1\
python script.py\
'\
"
'''
}
}
catch (error) {
echo "Error: $error"
def cause = error.getCauses()[0].getClass().toString()
if(cause.contains("ExceededTimeout")) { // If you want handle timeout as a special case
echo "This was a Timeout"
// Do whatever you want
}
}
Full Sample Pipeline
pipeline {
agent any
stages {
stage('TimerTest') {
steps {
script {
try {
timeout (time: 10, unit: 'SECONDS') {
echo "In timer"
sleep 15
}
}
catch (error) {
echo "XXXX: $error"
def cause = error.getCauses()[0].getClass().toString()
println "$cause"
if(cause.contains("ExceededTimeout")) {
echo "This was a Timeout"
// Do what ever you want
}
}
}
}
}
}
}

Running protractor on Jenkins doesn't fail the build when there is an error

I have set up Jenkins Slave on Windows VM. when there are failures in my tests , the build status always shows succeeded.
Here is how I run protractor tests on jenkins
Windows PowerShell command :
cd conf
protractor ConfProd.js
My Conf file:
var HtmlReporter = require('protractor-html-screenshot-reporter');
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub', //desktop
allScriptsTimeout: 60000,
baseUrl: 'https://myTest.com',
params: {
empUrl: 'https://employeeurl.com/',
},
// frameworks to use
frameworks: 'jasmine2',
directConnect: 'true',
//Capabilities to be passed to the webdriver instance.
multiCapabilities: [{
'browserName': 'chrome',
'chromeOptions' : {
args: ['--window-size=1200,1200']
},
specs: [
'../tests/*.spec.js'
],
},
{
'browserName': 'firefox',
'firefoxOptions' : {
args: ['--window-size=900,900']
},
specs: [
'../tests/*.spec.js'
],
exclude: ['../tests/EmployeeTests.spec.js'],
}],
onPrepare: function () {
jasmine.getEnv().addReporter(new HtmlReporter({
baseDirectory: '/tmp/screenshots',
docTitle: 'TestReports',
takeScreenShotsOnlyForFailedSpecs: true
}));
},
jasmineNodeOpts: {
showColors: true,
isVerbose: true,
includeStackTrace: true,
}
};
This is the message from console output on Jenkins:
[launcher] chrome #1 failed 4 test(s)
[launcher] firefox #2 failed 4 test(s)
[launcher] overall: 8 failed spec(s)
Checking for post-build
Performing post-build step
Checking if email needs to be generated
No emails were triggered.
Finished: SUCCESS

Set up Protractor tests with Jenkins

I need to get my protractor tests to run using Jenkins. I know this has been asked before, and this seems like a really good answer. But I'm confused about where the bash script comes from and how to move forward. Here's what I've got:
Protractor config file:
var HtmlScreenshotReporter = require('protractor-jasmine2-screenshot-reporter');
require('jasmine-reporters');
exports.config = {
seleniumAddress: 'http://localhost:4444/wd/hub',
capabilities: {
'browserName': 'chrome'
},
framework: 'jasmine2',
suites: {...},
jasmineNodeOpts: {
showColors: true,
defaultTimeoutInterval: 10000
},
onPrepare: function() {
global.isAngularSite = function(flag) {
browser.ignoreSynchronization = !flag;
};
browser.manage().window().setPosition(0,0);
browser.manage().window().setSize(1280, 1024);
}
jasmine.getEnv().addReporter(
new jasmine.JUnitXmlReporter('protractor_output', true, true)
);
}
How can I get my tests to run with Jenkins? Please help

Protractor tests hung up after finishing tests

I have configured grunt tasks to start the protractor webdriver and then run the protractor tests with protractor-runner, grunt config is like
.....
protractor_webdriver: {
start: {
options: {
path: 'node_modules/protractor/bin/',
command: 'webdriver-manager start --standalone'
}
}
},
protractor: {
options: {
configFile: proctatorConf,
noColor: false,
args: {
}
},
auto: {
options: {
keepAlive: true,
args: {
}
}
},
singlerun: {
options: {
keepAlive: false,
args: {
}
}
}
}
....
grunt.registerTask('test:e2e', [
'shell:update_webdriver',
'protractor_webdriver:start',
'protractor:singlerun'
]);
and in Jenkins i installed xvfb-plugin and said on the job configuration to startup Xvfb screen and shutdown after job finish,
And what is the problem is that when jenkins execute grunt test:e2e command, it executes all test, give me failure information like
Finished in 16.455 seconds
[31m4 tests, 6 assertions, 4 failures
and jenkins job hungs on that, what could be the problem for this?

Resources