Make a note of the profile setting that you stumbled upon when writing a test with Selenium. I'm new to Python, so please understand that the code may be strange.
The construction procedure is written on many sites, so I will omit it.
I referred to the following site. http://treeapps.hatenablog.com/entry/2014/10/16/015439 https://pypi.python.org/pypi/selenium
It is a configuration of Xvfb + FireFox + Selenium (2.44.0).
If the User-Agent is identified and the pages optimized for the PC / smartphone are sorted out, a test that specifies the UA (mobile Safari, etc.) of the smartphone is required. Also, when running the test in the development environment, I think that it may be an oleore certificate. In order to support these, the initial setting of profile is required.
sp_webdriver.py
import os
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
profile = FirefoxProfile()
#Ignore certificate warnings
profile.set_preference("webdriver_assume_untrusted_issuer", False)
profile.set_preference("webdriver_accept_untrusted_certs", True)
profile.accept_untrusted_certs = True
# User-Agent rewriting(Example: iOS 8.0)
profile.set_preference("general.useragent.override", "Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A365 Safari/600.1.4")
profile.update_preferences()
driver = webdriver.Firefox(profile)
I will write a test case. When actually writing, I think it is good to create a base class and then implement only the test part.
TestCase.py
# -*- coding: utf-8 -*-
import sys, os
import datetime
from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
import unittest
class TestCase(unittest.TestCase):
def setUp(self):
profile = FirefoxProfile()
profile.set_preference("webdriver_assume_untrusted_issuer", False)
profile.set_preference("webdriver_accept_untrusted_certs", True)
profile.accept_untrusted_certs = True
profile.set_preference("general.useragent.override", "Mozilla/5.0 (iPhone; CPU iPhone OS 8_0 like Mac OS X) AppleWebKit/600.1.4 (KHTML, like Gecko) Version/8.0 Mobile/12A365 Safari/600.1.4")
profile.update_preferences()
self.driver = webdriver.Firefox(profile)
self.base_url = "http://m.finance.yahoo.co.jp/"
self.driver.implicitly_wait(30)
self.accept_next_alert = True
def ssAssertEquals(self, left, right):
try:
#Take a screenshot on assertionError
self.assertEqual(left, right)
except AssertionError, e:
now = datetime.datetime.now()
self.driver.save_screenshot("/var/log" + self.__class__.__name__ + "_" + now.strftime("%Y%m%d%H%M%S") + ".png ")
raise e
def test_sitetop(self):
self.driver.get(self.base_url)
self.ssAssertEquals(u"Y!finance", self.driver.title)
def tearDown(self):
self.driver.quit()
if __name__ == "__main__":
unittest.main()
Recommended Posts