How to use testLog 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.

form.js

Source: form.js Github

copy
1const Nick = require("../../lib/Nick")
2const nick = new Nick()
3
4const testLog = (text) => {
5	console.log(`>> ${text}`)
6}
7
8const checkUrl = (url, checkbox, color, date, email, number, radio, range, text) => {
9	url = decodeURIComponent(url)
10	if(url.indexOf(`checkbox=${(0 + checkbox)}`) >= 0)
11		testLog(`Checkbox set to ${checkbox}`)
12	else
13		testLog("Checkbox set to false")
14	if(url.indexOf(`color=${color}` >= 0))
15		testLog(`Color set to ${color}`)
16	if(url.indexOf(`date=${date}`) >= 0) {
17		if (date === (new Date()).toISOString().slice(0, 10))
18			testLog("Date set to today's date")
19		else
20			testLog(`Date set to ${date}`)
21	}
22	if(url.indexOf(`email=${email}`) >= 0)
23		testLog(`Email set to ${email}`)
24	if(url.indexOf(`number=${number}`) >= 0)
25		testLog(`Number set to ${number}`)
26	if(url.indexOf(`radio=${(0 + radio)}`) >= 0)
27		testLog(`Radio set to ${radio}`)
28	else
29		testLog("Radio set to false")
30	if(url.indexOf(`range=${range}`) >= 0)
31		testLog(`Range set to ${range}`)
32	if(url.indexOf(`text=${text.replace(/\s/g, "+")}`) >= 0)
33		testLog(`Text set to ${text}`)
34}
35
36const testFill = async (tab, checkbox, color, date, email, number, radio, range, text, submit, file) => {
37	await tab.fill("form", {
38		"checkbox": checkbox,
39		"color": color,
40		"date": date,
41		"email": email,
42		"number": number,
43		"radio": radio,
44		"range": range,
45		"text": text
46	}, {submit: true})
47	testLog("Form filled")
48	await tab.wait(1000)
49	testLog("Wait done")
50	checkUrl(await tab.getUrl(), checkbox, color, date, email, number, radio, range, text)
51}
52
53;(async () => {
54	const tab = await nick.newTab()
55	await tab.open("localhost:8080/form.html")
56	testLog("Page opened")
57	await tab.waitUntilVisible("form")
58	testLog("Page loaded")
59	const content = await tab.getContent()
60	if (content.indexOf("<!-- HTML COMMENTS -->") >= 0)
61		testLog("Get content done")
62	var date = new Date();
63	var old = new Date("2000-01-01");
64	var currentDate = date.toISOString().slice(0,10)
65	var oldDate = old.toISOString().slice(0, 10)
66	await testFill(tab, true, "#35C2DB", currentDate, "[email protected]", 20, true, 1, "NickJS test")
67	await testFill(tab, false, "#35C2CB", oldDate, "[email protected]", 10, false, 10, "NickJS2 test")
68	nick.exit()
69})()
70.catch((err) => {
71	console.log(err)
72	nick.exit(1)
73})
74
Full Screen

scraping-challenge-05.js

Source: scraping-challenge-05.js Github

copy
1const Nick = require("../../lib/Nick")
2const nick = new Nick()
3
4const testLog = (text) => {
5	console.log(`>> ${text}`)
6}
7
8const scrape = (arg, done) => {
9	var data = $("div.person > div.panel-body").map(function () {
10		return({
11			name: $(this).find(".name").text().trim(),
12			birth_year: $(this).find(".birth_year").text().trim()
13		})
14	})
15	done(null, $.makeArray(data))
16}
17
18;(async () => {
19
20	const tab = await nick.newTab()
21
22	await nick.deleteAllCookies()
23	let cookies = await nick.getAllCookies()
24	if (Array.isArray(cookies)) {
25		testLog(`got an array of cookies`)
26		testLog(`we have ${cookies.length} cookies`)
27	}
28
29	await tab.open("http://scraping-challenges.phantombuster.com/cookies")
30
31	await tab.waitUntilVisible("div.container div.jumbotron h1")
32	testLog("'wrong cookies' message is visible")
33
34	await nick.setCookie({
35		name: "phantomCookie",
36		value: "sample_value",
37		domain: "scraping-challenges.phantombuster.com"
38	})
39	cookies = await nick.getAllCookies()
40	if (cookies.length >= 1) {
41		testLog(`we have at least one cookie`)
42	}
43	for (const c of cookies) {
44		if (c.value === "sample_value" && c.name === "phantomCookie") {
45			testLog(`found a cookie with value sample_value and name phantomCookie`)
46		}
47	}
48
49	await tab.open("http://scraping-challenges.phantombuster.com/cookies")
50	await tab.waitUntilVisible(".panel-body")
51	testLog("Page loaded")
52	await tab.inject("tests/assets/jquery-3.2.1.min.js")
53	testLog("Local jQuery injected")
54	const result = await tab.evaluate(scrape)
55	testLog("Evaluate done")
56	testLog(`Tenth result: ${result[9].name}`)
57	testLog(`Result size: ${result.length}`)
58
59	await tab.setCookie({
60		name: "test-cookie-1",
61		value: "test-value-1",
62		domain: "google.fr"
63	})
64	cookies = await tab.getAllCookies()
65	if (cookies.length >= 2) {
66		testLog(`we have at least two cookies`)
67	}
68	for (const c of cookies) {
69		if (c.value === "test-value-1") {
70			testLog(`we found our test-cookie-1`)
71		}
72	}
73
74	await nick.deleteCookie("test-cookie-1", "google.fr")
75	cookies = await tab.getAllCookies()
76	if (cookies.length >= 1) {
77		testLog(`we have at least one cookie`)
78	}
79	let found = false
80	for (const c of cookies) {
81		if (c.name === "test-cookie-1") {
82			found = true
83			console.log("We have found this cookie that should have been deleted...", JSON.stringify(c, undefined, 4))
84		}
85	}
86	if (!found) {
87		testLog(`we havent found the deleted cookie`)
88	}
89
90	await tab.deleteAllCookies()
91	cookies = await tab.getAllCookies()
92	if (cookies.length === 0) {
93		testLog(`all cookies were deleted`)
94	}
95
96	nick.exit(0)
97
98})()
99.catch((err) => {
100	console.log(err)
101	nick.exit(1)
102})
103
Full Screen

elements.js

Source: elements.js Github

copy
1const Nick = require("../../lib/Nick")
2const nick = new Nick()
3
4const testLog = (text) => {
5	console.log(`>> ${text}`)
6}
7
8;(async () => {
9	const tab = await nick.newTab()
10	await tab.open("localhost:8080/elements.html")
11	await tab.waitWhileVisible("#first")
12	testLog("Wait while visible done")
13	await tab.waitUntilVisible("#second")
14	testLog("Wait until visible done")
15	await tab.waitUntilPresent("#third")
16	testLog("Wait until present done")
17	await tab.waitWhilePresent("#fourth")
18	testLog("Wait while present done")
19	try {
20		await tab.waitWhileVisible("#second", 500)
21	} catch (error) {
22		testLog("Wait while visible fail")
23	}
24	try {
25		await tab.waitUntilVisible("#first", 500)
26	} catch (error) {
27		testLog("Wait until visible fail")
28	}
29	try {
30		await tab.waitUntilPresent("#fourth", 500)
31	} catch (error) {
32		testLog("Wait until present fail")
33	}
34	try {
35		await tab.waitWhilePresent("#third", 500)
36	} catch (error) {
37		testLog("Wait while present fail")
38	}
39	if (!await tab.isVisible("#first")) {
40		testLog("Is visisble fail")
41	}
42	if (await tab.isPresent("#third")) {
43		testLog("Is present done")
44	}
45	tab.isVisible("#second", (err, visible) => {
46		if (err) {
47			console.log(err)
48			nick.exit(1)
49		}
50		if (visible) {
51			testLog("Is visible done")
52		}
53		tab.isPresent("#fourth", (err, present) => {
54			if (err) {
55				console.log(err)
56				nick.exit(1)
57			}
58			if (!present) {
59				testLog("Is present fail")
60			}
61			nick.exit(0)
62		})
63	})
64
65})()
66.catch((err) => {
67	console.log(err)
68	nick.exit(1)
69})
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)