Best JavaScript code snippet using testcafe
index.js
Source:index.js
...38 if ( USING_TOOLBOX ) {39 await startDockerMachine();40 }41 debug( 'Checking if daemon is running' );42 while ( ! await detectDockerDaemon() ) {43 setStatus( 'docker', 'missing-daemon' );44 await sleep( 1000 );45 }46 debug( 'Preparing to start Docker' );47 setStatus( 'docker', 'starting' );48 cwds[ 'wordpress-folder' ] = preferences.value( 'basic', 'wordpress-folder' );49 cwds[ 'gutenberg-folder' ] = preferences.value( 'basic', 'gutenberg-folder' );50 port = preferences.value( 'site', 'port' ) || 9999;51 if ( ! cwds[ 'wordpress-folder' ] || ! port ) {52 setStatus( 'docker', 'missing-wordpress-folder' );53 debug( 'Bailing, preferences not set' );54 return;55 }56 const defaultOptions = {57 version: '3.7',58 services: {59 'wordpress-develop': {60 image: 'nginx:alpine',61 ports: [62 port + ':80',63 ],64 volumes: [65 './default.conf:/etc/nginx/conf.d/default.conf',66 normalize( cwds[ 'wordpress-folder' ] ) + ':/var/www',67 ],68 links: [69 'php',70 ],71 },72 php: {73 image: 'garypendergast/wordpress-develop-php',74 volumes: [75 './php-config.ini:/usr/local/etc/php/conf.d/php-config.ini',76 normalize( cwds[ 'wordpress-folder' ] ) + ':/var/www',77 ],78 links: [79 'mysql',80 ],81 },82 mysql: {83 image: 'mysql:5.7',84 environment: {85 MYSQL_ROOT_PASSWORD: 'password',86 MYSQL_DATABASE: 'wordpress_develop',87 },88 healthcheck: {89 test: [ 'CMD', 'mysql', '-e', 'SHOW TABLES FROM wordpress_develop', '-uroot', '-ppassword', '-hmysql', '--protocol=tcp' ],90 interval: '1s',91 retries: '100',92 },93 volumes: [94 'mysql:/var/lib/mysql',95 ],96 },97 },98 volumes: {99 mysql: {},100 },101 };102 const scriptOptions = {103 version: '3.7',104 services: {105 cli: {106 image: 'wordpress:cli',107 volumes: [108 normalize( cwds[ 'wordpress-folder' ] ) + ':/var/www',109 ],110 },111 phpunit: {112 image: 'garypendergast/wordpress-develop-phpunit',113 volumes: [114 './phpunit-config.ini:/usr/local/etc/php/conf.d/phpunit-config.ini',115 normalize( cwds[ 'wordpress-folder' ] ) + ':/wordpress-develop',116 'phpunit-uploads:/wordpress-develop/src/wp-content/uploads',117 ],118 init: true,119 },120 },121 volumes: {122 'phpunit-uploads': {},123 },124 };125 if ( cwds[ 'gutenberg-folder' ] ) {126 const gutenbergVolume = normalize( cwds[ 'gutenberg-folder' ] ) + ':/var/www/src/wp-content/plugins/gutenberg';127 defaultOptions.services[ 'wordpress-develop' ].volumes.push( gutenbergVolume );128 defaultOptions.services.php.volumes.push( gutenbergVolume );129 scriptOptions.services.cli.volumes.push( gutenbergVolume );130 scriptOptions.services[ 'phpunit-gutenberg' ] = {131 image: 'garypendergast/wordpress-develop-phpunit',132 volumes: [133 normalize( cwds[ 'wordpress-folder' ] ) + ':/wordpress-develop',134 normalize( cwds[ 'gutenberg-folder' ] ) + ':/wordpress-develop/src/wp-content/plugins/gutenberg',135 ],136 };137 }138 const defaultOptionsYaml = yaml.safeDump( defaultOptions, { lineWidth: -1 } );139 writeFileSync( normalize( TOOLS_DIR + '/docker-compose.yml' ), defaultOptionsYaml );140 const scriptOptionsYaml = yaml.safeDump( scriptOptions, { lineWidth: -1 } );141 writeFileSync( normalize( TOOLS_DIR + '/docker-compose.scripts.yml' ), scriptOptionsYaml );142 copyFileSync( normalize( __dirname + '/default.conf' ), normalize( TOOLS_DIR + '/default.conf' ) );143 copyFileSync( normalize( __dirname + '/php-config.ini' ), normalize( TOOLS_DIR + '/php-config.ini' ) );144 copyFileSync( normalize( __dirname + '/phpunit-config.ini' ), normalize( TOOLS_DIR + '/phpunit-config.ini' ) );145 debug( 'Starting docker containers' );146 await spawn( 'docker-compose', [147 '-f',148 'docker-compose.yml',149 'up',150 '-d',151 ], {152 cwd: TOOLS_DIR,153 encoding: 'utf8',154 env: {155 PATH: process.env.PATH,156 ...dockerEnv,157 },158 } ).catch( ( { stderr } ) => debug( stderr ) );159 debug( 'Docker containers started' );160 setStatus( 'docker', 'ready' );161 addAction( 'grunt_watch_first_run_finished', 'installWordPress', installWordPress );162 if ( didAction( 'grunt_watch_first_run_finished' ) ) {163 installWordPress();164 }165}166/**167 * When we're using Docker Toolbox, then we need to check that the host machine is up and running.168 */169async function startDockerMachine() {170 debug( 'Starting docker machine' );171 await spawn( 'docker-machine', [172 'start',173 'default',174 ], {175 cwd: TOOLS_DIR,176 encoding: 'utf8',177 env: {178 PATH: process.env.PATH,179 },180 } ).catch( ( { stderr } ) => debug( stderr ) );181 const vboxManage = normalize( process.env.VBOX_MSI_INSTALL_PATH + '/VBoxManage' );182 debug( 'Configuring machine port forwarding' );183 await spawn( '"' + vboxManage + '"', [184 'controlvm',185 '"default"',186 'natpf1',187 'delete',188 'wphttp',189 ], {190 cwd: TOOLS_DIR,191 encoding: 'utf8',192 env: {193 PATH: process.env.PATH,194 },195 shell: true,196 } ).catch( ( { stderr } ) => debug( stderr ) );197 await spawn( '"' + vboxManage + '"', [198 'controlvm',199 '"default"',200 'natpf1',201 'wphttp,tcp,127.0.0.1,' + port + ',,' + port,202 ], {203 cwd: TOOLS_DIR,204 encoding: 'utf8',205 env: {206 PATH: process.env.PATH,207 },208 shell: true,209 } ).catch( ( { stderr } ) => debug( stderr ) );210 debug( 'Collecting docker environment info' );211 await spawn( 'docker-machine', [212 'env',213 'default',214 '--shell',215 'cmd',216 ], {217 cwd: TOOLS_DIR,218 encoding: 'utf8',219 env: {220 PATH: process.env.PATH,221 },222 } )223 .then( ( { stdout } ) => {224 stdout.split( '\n' ).forEach( ( line ) => {225 // Environment info is in the form: SET ENV_VAR=value226 if ( ! line.startsWith( 'SET' ) ) {227 return;228 }229 const parts = line.trim().split( /[ =]/, 3 );230 if ( 3 === parts.length ) {231 dockerEnv[ parts[ 1 ] ] = parts[ 2 ];232 }233 } );234 debug( 'Docker environment: %O', dockerEnv );235 } )236 .catch( ( { stderr } ) => debug( stderr ) );237}238/**239 * Runs the WP-CLI commands to install WordPress.240 */241async function installWordPress() {242 setStatus( 'wordpress', 'installing' );243 debug( 'Waiting for mysqld to start in the MySQL container' );244 while ( 1 ) {245 const { stdout } = await spawn( 'docker', [246 'inspect',247 '--format',248 '{{json .State.Health.Status }}',249 'tools_mysql_1',250 ], {251 cwd: TOOLS_DIR,252 encoding: 'utf8',253 env: {254 PATH: process.env.PATH,255 ...dockerEnv,256 },257 } );258 if ( stdout.trim() === '"healthy"' ) {259 break;260 }261 await sleep( 1000 );262 }263 debug( 'Checking if a config file exists' );264 const configExists = await runCLICommand( 'config', 'path' );265 if ( ! configExists ) {266 debug( 'Creating wp-config.php file' );267 await runCLICommand( 'config',268 'create',269 '--dbname=wordpress_develop',270 '--dbuser=root',271 '--dbpass=password',272 '--dbhost=mysql',273 '--path=/var/www/build' );274 if ( existsSync( normalize( cwds[ 'wordpress-folder' ] + '/build/wp-config.php' ) ) ) {275 debug( 'Moving wp-config.php out of the build directory' );276 renameSync(277 normalize( cwds[ 'wordpress-folder' ] + '/build/wp-config.php' ),278 normalize( cwds[ 'wordpress-folder' ] + '/wp-config.php' )279 );280 }281 debug( 'Adding debug options to wp-config.php' );282 await runCLICommand( 'config', 'set', 'WP_DEBUG', 'true', '--raw', '--type=constant' );283 await runCLICommand( 'config', 'set', 'SCRIPT_DEBUG', 'true', '--raw', '--type=constant' );284 await runCLICommand( 'config', 'set', 'WP_DEBUG_DISPLAY', 'true', '--raw', '--type=constant' );285 }286 debug( 'Checking if WordPress is installed' );287 const isInstalled = await runCLICommand( 'core', 'is-installed' );288 if ( isInstalled ) {289 debug( 'Updating site URL' );290 await runCLICommand( 'option', 'update', 'home', 'http://localhost:' + port );291 await runCLICommand( 'option', 'update', 'siteurl', 'http://localhost:' + port );292 } else {293 debug( 'Installing WordPress' );294 await runCLICommand( 'core',295 'install',296 '--url=localhost:' + port,297 '--title=WordPress Develop',298 '--admin_user=admin',299 '--admin_password=password',300 '--admin_email=test@test.test',301 '--skip-email' );302 }303 setStatus( 'wordpress', 'ready' );304 debug( 'WordPress ready at http://localhost:%d/', port );305}306/**307 * Spawns a process to run a WP-CLI command in a Docker container.308 *309 * @param {...string} args The WP-CLI command and arguments to be run.310 *311 * @return {Promise} Promise that resolves to true if the command succeeded, false if it failed.312 */313function runCLICommand( ...args ) {314 return spawn( 'docker-compose', [315 '-f',316 'docker-compose.yml',317 '-f',318 'docker-compose.scripts.yml',319 'run',320 '--rm',321 'cli',322 ...args,323 ], {324 cwd: TOOLS_DIR,325 encoding: 'utf8',326 env: {327 PATH: process.env.PATH,328 ...dockerEnv,329 },330 } )331 .then( () => true )332 .catch( ( { stderr } ) => {333 debug( stderr.trim() );334 return false;335 } );336}337/**338 * Figure out if the Docker daemon is running. No daemon implies that the user339 * needs to install and/or open Docker.340 *341 * @return {boolean} true if the Docker daemon is running, false if it isn't.342 */343async function detectDockerDaemon() {344 try {345 await spawn( 'docker', [ 'info' ] );346 return true;347 } catch {348 return false;349 }350}351/**352 * Figure out if we're using Docker Toolbox or not. Uses Docker for Windows' version and Hyper-V353 * requirements as a baseline to determine whether Toolbox is being used.354 *355 * @return {boolean} true if Docker Toolbox is being used, false if it isn't.356 */357async function detectToolbox() {...
is-docker-daemon-running.js
Source:is-docker-daemon-running.js
...21 }22}23module.exports = function () {24 if (OS.win)25 return detectDockerDaemon(DOCKER_DAEMON_DETECTION_OPTIONS.windows);26 if (OS.linux)27 return detectDockerDaemon(DOCKER_DAEMON_DETECTION_OPTIONS.linux);28 throw new Error(OS_NOT_SUPPORTED_ERROR);...
Using AI Code Generation
1const TestCafe = require('testcafe');2const testcafe = await TestCafe.createTestCafe('localhost', 1337, 1338);3const runner = testcafe.createRunner();4 .src('test.js')5 .browsers('chrome')6 .run();7testcafe.close();8process.exit(failedCount);9test('My test', async t => {10 .click('#myButton');11});12var spawn = require('child_process').spawn;13var docker = spawn('docker', ['run', '-d', '-p', '8080:8080', 'myimage']);14docker.stdout.on('data', function (data) {15 console.log('stdout: ' + data);16});17docker.stderr.on('data', function (data) {18console.log('stderr: ' + data);19});20docker.on('exit', function (code) {21 console.log('child process exited with code ' + code);22});
Using AI Code Generation
1var TestcafeDocker = require('testcafe-docker');2var testcafeDocker = new TestcafeDocker();3testcafeDocker.detectDockerDaemon().then(function(dockerDaemonRunning) {4 console.log(dockerDaemonRunning);5});6var TestcafeDocker = require('testcafe-docker');7var testcafeDocker = new TestcafeDocker();8testcafeDocker.detectDockerImage('selenium/standalone-chrome').then(function(dockerImageRunning) {9 console.log(dockerImageRunning);10});11var TestcafeDocker = require('testcafe-docker');12var testcafeDocker = new TestcafeDocker();13testcafeDocker.detectDockerContainer('selenium_chrome').then(function(dockerContainerRunning) {14 console.log(dockerContainerRunning);15});
Using AI Code Generation
1import { detectDockerDaemon } from 'testcafe-browser-tools';2detectDockerDaemon()3 .then(result => {4 console.log(result);5 })6 .catch(error => {7 console.log(error);8 });9{ isDocker: true }10import { detectDockerDaemon } from 'testcafe-browser-tools';11detectDockerDaemon()12 .then(result => {13 if (result.isDocker) {14 return testcafe.createRunner()15 .src('test.js')16 .browsers('chrome:headless')17 .run();18 }19 return testcafe.createRunner()20 .src('test.js')21 .browsers('chrome')22 .run();23 })24 .then(failedCount => {25 console.log('Tests failed: ' + failedCount);26 })27 .catch(error => {28 console.log(error);29 });
Using AI Code Generation
1import { detectDockerDaemon } from 'testcafe-browser-tools';2const dockerDaemon = await detectDockerDaemon();3if (dockerDaemon) {4 console.log('Docker daemon is running');5} else {6 console.log('Docker daemon is not running');7}8import { getDockerInfo } from 'testcafe-browser-tools';9const dockerInfo = await getDockerInfo();10console.log(dockerInfo);11import { getBrowserInfo } from 'testcafe-browser-tools';12const browserInfo = await getBrowserInfo('chrome');13console.log(browserInfo);14import { getInstallations } from 'testcafe-browser-tools';15const installations = await getInstallations();16console.log(installations);17import { getBrowserInfo } from 'testcafe-browser-tools';18const browserInfo = await getBrowserInfo('chrome');19console.log(browserInfo);20import { getInstallations } from 'testcafe-browser-tools';21const installations = await getInstallations();22console.log(installations);23import { getBrowserInfo } from 'testcafe-browser-tools';24const browserInfo = await getBrowserInfo('chrome');25console.log(browserInfo);26import { getInstallations } from 'testcafe-browser-tools';27const installations = await getInstallations();28console.log(installations);29import { getBrowserInfo } from 'testcafe-browser-tools';30const browserInfo = await getBrowserInfo('chrome');31console.log(browserInfo);32import { getInstallations } from 'testcafe-browser-tools';33const installations = await getInstallations();34console.log(installations);
Using AI Code Generation
1import { detectDockerDaemon } from 'testcafe-docker';2detectDockerDaemon();3import { detectDockerDaemon } from 'testcafe-docker';4detectDockerDaemon();5import { detectDockerDaemon } from 'testcafe-docker';6detectDockerDaemon();7import { detectDockerDaemon } from 'testcafe-docker';8detectDockerDaemon();9import { detectDockerDaemon } from 'testcafe-docker';10detectDockerDaemon();11import { detectDockerDaemon } from 'testcafe-docker';12detectDockerDaemon();13import { detectDockerDaemon } from 'testcafe-docker';14detectDockerDaemon();15import { detectDockerDaemon } from 'testcafe-docker';16detectDockerDaemon();17import { detectDockerDaemon } from 'testcafe-docker';18detectDockerDaemon();19import { detectDockerDaemon } from 'testcafe-docker';20detectDockerDaemon();21import { detectDockerDaemon } from 'testcafe-docker';22detectDockerDaemon();23import { detectDockerDaemon } from 'testcafe-docker';24detectDockerDaemon();
Using AI Code Generation
1import { detectDockerDaemon } from 'testcafe-browser-tools';2detectDockerDaemon()3 .then((isDocker) => {4 console.log('Docker Daemon is running:', isDocker);5 });6import { detectDockerDaemon } from 'testcafe-browser-tools';7detectDockerDaemon()8 .then((isDocker) => {9 console.log('Docker Daemon is running:', isDocker);10 });11import { detectDockerDaemon } from 'testcafe-browser-tools';12detectDockerDaemon()13 .then((isDocker) => {14 console.log('Docker Daemon is running:', isDocker);15 });16import { detectDockerDaemon } from 'testcafe-browser-tools';17detectDockerDaemon()18 .then((isDocker) => {19 console.log('Docker Daemon is running:', isDocker);20 });21import { detectDockerDaemon } from 'testcafe-browser-tools';22detectDockerDaemon()23 .then((isDocker) => {24 console.log('Docker Daemon is running:', isDocker);25 });26import { detectDockerDaemon } from 'testcafe-browser-tools';27detectDockerDaemon()28 .then((isDocker) => {29 console.log('Docker Daemon is running:', isDocker);30 });31import { detectDockerDaemon } from 'testcafe-browser-tools';32detectDockerDaemon()33 .then((isDocker) => {34 console.log('Docker Daemon is running:', isDocker);35 });36import { detectDockerDaemon } from 'testcafe-browser-tools';37detectDockerDaemon()38 .then((isDocker) => {39 console.log('Docker
Using AI Code Generation
1import { detectDockerDaemon } from "testcafe";2const isDocker = await detectDockerDaemon();3import { detectDockerDaemon } from "testcafe";4const isDocker = await detectDockerDaemon();5import { detectDockerDaemon } from "testcafe";6const isDocker = await detectDockerDaemon();7import { detectDockerDaemon } from "testcafe";8const isDocker = await detectDockerDaemon();
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!