Merge pull request #2323 from atom/better-spec

Fix a few flaky tests
This commit is contained in:
Cheng Zhao 2015-07-24 16:21:16 +08:00
commit 48975d04e0
7 changed files with 55 additions and 66 deletions

View file

@ -130,10 +130,10 @@ If "on", the guest page will have web security disabled.
## Methods
The webview element must be loaded before using the methods.
The webview element must be loaded before using the methods.
**Example**
```javascript
webview.addEventListener("dom-ready", function(){
webview.addEventListener("dom-ready", function() {
webview.openDevTools();
});
```

View file

@ -4,7 +4,6 @@ path = require 'path'
remote = require 'remote'
http = require 'http'
url = require 'url'
auth = require 'basic-auth'
BrowserWindow = remote.require 'browser-window'
@ -258,48 +257,3 @@ describe 'browser-window module', ->
assert.equal url, 'https://www.github.com/'
done()
w.loadUrl "file://#{fixtures}/pages/will-navigate.html"
describe 'dom-ready event', ->
return if isCI and process.platform is 'darwin'
it 'emits when document is loaded', (done) ->
ipc = remote.require 'ipc'
server = http.createServer (req, res) ->
action = url.parse(req.url, true).pathname
if action == '/logo.png'
img = fs.readFileSync(path.join(fixtures, 'assets', 'logo.png'))
res.writeHead(200, {'Content-Type': 'image/png'})
setTimeout ->
res.end(img, 'binary')
, 2000
server.close()
server.listen 62542, '127.0.0.1'
ipc.on 'dom-ready', (e, state) ->
ipc.removeAllListeners 'dom-ready'
assert.equal state, 'interactive'
done()
w.webContents.on 'did-finish-load', ->
w.close()
w.loadUrl "file://#{fixtures}/pages/f.html"
describe 'basic auth', ->
it 'should authenticate with correct credentials', (done) ->
ipc = remote.require 'ipc'
server = http.createServer (req, res) ->
action = url.parse(req.url, true).pathname
if action == '/'
credentials = auth(req)
if credentials.name == 'test' and credentials.pass == 'test'
res.end('Authenticated')
server.close()
else if action == '/jquery.js'
js = fs.readFileSync(path.join(__dirname, 'static', 'jquery-2.0.3.min.js'))
res.writeHead(200, {'Content-Type': 'text/javascript'})
res.end(js, 'utf-8')
server.listen 62342, '127.0.0.1'
ipc.on 'console-message', (e, message) ->
ipc.removeAllListeners 'console-message'
assert.equal message, 'Authenticated'
done()
w.webContents.on 'did-finish-load', ->
w.close()
w.loadUrl "file://#{fixtures}/pages/basic-auth.html"

View file

@ -20,8 +20,7 @@ describe 'session module', ->
res.end('finished')
server.close()
port = remote.process.port
server.listen port, '127.0.0.1', ->
server.listen 0, '127.0.0.1', ->
{port} = server.address()
w.loadUrl "#{url}:#{port}"
w.webContents.on 'did-finish-load', ->

View file

@ -1,16 +1,21 @@
<html>
<body>
<script src='http://127.0.0.1:62342/jquery.js'></script>
<script src="../../static/jquery-2.0.3.min.js"></script>
<script type="text/javascript" charset="utf-8">
var ipc = require('ipc');
var port = location.search.substr("?port=".length);
$.ajax({
type: "GET",
url: "http://127.0.0.1:62342",
url: "http://127.0.0.1:" + port,
headers: {
"Authorization": "Basic " + btoa("test:test")
},
success: function (result){
require('ipc').send('console-message', result);
}
success: function(result) {
ipc.sendToHost(result);
},
error: function(xhr, status, error) {
ipc.sendToHost(error);
},
});
</script>
</body>

9
spec/fixtures/pages/dom-ready.html vendored Normal file
View file

@ -0,0 +1,9 @@
<html>
<body>
<script src="../../static/jquery-2.0.3.min.js"></script>
<script type="text/javascript" charset="utf-8">
var port = location.search.substr("?port=".length);
document.write("<img src='http://127.0.0.1:" + port + "/logo.png' />");
</script>
</body>
</html>

View file

@ -1,11 +0,0 @@
<html>
<body>
<img src='http://127.0.0.1:62542/logo.png' />
<script type="text/javascript" charset="utf-8">
var remote = require('remote')
remote.getCurrentWindow().webContents.on('dom-ready', function() {
require('ipc').send('dom-ready', document.readyState);
})
</script>
</body>
</html>

View file

@ -1,5 +1,6 @@
assert = require 'assert'
path = require 'path'
http = require 'http'
describe '<webview> tag', ->
fixtures = path.join __dirname, 'fixtures'
@ -228,3 +229,35 @@ describe '<webview> tag', ->
webview.setAttribute 'nodeintegration', 'on'
webview.src = "file://#{fixtures}/pages/history.html"
document.body.appendChild webview
describe 'basic auth', ->
auth = require 'basic-auth'
it 'should authenticate with correct credentials', (done) ->
message = 'Authenticated'
server = http.createServer (req, res) ->
credentials = auth(req)
if credentials.name == 'test' and credentials.pass == 'test'
res.end(message)
else
res.end('failed')
server.close()
server.listen 0, '127.0.0.1', ->
{port} = server.address()
webview.addEventListener 'ipc-message', (e) ->
assert.equal e.channel, message
done()
webview.src = "file://#{fixtures}/pages/basic-auth.html?port=#{port}"
webview.setAttribute 'nodeintegration', 'on'
document.body.appendChild webview
describe 'dom-ready event', ->
it 'emits when document is loaded', (done) ->
server = http.createServer (req) ->
# Never respond, so the page never finished loading.
server.listen 0, '127.0.0.1', ->
{port} = server.address()
webview.addEventListener 'dom-ready', ->
done()
webview.src = "file://#{fixtures}/pages/dom-ready.html?port=#{port}"
document.body.appendChild webview