python – 将对象C从A类发送到B类
发布时间:2020-12-20 11:17:29 所属栏目:Python 来源:网络整理
导读:我无法弄清楚如何在我的系统中设计类. 在classA中,我创建了对象selenium(它模拟了网站上的用户操作). 在这个ClassA中,我创建了另一个对象,如SearchScreen,Payment_Screen和Summary_Screen. # -*- coding: utf-8 -*-from selenium import seleniumimport unit
我无法弄清楚如何在我的系统中设计类.
在classA中,我创建了对象selenium(它模拟了网站上的用户操作). 在这个ClassA中,我创建了另一个对象,如SearchScreen,Payment_Screen和Summary_Screen. # -*- coding: utf-8 -*- from selenium import selenium import unittest,time,re class OurSiteTestCases(unittest.TestCase): def setUp(self): self.verificationErrors = [] self.selenium = selenium("localhost",5555,"*chrome","http://www.someaddress.com/") time.sleep(5) self.selenium.start() def test_buy_coffee(self): sel = self.selenium sel.open('/') sel.window_maximize() search_screen=SearchScreen(self.selenium) search_screen.choose('lavazza') payment_screen=PaymentScreen(self.selenium) payment_screen.fill_test_data() summary_screen=SummaryScreen(selenium) summary_screen.accept() def tearDown(self): self.selenium.stop() self.assertEqual([],self.verificationErrors) if __name__ == "__main__": unittest.main() 这是SearchScreen模块的示例: class SearchScreen: def __init__(self,selenium): self.selenium=selenium def search(self): self.selenium.click('css=button.search') 我想知道这些类的设计是否还有什么问题? 解决方法
你的方法很好.您有一组工具类,每个工具类都需要知道其目标.然后,您有一个工具包类,可以在特定目标上协调这些工具.
class AgreePrice: def __init__(self,connection): ... class PlaceOrder: def __init__(self,connection): ... class ConfirmAvailability: def __init__(self,connection): ... class BookingService: def __init__(self,connection): ... def book(self): for Command in (ConfirmAvailability,AgreePrice,PlaceOrder): command = Command(self.connection) command.run() assert command.success() 这些类结构没有任何问题,事实上它们总是出现,并且当单个“工具”类不能方便地放在一个函数中时,它们是一个相当好的设计. 如果你发现自己有一个拥有数十种方法的类,其中许多可以根据具体任务进行分组,这是一个很好的重构. 作为一般规则,您希望确保您的“工具”类(SearchScreen等)在概念上低于您的控制器(您的测试用例).它们适合你. 最简单的这些工具类是Function Object设计模式的一种形式.虽然在你的情况下,你在每个对象上调用的不仅仅是一个方法,所以它们更复杂一些. 或者,简而言之.你的设计很好,很常见. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |