Use selenium phantomjs webdriver with python unittest

Use selenium phantomjs webdriver with python unittest

liver

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,
            },
            #When passing through a 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

Use selenium phantomjs webdriver with python unittest
Use FireFox (via Homebrew-cask) with Selenium WebDriver Python bindings
[Python] Use JSON with Python
Scraping with Python + PhantomJS
Drive WebDriver with python
Use mecab with Python3
Use DynamoDB with Python
Use Python 3.8 with Anaconda
ScreenShot with Selenium (Python)
Use python with docker
Scraping with Selenium [Python]
Until you use PhantomJS with Python on Heroku
Use Trello API with python
Scraping with selenium in Python
Scraping with Selenium + Python Part 1
Use Twitter API with Python
Use TUN / TAP with Python
Python: Working with Firefox with selenium
Selenium WebDriver + Firefox49 (provisional) (Python)
Scraping with Selenium + Python Part 2
Challenge Python3 and Selenium Webdriver
Python: How to use async with
Use PointGrey camera with Python (PyCapture2)
Scraping with Selenium in Python (Basic)
Exclude specified cases with Python unittest
[Python] Use Basic/Digest authentication with Flask
Use NAIF SPICE TOOLKIT with Python
Use rospy with virtualenv in Python3
Use Python in pyenv with NeoVim
Use Windows 10 speech synthesis with Python
Use OpenCV with Python 3 in Window
Use PostgreSQL with Lambda (Python + psycopg2)
[Python, Selenium, PhantomJS] A story when scraping a website with lazy load
Get html from element with Python selenium
Use smbus with python3 under pyenv environment
Use DeepL with python (for dissertation translation)
WebUI test with Python2.6 + Selenium 2.44.0 --profile setting
[Python] Use string data with scikit-learn SVM
[Introduction to Python] Let's use foreach with Python
Use PIL and Pillow with Cygwin Python
Use cryptography library cryptography with Docker Python image
[Python2.7] Summary of how to use unittest
Reboot the router using Python, Selenium, PhantomJS
Use Application Insights with Python 3 (including bottles)
[Python] Automatically operate the browser with Selenium
Use C ++ functions from python with pybind11
Practice web scraping with Python and Selenium
Until you can use opencv with python
Use Python and MeCab with Azure Functions
Python + Selenium + Headless Chromium with aws lambda
Get the width of the div on the server side with Selenium + PhantomJS + Python
E2E test to select select box with CasperJS, Nightmare and Python + WebDriver + PhantomJS
Selenium, Phantomjs & BeautifulSoup4
Selenium + WebDriver (Chrome) + Python | Building environment for scraping
Scraping with selenium
[Python / Selenium] XPath
FizzBuzz with Python3
Scraping with Python
I want to use MATLAB feval with python
Statistics with python
Use additional Python packages with Serverless Framework (v1.x)