How to use attrs.split method in Cypress

Best JavaScript code snippet using cypress

Run Cypress automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

start.js

Source: start.js Github

copy
1const config = require('../config');
2const path = require('path');
3const express = require('express');
4const app = express();
5const ejs = require('ejs');
6const bodyParser = require('body-parser');
7const { parse } = require('node-html-parser');
8const rp = require('request-promise-native');
9const { htmlToText } = require('html-to-text');
10let ts = Date.now();
11let da = new Date(ts);
12utc = da.getTime() + da.getTimezoneOffset() * 60000;
13offset = -6;
14const date = new Date(utc + 3600000 * offset);
15const passwordProtected = require('./login');
16app.use(passwordProtected());
17
18module.exports = async client => {
19  const dataDir = path.resolve(`${process.cwd()}${path.sep}webapp`);
20  const templateDir = path.resolve(`${dataDir}${path.sep}templates`);
21  console.log(' Starting ComicServer...');
22  app.engine('json', ejs.renderFile);
23  app.use('/', express.static(path.resolve(`${dataDir}${path.sep}assets`)));
24  const renderTemplate = (res, req, template, data = {}) => {
25    res.render(
26      path.resolve(`${templateDir}${path.sep}${template}`),
27      Object.assign(data));
28  };
29
30  app.get('/', (req, res) => {
31    renderTemplate(res, req, 'betai.ejs');
32  });
33
34  app.post('/', (req, res) => {
35    renderTemplate(res, req, 'betai.ejs');
36  });
37
38  app.get('/versions', (req, res) => {
39    renderTemplate(res, req, 'betav.ejs');
40  });
41
42  app.get('/tomorrow/:comic/:year/:month/:day', async (req, res) => {
43    const today = req.params.day;
44    const acyear = req.params.year;
45    const acmonth = req.params.month;
46    const datestring = acyear + '-' + acmonth + '-' + today;
47    const comic = req.params.comic;
48    const parsedPage = parse(
49      await rp(
50        `https://licensing.andrewsmcmeel.com/features/${comic}?date=${datestring}`
51      )
52    );
53    const csRawImage = parsedPage
54      .querySelector('.text-center img')
55      .rawAttrs.split(/ src=/)[1]
56      .replace(/"/g, '');
57    const csImage = csRawImage.slice(0, 63);
58    const csRawDate = parsedPage
59      .querySelector('.text-center img')
60      .rawAttrs.split(/ alt=/)[1]
61      .replace(/"/g, '');
62    const csDate = csRawDate;
63    renderTemplate(res, req, 'betaf.ejs', { csImage, csDate, comic });
64  });
65
66  app.get('/tomorrow/:comic', async (req, res) => {
67    const today = date.getDate() + 1;
68    const acyear = date.getFullYear();
69    const acmonth = ('0' + (date.getMonth() + 1)).slice(-2);
70    const datestring = acyear + '-' + acmonth + '-' + today;
71    const comic = req.params.comic;
72    const parsedPage = parse(
73      await rp(
74        `https://licensing.andrewsmcmeel.com/features/${comic}?date=${datestring}`
75      )
76    );
77    const csRawImage = parsedPage
78      .querySelector('.text-center img')
79      .rawAttrs.split(/ src=/)[1]
80      .replace(/"/g, '');
81    const csImage = csRawImage.slice(0, 63);
82    const csRawDate = parsedPage
83      .querySelector('.text-center img')
84      .rawAttrs.split(/ alt=/)[1]
85      .replace(/"/g, '');
86    const csDate = csRawDate;
87    res.redirect(`/tomorrow/${comic}/${acyear}/${acmonth}/${today}`);
88  });
89
90  app.get('/:comic/:year/:month/:day', async (req, res) => {
91    const today = ('0' + (date.getDate())).slice(-2);
92    const acyear = date.getFullYear();
93    const acmonth = ('0' + (date.getMonth() + 1)).slice(-2);
94    const datestring = acyear + '/' + acmonth + '/' + today;
95    const comic = req.params.comic;
96    const year = req.params.year;
97    const month = req.params.month;
98    const dayy = ('0' + (req.params.day)).slice(-2);
99    const monthh = ('0' + (req.params.month)).slice(-2);
100    const day = req.params.day;
101    var ejsstring = year + "/" + monthh + "/" + dayy
102    const parsedPage = parse(
103      await rp(
104        `https://www.gocomics.com/${comic}/${year}/${month}/${day}`
105      ).catch(err => {
106        console.log('Request failed\n', err);
107      })
108    );
109    var csImage = parsedPage
110      .querySelector('.item-comic-image img')
111      .rawAttrs.split(/ src=/)[1]
112      .replace(/"/g, '');
113    const csRawDate = parsedPage
114      .querySelector('.item-comic-image img')
115      .rawAttrs.split(/ alt=/)[1]
116      .replace(/"/g, '');
117    const csDate = csRawDate.slice(0, -70);
118    const prev = day - 1;
119    const next = prev + 2;
120    const csRawParamsStart = parsedPage
121      .querySelector(
122        '.btn.btn-outline-secondary.gc-calendar-nav__datepicker.js-calendar-wrapper'
123      )
124      .rawAttrs.split(/ data-start=/)[1]
125      .replace(/"/g, '');
126    const csParamsStart = csRawParamsStart.slice(0, -63);
127    const csStartFormatted = csParamsStart.slice(0, 4);
128    const csRawParamsEnd = parsedPage
129      .querySelector(
130        '.btn.btn-outline-secondary.gc-calendar-nav__datepicker.js-calendar-wrapper'
131      )
132      .rawAttrs.split(/ data-end=/)[1]
133      .replace(/"/g, '');
134    const csParamsEnd = csRawParamsEnd.slice(0, -32);
135    const csEndFormatted = csParamsEnd.slice(0, 4);
136    const csTest = parsedPage
137      .querySelector('.gc-calendar-nav__next a')
138      .rawAttrs.split(/ href=/)[1]
139      .replace(/"/g, '');
140    const csLength = comic.length + 3;
141    const csStr = csTest.slice(csLength, -78);
142    const csTestTwo = parsedPage
143      .querySelector('.gc-calendar-nav__previous a.js-previous-comic')
144      .rawAttrs.split(/ href=/)[1]
145      .replace(/"/g, '');
146    const csStrPrev = csTestTwo.slice(csLength, -95);
147    const nextstring = csStr;
148    const prevstring = csStrPrev;
149    const csTitleandStuff = comic + "-" + datestring;
150    renderTemplate(res, req, 'beta.ejs', {
151      prevstring,
152      nextstring,
153      day,
154      acmonth,
155      datestring,
156      today,
157      comic,
158      csDate,
159      csImage,
160      prev,
161      next,
162      month,
163      year,
164      ejsstring,
165      csParamsStart,
166      csParamsEnd
167    });
168  });
169
170  app.get('/:comic/:year/:month/:day', async (req, res) => {
171    const today = ('0' + (date.getDate())).slice(-2);
172    const acyear = date.getFullYear();
173    const acmonth = ('0' + (date.getMonth() + 1)).slice(-2);
174    const datestring = acyear + '/' + acmonth + '/' + today;
175
176    const comic = req.params.comic;
177    const year = req.params.year;
178    const month = req.params.month;
179    const day = req.params.day
180    const hd = ('0' + (day)).slice(-2)
181    var ejsstring = year + '/' + month + '/' + hd;
182    const parsedPage = parse(
183      await rp(
184        `https://www.gocomics.com/${comic}/${year}/${month}/${day}`
185      ).catch(err => {
186        console.log('Request failed\n', err);
187      })
188    );
189    const csImage = parsedPage
190      .querySelector('.item-comic-image img')
191      .rawAttrs.split(/ src=/)[1]
192      .replace(/"/g, '');
193    const csRawDate = parsedPage
194      .querySelector('.item-comic-image img')
195      .rawAttrs.split(/ alt=/)[1]
196      .replace(/"/g, '');
197    const csDate = csRawDate.slice(0, -70);
198    const prev = day - 1;
199    const next = prev + 2;
200    const csRawParamsStart = parsedPage
201      .querySelector(
202        '.btn.btn-outline-secondary.gc-calendar-nav__datepicker.js-calendar-wrapper'
203      )
204      .rawAttrs.split(/ data-start=/)[1]
205      .replace(/"/g, '');
206    const csParamsStart = csRawParamsStart.slice(0, -63);
207    const csStartFormatted = csParamsStart.slice(0, 4);
208    const csRawParamsEnd = parsedPage
209      .querySelector(
210        '.btn.btn-outline-secondary.gc-calendar-nav__datepicker.js-calendar-wrapper'
211      )
212      .rawAttrs.split(/ data-end=/)[1]
213      .replace(/"/g, '');
214    const csParamsEnd = csRawParamsEnd.slice(0, -32);
215    const csEndFormatted = csParamsEnd.slice(0, 4);
216    const csTest = parsedPage
217      .querySelector('.gc-calendar-nav__next a')
218      .rawAttrs.split(/ href=/)[1]
219      .replace(/"/g, '');
220    const csLength = comic.length + 3;
221    const csStr = csTest.slice(csLength, -78);
222    const csTestTwo = parsedPage
223      .querySelector('.gc-calendar-nav__previous a.js-previous-comic')
224      .rawAttrs.split(/ href=/)[1]
225      .replace(/"/g, '');
226    const csStrPrev = csTestTwo.slice(csLength, -95);
227    const nextstring = csStr;
228    const prevstring = csStrPrev;
229    renderTemplate(res, req, 'comic.ejs', {
230      csStartFormatted,
231      prevstring,
232      nextstring,
233      csEndFormatted,
234      csParamsStart,
235      csParamsEnd,
236      day,
237      acmonth,
238      datestring,
239      today,
240      comic,
241      csDate,
242      csImage,
243      prev,
244      next,
245      month,
246      year,
247      ejsstring
248    });
249  });
250
251  app.get('/k/:comic/:year/:month/:day', async (req, res) => {
252    let options = {
253      json: true,
254      insecure: true,
255      rejectUnauthorized: false
256    };
257    const today = date.getDate();
258    const acyear = date.getFullYear();
259    const acmonth = ('0' + (date.getMonth() + 1)).slice(-2);
260    const datestring = acyear + '/' + acmonth + '/' + today;
261    const comic = req.params.comic;
262    const comicc = comic.charAt(0).toUpperCase() + comic.slice(1);
263    const year = req.params.year;
264    const month = req.params.month;
265    const day = req.params.day;
266    const parsedPage = parse(await rp(`https://v7.comicskingdom.net/comics/${comic}/${year}-${month}-${day}`, options))
267    const csRaw = parsedPage.querySelector("div.col-12.text-center img");
268    const csImagee = htmlToText(csRaw, {
269      wordwrap: 130
270    });
271    const csImage = csImagee.slice(1, -1)
272    renderTemplate(res, req, 'betak.ejs', {
273      day,
274      acmonth,
275      datestring,
276      today,
277      comic,
278      csImage,
279      month,
280      year,
281      comicc
282    });
283  });
284
285  app.get('/k/:comic', async (req, res) => {
286    const comic = req.params.comic;
287    let ts = Date.now();
288    let d = new Date(ts);
289    utc = d.getTime() + d.getTimezoneOffset() * 60000;
290    offset = -6;
291    d = new Date(utc + 3600000 * offset);
292    const mo = ("0" + (date.getMonth() + 1)).slice(-2);
293    const datestring = date.getFullYear() + '/' + mo + '/' + ("0" + date.getDate()).slice(-2);
294    res.redirect('/k/' + comic + '/' + datestring);
295  });
296
297  app.get('/amu/:term', async (req, res) => {
298    const term = req.params.term;
299    const parsedPage = parse(await rp(`https://www.gocomics.com/search/results?utf8=%E2%9C%93&terms=${term}`));
300    const csRawDate = parsedPage
301      .querySelector('.content-section-sm a').rawAttrs
302    const csDate = csRawDate.slice(6, -1);
303    renderTemplate(res, req, 'browse.ejs', {
304      term,
305      csDate
306    });
307  });
308
309  app.get('/search/:term', async (req, res) => {
310    const term = req.params.term;
311    renderTemplate(res, req, 'bknd.ejs', { term })
312  });
313
314  app.get('/:comic/random', async (req, res) => {
315    const comic = req.params.comic;
316    const parsedPage = parse(
317      await rp(`https://www.gocomics.com/random/${comic}`)
318    );
319    const ccsStr = parsedPage
320      .querySelector(
321        '.btn.btn-outline-secondary.gc-calendar-nav__datepicker.js-calendar-wrapper'
322      )
323      .rawAttrs.split(/ data-date=/)[1]
324      .replace(/"/g, '');
325    const csStr = ccsStr.slice(0, 10);
326    res.redirect('/' + comic + '/' + csStr);
327  });
328
329  app.get('/:comic', async (req, res) => {
330    const comic = req.params.comic;
331    let ts = Date.now();
332    let d = new Date(ts);
333    utc = d.getTime() + d.getTimezoneOffset() * 60000;
334    offset = -6;
335    d = new Date(utc + 3600000 * offset);
336    const mo = date.getMonth() + 1;
337    const datestring = date.getFullYear() + '/' + mo + '/' + date.getDate();
338    const parsedPage = parse(
339      await rp(`https://www.gocomics.com/${comic}/${datestring}`)
340    );
341    res.redirect('/' + comic + '/' + datestring);
342  });
343
344  app.get('*', (req, res) => {
345    renderTemplate(res, req, 'error.ejs');
346  });
347
348  app.listen(config.port, null, null, () =>
349    console.log(`ComicServer has started on ${config.domain}:${config.port}.`)
350  );
351};
Full Screen

Accelerate Your Automation Test Cycles With LambdaTest

Leverage LambdaTest’s cloud-based platform to execute your automation tests in parallel and trim down your test execution time significantly. Your first 100 automation testing minutes are on us.

Try LambdaTest

Run JavaScript Tests on LambdaTest Cloud Grid

Execute automation tests with Cypress on a cloud-based Grid of 3000+ real browsers and operating systems for both web and mobile applications.

Test now for Free
LambdaTestX

We use cookies to give you the best experience. Cookies help to provide a more personalized experience and relevant advertising for you, and web analytics for us. Learn More in our Cookies policy, Privacy & Terms of service

Allow Cookie
Sarah

I hope you find the best code examples for your project.

If you want to accelerate automated browser testing, try LambdaTest. Your first 100 automation testing minutes are FREE.

Sarah Elson (Product & Growth Lead)