From 674ca5988e32780f02c3c48ce9d677a29dc470f1 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Sat, 22 Feb 2020 17:25:13 +0900 Subject: Add find test cases --- e2e/find.test.ts | 113 +++++++++++++++++++++++++++++++++++++++++++++++++++++ e2e/lib/Console.ts | 5 +++ e2e/lib/Page.ts | 18 +++++++++ 3 files changed, 136 insertions(+) create mode 100644 e2e/find.test.ts (limited to 'e2e') diff --git a/e2e/find.test.ts b/e2e/find.test.ts new file mode 100644 index 0000000..74097ab --- /dev/null +++ b/e2e/find.test.ts @@ -0,0 +1,113 @@ +import * as path from 'path'; +import * as assert from 'assert'; + +import eventually from './eventually'; +import TestServer from './lib/TestServer'; +import { Builder, Lanthan } from 'lanthan'; +import { Key, WebDriver } from 'selenium-webdriver'; +import Page from './lib/Page'; + +describe("find test", () => { + const server = new TestServer().receiveContent('/', + `--hello--hello--hello--`, + ); + let lanthan: Lanthan; + let webdriver: WebDriver; + let page: Page; + + before(async() => { + 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(); + } + }); + + beforeEach(async() => { + page = await Page.navigateTo(webdriver, server.url()); + }); + + it('starts searching', async() => { + await page.sendKeys('/'); + const console = await page.getConsole(); + await console.execCommand("hello"); + await page.switchToTop(); + + let selection = await page.getSelection(); + assert.deepStrictEqual(selection, { from: 2, to: 7 }); + + // search next keyword + await page.sendKeys("n"); + selection = await page.getSelection(); + assert.deepStrictEqual(selection, { from: 9, to: 14 }); + + // search previous keyword + await page.sendKeys(Key.SHIFT, "N"); + selection = await page.getSelection(); + assert.deepStrictEqual(selection, { from: 2, to: 7 }); + + // search previous keyword by wrap-search + await page.sendKeys(Key.SHIFT, "N"); + selection = await page.getSelection(); + assert.deepStrictEqual(selection, { from: 16, to: 21 }); + }); + + it('shows error if pattern not found', async() => { + await page.sendKeys('/'); + let console = await page.getConsole(); + await console.execCommand('world'); + + await page.switchToTop(); + const selection = await page.getSelection(); + assert.deepStrictEqual(selection, { from: 0, to: 0 }); + + await eventually(async() => { + console = await page.getConsole(); + const message = await console.getErrorMessage(); + assert.strictEqual(message, 'Pattern not found: world'); + }); + }); + + it('search with last keyword if keyword is empty', async() => { + await page.sendKeys('/'); + let console = await page.getConsole(); + await console.execCommand('hello'); + await page.switchToTop(); + + await page.clearSelection(); + let selection = await page.getSelection(); + assert.deepStrictEqual(selection, { from: 0, to: 0 }); + + await page.sendKeys('/'); + console = await page.getConsole(); + await console.execCommand(''); + await page.switchToTop(); + + selection = await page.getSelection(); + assert.deepStrictEqual(selection, { from: 2, to: 7 }); + }); + + it('search with last keyword on new page', async() => { + await page.sendKeys('/'); + const console = await page.getConsole(); + await console.execCommand('hello'); + + await page.switchToTop(); + await page.sendKeys('n'); + let selection = await page.getSelection(); + assert.deepStrictEqual(selection, { from: 9, to: 14 }); + + page = await Page.navigateTo(webdriver, server.url()); + await page.sendKeys('n'); + selection = await page.getSelection(); + assert.deepStrictEqual(selection, { from: 2, to: 7 }); + }); +}); diff --git a/e2e/lib/Console.ts b/e2e/lib/Console.ts index 6a82387..e3bd2d6 100644 --- a/e2e/lib/Console.ts +++ b/e2e/lib/Console.ts @@ -35,6 +35,11 @@ export class Console { return p.getText(); } + async getInformationMessage(): Promise { + const p = await this.webdriver.findElement(By.css('.vimvixen-console-info')); + return p.getText(); + } + async inputKeys(...keys: string[]) { const input = await this.webdriver.findElement(By.css('input')); await input.sendKeys(...keys); diff --git a/e2e/lib/Page.ts b/e2e/lib/Page.ts index ad3f454..31605cb 100644 --- a/e2e/lib/Page.ts +++ b/e2e/lib/Page.ts @@ -6,6 +6,11 @@ type Hint = { text: string, }; +type Selection = { + from: number, + to: number, +}; + export default class Page { private constructor(private webdriver: WebDriver) { } @@ -66,6 +71,19 @@ export default class Page { return this.webdriver.executeScript(() => window.document.documentElement.clientHeight); } + async getSelection(): Promise { + const obj = await this.webdriver.executeScript(`return window.getSelection();`) as any; + return { from: obj.anchorOffset, to: obj.focusOffset }; + } + + async clearSelection(): Promise { + await this.webdriver.executeScript(`window.getSelection().removeAllRanges()`); + } + + async switchToTop(): Promise { + await this.webdriver.switchTo().defaultContent(); + } + async waitAndGetHints(): Promise { await this.webdriver.wait(until.elementsLocated(By.css('.vimvixen-hint'))); -- cgit v1.2.3