如何将Selenium对象传递给另一个类?

时间:2011-05-20 02:07:44

标签: python class selenium

我无法在我的第二课程中调用我的原始selenium实例,该实例具有GetRows()函数。我可以在函数类中获取selenium对象的方法是什么?

Class test(unittest.TestCase):
    def setUp(self):
        self.verificationerrors=[]
        self.selenium = selenium("localhost",4444,"","http://localhost")
        self.selenium.start()

    def test_untitiled(self):
        pass

    def teardown(self):
        pass

    def Get(self):     
        return self.selenium

Class Functions:
    def GetRows(self)
        selenium.get_xpath_count(path)   //Cant call selenium object here! :(
        sel = test().get()   // Does not allow me to return self. selenium :(

2 个答案:

答案 0 :(得分:0)

你的第二课要么扩展或实例化你的“测试”类,它负责创建Selenium对象

答案 1 :(得分:0)

我这样做的方法是将你需要做的所有事情抽象到你的第二堂课,并让你的测试只是调用它。

所以你的测试类看起来像这样

class SearchTests(unittest.TestCase):

    _count_regex = '^.* (\d+) - (\d+)'
    _total_count_regex = '^.* \d+ - \d+ of (\d+)'

    def setUp(self):
        self.selenium = selenium(server, 
                                port,
                                browser, 
                                baseurl)
        self.selenium.start()
        self.selenium.set_timeout(ConnectionParameters.page_load_timeout)

    def tearDown(self):
        self.selenium.stop()

    def test_that_character_escaping_doesnt_go_into_the_test(self):
        """ Test for Litmus 4857
            https://litmus.mozilla.org/show_test.cgi?id=4857"""
        amo_home_page = AddonsHomePage(self.selenium)
        amo_search_page = amo_home_page.search_for("personas%20plus")
        self.assertTrue(amo_search_page.is_text_present("No results found."))
        results_count = amo_search_page.results_count
        self.assertTrue("0 - 0 of 0" in results_count)

你的抽象看起来像这样

class AddonsSearchHomePage(AddonsHomePage):

    _results_count_header = "css=h3.results-count"
    _page_counter = "css=div.num-results"

    def __init__(self, selenium):
        self.selenium = selenium

    @property
    def results_count(self):
        return self.selenium.get_text(self._results_count_header)

    @property
    def page_results_count(self):
        return self.selenium.get_text(self._page_counter)

您可以在https://github.com/AutomatedTester/Addon-Tests

看到整个项目