From 6e6cd4c01e089781f06ee25d50766e79007ac172 Mon Sep 17 00:00:00 2001 From: Shin'ya Ueoka Date: Wed, 3 Apr 2019 21:44:42 +0900 Subject: [PATCH] Add integration tests for scroll --- e2e-lanthan/scroll.test.js | 150 +++++++++++++++++++++++++++++++ e2e-lanthan/server/MockServer.js | 58 ++++++++++++ e2e-lanthan/server/handlers.js | 17 ++++ 3 files changed, 225 insertions(+) create mode 100644 e2e-lanthan/scroll.test.js create mode 100644 e2e-lanthan/server/MockServer.js create mode 100644 e2e-lanthan/server/handlers.js diff --git a/e2e-lanthan/scroll.test.js b/e2e-lanthan/scroll.test.js new file mode 100644 index 0000000..7b13a72 --- /dev/null +++ b/e2e-lanthan/scroll.test.js @@ -0,0 +1,150 @@ +const express = require('express'); +const lanthan = require('lanthan'); +const path = require('path'); +const assert = require('power-assert'); + +const Key = lanthan.Key; + +const newApp = () => { + let app = express(); + app.get('/', (req, res) => { + res.send(` + + +`); + }); + return app; +}; + +describe("scroll test", () => { + + const port = 12321; + let http; + let firefox; + let session; + let body; + + before(async() => { + http = newApp().listen(port); + + firefox = await lanthan.firefox(); + await firefox.session.installAddon(path.join(__dirname, '..')); + session = firefox.session; + }); + + after(async() => { + if (firefox) { + await firefox.close(); + } + http.close(); + }); + + beforeEach(async() => { + await session.navigateTo(`http://127.0.0.1:${port}`); + body = await session.findElementByCSS('body'); + }); + + + it('scrolls up by k', async () => { + await body.sendKeys('j'); + + let pageYOffset = await session.executeScript(() => window.pageYOffset); + assert.equal(pageYOffset, 64); + }); + + it('scrolls down by j', async () => { + await session.executeScript(() => window.scrollTo(0, 200)); + await body.sendKeys('k'); + + let pageYOffset = await session.executeScript(() => window.pageYOffset); + assert.equal(pageYOffset, 136); + }); + + it('scrolls left by h', async () => { + await session.executeScript(() => window.scrollTo(100, 100)); + await body.sendKeys('h'); + + let pageXOffset = await session.executeScript(() => window.pageXOffset); + assert.equal(pageXOffset, 36); + }); + + it('scrolls left by l', async () => { + await session.executeScript(() => window.scrollTo(100, 100)); + await body.sendKeys('l'); + + let pageXOffset = await session.executeScript(() => window.pageXOffset); + assert.equal(pageXOffset, 164); + }); + + it('scrolls top by gg', async () => { + await session.executeScript(() => window.scrollTo(0, 100)); + await body.sendKeys('g', 'g'); + + let pageYOffset = await session.executeScript(() => window.pageYOffset); + assert.equal(pageYOffset, 0); + }); + + it('scrolls bottom by G', async () => { + await session.executeScript(() => window.scrollTo(0, 100)); + await body.sendKeys(Key.Shift, 'g'); + + let pageYOffset = await session.executeScript(() => window.pageYOffset); + assert(pageYOffset > 5000); + }); + + it('scrolls bottom by 0', async () => { + await session.executeScript(() => window.scrollTo(0, 100)); + await body.sendKeys(Key.Shift, '0'); + + let pageXOffset = await session.executeScript(() => window.pageXOffset); + assert(pageXOffset === 0); + }); + + it('scrolls bottom by $', async () => { + await session.executeScript(() => window.scrollTo(0, 100)); + await body.sendKeys(Key.Shift, '$'); + + let pageXOffset = await session.executeScript(() => window.pageXOffset); + assert(pageXOffset > 5000); + }); + + it('scrolls bottom by ', async () => { + await session.executeScript(() => window.scrollTo(0, 1000)); + await body.sendKeys(Key.Control, 'u'); + + let pageHeight = + await session.executeScript(() => window.document.documentElement.clientHeight); + let pageYOffset = await session.executeScript(() => window.pageYOffset); + assert(Math.abs(pageYOffset - (1000 - Math.floor(pageHeight / 2))) < 5); + }); + + it('scrolls bottom by ', async () => { + await session.executeScript(() => window.scrollTo(0, 1000)); + await body.sendKeys(Key.Control, 'd'); + + let pageHeight = + await session.executeScript(() => window.document.documentElement.clientHeight); + let pageYOffset = await session.executeScript(() => window.pageYOffset); + assert(Math.abs(pageYOffset - (1000 + Math.floor(pageHeight / 2))) < 5); + }); + + it('scrolls bottom by ', async () => { + await session.executeScript(() => window.scrollTo(0, 1000)); + await body.sendKeys(Key.Control, 'b'); + + let pageHeight = + await session.executeScript(() => window.document.documentElement.clientHeight); + let pageYOffset = await session.executeScript(() => window.pageYOffset); + assert(Math.abs(pageYOffset - (1000 - pageHeight)) < 5); + }); + + it('scrolls bottom by ', async () => { + await session.executeScript(() => window.scrollTo(0, 1000)); + await body.sendKeys(Key.Control, 'f'); + + let pageHeight = + await session.executeScript(() => window.document.documentElement.clientHeight); + let pageYOffset = await session.executeScript(() => window.pageYOffset); + assert(Math.abs(pageYOffset - (1000 + pageHeight)) < 5); + }); +}); diff --git a/e2e-lanthan/server/MockServer.js b/e2e-lanthan/server/MockServer.js new file mode 100644 index 0000000..131c177 --- /dev/null +++ b/e2e-lanthan/server/MockServer.js @@ -0,0 +1,58 @@ +var http = require('http'); +var url = require('url'); +var handlers = require('./handlers'); + +class MockServer { + constructor() { + this.handlers = []; + this.server = undefined; + } + + start() { + if (this.server) { + throw new Error('Server is already started'); + } + + let listener = (req, res) => { + if (req.method !== 'GET') { + res.writeHead(404, {'Content-Type': 'text/plain'}); + res.end('not found') + return + } + + let u = url.parse(req.url); + let handler = this.handlers.find(h => u.pathname == h.pathname); + if (!handler) { + res.writeHead(404, {'Content-Type': 'text/plain'}); + res.end('not found') + return + } + + handler.handler(req, res); + } + + this.server = http.createServer(listener); + this.server.listen(); + } + + stop() { + if (!this.server) { + throw new Error('Server is not started'); + } + this.server.close(); + this.server = undefined; + } + + port() { + if (!this.server) { + throw new Error('Server is not started'); + } + return this.server.address().port + } + + on(pathname, handler) { + this.handlers.push({ pathname, handler }); + } +} + +module.exports = MockServer diff --git a/e2e-lanthan/server/handlers.js b/e2e-lanthan/server/handlers.js new file mode 100644 index 0000000..979b4be --- /dev/null +++ b/e2e-lanthan/server/handlers.js @@ -0,0 +1,17 @@ +const handleText = (body) => { + return (req, res) => { + res.writeHead(200, {'Content-Type': 'text/plane'}); + res.end(body); + } +} + +const handleHtml = (body) => { + return (req, res) => { + res.writeHead(200, {'Content-Type': 'text/html'}); + res.end(body); + } +} + +module.exports = { + handleText, handleHtml +}