Best JavaScript code snippet using ghostjs
index.js
Source:index.js
...19}).listen(3000, function () {20 console.log('æå¡å¨å¯å¨ãããã')21});22function getAllMedicineList(res){23 startRequest(1)24 .then(startRequest)25 .then(startRequest)26 .then(startRequest)27 .then(startRequest)28 .then(startRequest)29 .then(startRequest)30 .then(startRequest)31 .then(startRequest)32 .then(startRequest)33 .then(startRequest)34 .then(startRequest)35 .then(startRequest)36 .then(startRequest)37 .then(startRequest)38 .then(startRequest)39 .then(startRequest)40 .then(startRequest)41 .then(startRequest)42 .then(startRequest)43 .then(startRequest)44 .then(startRequest)45 .then(startRequest)46 .then(function () {47 console.log('æ»æ£ç´¢æ¡æ°ï¼',urlArray.length)48 res.send(urlArray);49 })50}51function queryRealData(){52 var i= 0,len=urlArray.length;53 //éç¨http模ååæå¡å¨åèµ·ä¸æ¬¡get请æ±54 http.get(urlArray[i], function (res) {55 var html = ''; //ç¨æ¥åå¨è¯·æ±ç½é¡µçæ´ä¸ªhtmlå
容56 res.setEncoding('utf-8'); //é²æ¢ä¸æä¹±ç 57 //çå¬dataäºä»¶ï¼æ¯æ¬¡åä¸åæ°æ®58 res.on('data', function (chunk) {59 html += chunk;60 });61 //çå¬endäºä»¶ï¼å¦ææ´ä¸ªç½é¡µå
容çhtmlé½è·åå®æ¯ï¼å°±æ§è¡åè°å½æ°62 res.on('end', function () {63 var $ = cheerio.load(html); //éç¨cheerio模å解æhtml64 var ul=$("#content");65 // è¯æå称ï¼bt66 // å¾çï¼tp67 // æ¼é³ï¼py68 // è±æåï¼ywm69 // å«åï¼bm70 // åºå¤ï¼cc71 // æ¥æºï¼ly72 // åå½¢æï¼yxt73 // çå¢åé¨ï¼sjfb74 // æ§ç¶:xz75 // é´å«ï¼jb76 // å«éæµå®ï¼hlcd77 // æ§å³ï¼xw78 // å½ç»ï¼gj79 // åè½ä¸»æ²»ï¼gnzz80 // ç¨æ³ç¨éï¼yfyl81 // å¤æ¹ï¼ff82 // 注æï¼zy83 // è´®èï¼zc84 // å家论述:gjls85 // æå½ï¼zl86 if(ul.find('.bt span').next().text()){87 }88 index++;89 resolve(index)90 });91 }).on('error', function (err) {92 console.log(err)93 });94}95//åå§url96function startRequest(index) {97 return new Promise(function (resolve,reject) {98 //éç¨http模ååæå¡å¨åèµ·ä¸æ¬¡get请æ±99 http.get(getTopPageUrl(index), function (res) {100 var html = ''; //ç¨æ¥åå¨è¯·æ±ç½é¡µçæ´ä¸ªhtmlå
容101 res.setEncoding('utf-8'); //é²æ¢ä¸æä¹±ç 102 //çå¬dataäºä»¶ï¼æ¯æ¬¡åä¸åæ°æ®103 res.on('data', function (chunk) {104 html += chunk;105 });106 //çå¬endäºä»¶ï¼å¦ææ´ä¸ªç½é¡µå
容çhtmlé½è·åå®æ¯ï¼å°±æ§è¡åè°å½æ°107 res.on('end', function () {108 var $ = cheerio.load(html); //éç¨cheerio模å解æhtml109 var ul=$("#tab-content");110 var aArray=Array.prototype.slice.call($(ul).find('li').find('a'));...
contract.js
Source:contract.js
1import axios from 'axios';2export const startRequest = {3 type: 'REQUEST_START'4};5export const successRequest = {6 type: 'REQUEST_SUCCESS'7};8export const successContract = {9 type: 'CONTRACT_SUCCESS'10};11export const successGetRequest = request => ({12 type: 'GET_REQUEST_SUCCESS',13 request14});15export const successGetContract = contract => ({16 type: 'GET_CONTRACT_SUCCESS',17 contract18});19export const errorRequest = error => ({20 type: 'REQUEST_ERROR',21 error22});23export const sendRequest = data => {24 return dispatch => {25 const token = window.sessionStorage.getItem('jwtToken');26 dispatch(startRequest);27 axios({28 method: 'post',29 url: '/users/registerTutor/register',30 data,31 headers: {32 Authorization: `Bearer ${token}`33 }34 })35 .then(() => {36 dispatch(successRequest);37 })38 .catch(err => {39 dispatch(errorRequest(err.response.data));40 });41 };42};43export const acceptRequest = data => {44 return dispatch => {45 dispatch(startRequest);46 axios({47 method: 'post',48 url: '/tutors/contract/accept',49 data50 })51 .then(() => {52 dispatch(successRequest);53 })54 .catch(err => {55 dispatch(errorRequest(err.response.data));56 });57 };58};59export const declineRequest = data => {60 return dispatch => {61 dispatch(startRequest);62 axios({63 method: 'post',64 url: '/tutors/registerTutor/update',65 data66 })67 .then(() => {68 dispatch(successRequest);69 })70 .catch(err => {71 dispatch(errorRequest(err.response.data));72 });73 };74};75export const getTutorRequest = id => {76 return dispatch => {77 dispatch(startRequest);78 axios({79 method: 'post',80 url: '/tutors/registerTutor/getList',81 data: { id }82 })83 .then(res => {84 dispatch(successGetRequest(res.data));85 })86 .catch(err => {87 dispatch(errorRequest(err.response.data));88 });89 };90};91export const getTutorContract = id => {92 return dispatch => {93 dispatch(startRequest);94 axios({95 method: 'post',96 url: '/tutors/contract/getList',97 data: { id }98 })99 .then(res => {100 dispatch(successGetContract(res.data));101 })102 .catch(err => {103 dispatch(errorRequest(err.response.data));104 });105 };106};107export const getStudentRequest = id => {108 return dispatch => {109 dispatch(startRequest);110 const token = window.sessionStorage.getItem('jwtToken');111 axios({112 method: 'post',113 url: '/users/registerTutor/getList',114 data: { id },115 headers: {116 Authorization: `Bearer ${token}`117 }118 })119 .then(res => {120 dispatch(successGetRequest(res.data));121 })122 .catch(err => {123 dispatch(errorRequest(err.response.data));124 });125 };126};127export const getStudentContract = id => {128 return dispatch => {129 dispatch(startRequest);130 const token = window.sessionStorage.getItem('jwtToken');131 axios({132 method: 'post',133 url: '/users/contract/getList',134 data: { id },135 headers: {136 Authorization: `Bearer ${token}`137 }138 })139 .then(res => {140 dispatch(successGetContract(res.data));141 })142 .catch(err => {143 dispatch(errorRequest(err.response.data));144 });145 };146};147export const finishContract = data => {148 return dispatch => {149 const token = window.sessionStorage.getItem('jwtToken');150 dispatch(startRequest);151 axios({152 method: 'post',153 url: '/users/contract/update',154 data,155 headers: {156 Authorization: `Bearer ${token}`157 }158 })159 .then(() => {160 dispatch(successContract);161 })162 .catch(err => {163 dispatch(errorRequest(err.response.data));164 });165 };166};167/**168 * data= {169 * IDND,170 * IDNH,171 * DANHGIA,172 * NOIDUNG173 * } */174export const sendReview = data => {175 return dispatch => {176 const token = window.sessionStorage.getItem('jwtToken');177 dispatch(startRequest);178 axios({179 method: 'post',180 url: '/users/review/send',181 data,182 headers: {183 Authorization: `Bearer ${token}`184 }185 })186 .then(() => {187 dispatch(successRequest);188 })189 .catch(err => {190 dispatch(errorRequest(err.response.data));191 });192 };193};194/**195 * data= {196 * IDND,197 * IDNH,198 * TIEUDE,199 * NOIDUNG200 * } */201export const sendRefund = data => {202 return dispatch => {203 const token = window.sessionStorage.getItem('jwtToken');204 dispatch(startRequest);205 axios({206 method: 'post',207 url: '/users/refund/send',208 data,209 headers: {210 Authorization: `Bearer ${token}`211 }212 })213 .then(() => {214 dispatch(successRequest);215 })216 .catch(err => {217 dispatch(errorRequest(err.response.data));218 });219 };...
requestReducer.test.ts
Source:requestReducer.test.ts
...8 initState = { requests: [] };9 });10 describe("startRequestAction", () => {11 it("with normal request", () => {12 initState = reducer(initState, actions.startRequest({ url: TEST_URL }));13 expect(initState).toStrictEqual({14 requests: [{ status: "loading", url: TEST_URL, error: null }],15 });16 });17 it("with duplicated request", () => {18 initState = reducer(initState, actions.startRequest({ url: TEST_URL }));19 expect(() => {20 reducer(initState, actions.startRequest({ url: TEST_URL }));21 }).toThrowError("ì¤ë³µë ìì²ì
ëë¤");22 expect(initState).toStrictEqual({23 requests: [{ status: "loading", url: TEST_URL, error: null }],24 });25 });26 it("with multiple request", () => {27 initState = reducer(initState, actions.startRequest({ url: TEST_URL }));28 initState = reducer(initState, actions.startRequest({ url: TEST_URL1 }));29 expect(initState).toStrictEqual({30 requests: [31 { status: "loading" as RequestStatus, url: TEST_URL, error: null },32 { status: "loading" as RequestStatus, url: TEST_URL1, error: null },33 ],34 });35 });36 });37 describe("failRequestAction", () => {38 it("with none existing request", () => {39 expect(() => {40 reducer(initState, actions.failRequest({ error: new Error("ìë¬"), url: TEST_URL }));41 }).toThrowError("ìì²ì´ ì¡´ì¬íì§ ììµëë¤");42 });43 it("with normal fail", () => {44 initState = reducer(initState, actions.startRequest({ url: TEST_URL }));45 initState = reducer(initState, actions.failRequest({ error: Error("ìë¬"), url: TEST_URL }));46 expect(initState).toStrictEqual({47 requests: [{ url: TEST_URL, status: "failed", error: Error("ìë¬") }],48 });49 });50 it("with multiple fail", () => {51 initState = reducer(initState, actions.startRequest({ url: TEST_URL }));52 initState = reducer(initState, actions.startRequest({ url: TEST_URL1 }));53 initState = reducer(initState, actions.failRequest({ url: TEST_URL, error: Error("Err") }));54 initState = reducer(initState, actions.failRequest({ url: TEST_URL1, error: Error("Err1") }));55 expect(initState).toStrictEqual({56 requests: [57 { error: Error("Err"), url: TEST_URL, status: "failed" },58 { error: Error("Err1"), url: TEST_URL1, status: "failed" },59 ],60 });61 });62 });63 // finish requestìì±64 describe("endRequestAction", () => {65 it("with normal end", () => {66 initState = reducer(initState, actions.startRequest({ url: TEST_URL }));67 initState = reducer(initState, actions.endRequest({ url: TEST_URL }));68 expect(initState).toStrictEqual({69 requests: [],70 });71 });72 it("with none existing end", () => {73 initState = reducer(initState, actions.startRequest({ url: TEST_URL }));74 expect(() => {75 reducer(initState, actions.endRequest({ url: TEST_URL1 }));76 }).toThrowError("ìì²ì´ ì¡´ì¬íì§ ììµëë¤");77 expect(initState).toStrictEqual({78 requests: [{ url: TEST_URL, status: "loading", error: null }],79 });80 });81 test("with multiple end", () => {82 initState = reducer(initState, actions.startRequest({ url: TEST_URL }));83 initState = reducer(initState, actions.startRequest({ url: TEST_URL1 }));84 initState = reducer(initState, actions.endRequest({ url: TEST_URL }));85 initState = reducer(initState, actions.endRequest({ url: TEST_URL1 }));86 expect(initState).toStrictEqual({87 requests: [],88 });89 });90 });...
Using AI Code Generation
1var ghost = require('ghostjs'),2 phantom = require('phantom'),3phantom.create(function (ph) {4 ph.createPage(function (page) {5 page.open(url, function (status) {6 console.log("opened site? ", status);7 page.evaluate(function () {8 return document.title;9 }, function (result) {10 console.log('Page title is ' + result);11 ph.exit();12 });13 });14 });15});
Using AI Code Generation
1var ghost = require('ghostjs');2 ghost.wait(1000, function() {3 ghost.screenshot('google.png', function() {4 ghost.exit();5 });6 });7});8var ghost = require('ghostjs');9 ghost.wait(1000, function() {10 ghost.screenshot('google.png', function() {11 ghost.exit();12 });13 });14});15$('#submit').click(function() {16 var name = $('#name').val();17 var email = $('#email').val();18 var message = $('#message').val();19 socket.emit('send', {name: name, email: email, message: message});20});21var app = require('express')();22var http = require('http').Server(app);23var io = require('socket.io')(http);24io.on('connection', function(socket) {25 socket.on('send', function(data) {26 console.log("Received: " + data.name + ", " + data.email + ", " + data.message);27 });28});29http.listen(3000, function() {30 console.log('listening on *:3000');31});32io.on('connection', function(socket) {33 socket.on('send', function(data) {34 console.log("Received: " + data.name + ", " + data.email + ", " + data.message);35 });36});37io.on('connection', function(socket) {38 socket.on('send', function(data) {39 console.log("Received: " + data.name + ", " + data.email + ",
Using AI Code Generation
1var ghost = require('ghostjs');2 console.log(result.body);3});4var ghost = require('ghostjs');5 console.log(result.body);6});7var ghost = require('ghostjs');8 console.log(result.body);9});10var ghost = require('ghostjs');11 console.log(result.body);12});13var ghost = require('ghostjs');14 console.log(result.body);15});16var ghost = require('ghostjs');17 console.log(result.body);18});19var ghost = require('ghostjs');20 console.log(result.body);21});22var ghost = require('ghostjs');23 console.log(result.body);24});25var ghost = require('ghostjs');26 console.log(result.body);27});28var ghost = require('ghostjs');29 console.log(result.body);30});31var ghost = require('ghostjs');
Using AI Code Generation
1var ghost = require('ghostjs');2var request = require('request');3var fs = require('fs');4var cheerio = require('cheerio');5 browser.fill('input[name=q]', 'github');6 browser.press('input[name=q]', 'enter');7 browser.wait(2000);8 browser.html(function(err, html) {9 fs.writeFile('google.html', html, function(err) {10 if (err) throw err;11 console.log('File saved.');12 });13 });14 browser.close();15});16### ghost.start(options, callback)17### ghost.startRequest(url, options, callback)18### ghost.stop(callback)19### ghost.stopRequest(callback)20### ghost.html(callback)21### ghost.screenshot(filename, callback)22### ghost.wait(milliseconds, callback)23### ghost.focus(selector, callback)24### ghost.click(selector, callback)25### ghost.press(selector, key, callback)26### ghost.fill(selector, value, callback)27### ghost.evaluate(fn, args, callback)
Using AI Code Generation
1var ghostjs = require('ghostjs');2ghostjs.startRequest(url, function(err, response){3 console.log(response);4});5var ghostjs = require('ghostjs');6ghostjs.startRequest(url, function(err, response){7 console.log(response);8});9var ghostjs = require('ghostjs');10ghostjs.startRequest(url, function(err, response){11 console.log(response);12});13var ghostjs = require('ghostjs');14ghostjs.startRequest(url, function(err, response){15 console.log(response);16});17var ghostjs = require('ghostjs');18ghostjs.startRequest(url, function(err, response){19 console.log(response);20});21var ghostjs = require('ghostjs');22ghostjs.startRequest(url, function(err, response){23 console.log(response);24});25var ghostjs = require('ghostjs');26ghostjs.startRequest(url, function(err, response){27 console.log(response);28});29var ghostjs = require('ghostjs');30ghostjs.startRequest(url, function(err, response){31 console.log(response);32});33var ghostjs = require('ghostjs');34ghostjs.startRequest(url, function(err, response){35 console.log(response);36});37var ghostjs = require('ghostjs');
Using AI Code Generation
1var ghost = require('ghostjs');2ghost.startRequest(url, function(err, response) {3 console.log(response);4});5function test() {6 var a = 1;7 var b = 2;8 var c = 3;9 var d = 4;10 var e = 5;11 var f = 6;12 var g = 7;13 var h = 8;14 var i = 9;15 var j = 10;16 var k = 11;17 var l = 12;18 var m = 13;19 var n = 14;20 var o = 15;21 var p = 16;22 var q = 17;23 var r = 18;24 var s = 19;25 var t = 20;26 var u = 21;27 var v = 22;28 var w = 23;29 var x = 24;30 var y = 25;31 var z = 26;32 var aa = 27;33 var bb = 28;34 var cc = 29;35 var dd = 30;36 var ee = 31;37 var ff = 32;38 var gg = 33;39 var hh = 34;40 var ii = 35;41 var jj = 36;42 var kk = 37;43 var ll = 38;44 var mm = 39;45 var nn = 40;46 var oo = 41;47 var pp = 42;48 var qq = 43;49 var rr = 44;50 var ss = 45;51 var tt = 46;52 var uu = 47;53 var vv = 48;54 var ww = 49;55 var xx = 50;56 var yy = 51;
Using AI Code Generation
1const ghost = require('ghostjs');2});3const ghost = require('ghostjs');4});5ghost.startRequest(url, callback)6ghost.startRequestWithCookies(url, cookies, callback)7ghost.startRequestWithHeaders(url, headers, callback)8ghost.startRequestWithCookiesAndHeaders(url, cookies, headers, callback)9ghost.getCookie(url, callback)10ghost.getHeaders(url, callback)11ghost.getCookieAndHeaders(url, callback)
Using AI Code Generation
1var ghost = require('ghostjs');2####startRequest(url, callback)3var ghost = require('ghostjs');4 if(err){5 console.log(err);6 }7 else{8 console.log(res);9 }10});11####setUserAgent(userAgent, callback)12var ghost = require('ghostjs');13ghost.setUserAgent('Mozilla/5.0 (X11; Linux x86_64; rv:40.0) Gecko/20100101 Firefox/40.0', function(err, res){14 if(err){15 console.log(err);16 }17 else{18 console.log(res);19 }20});21####setViewport(width, height, callback)22var ghost = require('ghostjs');23ghost.setViewport(1366, 768, function(err, res){24 if(err){25 console.log(err);26 }27 else{28 console.log(res);29 }30});31####setZoomFactor(zoomFactor, callback)
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!!