Skip to content

Commit 4b5e61a

Browse files
committed
chore: implement testing of JavaScript exercises
1 parent 47e41a6 commit 4b5e61a

File tree

7 files changed

+44
-33
lines changed

7 files changed

+44
-33
lines changed

sources/academy/webscraping/scraping_basics_javascript/04_downloading_html.md

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ description: Lesson about building a Node.js application for watching prices. Us
55
slug: /scraping-basics-javascript/downloading-html
66
---
77

8+
import CodeBlock from '@theme/CodeBlock';
89
import LegacyJsCourseAdmonition from '@site/src/components/LegacyJsCourseAdmonition';
910
import Exercises from '../scraping_basics/_exercises.mdx';
11+
import LegoExercise from '!!raw-loader!roa-loader!./exercises/scrape_lego.mjs';
1012

1113
<LegacyJsCourseAdmonition />
1214

@@ -184,28 +186,17 @@ Letting our program visibly crash on error is enough for our purposes. Now, let'
184186

185187
<Exercises />
186188

187-
### Scrape AliExpress
189+
### Scrape LEGO
188190

189-
Download HTML of a product listing page, but this time from a real world e-commerce website. For example this page with AliExpress search results:
191+
Download HTML of a product listing page, but this time from a real world e-commerce website. For example this page with LEGO search results:
190192

191193
```text
192-
https://www.aliexpress.com/w/wholesale-darth-vader.html
194+
https://www.lego.com/en-us/themes/star-wars
193195
```
194196

195197
<details>
196198
<summary>Solution</summary>
197-
198-
```js
199-
const url = "https://www.aliexpress.com/w/wholesale-darth-vader.html";
200-
const response = await fetch(url);
201-
202-
if (response.ok) {
203-
console.log(await response.text());
204-
} else {
205-
throw new Error(`HTTP ${response.status}`);
206-
}
207-
```
208-
199+
<CodeBlock language="js">{LegoExercise.code}</CodeBlock>
209200
</details>
210201

211202
### Save downloaded HTML as a file

sources/academy/webscraping/scraping_basics_javascript/05_parsing_html.md

Lines changed: 3 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ description: Lesson about building a Node.js application for watching prices. Us
55
slug: /scraping-basics-javascript/parsing-html
66
---
77

8+
import CodeBlock from '@theme/CodeBlock';
89
import LegacyJsCourseAdmonition from '@site/src/components/LegacyJsCourseAdmonition';
910
import Exercises from '../scraping_basics/_exercises.mdx';
11+
import F1AcademyTeamsExercise from '!!raw-loader!roa-loader!./exercises/scrape_f1academy_teams.mjs';
1012

1113
<LegacyJsCourseAdmonition />
1214

@@ -183,22 +185,7 @@ https://www.f1academy.com/Racing-Series/Teams
183185

184186
<details>
185187
<summary>Solution</summary>
186-
187-
```js
188-
import * as cheerio from 'cheerio';
189-
190-
const url = "https://www.f1academy.com/Racing-Series/Teams";
191-
const response = await fetch(url);
192-
193-
if (response.ok) {
194-
const html = await response.text();
195-
const $ = cheerio.load(html);
196-
console.log($(".teams-driver-item").length);
197-
} else {
198-
throw new Error(`HTTP ${response.status}`);
199-
}
200-
```
201-
188+
<CodeBlock language="js">{F1AcademyTeamsExercise.code}</CodeBlock>
202189
</details>
203190

204191
### Scrape F1 Academy drivers
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import * as cheerio from 'cheerio';
2+
3+
const url = "https://www.f1academy.com/Racing-Series/Teams";
4+
const response = await fetch(url);
5+
6+
if (response.ok) {
7+
const html = await response.text();
8+
const $ = cheerio.load(html);
9+
console.log($(".teams-driver-item").length);
10+
} else {
11+
throw new Error(`HTTP ${response.status}`);
12+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
const url = "https://www.lego.com/en-us/themes/star-wars";
2+
const response = await fetch(url);
3+
4+
if (response.ok) {
5+
console.log(await response.text());
6+
} else {
7+
throw new Error(`HTTP ${response.status}`);
8+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
setup() {
2+
DIR=sources/academy/webscraping/scraping_basics_javascript/exercises
3+
}
4+
5+
@test "outputs the HTML with Star Wars products" {
6+
run npx node "$DIR/scrape_lego.mjs"
7+
[[ "$output" == *"Millennium Falcon"* ]]
8+
}
9+
10+
@test "outputs the number of F1 Academy teams" {
11+
run npx --package=cheerio node "$DIR/scrape_f1academy_teams.mjs"
12+
[[ "$output" == "6" ]]
13+
}

sources/academy/webscraping/scraping_basics_python/04_downloading_html.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ Letting our program visibly crash on error is enough for our purposes. Now, let'
146146
Download HTML of a product listing page, but this time from a real world e-commerce website. For example this page with LEGO search results:
147147

148148
```text
149-
https://www.lego.com/themes/star-wars
149+
https://www.lego.com/en-us/themes/star-wars
150150
```
151151

152152
<details>
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import httpx
22

3-
url = "https://www.lego.com/themes/star-wars"
3+
url = "https://www.lego.com/en-us/themes/star-wars"
44
response = httpx.get(url)
55
response.raise_for_status()
66
print(response.text)

0 commit comments

Comments
 (0)