Skip to content

Commit

Permalink
PR Feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
aalasolutions committed Jun 22, 2021
1 parent 0bd1681 commit 49e4e90
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 51 deletions.
85 changes: 85 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ A set of utility APIs for use while running [Testable](https://testable.io) scen
* [Iterate CSV](#iterate-csv)
* [Async code](#async-code)
* [Manual live event](#manual-live-event)
* [Suite and Tests](#suite-and-tests)
* [Wait for finish](#wait-for-finish)
* [Webdriver.io custom commands](#webdriverio-commands)
* [Screenshots](#screenshots)
Expand Down Expand Up @@ -342,6 +343,7 @@ const execute = testableUtils.execute;
execute(function(finished) {
events.on('my-event', function(symbol) {
request.get('http://sample.testable.io/stocks/' + symbol);
events.finish()
finished();
});
});
Expand Down Expand Up @@ -370,6 +372,89 @@ describe('Load Url Requested in Event', function() {
});
```

### Suite and Tests
You can run your tests in with same Webdriver.io API and can have multiple test suites.

Example (Selenium Webdriver):

```javascript
const webdriver = require('selenium-webdriver');
const util = require('util');
const fs = require('fs');
const writeFile = util.promisify(fs.writeFile);
const path = require('path');

const testableUtils = require('testable-utils');
const describe = testableUtils.describe;
const it = testableUtils.it;

async function takeScreenshot(driver, file){
let image = await driver.takeScreenshot();
await writeFile(file, image, 'base64');
}

(async function example() {
global.driver = await new webdriver.Builder()
.withCapabilities(webdriver.Capabilities.chrome())
.build();

try {
await describe('Google Related', async function() {
await it('Open google home page', async function() {
await driver.get('http://www.google.com');
await takeScreenshot(driver, path.join(process.env.OUTPUT_DIR || '.', 'HomePage.png'));
await driver.wait(webdriver.until.titleIs('Google'), 10000);
});

await it('Open google home page', async function() {
await driver.get("https://news.google.com");
await takeScreenshot(driver, path.join(process.env.OUTPUT_DIR || '.', 'News.png'));
await driver.wait(webdriver.until.titleIs('Google News'), 10000);
});
});
} finally {
await driver.quit();
}
})();
```

Example (Playwright):

```javascript
const { chromium } = require('playwright');
const testableUtils = require('testable-utils');
const describe = testableUtils.describe;
const it = testableUtils.it;

(async () => {
const browser = await chromium.launch({ headless: false });
const page = await browser.newPage();

await describe('Google Related', async function() {
await it('Open google home page', async function() {
// Instructs the blank page to navigate a URL
await page.goto('https://www.google.com');
await page.screenshot({path: 'homepage.png'});

// Fetches page's title
const title = await page.title();
console.info(`The title is: ${title}`);
});

await it('Open google news page', async function() {
await page.goto('https://news.google.com');
await page.screenshot({path: 'newspage.png'});

// Fetches page's title
const title = await page.title();
console.info(`The News title is: ${title}`);
});
});

await browser.close();
})();
```

### Wait For Finish

Use this function to wait for the remainder of the test duration before the script finishes. Returns a Promise so that you can run some cleanup code before the script exits.
Expand Down
38 changes: 19 additions & 19 deletions lib/events-watcher.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,26 +4,26 @@ const TailIntervalMs = 100;
const TailBlockSize = 10485760;

function watchForEvents(fileName, events, log) {
const tail = new Tail(fileName, '\n', { start: 0, interval: TailIntervalMs, blockSize: TailBlockSize });
tail.on('line', function(data) {
if (!data)
return;
try {
const event = JSON.parse(data);
if (event.name) {
log.debug(`Received event ${event.name} with contents ${event.contents || ''}`);
events.emit(event.name, event.contents);
}
} catch(err) {
log.error(`Error processing live event ${data}`, helper.toErrorMessage(null, err));
}
});
tail.on('error', function(data) {
log.error('Error tailing live event', data);
});
tail.watch();
const tail = new Tail(fileName, '\n', { start: 0, interval: TailIntervalMs, blockSize: TailBlockSize });
tail.on('line', function (data) {
if ( !data )
return;
try {
const event = JSON.parse(data);
if ( event.name ) {
log.debug(`Received event ${event.name} with contents ${event.contents || ''}`);
events.emit(event.name, event.contents);
}
} catch (err) {
log.error(`Error processing live event ${data}`, helper.toErrorMessage(null, err));
}
});
tail.on('error', function (data) {
log.error('Error tailing live event', data);
});
tail.watch();
events.finish = () => { tail.unwatch(); }
return tail;
return tail;
}

module.exports = watchForEvents;
29 changes: 7 additions & 22 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -135,21 +135,7 @@ if (registerCommands) {
});
}

let steps;
function testSteps(){
const StepsClass = new TestSteps();

const stepsObj = {};
stepsObj.describe = StepsClass.describe.bind(StepsClass);
stepsObj.it = StepsClass.it.bind(StepsClass);
stepsObj.before = StepsClass.before.bind(StepsClass);
stepsObj.after = StepsClass.after.bind(StepsClass);
stepsObj.beforeEach = StepsClass.beforeEach.bind(StepsClass);
stepsObj.afterEach = StepsClass.afterEach.bind(StepsClass);

steps = stepsObj;
}
testSteps();
const testSteps = new TestSteps();

module.exports.isLocal = isLocal;
module.exports.isSmokeTest = isSmokeTest;
Expand All @@ -162,10 +148,9 @@ module.exports.events = events;
module.exports.dataTable = csv;
module.exports.waitForFinish = waitForFinish;


module.exports.describe = steps.describe;
module.exports.it = steps.it;
module.exports.before = steps.before;
module.exports.after = steps.after;
module.exports.beforeEach = steps.beforeEach;
module.exports.afterEach = steps.afterEach;
module.exports.describe = testSteps.describe.bind(testSteps);
module.exports.it = testSteps.it.bind(testSteps);
module.exports.before = testSteps.before.bind(testSteps);
module.exports.after = testSteps.after.bind(testSteps);
module.exports.beforeEach = testSteps.beforeEach.bind(testSteps);
module.exports.afterEach = testSteps.afterEach.bind(testSteps);
10 changes: 0 additions & 10 deletions lib/testable-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,16 +231,6 @@ class TestSteps {
}

toErrorContent(error) {
if (_.isUndefined(error) || error === null)
return "";
if (_.isString(error))
return error;
if (!_.isError(error) && _.isBuffer(error))
return error.toString();
if (!_.isError(error) && _.isObject(error))
return util.inspect(error);
if (!_.isError(error))
return '' + error;
if (_.isUndefined(error.message))
return "Unknown error";
switch (error.message) {
Expand Down

0 comments on commit 49e4e90

Please sign in to comment.