Skip to content

Commit 17aea0b

Browse files
edewitvorburger
andauthored
Use playwright to do browser testing (#429)
With some fixes, incl. installing Playwright dependencies into build container instead of at each test execution (by @vorburger, rest by @edewit) Co-authored-by: Michael Vorburger <mike@vorburger.ch>
1 parent 50a69c3 commit 17aea0b

7 files changed

Lines changed: 219 additions & 7 deletions

File tree

Dockerfile-build

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,14 @@
1-
FROM docker.io/library/openjdk:11-jdk as build
1+
FROM docker.io/library/ubuntu:22.04 as build
2+
3+
RUN apt-get update -y
4+
5+
# https://github.com/OASIS-learn-study/minecraft-storeys-maker/pull/429
6+
RUN apt-get install -y openjdk-11-jdk curl \
7+
libgtk-3-0 libasound2 libx11-6 libxcomposite1 libxdamage1 libxext6 libxfixes3 \
8+
libxrandr2 libxrender1 libxtst6 libpangocairo-1.0-0 libpango-1.0-0 \
9+
libatk1.0-0 libcairo-gobject2 libcairo2 libgdk-pixbuf-2.0-0 \
10+
libglib2.0-0 libdbus-glib-1-2 libdbus-1-3 libxcb-shm0 \
11+
libx11-xcb1 libxcb1 libxcursor1 libxi6
212

313
# https://github.com/nodesource/distributions/blob/master/README.md#installation-instructions
414
RUN curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && apt install -y nodejs && node --version

test-mineflayer/.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
node_modules/
2+
/test-results/
3+
/playwright-report/
4+
/playwright/.cache/

test-mineflayer/package-lock.json

Lines changed: 57 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

test-mineflayer/package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,16 @@
44
"description": "",
55
"main": "index.js",
66
"scripts": {
7-
"test": "mocha storeys.test.js --timeout 50000 --browser chrome"
7+
"test": "mocha storeys.test.js --timeout 50000 --browser chrome",
8+
"playwright": "npx playwright install && npx playwright test"
89
},
910
"author": "",
1011
"license": "ISC",
1112
"dependencies": {
1213
"mineflayer": "^4.2.0"
1314
},
1415
"devDependencies": {
16+
"@playwright/test": "^1.27.1",
1517
"chai": "^4.3.6",
1618
"mocha": "^10.0.0"
1719
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
// @ts-check
2+
const { devices } = require('@playwright/test');
3+
4+
/**
5+
* Read environment variables from file.
6+
* https://github.com/motdotla/dotenv
7+
*/
8+
// require('dotenv').config();
9+
10+
11+
/**
12+
* @see https://playwright.dev/docs/test-configuration
13+
* @type {import('@playwright/test').PlaywrightTestConfig}
14+
*/
15+
const config = {
16+
testDir: './tests',
17+
/* Maximum time one test can run for. */
18+
timeout: 30 * 1000,
19+
expect: {
20+
/**
21+
* Maximum time expect() should wait for the condition to be met.
22+
* For example in `await expect(locator).toHaveText();`
23+
*/
24+
timeout: 5000
25+
},
26+
/* Run tests in files in parallel */
27+
fullyParallel: true,
28+
/* Fail the build on CI if you accidentally left test.only in the source code. */
29+
forbidOnly: !!process.env.CI,
30+
/* Retry on CI only */
31+
retries: process.env.CI ? 2 : 0,
32+
/* Opt out of parallel tests on CI. */
33+
workers: process.env.CI ? 1 : undefined,
34+
/* Reporter to use. See https://playwright.dev/docs/test-reporters */
35+
reporter: 'html',
36+
/* Shared settings for all the projects below. See https://playwright.dev/docs/api/class-testoptions. */
37+
use: {
38+
/* Maximum time each action such as `click()` can take. Defaults to 0 (no limit). */
39+
actionTimeout: 0,
40+
/* Base URL to use in actions like `await page.goto('/')`. */
41+
// baseURL: 'http://localhost:3000',
42+
43+
/* Collect trace when retrying the failed test. See https://playwright.dev/docs/trace-viewer */
44+
trace: 'on-first-retry',
45+
},
46+
47+
/* Configure projects for major browsers */
48+
projects: [
49+
{
50+
name: 'firefox',
51+
use: {
52+
...devices['Desktop Firefox'],
53+
},
54+
},
55+
/* Test against mobile viewports. */
56+
// {
57+
// name: 'Mobile Chrome',
58+
// use: {
59+
// ...devices['Pixel 5'],
60+
// },
61+
// },
62+
// {
63+
// name: 'Mobile Safari',
64+
// use: {
65+
// ...devices['iPhone 12'],
66+
// },
67+
// },
68+
69+
/* Test against branded browsers. */
70+
// {
71+
// name: 'Microsoft Edge',
72+
// use: {
73+
// channel: 'msedge',
74+
// },
75+
// },
76+
// {
77+
// name: 'Google Chrome',
78+
// use: {
79+
// channel: 'chrome',
80+
// },
81+
// },
82+
],
83+
84+
/* Folder for test artifacts such as screenshots, videos, traces, etc. */
85+
// outputDir: 'test-results/',
86+
87+
/* Run your local dev server before starting the tests */
88+
// webServer: {
89+
// command: 'npm run start',
90+
// port: 3000,
91+
// },
92+
};
93+
94+
module.exports = config;

test-mineflayer/storeys.test.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
const mineflayer = require('mineflayer');
2+
const { spawn } = require('node:child_process');
23

34
const expect = require('chai').expect
45

56
describe("Storeys plugin test", () => {
67
let bot;
8+
let loginURL;
79

810
before((done) => {
911
bot = mineflayer.createBot({
@@ -15,27 +17,35 @@ describe("Storeys plugin test", () => {
1517

1618
after(() => bot.end());
1719

18-
it("should connect to minecraft server and execute /make", (done) => {
20+
it("1 should connect to minecraft server and execute /make", (done) => {
1921
// given
2022
bot.chat("/make");
2123

2224
// then
2325
bot.on('messagestr', (msg, _, json) => {
2426
if (msg !== "Player joined the game") {
27+
loginURL = json.extra[0].extra[0].extra[0].extra[0].clickEvent.value;
2528
expect(msg).to.equal("Click here to open a browser and start MAKE actions");
2629
done();
2730
}
2831
});
2932
});
3033

31-
it("should execute /new", (done) => {
34+
it("2 should create new command /demo", (done) => {
35+
const child = spawn('npm', ['run', 'playwright'], { env: { ...process.env, URL: loginURL } });
36+
child.stdout.pipe(process.stdout);
37+
child.stderr.pipe(process.stderr);
38+
child.on('exit', done);
39+
});
40+
41+
it("3 should execute /demo", (done) => {
3242
// given
33-
bot.chat("/new");
43+
bot.chat("/demo");
3444

3545
// then
3646
bot.on('title', (msg) => {
37-
expect(msg).to.equal("{\"text\":\"Hello\"}");
47+
expect(msg).to.equal("{\"text\":\"automated test!\"}");
3848
done();
3949
});
4050
});
41-
});
51+
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
const { test, expect } = require('@playwright/test');
2+
3+
test("test blockly by creating command", async ({ page }) => {
4+
const { URL } = process.env;
5+
6+
await page.goto(URL);
7+
8+
await page.getByText("Events").click({ force: true });
9+
const whenEvent = page.getByText("When /");
10+
await whenEvent.click();
11+
12+
await page.getByText("Actions").click({ force: true });
13+
const title = page.getByText("title").first();
14+
await title.click();
15+
16+
await page.locator("text:has-text('abc') >> nth=0").click({ force: true });
17+
await page.keyboard.type("automated test!");
18+
19+
await title.dragTo(whenEvent, {
20+
force: true,
21+
targetPosition: {
22+
x: 20,
23+
y: 20,
24+
},
25+
});
26+
27+
const code = page.locator("textarea");
28+
await expect(code).toHaveText(
29+
`e.whenCommand("demo", function(m) {
30+
m.title('automated test!');
31+
32+
});`,
33+
{ timeout: 1000 }
34+
);
35+
});

0 commit comments

Comments
 (0)