UIテストするのにSeleniumとCypress(サイプレス)が最近のはやりの模様。
Seleniumを使うことにしたんですが、以下理由。
Cypressはセットアップが楽っていう評判なんだけど、Seleniumも別に大変じゃなかった。自分はChrome上で使う前提。スクリプトの言語はPythonで。
・Cypress================================== #インスト cd /your/project/path npm install cypress --save-dev #実行 ./node_modules/.bin/cypress open ・Selenium================================== #①Chromeのインスト vi /etc/yum.repos.d/google-chrome.repo #↑の中身 [google-chrome] name=google-chrome baseurl=http://dl.google.com/linux/chrome/rpm/stable/x86_64 enabled=1 gpgcheck=1 gpgkey=https://dl.google.com/linux/linux_signing_key.pub #libOSMesa は CentOS7 でクラッシュする問題に対応するために入れた方がいいらしい。 yum -y install google-chrome-stable libOSMesa #確認 google-chrome --version #②ChromeDriverとフォントのインストール yum -y install libX11 GConf2 fontconfig #ChromeDriver持ってくる #https://sites.google.com/a/chromium.org/chromedriver/downloadsのLastReleaseページ wget https://chromedriver.storage.googleapis.com/2.41/chromedriver_linux64.zip unzip chromedriver_linux64.zip mv chromedriver /usr/local/bin/ # IPA Fonts のインストール yum -y install ipa-gothic-fonts ipa-mincho-fonts ipa-pgothic-fonts ipa-pmincho-fonts fc-cache -fv #③Python3.6環境でseleniumのインスト #仮想環境作る python3.6 -m venv selenv #仮想環境に切り替え source bin/activate #seleniumのインスト pip install selenium
もちろんCypressの方が圧倒的に楽なんだけど、Seleniumも言うほど大変でもない。
Cypress使ってみて思ったのはスクリプト書くのが結構つらい。最初、Cypressのツール上でスクリプト書けるのかと思ってたんだけど、「好きなIDEでスクリプト書いてね♪」って公式に書いてあった。んーせめて、ページの要素のIDとかをコピペ出来たりみたいなサポートツール的なサムシングエルスが欲しい。相当ページの内容把握してないとサラサラとはかけなさそう。
その他、いくつか制約もあるみたいなのでちょっと微妙な気がする。性能が良いっていうのも見たけど、両方使ってみていまのとこ大差ないと思う。更新一杯するような複雑なスクリプト書くと違うんだろうか。
Cypressはまだまだ発展途上的な感じがする。インターフェースとかは好き。今後に期待。スクリプトを書くのがもっと楽になればいい感じになりそう。てかもうあるのかな?探してみた感じだとなさそうなんだけども。。。
SeleniumはKatalon RecorderをChrome拡張で入れれば、Chromeの操作記録をそのままPythonスクリプトに出来るのでかなり楽。ただ、スクリプトの中身、DOMの指定するとことかが正直微妙?
拡張性というかコードの保守していくとすぐ動かなくなりそう。そのまま使うのは無理っぽい。ただ、指定の仕方を変えればいいだけなので、1から書くよりは十分効率的かな。
スクリプト変える部分は例えば↓の感じ。
# -*- 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 from selenium.webdriver.chrome.options import Options#ここが必要 import unittest, time, re class UntitledTestCase(unittest.TestCase): def setUp(self): #↓ここ options = Options() options.add_argument('--headless') options.add_argument('--no-sandbox') options.add_argument('--disable-gpu') options.add_argument('--window-size=1280,1024') #↑ここ #self.driver = webdriver.Firefox()#ここを↓の行にする self.driver = webdriver.Chrome(chrome_options=options)#ここ self.driver.implicitly_wait(30) self.base_url = "https://www.katalon.com/" self.verificationErrors = [] self.accept_next_alert = True def test_untitled_test_case(self): driver = self.driver driver.get("http://hoge.fuga.com/moe/") driver.save_screenshot('screenshot01.png') driver.find_element_by_link_text("Wiki").click() driver.save_screenshot('screenshot01.png') def is_element_present(self, how, what): try: self.driver.find_element(by=how, value=what) except NoSuchElementException as e: return False return True def is_alert_present(self): try: self.driver.switch_to_alert() except NoAlertPresentException as e: return False return True def close_alert_and_get_its_text(self): try: alert = self.driver.switch_to_alert() alert_text = alert.text if self.accept_next_alert: alert.accept() else: alert.dismiss() return alert_text finally: self.accept_next_alert = True def tearDown(self): self.driver.quit() self.assertEqual([], self.verificationErrors) if __name__ == "__main__": unittest.main() =========================== 実行は「python hoge.py」みたいな感じ。
やっぱcypress使う事にした。それはまた書く。