WebUI test with Python2.6 + Selenium 2.44.0 --profile setting

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.

Building the environment

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).

profile settings

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)

Write a test case

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

WebUI test with Python2.6 + Selenium 2.44.0 --profile setting
Primality test with Python
Primality test with python
ScreenShot with Selenium (Python)
Scraping with Selenium [Python]
Scraping with selenium in Python
Scraping with Selenium + Python Part 1
Python: Working with Firefox with selenium
Scraping with Selenium in Python
Scraping with Selenium + Python Part 2
[Automation with python! ] Part 1: Setting file
Scraping with Selenium in Python (Basic)
Scraping with Python, Selenium and Chromedriver
Unit test log output with python
Write selenium test code in python
Get html from element with Python selenium
[Python] Super easy test with assert statement
Stress Test with Locust written in Python
Test Python non-functional programs with GitLab CI
Generate Japanese test data with Python faker
Post Test 3 (Working with PosgreSQL in Python)
How to do portmanteau test with python
Integrating with setuptools / python setup.py test / pytest-runner
[Python] Automatically operate the browser with Selenium
Practice web scraping with Python and Selenium
Use selenium phantomjs webdriver with python unittest
Python + Selenium + Headless Chromium with aws lambda
Scraping with selenium
[Python / Selenium] XPath
FizzBuzz with Python3
Scraping with selenium ~ 2 ~
Scraping with Python
Statistics with python
Automatic operation of Chrome with Python + Selenium + pandas
Scraping with Python
Python with Go
Try running Google Chrome with Python and Selenium
Twilio with Python
Integrate with Python
Play with 2016-Python
AES256 with python
Tested with Python
Scraping with Selenium
Beginning with Selenium
python starts with ()
Log in to Yahoo Business with Selenium Python
Drag and drop local files with Selenium (Python)
Python VM profile
with syntax (Python)
Python Integrity Test
Bingo with python
Zundokokiyoshi with python
[Small story] Test image generation with Python / OpenCV
ARC037 Baum test politely with Python recursive function
Excel with Python
Microcomputer with Python
Cast with python
Automate UI testing with Selenium API | Crawling websites with python
Use FireFox (via Homebrew-cask) with Selenium WebDriver Python bindings
[Cloudian # 8] Try setting the bucket versioning with Python (boto3)
Install selenium on Mac and try it with python