Skip to content

Instantly share code, notes, and snippets.

@mfairchild365
Created January 21, 2019 21:45
Show Gist options
  • Save mfairchild365/7cf5425b1950182e25198605fe7c9f00 to your computer and use it in GitHub Desktop.
Save mfairchild365/7cf5425b1950182e25198605fe7c9f00 to your computer and use it in GitHub Desktop.
script to import figure tests
let fs = require('fs');
let moment = require('moment');
let cheerio = require('cheerio');
let currentDateString = moment().format('YYYY-MM-DD');
let dataDir = __dirname+'/../data';
const TEST_SHELL = {
"type": "assertion",
"title": "todo",
"description": "This test was originally imported from the article titled '[how do you figure?'](https://www.scottohara.me/blog/2019/01/21/how-do-you-figure.html)' by Scott O'Hara.",
"features": ["html/figure_element", "html/figcaption_element"],
"supports_sr": true,
"supports_vc": false,
"css_target": "#target",
"html_file": "html_figure_tests.html",
"assertion": {
"aspect": "role",
'value': "figure"
},
"history": [
{
"date": currentDateString,
"message": "Test created. Thank you @scottaohara."
}
],
"at": {
"jaws": {
"browsers": {
"ie": {
"at_version": "2019",
"browser_version": "44",
"os_version": "1809",
"date": "2019-01-14",
"output": [
{
"command": "next_item",
"output": "(unknown)",
"result": "pass"
}
]
},
"firefox": {
"at_version": "2019",
"browser_version": "64.0.2",
"os_version": "1809",
"date": "2019-01-14",
"output": [
{
"command": "next_item",
"output": "(unknown)",
"result": "pass"
}
]
}
}
},
"narrator": {
"browsers": {
"edge": {
"at_version": "1804",
"browser_version": "42",
"os_version": "1804",
"date": "2019-01-14",
"output": [
{
"command": "next_item",
"output": "(unknown)",
"result": "pass"
}
],
}
}
},
"nvda": {
"browsers": {
"firefox": {
"at_version": "2018.4.1",
"browser_version": "64.0.2",
"os_version": "1809",
"date": "2019-01-14",
"output": [
{
"command": "next_item",
"output": "(unknown)",
"result": "pass"
}
]
}
}
},
"vo_macos": {
"browsers": {
"safari": {
"at_version": "10.14",
"browser_version": "12.0.2",
"os_version": "10.14",
"date": "2019-01-14",
"output": [
{
"command": "next_item",
"output": "(unknown)",
"result": "pass"
}
]
}
}
},
"vo_ios": {
"browsers": {
"ios_saf": {
"at_version": "12.1",
"browser_version": "12.1.2",
"os_version": "12.1",
"date": "2019-01-14",
"output": [
{
"command": "next_item",
"output": "(unknown)",
"result": "pass"
}
],
}
}
},
"talkback": {
"browsers": {
"and_chr": {
"at_version": "7.2",
"browser_version": "70",
"os_version": "7.2",
"date": "2019-01-14",
"output": [
{
"command": "next_item",
"output": "(unknown)",
"result": "pass"
}
],
}
}
}
}
};
let html = fs.readFileSync(dataDir+'/tests/html/html_figure_results.html', 'utf8');
let cell_map = [
{
"id": "nvda",
"browsers": [{
"id": "firefox",
"row": 2,
"column": 1
}]
},
{
"id": "jaws",
"browsers": [{
"id": "ie",
"row": 3,
"column": 4
}, {
"id": "firefox",
"row": 2,
"column": 4
}]
},
{
"id": "vo_macos",
"browsers": [{
"id": "safari",
"row": 4,
"column": 5
}]
},
{
"id": "vo_ios",
"browsers": [{
"id": "ios_saf",
"row": 5,
"column": 6
}]
},
{
"id": "talkback",
"browsers": [{
"id": "and_chr",
"row": 7,
"column": 7
}]
},
{
"id": "narrator",
"browsers": [{
"id": "edge",
"row": 8,
"column": 8
}]
}
];
if (!fs.existsSync(dataDir+'/tests/tech/html/html_figure/')) {
fs.mkdirSync(dataDir+'/tests/tech/html/html_figure/', { recursive: true });
}
let $ = cheerio.load(html);
$('table').each(function(i) {
// Clone the TEST_SHELL and make a new json object.
let test = JSON.parse(JSON.stringify(TEST_SHELL));
let expected = $(this).prev().text().replace('Expected announcement:', '').trim();
expected = expected.replace('Should announce ', '');
test.css_target = '#target_'+i;
test.title = 'Figure test '+i;
let rows = $('tr', this);
cell_map.forEach(function(at) {
at.browsers.forEach(function(browser) {
let row = rows[browser.row];
let cells = $('td', row);
let cell = $(cells[browser.column-1]).text();
if (cell.includes('pass')) {
test.at[at.id].browsers[browser.id].output[0].result = 'pass';
test.at[at.id].browsers[browser.id].output[0].output = expected;
} else if (cell.includes('fail')) {
test.at[at.id].browsers[browser.id].output[0].result = 'fail';
} else {
test.at[at.id].browsers[browser.id].output[0].result = 'partial';
}
if (!['pass', 'fail'].includes(cell.trim())) {
console.log(cell);
test.at[at.id].browsers[browser.id].notes = cell.trim();
}
});
});
let json_path = dataDir+'/tests/tech/html/html_figure/test_'+i+'.json';
// now write the test file
fs.writeFileSync(json_path, JSON.stringify(test, null, 2));
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment