python - 通过调用页面的 PageObject 导航到该页面

标签 python selenium selenium-webdriver pageobjects

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

和我的个人资料页面:

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)

关于python - 通过调用页面的 PageObject 导航到该页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36452171/

相关文章:

python - 如何从 Python 中的预定义模板列出可能的组合

php - 如何在 PHPUnit 中执行所有测试?

python - Selenium 中发现元素的顺序

testing - 如何让 WebDriver 解除 Firefox 安全警报?

在 Excel 电子表格文件中搜索空单元格时,Python xlwt 产生 AttributeError

python - 如何更改scrapy中的url长度?

python - 如何对双击按键进行编程以使非修饰键在程序中的行为类似于修饰键?

java - Selenium 无法找到特定字段

java - 如何确保我正在检查未选中的复选框 selenium java

java - 在 Docker 中运行 Selenium 测试时出现连接被拒绝的 UnreachableBrowserException