使用Python / Selenium从另一个类调用配置文件

时间:2018-01-31 23:06:46

标签: python selenium selenium-webdriver webdriver

无法调用来自班级chrome_configuration的方法Profile,我想在TestBase班级调用该方法:

class TestBase:

     driver = None

     def setup(self):
         Profile.chrome_configuration()


class Profile:

     driver = None

     def chrome_configuration(self):
         self.driver = webdriver.Chrome()
         self.driver.set_window_size(1900, 1200)
         self.driver.maximize_window()

1 个答案:

答案 0 :(得分:1)

您必须实例化该类或使该方法保持静态。以下是每个选项的示例。

#By initializing the profile class
class TestBase(object):

     driver = None

     profile = Profile() 

     def setup(self):
         profile.chrome_configuration()

#By making the method static
class Profile(object):

     driver = None

     @staticmethod
     def chrome_configuration(self):
         self.driver = webdriver.Chrome()
         self.driver.set_window_size(1900, 1200)
         self.driver.maximize_window()

此外,您的所有课程应始终从2.x中的object继承。它被称为新式课程。