diff --git a/e2e/blacklist.test.ts b/e2e/blacklist.test.ts index b3b5f0f..03f2f90 100644 --- a/e2e/blacklist.test.ts +++ b/e2e/blacklist.test.ts @@ -1,49 +1,29 @@ -import express from 'express'; import * as path from 'path'; import * as assert from 'assert'; +import TestServer from './lib/TestServer'; import { Builder, Lanthan } from 'lanthan'; import { WebDriver } from 'selenium-webdriver'; -import * as http from 'http'; import Page from './lib/Page'; -const newApp = () => { - let app = express(); - app.get('/*', (_req, res) => { - res.status(200).send(` - - -`); - }); - return app; -}; - -describe("navigate test", () => { - const port = 12321; - let http: http.Server; +describe("blacklist test", () => { + let server = new TestServer().receiveContent('/*', + ``, + ); let lanthan: Lanthan; let webdriver: WebDriver; let browser: any; before(async() => { - http = newApp().listen(port); - lanthan = await Builder .forBrowser('firefox') .spyAddon(path.join(__dirname, '..')) .build(); webdriver = lanthan.getWebDriver(); browser = lanthan.getWebExtBrowser(); - }); + await server.start(); - after(async() => { - if (lanthan) { - await lanthan.quit(); - } - http.close(); - }); - - it('should disable add-on if the URL is in the blacklist', async () => { + let url = server.url('/a').replace('http://', ''); await browser.storage.local.set({ settings: { source: 'json', @@ -51,24 +31,32 @@ describe("navigate test", () => { "keymaps": { "j": { "type": "scroll.vertically", "count": 1 } }, - "blacklist": [ "127.0.0.1:${port}/a" ] + "blacklist": [ "${url}" ] }`, }, }); + }); + + after(async() => { + await server.stop(); + if (lanthan) { + await lanthan.quit(); + } + }); - let page = await Page.navigateTo(webdriver, `http://127.0.0.1:${port}/a`); + it('should disable add-on if the URL is in the blacklist', async () => { + let page = await Page.navigateTo(webdriver, server.url('/a')); await page.sendKeys('j') - // not works - let pageYOffset = await webdriver.executeScript(() => window.pageYOffset); let scrollY = await page.getScrollY(); - assert.equal(pageYOffset, 0); + assert.equal(scrollY, 0); + }); - page = await Page.navigateTo(webdriver, `http://127.0.0.1:${port}/ab`); + it('should enabled add-on if the URL is not in the blacklist', async () => { + let page = await Page.navigateTo(webdriver, server.url('/ab')); await page.sendKeys('j'); - // works - scrollY = await page.getScrollY(); + let scrollY = await page.getScrollY(); assert.equal(scrollY, 64); }); }); diff --git a/e2e/clipboard.test.ts b/e2e/clipboard.test.ts index 5491e89..0fd45f7 100644 --- a/e2e/clipboard.test.ts +++ b/e2e/clipboard.test.ts @@ -1,8 +1,7 @@ -import express from 'express'; -import * as path from 'path'; import * as assert from 'assert'; -import * as http from 'http'; +import * as path from 'path'; +import TestServer from './lib/TestServer'; import eventually from './eventually'; import * as clipboard from './lib/clipboard'; import settings from './settings'; @@ -10,24 +9,13 @@ import { Builder, Lanthan } from 'lanthan'; import { WebDriver, Key } from 'selenium-webdriver'; import Page from './lib/Page'; -const newApp = () => { - let app = express(); - app.get('/', (_req, res) => { - res.status(200).send(``); - }); - return app; -}; - describe("clipboard test", () => { - const port = 12321; - let http: http.Server; + let server = new TestServer(12321).receiveContent('/happy', 'ok'); let lanthan: Lanthan; let webdriver: WebDriver; let browser: any; before(async() => { - http = newApp().listen(port); - lanthan = await Builder .forBrowser('firefox') .spyAddon(path.join(__dirname, '..')) @@ -38,13 +26,15 @@ describe("clipboard test", () => { await browser.storage.local.set({ settings, }); + + await server.start(); }); after(async() => { + await server.stop(); if (lanthan) { await lanthan.quit(); } - http.close(); }); beforeEach(async() => { @@ -55,38 +45,38 @@ describe("clipboard test", () => { }) it('should copy current URL by y', async () => { - let page = await Page.navigateTo(webdriver, `http://127.0.0.1:${port}/#should_copy_url`); + let page = await Page.navigateTo(webdriver, server.url('/#should_copy_url')); await page.sendKeys('y'); await eventually(async() => { let data = await clipboard.read(); - assert.equal(data, `http://127.0.0.1:${port}/#should_copy_url`); + assert.equal(data, server.url('/#should_copy_url')); }); }); it('should open an URL from clipboard by p', async () => { - await clipboard.write(`http://127.0.0.1:${port}/#open_from_clipboard`); + await clipboard.write(server.url('/#open_from_clipboard')); - let page = await Page.navigateTo(webdriver, `http://127.0.0.1:${port}/`); + let page = await Page.navigateTo(webdriver, server.url()); await page.sendKeys('p'); await eventually(async() => { let tabs = await browser.tabs.query({ active: true }); - assert.equal(tabs[0].url, `http://127.0.0.1:${port}/#open_from_clipboard`); + assert.equal(tabs[0].url, server.url('/#open_from_clipboard')); }); }); it('should open an URL from clipboard to new tab by P', async () => { - await clipboard.write(`http://127.0.0.1:${port}/#open_to_new_tab`); + await clipboard.write(server.url('/#open_to_new_tab')); - let page = await Page.navigateTo(webdriver, `http://127.0.0.1:${port}/`); + let page = await Page.navigateTo(webdriver, server.url()); await page.sendKeys(Key.SHIFT, 'p'); await eventually(async() => { let tabs = await browser.tabs.query({}); assert.deepEqual(tabs.map((t: any) => t.url), [ - `http://127.0.0.1:${port}/`, - `http://127.0.0.1:${port}/#open_to_new_tab`, + server.url(), + server.url('/#open_to_new_tab'), ]); }); }); @@ -94,26 +84,26 @@ describe("clipboard test", () => { it('should open search result with keywords in clipboard by p', async () => { await clipboard.write(`an apple`); - let page = await Page.navigateTo(webdriver, `http://127.0.0.1:${port}/`); + let page = await Page.navigateTo(webdriver, server.url()); await page.sendKeys(Key.SHIFT, 'p'); await eventually(async() => { let tabs = await browser.tabs.query({ active: true }); - assert.equal(tabs[0].url, `http://127.0.0.1:${port}/google?q=an%20apple`); + assert.equal(tabs[0].url, server.url('/google?q=an%20apple')); }); }); it('should open search result with keywords in clipboard to new tabby P', async () => { await clipboard.write(`an apple`); - let page = await Page.navigateTo(webdriver, `http://127.0.0.1:${port}/`); + let page = await Page.navigateTo(webdriver, server.url()); await page.sendKeys(Key.SHIFT, 'p'); await eventually(async() => { let tabs = await browser.tabs.query({}); assert.deepEqual(tabs.map((t: any) => t.url), [ - `http://127.0.0.1:${port}/`, - `http://127.0.0.1:${port}/google?q=an%20apple`, + server.url(), + server.url('/google?q=an%20apple'), ]); }); }); diff --git a/e2e/command_addbookmark.test.ts b/e2e/command_addbookmark.test.ts index bd7ae1a..4fa30d8 100644 --- a/e2e/command_addbookmark.test.ts +++ b/e2e/command_addbookmark.test.ts @@ -1,53 +1,40 @@ -import express from 'express'; import * as path from 'path'; import * as assert from 'assert'; -import * as http from 'http'; +import TestServer from './lib/TestServer'; import eventually from './eventually'; import { Builder, Lanthan } from 'lanthan'; import { WebDriver } from 'selenium-webdriver'; import Page from './lib/Page'; -const newApp = () => { - let app = express(); - app.get('/happy', (_req, res) => { - res.send(` - - - how to be happy - -`); - }); - return app; -}; - describe('addbookmark command test', () => { - const port = 12321; - let http: http.Server; + let server = new TestServer().receiveContent('/happy', ` + + how to be happy`, + ); let lanthan: Lanthan; let webdriver: WebDriver; let browser: any; before(async() => { - http = newApp().listen(port); - lanthan = await Builder .forBrowser('firefox') .spyAddon(path.join(__dirname, '..')) .build(); webdriver = lanthan.getWebDriver(); browser = lanthan.getWebExtBrowser(); + await server.start(); }); after(async() => { - http.close(); + await server.stop(); if (lanthan) { await lanthan.quit(); } }); beforeEach(async() => { - await webdriver.navigate().to(`http://127.0.0.1:${port}/happy`); + await webdriver.navigate().to(server.url('/happy')); }); it('should add a bookmark from the current page', async() => { @@ -58,7 +45,7 @@ describe('addbookmark command test', () => { await eventually(async() => { var bookmarks = await browser.bookmarks.search({ title: 'how to be happy' }); assert.equal(bookmarks.length, 1); - assert.equal(bookmarks[0].url, `http://127.0.0.1:${port}/happy`); + assert.equal(bookmarks[0].url, server.url('/happy')); }); }); }); diff --git a/e2e/command_bdelete.test.ts b/e2e/command_bdelete.test.ts index 0a9c757..4c76c49 100644 --- a/e2e/command_bdelete.test.ts +++ b/e2e/command_bdelete.test.ts @@ -1,46 +1,30 @@ -import express from 'express'; import * as path from 'path'; import * as assert from 'assert'; -import * as http from 'http'; +import TestServer from './lib/TestServer'; import eventually from './eventually'; import { Builder, Lanthan } from 'lanthan'; import { WebDriver } from 'selenium-webdriver'; import Page from './lib/Page'; -const newApp = () => { - let app = express(); - app.get('/*', (req, res) => { - res.send(` - - - my_${req.path.slice(1)} - -

${req.path}

-`); - }); - return app; -}; - describe('bdelete/bdeletes command test', () => { - const port = 12321; - let http: http.Server; + let server = new TestServer().receiveContent('/*', 'ok'); let lanthan: Lanthan; let webdriver: WebDriver; let browser: any; before(async() => { - http = newApp().listen(port); lanthan = await Builder .forBrowser('firefox') .spyAddon(path.join(__dirname, '..')) .build(); webdriver = lanthan.getWebDriver(); browser = lanthan.getWebExtBrowser(); + await server.start(); }); after(async() => { - http.close(); + await server.stop(); if (lanthan) { await lanthan.quit(); } @@ -51,11 +35,11 @@ describe('bdelete/bdeletes command test', () => { for (let tab of tabs.slice(1)) { await browser.tabs.remove(tab.id); } - await browser.tabs.update(tabs[0].id, { url: `http://127.0.0.1:${port}/site1`, pinned: true }); - await browser.tabs.create({ url: `http://127.0.0.1:${port}/site2`, pinned: true }) - await browser.tabs.create({ url: `http://127.0.0.1:${port}/site3`, pinned: true }) - await browser.tabs.create({ url: `http://127.0.0.1:${port}/site4` }) - await browser.tabs.create({ url: `http://127.0.0.1:${port}/site5` }) + await browser.tabs.update(tabs[0].id, { url: server.url('/site1'), pinned: true }); + await browser.tabs.create({ url: server.url('/site2'), pinned: true }) + await browser.tabs.create({ url: server.url('/site3'), pinned: true }) + await browser.tabs.create({ url: server.url('/site4'), }) + await browser.tabs.create({ url: server.url('/site5'), }) await eventually(async() => { let handles = await webdriver.getAllWindowHandles(); @@ -72,10 +56,10 @@ describe('bdelete/bdeletes command test', () => { await eventually(async() => { let tabs = await browser.tabs.query({}); assert.deepEqual(tabs.map((t: any) => t.url), [ - `http://127.0.0.1:${port}/site1`, - `http://127.0.0.1:${port}/site2`, - `http://127.0.0.1:${port}/site3`, - `http://127.0.0.1:${port}/site4`, + server.url('/site1'), + server.url('/site2'), + server.url('/site3'), + server.url('/site4'), ]) }); }); @@ -121,10 +105,10 @@ describe('bdelete/bdeletes command test', () => { await eventually(async() => { let tabs = await browser.tabs.query({}); assert.deepEqual(tabs.map((t: any) => t.url), [ - `http://127.0.0.1:${port}/site1`, - `http://127.0.0.1:${port}/site2`, - `http://127.0.0.1:${port}/site3`, - `http://127.0.0.1:${port}/site4`, + server.url('/site1'), + server.url('/site2'), + server.url('/site3'), + server.url('/site4'), ]) }); }); @@ -137,10 +121,10 @@ describe('bdelete/bdeletes command test', () => { await eventually(async() => { let tabs = await browser.tabs.query({}); assert.deepEqual(tabs.map((t: any) => t.url), [ - `http://127.0.0.1:${port}/site2`, - `http://127.0.0.1:${port}/site3`, - `http://127.0.0.1:${port}/site4`, - `http://127.0.0.1:${port}/site5`, + server.url('/site2'), + server.url('/site3'), + server.url('/site4'), + server.url('/site5'), ]) }); }); @@ -153,9 +137,9 @@ describe('bdelete/bdeletes command test', () => { await eventually(async() => { let tabs = await browser.tabs.query({}); assert.deepEqual(tabs.map((t: any) => t.url), [ - `http://127.0.0.1:${port}/site1`, - `http://127.0.0.1:${port}/site2`, - `http://127.0.0.1:${port}/site3`, + server.url('/site1'), + server.url('/site2'), + server.url('/site3'), ]) }); }); diff --git a/e2e/command_buffer.test.ts b/e2e/command_buffer.test.ts index 97c7bec..d1e9ccb 100644 --- a/e2e/command_buffer.test.ts +++ b/e2e/command_buffer.test.ts @@ -1,46 +1,39 @@ -import express from 'express'; import * as path from 'path'; import * as assert from 'assert'; -import * as http from 'http'; +import { Request, Response } from 'express'; +import TestServer from './lib/TestServer'; import eventually from './eventually'; import { Builder, Lanthan } from 'lanthan'; import { WebDriver } from 'selenium-webdriver'; import Page from './lib/Page'; -const newApp = () => { - let app = express(); - app.get('/*', (req, res) => { - res.send(` - - - my_${req.path.slice(1)} - -

${req.path}

-`); - }); - return app; -}; - describe('buffer command test', () => { - const port = 12321; - let http: http.Server; + let server = new TestServer().handle('/*', (req: Request, res: Response) => { + res.send(` + + + + my_${req.path.slice(1)} + + `); + }); let lanthan: Lanthan; let webdriver: WebDriver; let browser: any; before(async() => { - http = newApp().listen(port); lanthan = await Builder .forBrowser('firefox') .spyAddon(path.join(__dirname, '..')) .build(); webdriver = lanthan.getWebDriver(); browser = lanthan.getWebExtBrowser(); + await server.start(); }); after(async() => { - http.close(); + await server.stop(); if (lanthan) { await lanthan.quit(); } @@ -51,9 +44,9 @@ describe('buffer command test', () => { for (let tab of tabs.slice(1)) { await browser.tabs.remove(tab.id); } - await browser.tabs.update(tabs[0].id, { url: `http://127.0.0.1:${port}/site1` }); + await browser.tabs.update(tabs[0].id, { url: server.url('/site1') }); for (let i = 2; i <= 5; ++i) { - await browser.tabs.create({ url: `http://127.0.0.1:${port}/site${i}`}) + await browser.tabs.create({ url: server.url('/site' + i) }); } await eventually(async() => { diff --git a/e2e/command_open.test.ts b/e2e/command_open.test.ts index 0a713af..d45fa65 100644 --- a/e2e/command_open.test.ts +++ b/e2e/command_open.test.ts @@ -1,37 +1,17 @@ -import express from 'express'; import * as path from 'path'; import * as assert from 'assert'; -import * as http from 'http'; +import TestServer from './lib/TestServer'; import settings from './settings'; import eventually from './eventually'; import { Builder, Lanthan } from 'lanthan'; import { WebDriver } from 'selenium-webdriver'; import Page from './lib/Page'; -const newApp = () => { - - let app = express(); - for (let name of ['google', 'yahoo', 'bing', 'duckduckgo', 'twitter', 'wikipedia']) { - app.get('/' + name, (_req, res) => { - res.send(` - -

${name.charAt(0).toUpperCase() + name.slice(1)}

-`); - }); - } - app.get('/', (_req, res) => { - res.send(` - -

home

-`); - }); - return app; -}; - describe("open command test", () => { - const port = 12321; - let http: http.Server; + let server = new TestServer(12321) + .receiveContent('/google', 'google') + .receiveContent('/yahoo', 'yahoo'); let lanthan: Lanthan; let webdriver: WebDriver; let browser: any; @@ -44,15 +24,16 @@ describe("open command test", () => { .build(); webdriver = lanthan.getWebDriver(); browser = lanthan.getWebExtBrowser(); - http = newApp().listen(port); await browser.storage.local.set({ settings, }); + + await server.start(); }); after(async() => { - http.close(); + await server.stop(); if (lanthan) { await lanthan.quit(); } @@ -60,7 +41,7 @@ describe("open command test", () => { beforeEach(async() => { await webdriver.switchTo().defaultContent(); - page = await Page.navigateTo(webdriver, `http://127.0.0.1:${port}`); + page = await Page.navigateTo(webdriver, server.url()); }) it('should open default search for keywords by open command ', async() => { @@ -70,7 +51,7 @@ describe("open command test", () => { await eventually(async() => { let tabs = await browser.tabs.query({ active: true }); let url = new URL(tabs[0].url); - assert.equal(url.href, `http://127.0.0.1:${port}/google?q=an%20apple`) + assert.equal(url.href, server.url('/google?q=an%20apple')) }); }); @@ -81,7 +62,7 @@ describe("open command test", () => { await eventually(async() => { let tabs = await browser.tabs.query({ active: true }) let url = new URL(tabs[0].url); - assert.equal(url.href, `http://127.0.0.1:${port}/yahoo?q=an%20apple`) + assert.equal(url.href, server.url('/yahoo?q=an%20apple')) }); }); @@ -92,7 +73,7 @@ describe("open command test", () => { await eventually(async() => { let tabs = await browser.tabs.query({ active: true }) let url = new URL(tabs[0].url); - assert.equal(url.href, `http://127.0.0.1:${port}/google?q=`) + assert.equal(url.href, server.url('/google?q=')) }); }); @@ -103,7 +84,7 @@ describe("open command test", () => { await eventually(async() => { let tabs = await browser.tabs.query({ active: true }) let url = new URL(tabs[0].url); - assert.equal(url.href, `http://127.0.0.1:${port}/yahoo?q=`) + assert.equal(url.href, server.url('/yahoo?q=')) }); }); diff --git a/e2e/command_quit.test.ts b/e2e/command_quit.test.ts index 29de86a..d187ccb 100644 --- a/e2e/command_quit.test.ts +++ b/e2e/command_quit.test.ts @@ -1,46 +1,30 @@ -import express from 'express'; import * as path from 'path'; import * as assert from 'assert'; -import * as http from 'http'; +import TestServer from './lib/TestServer'; import eventually from './eventually'; import { Builder, Lanthan } from 'lanthan'; import { WebDriver } from 'selenium-webdriver'; import Page from './lib/Page'; -const newApp = () => { - let app = express(); - app.get('/*', (req, res) => { - res.send(` - - - my_${req.path.slice(1)} - -

${req.path}

-`); - }); - return app; -}; - describe('quit/quitall command test', () => { - const port = 12321; - let http: http.Server; + let server = new TestServer().receiveContent('/*', 'ok'); let lanthan: Lanthan; let webdriver: WebDriver; let browser: any; before(async() => { - http = newApp().listen(port); lanthan = await Builder .forBrowser('firefox') .spyAddon(path.join(__dirname, '..')) .build(); webdriver = lanthan.getWebDriver(); browser = lanthan.getWebExtBrowser(); + await server.start(); }); after(async() => { - http.close(); + await server.stop(); if (lanthan) { await lanthan.quit(); } @@ -51,9 +35,9 @@ describe('quit/quitall command test', () => { for (let tab of tabs.slice(1)) { await browser.tabs.remove(tab.id); } - await browser.tabs.update(tabs[0].id, { url: `http://127.0.0.1:${port}/site1` }); + await browser.tabs.update(tabs[0].id, { url: server.url('/site1') }); for (let i = 2; i <= 5; ++i) { - await browser.tabs.create({ url: `http://127.0.0.1:${port}/site${i}`}) + await browser.tabs.create({ url: server.url('/site' + i) }) } await eventually(async() => { diff --git a/e2e/command_tabopen.test.ts b/e2e/command_tabopen.test.ts index 9cf49f6..bc5d13e 100644 --- a/e2e/command_tabopen.test.ts +++ b/e2e/command_tabopen.test.ts @@ -1,44 +1,23 @@ -import express from 'express'; import * as path from 'path'; import * as assert from 'assert'; -import * as http from 'http'; +import TestServer from './lib/TestServer'; import settings from './settings'; import eventually from './eventually'; import { Builder, Lanthan } from 'lanthan'; import { WebDriver } from 'selenium-webdriver'; import Page from './lib/Page'; -const newApp = () => { - - let app = express(); - for (let name of ['google', 'yahoo', 'bing', 'duckduckgo', 'twitter', 'wikipedia']) { - app.get('/' + name, (_req, res) => { - res.send(` - -

${name.charAt(0).toUpperCase() + name.slice(1)}

-`); - }); - } - app.get('/', (_req, res) => { - res.send(` - -

home

-`); - }); - return app; -}; - describe("tabopen command test", () => { - const port = 12321; - let http: http.Server; + let server = new TestServer(12321) + .receiveContent('/google', 'google') + .receiveContent('/yahoo', 'yahoo'); let lanthan: Lanthan; let webdriver: WebDriver; let browser: any; let page: Page; before(async() => { - http = newApp().listen(port); lanthan = await Builder .forBrowser('firefox') .spyAddon(path.join(__dirname, '..')) @@ -49,10 +28,12 @@ describe("tabopen command test", () => { await browser.storage.local.set({ settings, }); + + await server.start(); }); after(async() => { - http.close(); + await server.stop(); if (lanthan) { await lanthan.quit(); } @@ -64,7 +45,7 @@ describe("tabopen command test", () => { await browser.tabs.remove(tab.id); } - page = await Page.navigateTo(webdriver, `http://127.0.0.1:${port}`); + page = await Page.navigateTo(webdriver, server.url()); }) it('should open default search for keywords by tabopen command ', async() => { @@ -75,7 +56,7 @@ describe("tabopen command test", () => { let tabs = await browser.tabs.query({}); assert.equal(tabs.length, 2); let url = new URL(tabs[1].url); - assert.equal(url.href, `http://127.0.0.1:${port}/google?q=an%20apple`) + assert.equal(url.href, server.url('/google?q=an%20apple') ) }); }); @@ -87,7 +68,7 @@ describe("tabopen command test", () => { let tabs = await browser.tabs.query({}); assert.equal(tabs.length, 2); let url = new URL(tabs[1].url); - assert.equal(url.href, `http://127.0.0.1:${port}/yahoo?q=an%20apple`) + assert.equal(url.href, server.url('/yahoo?q=an%20apple')) }); }); @@ -99,7 +80,7 @@ describe("tabopen command test", () => { let tabs = await browser.tabs.query({}); assert.equal(tabs.length, 2); let url = new URL(tabs[1].url); - assert.equal(url.href, `http://127.0.0.1:${port}/google?q=`) + assert.equal(url.href, server.url('/google?q=')) }); }); @@ -111,7 +92,7 @@ describe("tabopen command test", () => { let tabs = await browser.tabs.query({}); assert.equal(tabs.length, 2); let url = new URL(tabs[1].url); - assert.equal(url.href, `http://127.0.0.1:${port}/yahoo?q=`) + assert.equal(url.href, server.url('/yahoo?q=')) }); }); diff --git a/e2e/command_winopen.test.ts b/e2e/command_winopen.test.ts index 6ebeca2..fa52672 100644 --- a/e2e/command_winopen.test.ts +++ b/e2e/command_winopen.test.ts @@ -1,44 +1,23 @@ -import express from 'express'; import * as path from 'path'; import * as assert from 'assert'; -import * as http from 'http'; +import TestServer from './lib/TestServer'; import settings from './settings'; import eventually from './eventually'; import { Builder, Lanthan } from 'lanthan'; import { WebDriver } from 'selenium-webdriver'; import Page from './lib/Page'; -const newApp = () => { - - let app = express(); - for (let name of ['google', 'yahoo', 'bing', 'duckduckgo', 'twitter', 'wikipedia']) { - app.get('/' + name, (_req, res) => { - res.send(` - -

${name.charAt(0).toUpperCase() + name.slice(1)}

-`); - }); - } - app.get('/', (_req, res) => { - res.send(` - -

home

-`); - }); - return app; -}; - describe("winopen command test", () => { - const port = 12321; - let http: http.Server; + let server = new TestServer(12321) + .receiveContent('/google', 'google') + .receiveContent('/yahoo', 'yahoo'); let lanthan: Lanthan; let webdriver: WebDriver; let browser: any; let page: Page; before(async() => { - http = newApp().listen(port); lanthan = await Builder .forBrowser('firefox') .spyAddon(path.join(__dirname, '..')) @@ -48,10 +27,12 @@ describe("winopen command test", () => { await browser.storage.local.set({ settings, }); + + await server.start(); }); after(async() => { - http.close(); + await server.stop(); if (lanthan) { await lanthan.quit(); } @@ -63,7 +44,7 @@ describe("winopen command test", () => { await browser.windows.remove(win.id); } - page = await Page.navigateTo(webdriver, `http://127.0.0.1:${port}/area`); + page = await Page.navigateTo(webdriver, server.url()); }) it('should open default search for keywords by winopen command ', async() => { @@ -76,7 +57,7 @@ describe("winopen command test", () => { let tabs = await browser.tabs.query({ windowId: wins[1].id }); let url = new URL(tabs[0].url); - assert.equal(url.href, `http://127.0.0.1:${port}/google?q=an%20apple`) + assert.equal(url.href, server.url('/google?q=an%20apple')) }); }); @@ -90,7 +71,7 @@ describe("winopen command test", () => { let tabs = await browser.tabs.query({ windowId: wins[1].id }); let url = new URL(tabs[0].url); - assert.equal(url.href, `http://127.0.0.1:${port}/yahoo?q=an%20apple`) + assert.equal(url.href, server.url('/yahoo?q=an%20apple')) }); }); @@ -104,7 +85,7 @@ describe("winopen command test", () => { let tabs = await browser.tabs.query({ windowId: wins[1].id }); let url = new URL(tabs[0].url); - assert.equal(url.href, `http://127.0.0.1:${port}/google?q=`) + assert.equal(url.href, server.url('/google?q=')) }); }); @@ -118,7 +99,7 @@ describe("winopen command test", () => { let tabs = await browser.tabs.query({ windowId: wins[1].id }); let url = new URL(tabs[0].url); - assert.equal(url.href, `http://127.0.0.1:${port}/yahoo?q=`) + assert.equal(url.href, server.url('/yahoo?q=')) }); }); diff --git a/e2e/completion.test.ts b/e2e/completion.test.ts index 4884d21..67577bf 100644 --- a/e2e/completion.test.ts +++ b/e2e/completion.test.ts @@ -1,7 +1,5 @@ -import express from 'express'; import * as path from 'path'; import * as assert from 'assert'; -import * as http from 'http'; import eventually from './eventually'; import settings from './settings'; @@ -9,20 +7,7 @@ import { Builder, Lanthan } from 'lanthan'; import { WebDriver, Key } from 'selenium-webdriver'; import Page from './lib/Page'; -const newApp = () => { - let app = express(); - app.get('/', (_req, res) => { - res.send(` - - ok -`); - }); - return app; -}; - describe("general completion test", () => { - const port = 12321; - let http: http.Server; let lanthan: Lanthan; let webdriver: WebDriver; let browser: any; @@ -35,7 +20,6 @@ describe("general completion test", () => { .build(); webdriver = lanthan.getWebDriver(); browser = lanthan.getWebExtBrowser(); - http = newApp().listen(port); await browser.storage.local.set({ settings, @@ -43,14 +27,13 @@ describe("general completion test", () => { }); after(async() => { - http.close(); if (lanthan) { await lanthan.quit(); } }); beforeEach(async() => { - page = await Page.navigateTo(webdriver, `http://127.0.0.1:${port}`); + page = await Page.navigateTo(webdriver, 'about:blank'); }); it('should all commands on empty line', async() => { diff --git a/e2e/completion_buffers.test.ts b/e2e/completion_buffers.test.ts index e927f54..086da5d 100644 --- a/e2e/completion_buffers.test.ts +++ b/e2e/completion_buffers.test.ts @@ -1,32 +1,24 @@ -import express from 'express'; -import * as path from 'path'; import * as assert from 'assert'; -import * as http from 'http'; +import * as path from 'path'; +import { Request, Response } from 'express' +import TestServer from './lib/TestServer'; import settings from './settings'; import eventually from './eventually'; import { Builder, Lanthan } from 'lanthan'; import { WebDriver } from 'selenium-webdriver'; import Page from './lib/Page'; -const newApp = () => { - - let app = express(); - app.get('/*', (req, res) => { - res.send(` - - - title_${req.path.slice(1)} - -

home

-`); - }); - return app; -}; - describe("completion on buffer/bdelete/bdeletes", () => { - const port = 12321; - let http: http.Server; + let server = new TestServer().handle('/*', (req: Request, res: Response) => { + res.send(` + + + + title_${req.path.slice(1)} + + `); + }); let lanthan: Lanthan; let webdriver: WebDriver; let browser: any; @@ -39,15 +31,16 @@ describe("completion on buffer/bdelete/bdeletes", () => { .build(); webdriver = lanthan.getWebDriver(); browser = lanthan.getWebExtBrowser(); - http = newApp().listen(port); await browser.storage.local.set({ settings, }); + + await server.start(); }); after(async() => { - http.close(); + await server.stop(); if (lanthan) { await lanthan.quit(); } @@ -59,10 +52,10 @@ describe("completion on buffer/bdelete/bdeletes", () => { await browser.tabs.remove(tab.id); } - await browser.tabs.update(tabs[0].id, { url: `http://127.0.0.1:${port}/site1`, pinned: true }); - await browser.tabs.create({ url: `http://127.0.0.1:${port}/site2`, pinned: true }) + await browser.tabs.update(tabs[0].id, { url: server.url('/site1'), pinned: true }); + await browser.tabs.create({ url:server.url('/site2'), pinned: true }) for (let i = 3; i <= 5; ++i) { - await browser.tabs.create({ url: `http://127.0.0.1:${port}/site${i}` }) + await browser.tabs.create({ url: server.url('/site' + i) }); } await eventually(async() => { @@ -102,7 +95,7 @@ describe("completion on buffer/bdelete/bdeletes", () => { assert.deepEqual(items[0], { type: 'title', text: 'Buffers' }); assert.ok(items[1].text.startsWith('2:')); assert.ok(items[1].text.includes('title_site2')); - assert.ok(items[1].text.includes(`http://127.0.0.1:${port}/site2`)); + assert.ok(items[1].text.includes(server.url('/site2'))); }); }) diff --git a/e2e/completion_open.test.ts b/e2e/completion_open.test.ts index 9c6cf95..c475733 100644 --- a/e2e/completion_open.test.ts +++ b/e2e/completion_open.test.ts @@ -1,60 +1,47 @@ -import express from 'express'; import * as path from 'path'; import * as assert from 'assert'; -import * as http from 'http'; +import TestServer from './lib/TestServer'; import settings from './settings'; import eventually from './eventually'; import { Builder, Lanthan } from 'lanthan'; import { WebDriver } from 'selenium-webdriver'; import Page from './lib/Page'; -const newApp = () => { - - let app = express(); - app.get('/', (_req, res) => { - res.send(` - - ok -`); - }); - return app; -}; - describe("completion on open/tabopen/winopen commands", () => { - const port = 12321; - let http: http.Server; + let server = new TestServer().receiveContent('/*', 'ok'); let lanthan: Lanthan; let webdriver: WebDriver; let browser: any; let page: Page; before(async() => { + await server.start(); + lanthan = await Builder .forBrowser('firefox') .spyAddon(path.join(__dirname, '..')) .build(); webdriver = lanthan.getWebDriver(); browser = lanthan.getWebExtBrowser(); - http = newApp().listen(port); await browser.storage.local.set({ settings, }); // Add item into hitories - await webdriver.navigate().to(`https://i-beam.org/404`); + await webdriver.navigate().to(('https://i-beam.org/404')); }); after(async() => { - http.close(); + await server.stop(); if (lanthan) { await lanthan.quit(); } }); beforeEach(async() => { - page = await Page.navigateTo(webdriver, `http://127.0.0.1:${port}`); + page = await Page.navigateTo(webdriver, server.url()); }); it('should show completions from search engines, bookmarks, and histories by "open" command', async() => { diff --git a/e2e/console.test.ts b/e2e/console.test.ts index ede2c3c..e11df10 100644 --- a/e2e/console.test.ts +++ b/e2e/console.test.ts @@ -1,53 +1,37 @@ -import express from 'express'; import * as path from 'path'; import * as assert from 'assert'; -import * as http from 'http'; +import TestServer from './lib/TestServer'; import { Builder, Lanthan } from 'lanthan'; import { WebDriver, Key } from 'selenium-webdriver'; import Page from './lib/Page'; -const newApp = () => { - let app = express(); - app.get('/', (_req, res) => { - res.send(` - - - Hello, world! - -`); - }); - return app; -}; - - describe("console test", () => { - const port = 12321; - let http: http.Server; + let server = new TestServer().receiveContent('/', + `Hello, world!`, + ); let lanthan: Lanthan; let webdriver: WebDriver; let page: Page; before(async() => { - http = newApp().listen(port); lanthan = await Builder .forBrowser('firefox') .spyAddon(path.join(__dirname, '..')) .build(); webdriver = lanthan.getWebDriver(); + await server.start(); }); after(async() => { + await server.stop(); if (lanthan) { await lanthan.quit(); } - if (http) { - http.close(); - } }); beforeEach(async() => { - page = await Page.navigateTo(webdriver, `http://127.0.0.1:${port}`); + page = await Page.navigateTo(webdriver, server.url()); }); it('open console with :', async() => { @@ -65,7 +49,7 @@ describe("console test", () => { it('open console with open command and current URL by O', async() => { await page.sendKeys(Key.SHIFT, 'o'); let console = await page.getConsole(); - assert.equal(await console.currentValue(), `open http://127.0.0.1:${port}/`); + assert.equal(await console.currentValue(), `open ${server.url()}`); }); it('open console with tabopen command by t', async() => { @@ -77,7 +61,7 @@ describe("console test", () => { it('open console with tabopen command and current URL by T', async() => { await page.sendKeys(Key.SHIFT, 't'); let console = await page.getConsole(); - assert.equal(await console.currentValue(), `tabopen http://127.0.0.1:${port}/`); + assert.equal(await console.currentValue(), `tabopen ${server.url()}`); }); it('open console with winopen command by w', async() => { @@ -89,7 +73,7 @@ describe("console test", () => { it('open console with winopen command and current URL by W', async() => { await page.sendKeys(Key.SHIFT, 'W'); let console = await page.getConsole(); - assert.equal(await console.currentValue(), `winopen http://127.0.0.1:${port}/`); + assert.equal(await console.currentValue(), `winopen ${server.url()}`); }); it('open console with buffer command by b', async() => { diff --git a/e2e/follow.test.ts b/e2e/follow.test.ts index fdaedfb..a7ce0ed 100644 --- a/e2e/follow.test.ts +++ b/e2e/follow.test.ts @@ -1,46 +1,40 @@ -import express from 'express'; import * as path from 'path'; import * as assert from 'assert'; -import * as http from 'http'; +import TestServer from './lib/TestServer'; import eventually from './eventually'; import { Builder, Lanthan } from 'lanthan'; import { WebDriver, Key } from 'selenium-webdriver'; import Page from './lib/Page'; const newApp = () => { - let app = express(); - - app.get('/', (_req, res) => { - res.send(` - - hello -`); - }); - - app.get('/follow-input', (_req, res) => { - res.send(` - - -`); - }); - - app.get('/area', (_req, res) => { - res.send(` - - - - - - - - - -`); - }); + let server = new TestServer(); + + server.receiveContent('/', ` + + + hello + `); + + server.receiveContent('/follow-input', ` + + + + `); + + server.receiveContent('/area', ` + + + + + + + + + `); /* * test case: link2 is out of the viewport @@ -52,16 +46,13 @@ const newApp = () => { * | | * +-----------------+ */ - app.get('/test1', (_req, res) => { - res.send(` - - -
link1
-
-
link2
- -`); - }); + server.receiveContent('/test1', ` + + +
link1
+
+
link2
+ `); /* * test case 2: link2 and link3 are out of window of the frame @@ -74,22 +65,18 @@ const newApp = () => { * | | * +-----------------+ */ - app.get('/test2', (_req, res) => { - res.send(` - -