Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tests: Update flaky test to download a python file. #326

Merged
merged 1 commit into from Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 3 additions & 1 deletion .gitignore
Expand Up @@ -10,11 +10,13 @@ Thumbs.db
Desktop.ini
$RECYCLE.BIN/
*.swp

# Temp directory used for unit testing
tests/spec/test-files/temp-test-files/

# Node
node_modules/

# vscode
.history
.vscode/settings.json
.vscode/settings.json
38 changes: 26 additions & 12 deletions tests/spec/puppeteer-spec.js
@@ -1,4 +1,5 @@
const fs = require("fs");
const path = require("path");
const puppeteer = require("puppeteer");

jest.setTimeout(20000);
Expand All @@ -11,7 +12,10 @@ describe("Puppeteer basic tests for the Python Editor.", function() {
beforeAll(async() => {
// Setup a headless Chromium browser.
// Flags allow Puppeteer to run within a container.
browser = await puppeteer.launch({headless: true, args: ["--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage"]});
browser = await puppeteer.launch({
headless: true,
args: ["--no-sandbox", "--disable-setuid-sandbox", "--disable-dev-shm-usage"]
});
});

afterAll(async() => {
Expand Down Expand Up @@ -151,50 +155,60 @@ describe("Puppeteer basic tests for the Python Editor.", function() {
expect(codeName).toEqual("too-large");
});

it("Saves a python file with the correct filename", async function(){
if (fs.existsSync("./spec/test-files/temp-test-files/program_test.py")) {
fs.unlinkSync("./spec/test-files/temp-test-files/program_test.py");
}
it("Saves a python file with the correct filename", async function() {
const downloadFolder = path.join(process.cwd(), "spec", "test-files", "temp-test-files");
const filePath = path.join(downloadFolder, "program_test.py");
if (!fs.existsSync(downloadFolder)) fs.mkdirSync(downloadFolder);
if (fs.existsSync(filePath)) fs.unlinkSync(filePath);
const page = await browser.newPage();
await page._client.send('Page.setDownloadBehavior', {behavior: 'allow', downloadPath: './spec/test-files/temp-test-files'});
await page._client.send('Page.setDownloadBehavior', {
behavior: 'allow',
downloadPath: downloadFolder
});
await page.goto("http://localhost:5000/editor.html");

await page.evaluate( () => document.getElementById("script-name").value = "program test")
const scriptName = await page.evaluate("document.getElementById('script-name').value");
for (let ms=0; ms<100; ms++) {
await page.evaluate(() => document.getElementById("script-name").value = "program test")
for (let ms = 0; ms < 100; ms++) {
let scriptName = await page.evaluate("document.getElementById('script-name').value");
if (scriptName === "program test") break;
await page.waitFor(10);
}
await page.click("#command-files");
await page.click("#show-files");
await page.waitFor(100);
await page.click(".save-button.save");
await page.waitFor(1000); //waiting to ensure file is saved
const fileExists = fs.existsSync("./spec/test-files/temp-test-files/program_test.py");
fs.unlinkSync("./spec/test-files/temp-test-files/program_test.py");
await page.waitFor(500); //waiting to ensure file is saved
const fileExists = fs.existsSync(filePath);
fs.unlinkSync(filePath);
fs.rmdirSync(downloadFolder);

expect(fileExists).toBeTruthy();
});

it("Correctly handles an mpy file", async function(){
const page = await browser.newPage();
await page.goto("http://localhost:5000/editor.html");

await page.click("#command-files");
let fileInput = await page.$("#file-upload-input");
await fileInput.uploadFile("./spec/test-files/samplempyfile.mpy");
const modalContent = await page.evaluate("$('#modal-msg-content').text()");
const modalDisplay = await page.evaluate("$('#modal-msg-overlay-container').css('display')");

expect(modalContent).toContain("This version of the Python Editor doesn\'t currently support adding .mpy files.");
expect(modalDisplay).toContain("block");
});

it("Correctly handles a file with an invalid extension", async function(){
const page = await browser.newPage();
await page.goto("http://localhost:5000/editor.html");

await page.click("#command-files");
let fileInput = await page.$("#file-upload-input");
await fileInput.uploadFile("./spec/test-files/sampletxtfile.txt");
const modalContent = await page.evaluate("$('#modal-msg-content').text()");
const modalDisplay = await page.evaluate("$('#modal-msg-overlay-container').css('display')");

expect(modalContent).toContain("The Python Editor can only load files with the .hex or .py extensions.");
expect(modalDisplay).toContain("block");
});
Expand Down