LoginSignup
0
0

More than 5 years have passed since last update.

Selenium2(WebDriver)で画面の出力結果が影響を受けてないことをテストしてみた

Last updated at Posted at 2018-01-12

Seleniumでどんなことができるか遊んでみたのでメモ。

環境:

Windows OS
python 3.6.1
Selenium 3.8.1(python)

事前準備:

1.pythonは入っていたので、pipでseleniumのinstall
>pip install -U selenium
2.検査に使用するwebdriverのバイナリを取得
chrome用:https://sites.google.com/a/chromium.org/chromedriver/downloads
今回はお試しなのでChrome特化で作ってるが、本来はUAを複数指定できるような汎化を行いたい。

実際に書いたコード:

exam.py
from selenium import webdriver
from filecmp import cmp

#Constants
BASE_FILE = "base.png"
EXAM_FILE = "test.png"
CHROME_DRV = "C:\selenium\drivers\chromedriver"
BASE_URI = "http://localhost/original/"
EXAM_URI = "http://localhost/revised/"

#Chrome
def getExamScreen(target_uri, export_file, ua):
    driver = webdriver.Chrome(executable_path = ua)

    driver.set_window_size(800,600)
    driver.get(target_uri)
    driver.get_screenshot_as_file(export_file)

    driver.close() 

def chkNotDiff():
    if(cmp(BASE_FILE, EXAM_FILE)):
        print("Same!")
    else:
        print("Different!")


#original env
getExamScreen(BASE_URI, BASE_FILE, CHROME_DRV)

#changed env
getExamScreen(EXAM_URI, EXAM_FILE, CHROME_DRV)

chkNotDiff()

ポイントは、プログラムの修正結果が表示に影響を与えてないのであれば、変更前に描画された画面のスクリーンショット(base.png)と、修正後に描画されたスクリーンショットの内容(test.png)は等価である、という仮説である。
このためには現状のスクリーンショットも評価用の画面も同じ条件でファイル作成する必要があるので、seleniumで取得している。
現状サイトが準備できない場合は、テスト用のbase.pngをあらかじめ検証環境を利用して作成して事前準備して、original envとなっている処理を行わなければ問題ないだろう。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0