3

Selenium IDEを使用してPythonにエクスポートされたSelenium Webdriverテストケースがあります。次に、いくつかの追加機能を追加したかったので、セレン テスト ケースの間にカスタム コードを挿入しました。

問題は、私が間違いを犯した場合、またはカスタム作成された python コードがエラーを返した場合でも、セレン テスト ケースは引き続き実行されることです。

そのようなエラーがある場合は、セレンのテストケースの実行を停止したいのですが。以下のシナリオを、単純な Google 検索と、それに続く shutil モジュールを使用したログ移動コードで複製しました。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
import unittest, time, re
import os, shutil

class SeleniumException(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome()
        self.driver.implicitly_wait(30)
        self.base_url = "https://www.google.co.in/"
        self.verificationErrors = []

        self.attachment = 1
        self.file_source_location = "F:\\python_test\\logfiles\\"
        self.file_move_location = "F:\\logfiles_back\\"

    def test_selenium_exception(self):
        driver = self.driver
        driver.get(self.base_url + "/")
        driver.find_element_by_id("gbqfq").clear()
        driver.find_element_by_id("gbqfq").send_keys("Check this out")
        if self.attachment:
            try:
                for contents in os.listdir(self.file_source_location):
                    src_file = os.path.join(self.file_source_location, contents)
                    dst_file = os.path.join(self.file_move_location, contents)
                    shutil.move(src_file, dst_file)
                print'files_moved_success'
                driver.find_element_by_link_text("check out - definition of check out by the Free Online Dictionary ...").click()
            except Exception as e:
                print e

    def is_element_present(self, how, what):
        try: self.driver.find_element(by=how, value=what)
        except NoSuchElementException, e: return False
        return True

    def tearDown(self):
        self.driver.quit()
        self.assertEqual([], self.verificationErrors)

if __name__ == "__main__":
    unittest.main()

このコードでは、間違ったパスなどの理由で logfiles から logfiles_back への移動が失敗した場合、catch ブロックが実行され、セレン テスト ケースでエラーの報告を終了する必要があります。

現在起こっていることは、エラーを報告し、テスト ケースの実行を完了することです。

これを可能にする方法は?

4

1 に答える 1

6

トリガーされた実際の例外を発生させたい場合はraise、それ自体を呼び出すことができます。これにより、最後のアクティブな例外が発生します。

try:
    for contents in os.listdir(self.file_source_location):
        src_file = os.path.join(self.file_source_location, contents)
        dst_file = os.path.join(self.file_move_location, contents)
        shutil.move(src_file, dst_file)
    print'files_moved_success'
    driver.find_element_by_link_text("check out - definition of check out by the Free Online Dictionary ...").click()
except Exception as e:
    print e
    raise # Raise the exception that brought you here 

トレースバックが必要なく、単に終了したい場合は、sys.exit(1)after を呼び出すこともできます (または使用したいエラー コード) print e:

import sys
# ...
try:
    for contents in os.listdir(self.file_source_location):
        src_file = os.path.join(self.file_source_location, contents)
        dst_file = os.path.join(self.file_move_location, contents)
        shutil.move(src_file, dst_file)
    print'files_moved_success'
    driver.find_element_by_link_text("check out - definition of check out by the Free Online Dictionary ...").click()
except Exception as e:
    print e
    sys.exit(1)
于 2012-12-11T06:23:00.197 に答える