pip installer sélénium
phantomjs
souhaités_capabilities
driver.quit ()
à la fin.test.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import os
import unittest
import commands
from selenium import webdriver
from selenium.common.exceptions import NoSuchElementException
USER_AGENT = "Mozilla/5.0 (iPhone; CPU iPhone OS 7_1_2 like Mac OS X) AppleWebKit/537.51.2 (KHTML, like Gecko) Version/7.0 Mobile/11D257 Safari/9537.53"
def unittest_phantomjs_webdriver_decorator(klass):
def __setUpClass(cls):
(status, phantomjs_path) = commands.getstatusoutput("which phantomjs")
if not status == 0:
raise StandardError("phantomjs is not found!!")
cls.driver = webdriver.PhantomJS(
executable_path=phantomjs_path,
service_log_path="/dev/stdout",
desired_capabilities={
'phantomjs.page.settings.userAgent': USER_AGENT,
},
#En passant par un proxy
#service_args=[
# '--proxy=127.0.0.1:9999',
# '--proxy-type=http',
#],
)
cls.driver.set_window_size(1400, 1000)
cls.driver.implicitly_wait(1)
def __tearDownClass(cls):
cls.driver.quit()
klass.setUpClass = classmethod(__setUpClass)
klass.tearDownClass = classmethod(__tearDownClass)
return klass
@unittest_phantomjs_webdriver_decorator
class AccessTest(unittest.TestCase):
def test_google(self):
driver = self.driver
url = """http://google.com"""
driver.get(url)
driver.save_screenshot("google.png ")
def test_amazon(self):
driver = self.driver
url = """http://amazon.com"""
driver.get(url)
driver.save_screenshot("amazon.png ")
if __name__ == "__main__":
unittest.main()
Recommended Posts