Skip to content

Commit 3cffd31

Browse files
Cong Liurogerwang
authored andcommitted
[test] add test case for nwjs#5355
1 parent 6c12fe8 commit 3cffd31

File tree

3 files changed

+81
-0
lines changed

3 files changed

+81
-0
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<title>cookie-lost-remote-page</title>
7+
</head>
8+
<body>
9+
<button id="get-cookie" onclick="getcookie()">Get Cookie</button>
10+
<script>
11+
function getcookie() {
12+
var h1 = document.createElement('h1');
13+
h1.setAttribute('id', 'result');
14+
h1.innerHTML = document.cookie;
15+
document.body.appendChild(h1);
16+
}
17+
</script>
18+
</body>
19+
</html>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
(function(){
2+
const http = require('http');
3+
let id = 1;
4+
5+
nw.Window.open(`http://localhost:${nw.App.manifest.port}/`, (w)=>{
6+
w.showDevTools();
7+
w.closeDevTools();
8+
});
9+
10+
// Create an HTTP server
11+
let srv = http.createServer( (req, res) => {
12+
let headers = {'Content-Type': 'text/html'};
13+
let cookies = req.headers['cookie'];
14+
if (!cookies) {
15+
headers['Set-Cookie'] = ['sid=' + (id++)];
16+
}
17+
res.writeHead(200, headers);
18+
require('fs').createReadStream('index.html').pipe(res);
19+
});
20+
21+
srv.listen(nw.App.manifest.port, '127.0.0.1');
22+
})()
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import time
2+
import os
3+
import sys
4+
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
5+
from nw_util import *
6+
7+
from selenium import webdriver
8+
from selenium.webdriver.chrome.options import Options
9+
from selenium.webdriver.common import utils
10+
11+
test_dir = os.path.dirname(os.path.abspath(__file__))
12+
chrome_options = Options()
13+
chrome_options.add_argument("nwapp=" + test_dir)
14+
15+
port = str(utils.free_port())
16+
17+
pkgjson = '''
18+
{
19+
"name": "cookie-lost-remote-page",
20+
"main": "main.js",
21+
"port": "%s"
22+
}
23+
''' % port
24+
25+
with open(os.path.join(test_dir, 'package.json'), 'w') as bg:
26+
bg.write(pkgjson)
27+
28+
driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
29+
time.sleep(1)
30+
driver.implicitly_wait(2)
31+
try:
32+
wait_window_handles(driver, 1)
33+
switch_to_app(driver)
34+
print driver.current_url
35+
driver.find_element_by_id('get-cookie').click()
36+
result = driver.find_element_by_id('result').get_attribute('innerHTML')
37+
print result
38+
assert('sid=1' in result)
39+
finally:
40+
driver.quit()

0 commit comments

Comments
 (0)