Skip to content

[LAB4] 313553034 #313

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

Open
wants to merge 8 commits into
base: 313553034
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions lab3/main_test.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,59 @@
<<<<<<< HEAD
// const {describe, it} = require('node:test');
const assert = require('assert');
const { Calculator } = require('./main');
const test = require('node:test');
// TODO: write your tests here

const calc = new Calculator();
test('exp', () => {
const cases = [
{ input: 1, expected: Math.exp(1) },
{ input: 0, expected: 1 },
{ input: -2, expected: Math.exp(-2) }
];

for (const tc of cases) {
assert.ok(Math.abs(calc.exp(tc.input) - tc.expected) < 1e-10);
}
});


test('log', () => {
const cases = [
{ input: 1, expected: 0 },
{ input: Math.E, expected: 1 },
{ input: 10, expected: Math.log(10) }
];

for (const tc of cases) {
assert.ok(Math.abs(calc.log(tc.input) - tc.expected) < 1e-10);
}
});

// error
test('exp type error or overflow', () => {
const badInputs = [Infinity, -Infinity, NaN];
for (const val of badInputs) {
assert.throws(() => calc.exp(val), /unsupported operand type/);
}

assert.throws(() => calc.exp(10000), /overflow/);
});

test('log errors', () => {
const badInputs = [Infinity, -Infinity, NaN];
for (const val of badInputs) {
assert.throws(() => calc.log(val), /unsupported operand type/);
}

assert.throws(() => calc.log(0), /math domain error \(1\)/);
assert.throws(() => calc.log(-1), /math domain error \(2\)/);
});
=======
const {describe, it} = require('node:test');
const assert = require('assert');
const { Calculator } = require('./main');

// TODO: write your tests here
>>>>>>> 0fddd2447c7e3144c1f3e22b6e6b7b6c5e1d49fb
29 changes: 29 additions & 0 deletions lab4/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Lab4

## Introduction

In this lab, you will write tests in `main_test.js`. You can learn how to use [Puppeteer](https://pptr.dev/) to tests a web UI.

## Preparation (Important!!!)

1. Sync fork your branch (e.g., `SQLab:312XXXXXX`)
2. `git checkout -b lab4` (**NOT** your student ID !!!)

## Requirement

1. (100%) Goto https://pptr.dev/, type `chipi chipi chapa chapa` into the search box, click on **1st** result in the **Docs** section, and print the title.

For the detailed steps and hints, please check the slide of this lab.

You can run `validate.sh` in your local to test if you satisfy the requirements.

Please note that you must not alter files other than `main_test.js`. You will get 0 points if

1. you modify other files to achieve requirements.
2. you can't pass all CI on your PR.

## Submission

You need to open a pull request to your branch (e.g. 312XXXXXX, your student number) and contain the code that satisfies the abovementioned requirements.

Moreover, please submit the URL of your PR to E3. Your submission will only be accepted when you present at both places.
37 changes: 37 additions & 0 deletions lab4/main_test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const puppeteer = require('puppeteer');

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

(async () => {
// Launch the browser and open a new blank page
const browser = await puppeteer.launch();
const page = await browser.newPage();

// Navigate the page to a URL
await page.goto('https://pptr.dev/');

// Hints:
// Click search button
await page.waitForSelector('button.DocSearch-Button', { visible: true });
await page.click('button.DocSearch-Button');

// Type into search box
await page.waitForSelector('#docsearch-input');
await page.type('#docsearch-input', 'andy popoo');
await sleep(300);
// Wait for search results
await page.waitForSelector('#docsearch-hits1-item-4 > a > div', { visible: true });

await page.click('#docsearch-hits1-item-4 > a > div');
// Find the first result in the Docs section and click it
await page.waitForSelector('h1');
const title = await page.$eval('h1', el => el.textContent.trim());
// Wait for navigation to finish and the title to appear


console.log(title)
// Close the browser
await browser.close();
})();
Loading
Loading