我正在编写一些硒测试,想知道如何通过在测试中调用它的类来导航到ProfilePage
所以我得到了BasePage类,

class BasePage(object):
    def __init__(self,driver):
        self.driver = driver
        self._validate_page(driver)

    @abstractclassmethod
    def _validate_page(self,driver):
        return

class PageNotDisplayedException(Exception):
 """throw this exception when page was not displayed, or different page is displayed"""
       pass


我的BaseTestCase类

class BaseTestCase(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Firefox()
        self.driver.implicitly_wait(5)
        self.driver.maximize_window()

        # navigate to the application home page
        self.driver.get('http://www.facebook.com')

    def navigateTo(self, to):
        self.driver.get(to)

    def tearDown(self):
        # close the browser window
        self.driver.quit()


和我的烟雾测试。在我的冒烟测试中,我想放一些类似NavigationTo(ProfilePage)的测试,直接测试webdriver。

class SmokeTest(BaseTestCase):
    def test_login(self):
        LoginPage(self.driver).login(self.driver)
        navigateTo(self.ProfilePage)


和我的ProfilePage:

class ProfilePage(BasePage):

    _page_id = ".//[@class='profile']"
    url = "https://www.facebook.com/my.profile"

    def __init__(self,driver):
        super(ProfilePage, self).__init__(driver)

    def _validate_page(self, driver):
        try:
            driver.find_element_by_xpath(self._page_id)
        except:
            raise PageNotDisplayedException
            ("Profile Page could not be displayed")

最佳答案

您的ProfilePage页面对象类应具有与该页面关联的url属性:

class ProfilePage(BasePage):
     url = "https://my.url/profile"


然后,navigateTo方法应使用以下属性:

def navigateTo(self, page):
    self.driver.get(page.url)


然后,您应该在测试中调用self.navigateTo

class SmokeTest(BaseTestCase):
    def test_login(self):
        LoginPage(self.driver).login(self.driver)

        profilePage = ProfilePage(self.driver)
        self.navigateTo(profilePage)

09-12 22:52