I’m currently trying to set up a cronjob to do end-to-end autotesting of the OAuth code I wrote for Tandem Exchange, which is occasionally failing these days due to the OAuth provider systems being unavailable or very very slow (which is the same thing). It’s actually one of the biggest problems in web authentication actually, the fact that once you do start relying on OAuth providers to authenticate users on your website, you inherit a critical dependency on their servers always being reachable. (In our case, I’ve been seeing stupid downtimes by QQ, one of the largest Chinese social networks.) And it’s not something you can test with mock objects.
Set up a virtualenv:
1
2
3
4
|
$ mkdir webtest
$ virtualenv --no-site-packages webtest
$ cd webtest
$ . bin/activate
|
Install Selenium in the virtualenv:
1
2
3
4
5
6
7
8
9
10
|
(webtest)[webtest]$ pip install selenium
Downloading/unpacking selenium
Downloading selenium-2.35.0.tar.gz (2.6MB): 2.6MB downloaded
Running setup.py egg_info for package selenium
Installing collected packages: selenium
Running setup.py install for selenium
Successfully installed selenium
Cleaning up...
|
Download and untar the PhantomJS executable somewhere on the PATH:
1
2
3
4
5
6
7
8
9
10
11
|
$ wget https://phantomjs.googlecode.com/files/phantomjs-1.9.1-linux-x86_64.tar.bz2
--2013-09-03 16:09:27-- https://phantomjs.googlecode.com/files/phantomjs-1.9.1-linux-x86_64.tar.bz2
Resolving phantomjs.googlecode.com... 2607:f8b0:4003:c01::52, 173.194.77.82
Connecting to phantomjs.googlecode.com|2607:f8b0:4003:c01::52|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 13301808 (13M) [application/x-bzip2]
Saving to: “phantomjs-1.9.1-linux-x86_64.tar.bz2”
100%[================================================================================================================>] 13,301,808 5.13M/s in 2.5s
2013-09-03 16:09:30 (5.13 MB/s) - “phantomjs-1.9.1-linux-x86_64.tar.bz2” saved [13301808/13301808]
|
During the first attempts at using PhantomJS, I had problems with SSL-based addresses always returning a .current_url
of “about:blank
“, which you have to fix using a somewhat obscure flag in the webdriver.PhantomJS() constructor.
The fix looks like this, in Python:
1 |
driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true'])
|
And when the unit test runs (via nosetests):
1
2
3
4
5
6
7
8
9
10
11
12
|
(webtest)[webtest]$ nosetests -v --ipdb
Try logging in with Facebook Test Account that has already authorized TX. ... ok
Try logging in with Google Test Account that has already authorized TX. ... ok
Try logging in with Qzone Test Account that has already authorized TX. ...
- TRACEBACK --------------------------------------------------------------------
Traceback (most recent call last):
File "/usr/local/lib/python2.7/unittest/case.py", line 331, in run
testMethod()
File "/home/webtest/test.py", line 132, in test_oauth_with_cleanup
WebDriverWait(self.driver, 10).until(EC.presence_of_element_located((By.ID, 'u')))
File "/home/webtest/lib/python2.7/site-packages/selenium/webdriver/support/wait.py", line 71, in until
raise TimeoutException(message)
|
For the most part the Python-based selenium module works, but it is pretty verbose, as it sticks to the original Webdriver API very closely. There are higher-level abstractions, such as Splinter, but I get the impression that making sure it starts PhantomJS properly will be an ordeal in and of itself. I’ve gotten Facebook and Google OAuth testing working headlessly, which is pretty cool, but the next step of getting the QZone OAuth test working is getting jammed up on the fact that QZone is behaving exactly as users see it behave (which is to say, problematically).
But then, that’s the objective of the unit testing, to reveal the source of the latest OAuth problems.
Actually, I’ve also noticed that it’s pretty slow not only from the shared-hosting server we’re using, but also from where I am currently located. So two different points on the globe. I can’t help but wonder how much the Great Firewall of China is slowing things down.
Thanks for the helpful tip. I’m using phantomjs to access https through splinter and a small modification of your tip worked for me.
Instead of initializing the webdriver with
browser = Browser(‘phantomjs’)
initialize it with:
browser = Browser(‘phantomjs’, service_args=[‘–ssl-protocol=any’])
and it works when you have sslv3 disabled.