- 追加された行はこの色です。
- 削除された行はこの色です。
* 目次 [#z23d6087]
#contents
* セットアップ [#sbf0d636]
webdriverは、現在利用しているブラウザとバージョンを合わせる必要がある。
仮にこの記事ではc:\binフォルダを作成し
環境変数のPATHで上記のパスを追加した。
* 基本コード [#n0139e9b]
簡単なサンプルコードを示す
test_base.pyとして保存
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
# ヘッドレスモードのオプションを設定
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu') # GPUハードウェアアクセラレーションを無効化
# ChromeドライバーのパスをServiceオブジェクトとして指定
service = Service('c:\\bin\\chromedriver.exe')
# Serviceオブジェクトを使用してChromeドライバーのインスタンスを作成
# driver = webdriver.Chrome(service=service)
driver = webdriver.Chrome(service=service, options=options)
# ウェブサイトにアクセス
driver.get('https://www.yahoo.co.jp/')
search_box = driver.find_element(By.NAME, 'p')
search_box.send_keys('Python')
search_box.send_keys(Keys.RETURN)
# 検索結果ページの特定の要素(例えば、特定のクラス名を持つ要素)が表示されるまで待機
WebDriverWait(driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'sw-CardBase')) # Yahoo Japan検索結果ページの特定のクラス名
)
# ページの高さを取得
total_height = driver.execute_script("return document.body.parentNode.scrollHeight")
# ウィンドウサイズをページの高さに合わせる
driver.set_window_size(1920, total_height)
# スクリーンショットの取得と保存
driver.save_screenshot('screenshot.png')
# ウェブページのタイトルを取得して表示
print(driver.title)
# ブラウザを閉じる
driver.quit()
* テスト用にブラッシュアップする [#j4a4e5e3]
** 実行方法 [#t25d5e38]
pythonの標準テストフレームワークであるunittestは以下のコマンドラインでテスト可能である。
*** すべてのテストケースの実行方法 [#b20cd0fe]
テストケースのあるフォルダで、
python -m unittest
*** 部分的なテストケースの実行方法 [#n99e4223]
上記のコマンドに続けて、以下のように記載する
python -m unittest test_yahoo_search.YahooSearchTest
** テストコードのベースクラス [#s5bea9f6]
test_yahoo_search.py
test_base.pyとして保存
import unittest
import os
import time
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.chrome.service import Service
class TestBase(unittest.TestCase):
def setUp(self):
# ヘッドレスモードでChromeを起動するためのオプションを設定
options = Options()
options.add_argument('--headless')
options.add_argument('--disable-gpu')
service = Service('c:\\bin\\chromedriver.exe')
self.driver = webdriver.Chrome(service=service, options=options)
def take_screenshot(self, test_method_name):
# 現在の時刻を取得してタイムスタンプを作成
timestamp = time.strftime("%Y%m%d-%H%M%S")
# スクリーンショットの保存ディレクトリを作成(存在しない場合)
directory = f'screenshots/{test_method_name}'
if not os.path.exists(directory):
os.makedirs(directory)
# スクリーンショットのファイル名を設定して保存
screenshot_file = f'{directory}/screenshot-{timestamp}.png'
self.driver.save_screenshot(screenshot_file)
def take_screenshot_defaultsize(self, test_method_name):
# ウィンドウサイズを合わせる
driver.set_window_size(1920, 1080)
self.driver.save_screenshot(screenshot_file)
def take_screenshot_allheight(self, test_method_name):
# ページの高さを取得
total_height = driver.execute_script("return document.body.parentNode.scrollHeight")
# ウィンドウサイズをページの高さに合わせる
driver.set_window_size(1920, total_height)
take_screenshot(self, test_method_name)
def tearDown(self):
# ブラウザを閉じる
self.driver.quit()
** TestBaseを利用したテストケース [#n8ed68a5]
** テストベースを利用したテストケース [#n8ed68a5]
テストベースを利用することで、テストで使う共通処理を再利用できる
test_yahoo_search.py
from test_base import TestBase
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
class YahooSearchTest(TestBase):
def test_search_in_yahoo_japan(self):
# Yahoo Japanのページにアクセス
self.driver.get('https://www.yahoo.co.jp/')
# 検索ボックスを見つけて、キーワードを入力して検索を実行
search_box = self.driver.find_element(By.NAME, 'p')
search_box.send_keys('Python')
search_box.send_keys(Keys.RETURN)
# 検索結果が表示されるのを待機
WebDriverWait(self.driver, 10).until(
EC.presence_of_element_located((By.CLASS_NAME, 'sw-CardBase'))
)
# スクリーンショットを取得して保存
self.take_screenshot_allheight(self.__class__.__name__ + '.' + self._testMethodName)
# その他のテストコード...
# その他のテストケース...
* git bashで文字化けする場合の対応方法 [#t2ebd4ab]
pythonをgitbashで使うと、windowsの場合は文字化けしていが、修正方法がある。しかし、よくネットで出てくる修正方法が古く、今時点での修正方法はコントロールパネルの地域からβ版扱いのオプションにチェックを入れる必要がある。
pythonをgitbashで使うと、windowsの場合は文字化けしていが、修正方法がある。しかし、よくネットで出てくる修正方法が古いのか、今時点での修正方法はコントロールパネルの地域からβ版扱いのオプションにチェックを入れる必要がある。
&ref(スクリーンショット 2023-12-10 115206.png);
&ref(スクリーンショット 2023-12-10 115425.png);