Skip to content

Commit 469a87f

Browse files
author
Vasyl Vavrychuk
committed
extracted WebKitDriverProxy
1 parent a31a969 commit 469a87f

File tree

2 files changed

+124
-105
lines changed

2 files changed

+124
-105
lines changed

web/WebDriverJsDemo.html

Lines changed: 43 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
<td>Web page</td>
3535
<td>
3636
<input name="webPage" type="text"/>
37-
<input type="button" value="GET" onclick="wd.get()"/>
38-
<input type="button" value="Source" onclick="wd.source()"/>
39-
<input type="button" value="Screenshot" onclick="wd.screenshot()"/>
40-
<select onmouseup="wd.logs(this.options[this.selectedIndex].value)">
37+
<input type="button" value="GET" onclick="wd.onGet()"/>
38+
<input type="button" value="Source" onclick="wd.onSource()"/>
39+
<input type="button" value="Screenshot" onclick="wd.onScreenshot()"/>
40+
<select onmouseup="wd.onLogs(this.options[this.selectedIndex].value)">
4141
<option>Logs</option>
4242
<option>browser</option>
4343
<option>driver</option>
@@ -55,66 +55,66 @@
5555
<option>xpath</option>
5656
</select>
5757
<input name="findElementKey" type="text"/>
58-
<input type="submit" value="Find element" onclick="wd.findElement()">
58+
<input type="submit" value="Find element" onclick="wd.onFindElement()">
5959
</div>
6060

6161
<div class="commandBlock">
6262
<div style="position: absolute;">Key down:</div>
6363
<div id="keyboard" style="margin-left: 80px;">
6464
<div>
65-
<input type="submit" value="ESC" onclick="wd.sendKeys(webdriver.Key.ESCAPE)"/>
66-
<input type="submit" value="Q" onclick="wd.sendKeys(this.value)"/>
67-
<input type="submit" value="W" onclick="wd.sendKeys(this.value)"/>
68-
<input type="submit" value="E" onclick="wd.sendKeys(this.value)"/>
69-
<input type="submit" value="R" onclick="wd.sendKeys(this.value)"/>
70-
<input type="submit" value="T" onclick="wd.sendKeys(this.value)"/>
71-
<input type="submit" value="Y" onclick="wd.sendKeys(this.value)"/>
72-
<input type="submit" value="U" onclick="wd.sendKeys(this.value)"/>
73-
<input type="submit" value="I" onclick="wd.sendKeys(this.value)"/>
74-
<input type="submit" value="O" onclick="wd.sendKeys(this.value)"/>
75-
<input type="submit" value="P" onclick="wd.sendKeys(this.value)"/>
76-
<input type="submit" value="" onclick="wd.sendKeys(webdriver.Key.BACK_SPACE)"/>
65+
<input type="submit" value="ESC" onclick="wd.onSendKeys(webdriver.Key.ESCAPE)"/>
66+
<input type="submit" value="Q" onclick="wd.onSendKeys(this.value)"/>
67+
<input type="submit" value="W" onclick="wd.onSendKeys(this.value)"/>
68+
<input type="submit" value="E" onclick="wd.onSendKeys(this.value)"/>
69+
<input type="submit" value="R" onclick="wd.onSendKeys(this.value)"/>
70+
<input type="submit" value="T" onclick="wd.onSendKeys(this.value)"/>
71+
<input type="submit" value="Y" onclick="wd.onSendKeys(this.value)"/>
72+
<input type="submit" value="U" onclick="wd.onSendKeys(this.value)"/>
73+
<input type="submit" value="I" onclick="wd.onSendKeys(this.value)"/>
74+
<input type="submit" value="O" onclick="wd.onSendKeys(this.value)"/>
75+
<input type="submit" value="P" onclick="wd.onSendKeys(this.value)"/>
76+
<input type="submit" value="" onclick="wd.onSendKeys(webdriver.Key.BACK_SPACE)"/>
7777
</div>
7878
<div>
79-
<input type="submit" value="A" onclick="wd.sendKeys(this.value)" style="margin-left: 53px;"/>
80-
<input type="submit" value="S" onclick="wd.sendKeys(this.value)"/>
81-
<input type="submit" value="D" onclick="wd.sendKeys(this.value)"/>
82-
<input type="submit" value="F" onclick="wd.sendKeys(this.value)"/>
83-
<input type="submit" value="G" onclick="wd.sendKeys(this.value)"/>
84-
<input type="submit" value="H" onclick="wd.sendKeys(this.value)"/>
85-
<input type="submit" value="J" onclick="wd.sendKeys(this.value)"/>
86-
<input type="submit" value="K" onclick="wd.sendKeys(this.value)"/>
87-
<input type="submit" value="L" onclick="wd.sendKeys(this.value)"/>
88-
<input type="submit" value="Enter" onclick="wd.sendKeys(webdriver.Key.ENTER)"/>
79+
<input type="submit" value="A" onclick="wd.onSendKeys(this.value)" style="margin-left: 53px;"/>
80+
<input type="submit" value="S" onclick="wd.onSendKeys(this.value)"/>
81+
<input type="submit" value="D" onclick="wd.onSendKeys(this.value)"/>
82+
<input type="submit" value="F" onclick="wd.onSendKeys(this.value)"/>
83+
<input type="submit" value="G" onclick="wd.onSendKeys(this.value)"/>
84+
<input type="submit" value="H" onclick="wd.onSendKeys(this.value)"/>
85+
<input type="submit" value="J" onclick="wd.onSendKeys(this.value)"/>
86+
<input type="submit" value="K" onclick="wd.onSendKeys(this.value)"/>
87+
<input type="submit" value="L" onclick="wd.onSendKeys(this.value)"/>
88+
<input type="submit" value="Enter" onclick="wd.onSendKeys(webdriver.Key.ENTER)"/>
8989
</div>
9090
<div>
91-
<input type="submit" value="Z" onclick="wd.sendKeys(this.value)" style="margin-left: 53px;"/>
92-
<input type="submit" value="X" onclick="wd.sendKeys(this.value)"/>
93-
<input type="submit" value="C" onclick="wd.sendKeys(this.value)"/>
94-
<input type="submit" value="V" onclick="wd.sendKeys(this.value)"/>
95-
<input type="submit" value="B" onclick="wd.sendKeys(this.value)"/>
96-
<input type="submit" value="N" onclick="wd.sendKeys(this.value)"/>
97-
<input type="submit" value="M" onclick="wd.sendKeys(this.value)"/>
98-
<input type="submit" value="&#8657;" onclick="wd.sendKeys(webdriver.Key.UP)" style="margin-left: 45px;"/>
91+
<input type="submit" value="Z" onclick="wd.onSendKeys(this.value)" style="margin-left: 53px;"/>
92+
<input type="submit" value="X" onclick="wd.onSendKeys(this.value)"/>
93+
<input type="submit" value="C" onclick="wd.onSendKeys(this.value)"/>
94+
<input type="submit" value="V" onclick="wd.onSendKeys(this.value)"/>
95+
<input type="submit" value="B" onclick="wd.onSendKeys(this.value)"/>
96+
<input type="submit" value="N" onclick="wd.onSendKeys(this.value)"/>
97+
<input type="submit" value="M" onclick="wd.onSendKeys(this.value)"/>
98+
<input type="submit" value="&#8657;" onclick="wd.onSendKeys(webdriver.Key.UP)" style="margin-left: 45px;"/>
9999
</div>
100100
<div>
101-
<input type="submit" value=" " onclick="wd.sendKeys(this.value)" style="width: 129px; margin-left: 100px;"/>
102-
<input type="submit" value="&#8656;" onclick="wd.sendKeys(webdriver.Key.LEFT)" style="margin-left: 91px;"/>
103-
<input type="submit" value="&#8659;" onclick="wd.sendKeys(webdriver.Key.DOWN)"/>
104-
<input type="submit" value="&#8658;" onclick="wd.sendKeys(webdriver.Key.RIGHT)"/>
101+
<input type="submit" value=" " onclick="wd.onSendKeys(this.value)" style="width: 129px; margin-left: 100px;"/>
102+
<input type="submit" value="&#8656;" onclick="wd.onSendKeys(webdriver.Key.LEFT)" style="margin-left: 91px;"/>
103+
<input type="submit" value="&#8659;" onclick="wd.onSendKeys(webdriver.Key.DOWN)"/>
104+
<input type="submit" value="&#8658;" onclick="wd.onSendKeys(webdriver.Key.RIGHT)"/>
105105
</div>
106106
</div>
107107
</div>
108108

109109
<div class="commandBlock">
110-
<input type="submit" value="List window handles" onclick="wd.listWindowHandles()"/>
110+
<input type="submit" value="List window handles" onclick="wd.onListWindowHandles()"/>
111111
<select id="windowList" style="visibility: hidden;">
112112
</select>
113-
<input id="chooseWindow" type="submit" value="Choose window" onclick="wd.chooseWindow()" style="visibility: hidden;"/>
113+
<input id="chooseWindow" type="submit" value="Choose window" onclick="wd.onChooseWindow()" style="visibility: hidden;"/>
114114
</div>
115115

116116
<div class="commandBlock">
117-
<input type="submit" value="Set window size" onclick="wd.setWindowSize()"/>
117+
<input type="submit" value="Set window size" onclick="wd.onSetWindowSize()"/>
118118
<input name="windowSizeWidth" type="text" style="width: 50px"/>
119119
<span class="unitLabel">px</span> width and
120120
<input name="windowSizeHeight" type="text" style="width: 50px"/>
@@ -126,7 +126,7 @@
126126
</div>
127127

128128
<div style="margin-top: 20px">
129-
<input type="submit" value="Quit" onclick="wd.quit()"/>
129+
<input type="submit" value="Quit" onclick="wd.onQuit()"/>
130130
</div>
131131
</body>
132132
</html>

web/webdriver-app.js

Lines changed: 81 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@ if (!String.prototype.endsWith) {
2525
});
2626
}
2727

28-
webdriver.FirstSessionBuilder = function() {
28+
webdriver.ExistingSessionBuilder = function() {
2929
webdriver.AbstractBuilder.call(this);
3030
};
3131

32-
webdriver.FirstSessionBuilder.prototype = new webdriver.AbstractBuilder();
32+
webdriver.ExistingSessionBuilder.prototype = Object.create(webdriver.AbstractBuilder.prototype);
3333

34-
webdriver.FirstSessionBuilder.prototype.build = function() {
34+
webdriver.ExistingSessionBuilder.prototype.build = function() {
3535
var self = this;
3636
var client = new webdriver.http.CorsClient(this.getServerUrl());
3737
var executor = new webdriver.http.Executor(client);
@@ -60,6 +60,31 @@ webdriver.WebDriver.prototype.visualizerShowPoint = function() {
6060
'WebDriver.visualizerShowPoint()');
6161
};
6262

63+
function WebDriverProxy() {
64+
}
65+
66+
WebDriverProxy.prototype.setServerUrl = function(serverUrl) {
67+
var self = this;
68+
this.driver_ = new webdriver.ExistingSessionBuilder().
69+
usingServer(serverUrl).
70+
withCapabilities({'browserName': 'qtwebkit'}).
71+
build();
72+
73+
for (var propertyName in this.driver_) {
74+
var property = this.driver_[propertyName];
75+
if (typeof property === 'function') {
76+
this[propertyName] = function(impl) {
77+
return function() {
78+
return impl.apply(self.driver_, arguments);
79+
};
80+
}(property);
81+
}
82+
}
83+
};
84+
85+
WebDriverProxy.prototype.quit = function() {
86+
};
87+
6388
window.onerror = function(errorMsg, url, lineNumber, columnNumber, error) {
6489
console.log(error.stack);
6590
return false;
@@ -192,8 +217,8 @@ Util.webDriverKeyFromJs = function(keyCode) {
192217
};
193218

194219
function VisualizerXsltProcessors() {
195-
this.widget = this._create('widget_view_visualizer.xsl');
196-
this.qml = this._create('qml_view_visualizer.xsl');
220+
this.widget = this.create_('widget_view_visualizer.xsl');
221+
this.qml = this.create_('qml_view_visualizer.xsl');
197222
}
198223

199224
VisualizerXsltProcessors.prototype.get = function(webPage) {
@@ -203,7 +228,7 @@ VisualizerXsltProcessors.prototype.get = function(webPage) {
203228
return this.qml;
204229
};
205230

206-
VisualizerXsltProcessors.prototype._create = function(name) {
231+
VisualizerXsltProcessors.prototype.create_ = function(name) {
207232
var stylesheet = Util.loadFile(name);
208233
stylesheet = (new DOMParser()).parseFromString(stylesheet, 'application/xml');
209234
var processor = new XSLTProcessor();
@@ -215,7 +240,8 @@ VisualizerXsltProcessors.prototype._create = function(name) {
215240
}
216241
};
217242

218-
function VisualizerController() {
243+
function VisualizerController(driver) {
244+
this.driver = driver;
219245
this.xsltProcessors = new VisualizerXsltProcessors();
220246
}
221247

@@ -458,67 +484,63 @@ VisualizerController.prototype.showVisualizationWindow = function(source, size)
458484
};
459485

460486
function WebDriverJsController() {
461-
this.visualizer = new VisualizerController();
487+
this.driver = new WebDriverProxy();
488+
this.visualizer = new VisualizerController(this.driver);
462489
}
463490

464-
WebDriverJsController.prototype._constructWebDriver = function() {
465-
var webDriverUrlPort = document.getElementsByName('webDriverUrlPort')[0].value;
466-
var webPage = document.getElementsByName('webPage')[0].value;
467-
468-
if (this.webDriverUrlPort == webDriverUrlPort &&
469-
this.webPage == webPage)
470-
return;
491+
WebDriverJsController.prototype.setServerUrl = function(serverUrl) {
492+
this.driver.setServerUrl(serverUrl);
471493

472-
if (this.webDriverUrlPort != webDriverUrlPort) {
473-
this._driver = this.visualizer.driver = new webdriver.FirstSessionBuilder().
474-
usingServer(webDriverUrlPort).
475-
withCapabilities({'browserName': 'qtwebkit'}).
476-
build();
494+
this.serverUrl = serverUrl;
495+
if (localStorage)
496+
localStorage.serverUrl = serverUrl;
497+
}
477498

478-
this.webDriverUrlPort = webDriverUrlPort;
479-
if (localStorage)
480-
localStorage.webDriverUrlPort = webDriverUrlPort;
481-
}
499+
WebDriverJsController.prototype.setWebPage = function(webPage) {
500+
this.element = null;
482501

483-
if (this.webPage != webPage) {
484-
this._driver.get(webPage);
485-
this.element = null;
502+
this.driver.get(webPage);
486503

487-
this.webPage = webPage;
488-
if (localStorage)
489-
localStorage.webPage = webPage;
490-
}
504+
this.webPage = webPage;
505+
if (localStorage)
506+
localStorage.webPage = webPage;
491507
};
492508

493-
WebDriverJsController.prototype.driver = function() {
494-
if (this._driver === null)
495-
this._constructWebDriver();
496-
return this._driver;
509+
WebDriverJsController.prototype.updateDriver = function() {
510+
var webDriverUrlPort = document.getElementsByName('webDriverUrlPort')[0].value;
511+
if (webDriverUrlPort !== this.webDriverUrlPort)
512+
this.setServerUrl(webDriverUrlPort);
513+
514+
var webPage = document.getElementsByName('webPage')[0].value;
515+
if (webPage !== this.webPage)
516+
this.setWebPage(webPage);
497517
};
498518

499-
WebDriverJsController.prototype.get = function() {
519+
WebDriverJsController.prototype.onGet = function() {
500520
this.webPage = null;
501-
this._constructWebDriver();
521+
this.updateDriver();
502522
};
503523

504-
WebDriverJsController.prototype.source = function() {
505-
this._constructWebDriver();
524+
WebDriverJsController.prototype.onSource = function() {
525+
this.updateDriver();
506526
this.visualizer.updateSource(this.webPage);
507527
};
508528

509-
WebDriverJsController.prototype.screenshot = function() {
510-
this.driver().takeScreenshot().then(function(data) {
529+
WebDriverJsController.prototype.onScreenshot = function() {
530+
this.updateDriver();
531+
this.driver.takeScreenshot().then(function(data) {
511532
data = base64_arraybuffer.decode(data);
512533
data = new Blob([data], {type: 'image/png'});
513534
saveAs(data, 'screenshot.png');
514535
})
515536
};
516537

517-
WebDriverJsController.prototype.logs = function(type) {
538+
WebDriverJsController.prototype.onLogs = function(type) {
518539
if (type === 'Logs')
519540
return;
520541

521-
this.driver().manage().logs().get(type).then(function(entries) {
542+
this.updateDriver();
543+
this.driver.manage().logs().get(type).then(function(entries) {
522544
var lines = [];
523545
for (var entryIndex in entries) {
524546
var entry = entries[entryIndex];
@@ -530,34 +552,34 @@ WebDriverJsController.prototype.logs = function(type) {
530552
})
531553
};
532554

533-
WebDriverJsController.prototype.findElement = function() {
555+
WebDriverJsController.prototype.onFindElement = function() {
534556
var criteria = document.getElementsByName('findElementCriteria')[0].value;
535557
var key = document.getElementsByName('findElementKey')[0].value;
536558
if (criteria === 'id')
537-
this.element = this.driver().findElement(webdriver.By.id(key));
559+
this.element = this.driver.findElement(webdriver.By.id(key));
538560
else if (criteria === 'name')
539-
this.element = this.driver().findElement(webdriver.By.name(key));
561+
this.element = this.driver.findElement(webdriver.By.name(key));
540562
else if (criteria === 'tagName')
541-
this.element = this.driver().findElement(webdriver.By.tagName(key));
563+
this.element = this.driver.findElement(webdriver.By.tagName(key));
542564
else if (criteria === 'xpath')
543-
this.element = this.driver().findElement(webdriver.By.xpath(key));
565+
this.element = this.driver.findElement(webdriver.By.xpath(key));
544566
};
545567

546-
WebDriverJsController.prototype.sendKeys = function(key) {
568+
WebDriverJsController.prototype.onSendKeys = function(key) {
547569
if (this.element) {
548570
this.element.sendKeys(key);
549571
} else {
550-
this.driver().actions().sendKeys(key).perform();
572+
this.driver.actions().sendKeys(key).perform();
551573
}
552574
};
553575

554-
WebDriverJsController.prototype.click = function() {
576+
WebDriverJsController.prototype.onClick = function() {
555577
this.element.click();
556578
};
557579

558-
WebDriverJsController.prototype.listWindowHandles = function() {
580+
WebDriverJsController.prototype.onListWindowHandles = function() {
559581
var select = document.getElementById('windowList');
560-
this.driver().getAllWindowHandles().then(function(handles) {
582+
this.driver.getAllWindowHandles().then(function(handles) {
561583
select.innerHTML = '';
562584
for (var handle in handles) {
563585
var item = document.createElement('option');
@@ -569,24 +591,21 @@ WebDriverJsController.prototype.listWindowHandles = function() {
569591
});
570592
};
571593

572-
WebDriverJsController.prototype.chooseWindow = function() {
594+
WebDriverJsController.prototype.onChooseWindow = function() {
573595
var handle = document.getElementById('windowList').value;
574-
this.driver().switchTo().window(handle);
596+
this.driver.switchTo().window(handle);
575597
};
576598

577-
WebDriverJsController.prototype.setWindowSize = function() {
599+
WebDriverJsController.prototype.onSetWindowSize = function() {
578600
var width = document.getElementsByName('windowSizeWidth')[0].value;
579601
var height = document.getElementsByName('windowSizeHeight')[0].value;
580602
width = parseInt(width);
581603
height = parseInt(height);
582-
this.driver().manage().window().setSize(width, height);
604+
this.driver.manage().window().setSize(width, height);
583605
};
584606

585-
WebDriverJsController.prototype.quit = function() {
586-
if (!this._driver)
587-
return;
588-
589-
this._driver.quit();
607+
WebDriverJsController.prototype.onQuit = function() {
608+
this.driver.quit();
590609
this.visualizer.quit();
591610
this.webDriverUrlPort = null;
592611
this.webPage = null;

0 commit comments

Comments
 (0)