Skip to content

Commit 3c61ca9

Browse files
committed
[test] add nwuseragent case for nwjs#5397
1 parent 98bae2b commit 3c61ca9

File tree

3 files changed

+76
-0
lines changed

3 files changed

+76
-0
lines changed
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/env python
2+
3+
from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
4+
import sys
5+
6+
class RequestHandler(BaseHTTPRequestHandler):
7+
8+
def do_GET(self):
9+
10+
request_path = self.path
11+
12+
print(self.headers['User-Agent'])
13+
14+
self.send_response(200)
15+
self.send_header("Set-Cookie", "foo=bar")
16+
17+
def log_request(self, code):
18+
pass
19+
20+
def main():
21+
port = int(sys.argv[1])
22+
print('Listening on localhost:%s' % port)
23+
server = HTTPServer(('', port), RequestHandler)
24+
server.serve_forever()
25+
26+
27+
if __name__ == "__main__":
28+
main()

test/sanity/nwuseragent/package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"name":"test-nwuseragent",
3+
"main":"index.html"
4+
}

test/sanity/nwuseragent/test.py

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
import time
2+
import os
3+
import subprocess
4+
5+
from selenium import webdriver
6+
from selenium.webdriver.chrome.options import Options
7+
from selenium.webdriver.common import utils
8+
9+
chrome_options = Options()
10+
chrome_options.add_argument("nwapp=" + os.path.dirname(os.path.abspath(__file__)))
11+
12+
testdir = os.path.dirname(os.path.abspath(__file__))
13+
os.chdir(testdir)
14+
15+
port = str(utils.free_port())
16+
server = subprocess.Popen(['python', '-u', 'echo-user-agent.py', port], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
17+
print server.stdout.readline()
18+
19+
html = open('index.html', 'w')
20+
html.write('''
21+
<!DOCTYPE html>
22+
<html lang="en">
23+
<head>
24+
<meta charset="utf8">
25+
<title>test</title>
26+
</head>
27+
<body>
28+
<iframe id="iframe" nwuseragent="test-agent" src="http://localhost:%s/iframe.html"></iframe>
29+
</body>
30+
</html>
31+
''' % (port))
32+
33+
html.close()
34+
35+
driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
36+
try:
37+
print driver.current_url
38+
time.sleep(1)
39+
user_agent = server.stdout.readline()
40+
print "user agent: " + user_agent
41+
server.terminate()
42+
assert("test-agent" in user_agent)
43+
finally:
44+
driver.quit()

0 commit comments

Comments
 (0)