LoginSignup
0
2

More than 3 years have passed since last update.

【ツール配布】安全確認回避、ポップアップ回避のseleniumライブラリ

Last updated at Posted at 2019-11-28

コード

github:pyAutoTest

目的

やりたいこと

  • seleniumをシナリオで動かしたい
    • seleniumはコード書く必要あるが、毎回コードを書きたくない
      • 他部署の非エンジニアでも使えるようにしたい
    • ブラウザでコード出力できるが、毎回手作業したくない
  • システム毎に改修する部分は一ヶ所にまとめたい
  • デバッグ中にオレオレ証明書というだけで止めたくない
  • クライアント証明書提出で止めたくない

後回しでいいこと

  • 処理速度
    • 夜間に走らせるため
  • 結果確認の自動化
    • 証拠残す必要もあるのでスクショの確認でOK
    • 今後の課題

実装

オレオレ証明書回避

コード

def avoid_security_check(self):
    ok_flg = True

    if self.__driver.title:
        # ie
        if self.__driver.title == "このサイトは安全ではありません":
            ok_flg  = self.click("moreInfoContainer")

            ok_flg &= self.click("overridelink")

            time.sleep(0.5)

        # edge
        elif self.__driver.title == "証明書エラー: ナビゲーションはブロックされました":
            ok_flg  = self.click("moreInformationDropdownSpan")

            ok_flg &= self.click("invalidcert_continue")

            time.sleep(0.5)

    return ok_flg

説明

  • ページタイトルを確認して、忠告していればOK
    • 遷移に時間かかるため、sleep必須

クライアント証明書提出

コード

def cert(self, cmd):
    self.__pki_flg = False

    ok_flg = True
    with concurrent.futures.ThreadPoolExecutor(max_workers=2) as executor:
        futures = []
        futures.append(executor.submit(self.post_cert_chrome, cmd))
        futures.append(executor.submit(self.submit, cmd))
        for future in concurrent.futures.as_completed(futures):
            ok_flg &= future.result()

    return ok_flg

def submit(self):
    ok_flg  = self.click("submit")

    return ok_flg

def post_cert_chrome(self, cmd):
    # cmd check is practiced
    if cmd == "ok":
        ok_flg = self.move_click("img/chrome/ok.png")

    elif cmd == "cancel":
        ok_flg = self.move_click("img/chrome/cancel.png")

    elif cmd.isdecimal():
        diff = 80 + (int(cmd) * 40)
        ok_flg  = self.move_click("img/chrome/next.png", 0, diff)

        ok_flg &= self.move_click("img/chrome/ok.png")

    return ok_flg

def move_click(self, img_path, x=0, y=0, seconds=0.2):
    # search from image
    pos, ok_flg = self.search_from_image(img_path)
    if ok_flg:
        # move mouse
        pyautogui.moveTo(pos.x + x, pos.y + y, seconds)
        # click
        pyautogui.click()

    return ok_flg

def search_from_image(self, img_path):
    # check image
    if not os.path.isfile(img_path):
        self.__log.printLog(LOG_LEVEL.EEROR, "failed to setting")
        return None, False

    i = 30
    while i > 0:
        i -= 1

        # get position
        pos = pyautogui.locateCenterOnScreen(img_path)

        if pos:
            break

        time.sleep(0.2)

    if not pos:
        return None, False

    return pos, True

説明

  • ポップアップ中はseleniumが止まるため、並列処理
    • selenium
      • ポップアップ出現のトリガを行う
    • pyautogui
      • ポップアップ画像を検索し、押下
        • ポップアップ出現までラグのある場合も考慮し、指定時間内はループ

まとめ

  • メリット
    • サンプルとしての有用性はある
      • 安全確認の回避
      • ポップアップの回避
  • 今後の課題
    • 結果確認が目視のため、自動化したい
    • ポップアップ対応のため、不都合がある
      • 画像検索するため非表示実行できない
      • ツールでマウス操作する場面があるため、テスト中にPC操作ができない
0
2
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
2