通过调用它的PageObject导航到页面

时间:2016-04-06 13:09:31

标签: python selenium selenium-webdriver pageobjects

我正在编写一些硒测试,并想知道我如何导航到我的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()

和我的烟雾。在我的烟雾中,我想把一些像navigateTo(ProfilePage)这样的东西直接调用webdriver。

class SmokeTest(BaseTestCase):
    def test_login(self):
        LoginPage(self.driver).login(self.driver)
        navigateTo(self.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")

1 个答案:

答案 0 :(得分:1)

您的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)