ksaitoの日記

日々試したことの覚え書き

Selenium2をpythonで使う

移転しました。

自動的にリダイレクトします。

Selenium2をpythonで使ってみました。

テストシナリオの作成

FirefoxSelenium IDEを使って操作をキャプチャします。 yahooで"test"を検索して画像検索に切替えるというシンプルな操作をキャプチャします。

f:id:ksaito11:20160724204351p:plain

エクスポートでpythonを出力します。

f:id:ksaito11:20160724204528p:plain

こんな感じでpythonのunittestのソースが出力されます。

PS C:\Users\Administrator\Documents\python> head sele.py
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re

class Sele(unittest.TestCase):
PS C:\Users\Administrator\Documents\python>

テストケースの実行

出力されたファイルを実行するとfirefoxが起動してシナリオを実行します。

PS C:\Users\Administrator\Documents\python> python sele.py
.
----------------------------------------------------------------------
Ran 1 test in 12.437s

OK
PS C:\Users\Administrator\Documents\python> 

インタプリタでの実行

インタプリタで実行します。

TestSuiteにして、2回実行してみます。

PS C:\Users\Administrator\Documents\python>> python
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec  5 2015, 20:40:30) [MSC v.1500 64 bit (AMD64)] on wi
Type "help", "copyright", "credits" or "license" for more information.
>>> import unittest
>>> import sele
>>> suite = unittest.TestLoader().loadTestsFromTestCase(sele.Sele)
>>> s2 = unittest.TestSuite([suite,suite])
>>> unittest.TextTestRunner(verbosity=1).run(s2)
..
----------------------------------------------------------------------
Ran 2 tests in 30.282s

OK
<unittest.runner.TextTestResult run=2 errors=0 failures=0>
>>>