为页面中的帧实现PageObjects模式 [英] Implementing PageObjects pattern for frames in a page

查看:123
本文介绍了为页面中的帧实现PageObjects模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为页面中的帧实现pageObject模式(Selenium)..我有一个主页,左框架和右框架,我想为每个框架创建页面对象..

How to implement the pageObject pattern(Selenium) for frames in a page..I have a home page and there is a left frame and Right frame and i would like to create page object for each frame..

例如,我有如下左边的LeftFrame页面对象:

For example, I have the LeftFrame Page Object as below:

Public Class HomePageLeftFrame{

private WebElement link;
private WebElement textField;

}

如何为两个元素编写@FindBy注释HomePageLeftFrame对象......有没有办法?

How to write the @FindBy annotation for the two elements in the HomePageLeftFrame object...Is there a way?

注意:根据有关pageObjects的selenium的文档,提到了页面对象可以是一个完整的HTML页面或页面的一部分。我的理解是正确的上述实现??

Note: As per the documentation on the selenium for pageObjects it is mentioned that page objects can be a whole HTML page or part of a page..Is my understanding correct for the above implementation??

推荐答案

写作之前我的建议首先,我想分享我开发POM框架的应用程序。所以基本上我的应用程序为每个html页面有三个不同的框架:

Before writing my suggestion first I'd like to share my application for which I developed a POM framework. So basically my application has three different frames for each html page:


  1. 顶部框架:它是包含整个应用程序的共享视图常见的功能主义者,如注销,帮助,管理员设置等。

  1. Top frame : It is shared view for whole application which contains common functionalists like Log-Out, help, Admin Settings etc.

左侧导航面板:在我的应用程序的大多数地方,这个框架可用,它提供导航在应用程序的不同部分之间。

Left navigation panel: At most of places in my application this frame is available and it provides navigation in between different section of application.

内容框架:这是用户执行主要操作的框架。

Content Frame: This is the frame where user does main operation.

为这种类型的应用程序设计基于页面对象的框架实际上是一项繁琐的工作,但我决定以这样的方式设计它以便我们可以避免所有重复的代码。首先,我将共享框架结构:

To design a Page Object based framework for this type of application is really a tedious job, But I decided to design it in such a manner so that we can avoid all the repetitive code.First of all I'll share framework structure:

org.xyz.automation.sdk
     - TopFrame.java
     - MainNavigator.java
     - WebDriverFactory.java
     - Configuration.java
org.xyz.automation.sdk.global.pages
     - LoginPage.java
     - TopFrame.java
org.xyz.automation.sdk.global
     - Navigator.java
org.xyz.automation.sdk.leftpanel.pages
     - LeftFramePage.java
org.xyz.automation.sdk.leftpanel
     - LeftFrameNavigator.java

所以基本上 org.xyz.automation.sdk 包含所有常见配置类,它们负责根据您的系统配置实例化Web驱动程序。

So basically org.xyz.automation.sdk contains all common configuration classes which has responsibility to instantiate web driver as per your system configuration.

org.xyz.automation.sdk.global.pages :这个包包含常见视图的页面对象类,正如我已经说过的TopFrame是东西你可以在整个应用程序中看到,所以我在 TopFrame.java 中建模了这个视图。要从Page对象类中隔离框架导航,我将它保存在一个单独的包中,这是什么都没有但是Page Object类的父包。

org.xyz.automation.sdk.global.pages: This package contains page object class of common views, as I already said TopFrame is something which you can see throughout whole application so I modeled this view in TopFrame.java.To segregate the frame navigation from Page object class, I kept that in a separate package which is nothing but parent package of Page Object class.

org.xyz.automation.sdk.global 包含 Navigation.java 负责框架导航和所有其他导航。从 global.pages 中保留它的唯一好处是,将来当你的应用程序不支持框架时,你不需要改变任何东西Page类,只修改导航器类。

org.xyz.automation.sdk.global contains Navigation.java which takes care of frame navigation and all other navigations. The only benefit of keeping it out from global.pages is that in future when your application won't support frames, you do not need to change any thing Page class, modify only navigator class.

org.xyz.automation.sdk.leftpanel.pages :与上面类似,此包含页面左侧面板的对象java类,我再次从 leftpanel.pages 包中分离出左侧面板的导航。

org.xyz.automation.sdk.leftpanel.pages: Similar to above this package contains page object java class of left panel and again I separated out navigation of this left panel from leftpanel.pages package.

@FindBy
这只是查找建模页面的网页元素的快捷方式。页面对象的基本概念是每个类应该只为单个视图建模,因此建模类应该只包含实际出现在建模视图上的那些web元素。对于例如 TopFrame.java 应包含仅在该帧上显示的注销元素。

@FindBy This is nothing but a shortcut way to find web-elements of modeled page. The very basic concept of page object is each class should model a single view only so modeled class should contain only those web-elements which actually appear on modeled view. For e.g. TopFrame.java should contain log out element as it appears on that frame only.

    @FindBy(name = "logOut") // To find the element by name
    @CacheLookup
    private WebElement logOutLInk;

并且在对其执行任何操作时,您应将其视为的实例WebElement

and while performing any action on it you should treat it as an instance of WebElement.

logOutLInk.click();

  /**
   * Class which models the view of Left Navigation Frame
   */
    public class LeftNavigationFrame{

        @FindBy(name= "exampleName")
        private WebElement exampleButton;

        private WebDriver driver;

        public LeftNavigationFrame(WebDriver driver) {
            this.driver = driver;
        }

        /**
         * Opens a new page by clicking example button
         */
        public void openNewPage() {
            exampleButton.click();
    }
}

Navigator类示例:

Example of Navigator class:

  /**
   * Class which provides convenient methods to navigate on left frame
   */
    public class LeftFrameNavigator{
        private WebDriver driver;

        public LeftFrameNavigator(WebDriver driver) {
            this.driver = driver;
        }

        /**
         * Changes scope to the left frame 
         *
         * @return Page Object class of LeftNavigationFrame
         */
        public LeftNavigationFrame switchToLeftFrame() {
            // Code to switch Frame
            return new LeftNavigationFrame(driver);
    }
}

这是您的测试类的示例:

This is an example of your test class:

      /**
       * Class which contains test cases of xyz
       */
        public class doTesting{
        LeftFrameNavigator leftNav;

            @BeforeTest
            public void instantiateRequiredClasses() {
            LeftFrameNavigator leftNav = new LeftFrameNavigator();
            }

            @Test
            public void doTestingHere() {
            LeftNavigationFrame leftFrame = leftNav.switchToLeftFrame();
            leftFrame.openNewPage()
            }
        }
    }

这篇关于为页面中的帧实现PageObjects模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆