Skip to content

Commit 34b380a

Browse files
Cong Liurogerwang
authored andcommitted
[test] added a test case for cookie lost after devtools closed
Reland previous patch in e603924
1 parent 07ccab6 commit 34b380a

File tree

4 files changed

+112
-1
lines changed

4 files changed

+112
-1
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
(function(){
2+
const http = require('http');
3+
let id = 1;
4+
5+
// Create an HTTP server
6+
var srv = http.createServer( (req, res) => {
7+
let headers = {'Content-Type': 'text/plain'};
8+
let cookies = req.headers['cookie'];
9+
if (!cookies) {
10+
headers['Set-Cookie'] = ['sid=' + (id++)];
11+
}
12+
res.writeHead(200, headers);
13+
res.end(`Request Cookie: ${cookies}`);
14+
});
15+
16+
srv.listen(nw.App.manifest.port, '127.0.0.1');
17+
})()
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<!DOCTYPE html>
2+
<html>
3+
<head>
4+
<meta charset="utf-8">
5+
<meta http-equiv="X-UA-Compatible" content="IE=edge">
6+
<title>index.html</title>
7+
</head>
8+
<body>
9+
<h1>Index</h1>
10+
<button id="open-devtools" onclick="openDevTools()">Open DevTools</button>
11+
<script>
12+
let button = document.createElement('button');
13+
let id = 1;
14+
button.onclick=()=>{
15+
var xhr = new XMLHttpRequest();
16+
xhr.open('GET', `http://localhost:${nw.App.manifest.port}/`, true);
17+
xhr.send();
18+
xhr.onload = ()=>{
19+
let output = document.createElement('h1');
20+
output.setAttribute('id', 'result-' + (id++));
21+
output.innerHTML = xhr.responseText;
22+
document.body.appendChild(output);
23+
};
24+
};
25+
button.setAttribute('id', 'get-cookie');
26+
button.innerHTML = 'Get Cookies';
27+
document.body.appendChild(button);
28+
29+
function openDevTools() {
30+
chrome.developerPrivate.openDevTools({renderProcessId: -1, renderViewId: -1, extensionId: chrome.runtime.id});
31+
}
32+
</script>
33+
</body>
34+
</html>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
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-devtools-close",
20+
"main": "index.html",
21+
"bg-script": "bg.js",
22+
"port": "%s"
23+
}
24+
''' % port
25+
26+
with open(os.path.join(test_dir, 'package.json'), 'w') as bg:
27+
bg.write(pkgjson)
28+
29+
idx = 1
30+
def click_expect(expected):
31+
global idx
32+
driver.find_element_by_id('get-cookie').click()
33+
result = driver.find_element_by_id('result-%s' % idx).get_attribute('innerHTML')
34+
idx += 1
35+
print result
36+
assert(expected in result)
37+
38+
driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
39+
driver.implicitly_wait(2)
40+
time.sleep(1)
41+
try:
42+
switch_to_app(driver)
43+
print driver.current_url
44+
click_expect('undefined')
45+
click_expect('sid=1')
46+
driver.find_element_by_id('open-devtools').click()
47+
print 'wait for devtools open'
48+
wait_window_handles(driver, 2)
49+
click_expect('sid=1')
50+
print 'close devtools'
51+
switch_to_devtools(driver, devtools_window=driver.window_handles[-1])
52+
driver.close()
53+
driver.switch_to_window(driver.window_handles[0])
54+
wait_window_handles(driver, 1)
55+
print 'devtools closed'
56+
click_expect('sid=1')
57+
click_expect('sid=1')
58+
finally:
59+
#time.sleep(50)
60+
driver.quit()

test/sanity/nw_util.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ def wait_switch_window_name(driver, name, timeout=60):
6060

6161
def switch_to_app(driver, window_handle=None):
6262
def is_app_url(url):
63-
return url.startswith('chrome-extension://') or url.startswith('file://') or url.startswith('http://') or url.startswith('https://')
63+
return (url.startswith('chrome-extension://') or url.startswith('file://') or url.startswith('http://') or url.startswith('https://')) and not url.endswith('/_generated_background_page.html')
6464

6565
if window_handle is not None:
6666
driver.switch_to_window(window_handle)

0 commit comments

Comments
 (0)