Skip to content

Commit 76d8fc9

Browse files
committed
[py] Fix cookie tests for PhantomJS and other improvements
1 parent 32ee671 commit 76d8fc9

1 file changed

Lines changed: 43 additions & 71 deletions

File tree

py/test/selenium/webdriver/common/cookie_tests.py

Lines changed: 43 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -18,104 +18,76 @@
1818
import calendar
1919
import time
2020
import random
21-
import pytest
2221

23-
from test.selenium.webdriver.common import utils
22+
import pytest
2423

2524

26-
COOKIE_A = {
27-
"name": "foo",
28-
"value": "bar",
29-
"path": "/",
30-
"secure": False}
25+
@pytest.fixture
26+
def cookie(webserver):
27+
cookie = {
28+
'name': 'foo',
29+
'value': 'bar',
30+
'domain': webserver.host,
31+
'path': '/',
32+
'secure': False}
33+
return cookie
3134

3235

3336
@pytest.fixture(autouse=True)
3437
def pages(request, driver, pages):
35-
pages.load("simpleTest.html")
36-
37-
def fin():
38-
driver.delete_all_cookies()
39-
request.addfinalizer(fin)
40-
return pages
38+
pages.load('simpleTest.html')
39+
yield pages
40+
driver.delete_all_cookies()
4141

4242

4343
class TestCookie(object):
4444

45-
def testAddCookie(self, driver):
46-
if driver.capabilities['browserName'] == 'phantomjs' and driver.capabilities['version'].startswith('2.1'):
47-
pytest.xfail("phantomjs driver 2.1 broke adding cookies")
48-
driver.execute_script("return document.cookie")
49-
driver.add_cookie(COOKIE_A)
50-
cookie_returned = str(driver.execute_script("return document.cookie"))
51-
assert COOKIE_A["name"] in cookie_returned
52-
53-
def testAddingACookieThatExpiredInThePast(self, driver):
54-
if driver.capabilities['browserName'] == 'phantomjs' and driver.capabilities['version'].startswith('2.1'):
55-
pytest.xfail("phantomjs driver 2.1 broke adding cookies")
45+
def testAddCookie(self, cookie, driver):
46+
driver.add_cookie(cookie)
47+
returned = driver.execute_script('return document.cookie')
48+
assert cookie['name'] in returned
49+
50+
def testAddingACookieThatExpiredInThePast(self, cookie, driver):
5651
if driver.name == 'internet explorer':
5752
pytest.skip("Issue needs investigating")
58-
cookie = COOKIE_A.copy()
59-
cookie["expiry"] = calendar.timegm(time.gmtime()) - 1
60-
driver.add_cookie(cookie)
61-
cookies = driver.get_cookies()
62-
assert 0 == len(cookies)
53+
expired = cookie.copy()
54+
expired['expiry'] = calendar.timegm(time.gmtime()) - 1
55+
driver.add_cookie(expired)
56+
assert 0 == len(driver.get_cookies())
6357

64-
def testDeleteAllCookie(self, driver):
65-
if driver.capabilities['browserName'] == 'phantomjs' and driver.capabilities['version'].startswith('2.1'):
66-
pytest.xfail("phantomjs driver 2.1 broke adding cookies")
67-
driver.add_cookie(utils.convert_cookie_to_json(COOKIE_A))
58+
def testDeleteAllCookie(self, cookie, driver):
59+
driver.add_cookie(cookie)
6860
driver.delete_all_cookies()
6961
assert not driver.get_cookies()
7062

71-
def testDeleteCookie(self, driver):
72-
if driver.capabilities['browserName'] == 'phantomjs' and driver.capabilities['version'].startswith('2.1'):
73-
pytest.xfail("phantomjs driver 2.1 broke adding cookies")
74-
driver.add_cookie(utils.convert_cookie_to_json(COOKIE_A))
75-
driver.delete_cookie("foo")
63+
def testDeleteCookie(self, cookie, driver):
64+
driver.add_cookie(cookie)
65+
driver.delete_cookie('foo')
7666
assert not driver.get_cookies()
7767

7868
def testShouldGetCookieByName(self, driver):
79-
key = "key_%d" % int(random.random() * 10000000)
69+
key = 'key_{}'.format(int(random.random() * 10000000))
8070
driver.execute_script("document.cookie = arguments[0] + '=set';", key)
81-
8271
cookie = driver.get_cookie(key)
83-
assert "set" == cookie["value"]
84-
85-
def testGetAllCookies(self, driver, pages):
86-
if driver.capabilities['browserName'] == 'phantomjs' and driver.capabilities['version'].startswith('2.1'):
87-
pytest.xfail("phantomjs driver 2.1 broke adding cookies")
88-
key1 = "key_%d" % int(random.random() * 10000000)
89-
key2 = "key_%d" % int(random.random() * 10000000)
72+
assert 'set' == cookie['value']
9073

74+
def testGetAllCookies(self, cookie, driver, pages, webserver):
9175
cookies = driver.get_cookies()
9276
count = len(cookies)
9377

94-
one = {"name": key1,
95-
"value": "value"}
96-
two = {"name": key2,
97-
"value": "value"}
78+
for i in range(2):
79+
cookie['name'] = 'key_{}'.format(int(random.random() * 10000000))
80+
driver.add_cookie(cookie)
9881

99-
driver.add_cookie(one)
100-
driver.add_cookie(two)
82+
pages.load('simpleTest.html')
83+
assert count + 2 == len(driver.get_cookies())
10184

102-
pages.load("simpleTest.html")
103-
cookies = driver.get_cookies()
104-
assert count + 2 == len(cookies)
105-
106-
def testShouldNotDeleteCookiesWithASimilarName(self, driver):
107-
if driver.capabilities['browserName'] == 'phantomjs' and driver.capabilities['version'].startswith('2.1'):
108-
pytest.xfail("phantomjs driver 2.1 broke adding cookies")
109-
cookieOneName = "fish"
110-
cookie1 = {"name": cookieOneName,
111-
"value": "cod"}
112-
cookie2 = {"name": cookieOneName + "x",
113-
"value": "earth"}
114-
driver.add_cookie(cookie1)
85+
def testShouldNotDeleteCookiesWithASimilarName(self, cookie, driver, webserver):
86+
cookie2 = cookie.copy()
87+
cookie2['name'] = '{}x'.format(cookie['name'])
88+
driver.add_cookie(cookie)
11589
driver.add_cookie(cookie2)
116-
117-
driver.delete_cookie(cookieOneName)
90+
driver.delete_cookie(cookie['name'])
11891
cookies = driver.get_cookies()
119-
120-
assert cookie1["name"] != cookies[0]["name"], str(cookies)
121-
assert cookie2["name"] == cookies[0]["name"], str(cookies)
92+
assert cookie['name'] != cookies[0]['name']
93+
assert cookie2['name'] == cookies[0]['name']

0 commit comments

Comments
 (0)