Skip to content

Commit 80421f6

Browse files
author
Vasyl Vavrychuk
committed
multiple visualizers support
1 parent db36f6a commit 80421f6

File tree

2 files changed

+155
-14
lines changed

2 files changed

+155
-14
lines changed

web/WebDriverJsDemo.html

+42-14
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<script src="base64-arraybuffer.js"></script>
1717
<script src="FileSaver.js"></script>
1818
<script>
19+
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith
1920
if (!String.prototype.startsWith) {
2021
Object.defineProperty(String.prototype, 'startsWith', {
2122
enumerable: false,
@@ -27,6 +28,19 @@
2728
}
2829
});
2930
}
31+
if (!String.prototype.endsWith) {
32+
Object.defineProperty(String.prototype, 'endsWith', {
33+
enumerable: false,
34+
configurable: false,
35+
writable: false,
36+
value: function (searchString, position) {
37+
position = position || this.length;
38+
position = position - searchString.length;
39+
var lastIndex = this.lastIndexOf(searchString);
40+
return lastIndex !== -1 && lastIndex === position;
41+
}
42+
});
43+
}
3044

3145
function loadFile(url) {
3246
var req = new XMLHttpRequest();
@@ -159,6 +173,30 @@
159173
}
160174
}
161175

176+
function VisualizerXsltProcessors() {
177+
this.widget = this._create('widget_view_visualizer.xsl');
178+
this.qml = this._create('qml_view_visualizer.xsl');
179+
}
180+
181+
VisualizerXsltProcessors.prototype.get = function(webPage) {
182+
if (webPage.startsWith('qtwidget://'))
183+
return this.widget;
184+
if (webPage.endsWith('.qml'))
185+
return this.qml;
186+
}
187+
188+
VisualizerXsltProcessors.prototype._create = function(name) {
189+
var stylesheet = loadFile(name);
190+
stylesheet = (new DOMParser()).parseFromString(stylesheet, 'application/xml');
191+
var processor = new XSLTProcessor();
192+
try {
193+
processor.importStylesheet(stylesheet);
194+
return processor;
195+
} catch (err) {
196+
console.log(err);
197+
}
198+
}
199+
162200
function WebDriverJsDemo() {
163201
}
164202

@@ -186,7 +224,7 @@
186224

187225
this.driver.getSession().then(function(session) {
188226
self.setSession(session.getId());
189-
self._constructXsltProcessor();
227+
self.xsltProcessors = new VisualizerXsltProcessors();
190228
});
191229
}
192230

@@ -199,17 +237,6 @@
199237
}
200238
}
201239

202-
WebDriverJsDemo.prototype._constructXsltProcessor = function() {
203-
var stylesheet = loadFile(this.webDriverUrlPort + '/widget_view_visualizer.xsl');
204-
stylesheet = (new DOMParser()).parseFromString(stylesheet, 'application/xml');
205-
this.xsltProcessor = new XSLTProcessor();
206-
try {
207-
this.xsltProcessor.importStylesheet(stylesheet);
208-
} catch (err) {
209-
console.log(err);
210-
}
211-
}
212-
213240
WebDriverJsDemo.prototype.get = function() {
214241
this.webPage = null;
215242
this._constructWebDriver();
@@ -348,7 +375,7 @@
348375
}
349376

350377
WebDriverJsDemo.prototype.showVisualizationWindow = function(source, size) {
351-
var isQt = this.webPage.startsWith('qtwidget://');
378+
var isQt = this.webPage.startsWith('qtwidget://') || this.webPage.endsWith('.qml');
352379
var isWinOpened = this.isVisualizerOpened();
353380

354381
if (!isQt && isWinOpened) {
@@ -361,8 +388,9 @@
361388
this.openVisualizationWindow(size);
362389

363390
if (isQt) {
391+
var xsltProcessor = this.xsltProcessors.get(this.webPage);
364392
var receivedDoc = (new DOMParser()).parseFromString(source, 'application/xml');
365-
receivedDoc = this.xsltProcessor.transformToDocument(receivedDoc);
393+
receivedDoc = xsltProcessor.transformToDocument(receivedDoc);
366394
var visualizerDoc = this.visualizationWin.document;
367395
visualizerDoc.replaceChild(receivedDoc.documentElement, visualizerDoc.documentElement);
368396
} else {

web/qml_view_visualizer.xsl

+113
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
2+
<xsl:template match="/*">
3+
<html>
4+
<head>
5+
<title><xsl:value-of select="@name"/></title>
6+
</head>
7+
<body>
8+
<xsl:apply-templates/>
9+
</body>
10+
</html>
11+
</xsl:template>
12+
13+
<xsl:template match="QCheckBox">
14+
<span>
15+
<xsl:call-template name="style"/>
16+
<input type="checkbox">
17+
<xsl:copy-of select="@elementId"/>
18+
<xsl:if test="@checked = 'true'">
19+
<xsl:attribute name="checked"/>
20+
</xsl:if>
21+
</input>
22+
<span>
23+
<xsl:value-of select="@text"/>
24+
</span>
25+
</span>
26+
</xsl:template>
27+
<xsl:template match="QLabel">
28+
<span>
29+
<xsl:copy-of select="@elementId"/>
30+
<xsl:call-template name="style"/>
31+
<xsl:value-of select="@text"/>
32+
</span>
33+
</xsl:template>
34+
<xsl:template match="QLineEdit">
35+
<input type="text">
36+
<xsl:copy-of select="@elementId"/>
37+
<xsl:call-template name="style"/>
38+
<xsl:attribute name="value">
39+
<xsl:value-of select="@text"/>
40+
</xsl:attribute>
41+
<xsl:if test="@enabled = 'false'">
42+
<xsl:attribute name="disabled"/>
43+
</xsl:if>
44+
</input>
45+
</xsl:template>
46+
<xsl:template match="QPushButton|QToolButton">
47+
<input type="submit">
48+
<xsl:copy-of select="@elementId"/>
49+
<xsl:call-template name="style"/>
50+
<xsl:attribute name="value">
51+
<xsl:value-of select="@text"/>
52+
</xsl:attribute>
53+
</input>
54+
</xsl:template>
55+
<xsl:template match="QRadioButton">
56+
<span>
57+
<xsl:call-template name="style"/>
58+
<input type="radio">
59+
<xsl:copy-of select="@elementId"/>
60+
<xsl:attribute name="name">
61+
<xsl:text>parent:</xsl:text>
62+
<xsl:value-of select="../@elementId"/>
63+
</xsl:attribute>
64+
</input>
65+
<span>
66+
<xsl:value-of select="@text"/>
67+
</span>
68+
</span>
69+
</xsl:template>
70+
<xsl:template match="QScrollArea">
71+
<div>
72+
<xsl:copy-of select="@elementId"/>
73+
<xsl:call-template name="style"/>
74+
<xsl:apply-templates/>
75+
</div>
76+
</xsl:template>
77+
<xsl:template match="QTextEdit|QPlainTextEdit">
78+
<textarea>
79+
<xsl:copy-of select="@elementId"/>
80+
<xsl:call-template name="style"/>
81+
<xsl:if test="@enabled = 'false'">
82+
<xsl:attribute name="disabled"/>
83+
</xsl:if>
84+
<xsl:value-of select="@plainText"/>
85+
</textarea>
86+
</xsl:template>
87+
<xsl:template match="QWebView">
88+
<iframe>
89+
<xsl:copy-of select="@elementId"/>
90+
<xsl:call-template name="style"/>
91+
<xsl:attribute name="src">
92+
<xsl:value-of select="@url"/>
93+
</xsl:attribute>
94+
</iframe>
95+
</xsl:template>
96+
97+
<xsl:template name="style">
98+
<xsl:attribute name="style">
99+
<xsl:if test="@width">
100+
<xsl:text>width: </xsl:text><xsl:value-of select="@width"/><xsl:text>;</xsl:text>
101+
</xsl:if>
102+
<xsl:if test="@height">
103+
<xsl:text>height: </xsl:text><xsl:value-of select="@height"/><xsl:text>;</xsl:text>
104+
</xsl:if>
105+
<xsl:if test="@visible = 'false'">
106+
<xsl:text>display: none;</xsl:text>
107+
</xsl:if>
108+
<xsl:if test="@styleSheet">
109+
<xsl:value-of select="@styleSheet"/><xsl:text>;</xsl:text>
110+
</xsl:if>
111+
</xsl:attribute>
112+
</xsl:template>
113+
</xsl:stylesheet>

0 commit comments

Comments
 (0)