LoginSignup
16
9

More than 5 years have passed since last update.

SeleniumとSikuliのMIX

Last updated at Posted at 2017-02-28

テスト遂行自動化の方式は二つにわかれます。

  1. オブジェクト方式:Selenium
  2. イメージ認識方式:Sikulix

この投稿ではSikulixとSeleniumを混ぜてSikulixの中にSeleniumを使います。※

SikulixはJython2.7を含めているので以下の環境が必要です。

  • Python2.7x
  • Selenium lib for Python
  • Sikulix - IDE

今からはSikulixスクリプトでSeleniumをimportする方式で作業をします。

まずSeleniumのPython駆動スクリプトを作成します。

# -*- 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 Yahoo(unittest.TestCase):
    def setUp(self):
        chromeDriver = "/Users/SJoonKim/Desktop/sikuli/chromedriver"
        self.driver = webdriver.Chrome(chromeDriver)
        self.driver.implicitly_wait(30)
        self.base_url = "http://www.yahoo.co.jp/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_yahoo(self):
        driver = self.driver
        driver.get(self.base_url + "/?gws_rd=ssl")
        driver.find_element_by_id("srchtxt").clear()
        driver.find_element_by_id("srchtxt").send_keys(u"Trump")
        driver.find_element_by_id("srchtxt").send_keys(Keys.RETURN)
        time.sleep(10)

    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()

yahoo.co.jpで「Trump」を検索するスクリプトです。

もし確認したい結果がTrumpさんの写真であれば、Seleniumで画像検査はできません。その時にはSikulixの力を貸さなければなりません。

Sikulix IDEをたちあげて上のスクリプトをコピペしましょう。

そのまま遂行をすればエラーが発生します。

SikulixはPCにあるPython環境ではなくて自分のJython環境で動くので、Selenium libが認識できません。
PCにあるPython環境を認識させるコードを書き入れます。

# -*- coding: utf-8 -*-

#### 認識のためいれたコード ####
import sys
#loading python site packages selenium
#Pathはpython2.7が設置された所です。
installedSitePakagePath = "/Library/Python/2.7/site-packages/"
sys.path.append(installedSitePakagePath)


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 Yahoo(unittest.TestCase):
    def setUp(self):
        chromeDriver = "/自分のPC/chromedriver"
        self.driver = webdriver.Chrome(chromeDriver)
        self.driver.implicitly_wait(30)
        self.base_url = "http://www.yahoo.co.jp/"
        self.verificationErrors = []
        self.accept_next_alert = True

    def test_yahoo(self):
        driver = self.driver
        driver.get(self.base_url + "/?gws_rd=ssl")
        driver.find_element_by_id("srchtxt").clear()
        driver.find_element_by_id("srchtxt").send_keys("Trump")
        driver.find_element_by_id("srchtxt").send_keys(Keys.RETURN)

        #Sikulixを利用する部分。イメージに合う部分を探す。
        find("1488176279975.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)

#Sikulixではmain()を使えません。代わりに以下のように変更

suite = unittest.TestLoader().loadTestsFromTestCase(Yahoo)
unittest.TextTestRunner(verbosity=2).run(suite)

コードの中で

find("1488176279975.png")

部分はSikulixIDEでこんなに見えます。

스크린샷 2017-02-28 오전 10.38.29.png

IDEでyahoo.sikuliをsaveします。
遂行はSikulixIDEじゃなくてCommand lineでしましょう。

java -jar sikulix.jar -r yahoo.sikuli

MacOSの場合はSikulix.appなかでjarファイルが存在します。

java -jar Sikulix.app/Contents/Java/sikulix.jar -r yahoo.sikuli

※ Seleniumの中でSikulixを使う方法もあるんですが、Pythonではちょっと複雑です。

16
9
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
16
9