UNPKG

dce-selenium

Version:

Selenium library to simplify testing and automatically snapshot the DOM.

111 lines (97 loc) 2.73 kB
const fs = require('fs'); const path = require('path'); const express = require('express'); const https = require('https'); const ejs = require('ejs'); const { By } = require('selenium-webdriver'); /* eslint-disable no-console */ // Post sender port const PORT = 8099; // EJS template for post page const postForm = ejs.compile( fs.readFileSync( path.join(__dirname, 'simRequestViaForm.ejs'), 'utf-8' ) ); // Use self-signed certificates // cy.log('- Reading in SSL certificates'); const sslKey = path.join(__dirname, 'ssl/key.pem'); const sslCertificate = path.join(__dirname, 'ssl/cert.pem'); // Read in files if they're not already read in let key; try { key = fs.readFileSync(sslKey, 'utf-8'); } catch (err) { key = sslKey; } let cert; try { cert = fs.readFileSync(sslCertificate, 'utf-8'); } catch (err) { cert = sslCertificate; } // Create fake app const app = express(); // Start HTTPS server const server = https.createServer({ key, cert, }, app); const waitForReady = new Promise((resolve) => { server.listen(PORT, (err) => { if (err) { console.log('We need a test server to be able to send POST requests.'); process.exit(0); } resolve(); }); }); app.get('/post', (req, res) => { return res.send( postForm({ url: decodeURIComponent(req.query.url), body: JSON.parse(decodeURIComponent(req.query.body)), method: 'POST', }) ); }); app.get('/delete', (req, res) => { return res.send( postForm({ url: decodeURIComponent(req.query.url), body: JSON.parse(decodeURIComponent(req.query.body)), method: 'DELETE', }) ); }); app.get('/put', (req, res) => { return res.send( postForm({ url: decodeURIComponent(req.query.url), body: JSON.parse(decodeURIComponent(req.query.body)), method: 'PUT', }) ); }); app.get('/verifycert', (req, res) => { return res.send('Certificates accepted'); }); /*------------------------------------------------------------------------*/ /* POST Request Sender */ /*------------------------------------------------------------------------*/ module.exports = async (opts) => { const { driver, url, body, method, } = opts; await waitForReady; try { await driver.webdriver.get(`https://localhost:${PORT}/${method.toLowerCase()}?url=${encodeURIComponent(url)}&body=${encodeURIComponent(JSON.stringify(body))}`); } catch (err) { throw new Error(`We had trouble contacting our POST simulator server. Please visit https://localhost:${PORT}/verifycert and accept the self-signed certificate there.`); } await driver.webdriver.findElement(By.css('#submit')).click(); };