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:
$ mkdir webtest
$ virtualenv --no-site-packages webtest
$ cd webtest
$ . bin/activate
Install Selenium in the virtualenv:
(webtest)[webtest]$ pip install 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
Download and untar the PhantomJS executable somewhere on the PATH:
$ 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, 220.127.116.11
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:
driver = webdriver.PhantomJS(service_args=['--ignore-ssl-errors=true'])
And when the unit test runs (via nosetests):
(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
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
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.