askui
Version:
Reliable, automated end-to-end-testing that depends on what is shown on your screen instead of the technology you are running on
31 lines (30 loc) • 1.27 kB
JavaScript
import fs from 'fs';
import path from 'path';
import { JSDOM } from 'jsdom';
import { DetectedElement } from '../model/annotation-result/detected-element';
export class Annotation {
constructor(image, detected_elements = []) {
this.image = image;
this.detected_elements = detected_elements;
}
toHtml() {
const template = Annotation.getHtmlTemplate();
const script = template.window.document.createElement('script');
script.innerHTML = `
var el = document.getElementsByTagName("bounding-box-renderer");
el[0].setAttribute("shouldrenderimage", true);
el[0].setAttribute("imagestr", "${this.image.trim()}");
el[0].setAttribute("detectedobjects", JSON.stringify(${JSON.stringify(this.detected_elements)}));
`;
template.window.document.body.appendChild(script);
return template;
}
static fromJson(json, resizeRatio = 1) {
const annotation = json;
return new Annotation(annotation.image, annotation.detected_elements.map((data) => DetectedElement.fromJson(data, resizeRatio)));
}
static getHtmlTemplate() {
const templatePath = path.join(__dirname, 'template.html');
return new JSDOM(fs.readFileSync(templatePath, 'utf8'));
}
}