首页 > 解决方案 > 如何使用 selenium 和 java 将应用程序的所有页面类的页眉、页脚等可重复部分添加到页面对象模型?

问题描述

我正在尝试使用 Selenium 和 JAVA 自动化 Web 测试,

我有一个 Web 应用程序,其中某些部分(如页眉和页脚)在多个页面中重复。我正在使用页面对象模型,为每个网页创建页面类文件。所有网页都有相同的页眉和页脚。

我创建了两个页面类文件 HomePage.java 和 SmallBusinessPage.java 以及用于组件页眉和页脚的两个单独的类文件 Header.java 和 Footer.java。这些文件与页面类文件位于单独的包中。我的测试类文件是TestCaseDevelopment.java,基类是TestBase.java。我需要使用 HomePage 对象从测试类 TestCaseDevelopment.java 访问标题链接“注册”(其定位器在 Header.java 文件中)。(主页包含 HeaderHomePage 定位器,它在网页上找到整个标题。Header.java 文件中的 LinkEnroll 定位器在标题中定位链接。所以链接的完整定位器是 HeaderHomePage+LinkEnroll,//header//div[@id='masthead']//a[normalize-space()='Enroll']这意味着我无法访问它。

我不想在每个页面类文件中为页眉和页脚创建重复项。如何将这些部分定义为单独的类文件并将它们包含在我的页面类文件中?

主页.java

public class HomePage extends TestBase {

    @FindBy(xpath = "//header//div[@id='masthead']")
    public Header HeaderHomePage;

    public HomePage() {
        PageFactory.initElements(driver, this);
    }
}

SmallBusinessPage.java

public class SmallBusinessPage extends TestBase{

    @FindBy(xpath = "//header//div[@id='masthead']")
    public Header HeaderSmallBusiness;

    public SmallBusinessPage() {
        PageFactory.initElements(driver, this);
    }
}

标题

public class Header extends TestBase{

    @FindBy(xpath="//a[normalize-space()='Enroll']")
    WebElement LinkEnroll;

    public Header() {
        PageFactory.initElements(driver, this);
    }
}

页脚

public class Footer extends TestBase {

    @FindBy(xpath="//footer//a[text()='Careers']")
    public WebElement LinkCareers;

    public Footer() {
        PageFactory.initElements(driver, this);
    }
}

测试库

public class TestBase {

    public static WebDriver driver;
    public static Properties prop;
    Header header = new Header();

    public TestBase(){

        try {
            prop = new Properties();
            FileInputStream istream = new FileInputStream("C:\\QA -Selenium\\WS\\WellsFargoTest\\src\\main\\java\\com\\wellsfargo\\qa\\config\\config.properties");
            prop.load(istream);
        }
        catch(FileNotFoundException e) {
            e.printStackTrace();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }

    public static void initialization() {
        String browserName = prop.getProperty("browser");
        if(browserName.equals("chrome")){
            System.setProperty("webdriver.chrome.driver","C:\\Selenium\\chromedriver");
            driver =new ChromeDriver();
        }
        if(browserName.equals("FF")){
            System.setProperty("webdriver.gecko.driver","C:\\Selenium\\geckodriver");
            driver = new FirefoxDriver();
        }
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(TestUtility.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);        
        driver.manage().timeouts().implicitlyWait(TestUtility.IMLICIT_WAIT, TimeUnit.SECONDS);  
        driver.get(prop.getProperty("url"));
    }
}

测试用例开发

public class TestCaseDevelopment extends TestBase{

    HomePage homePage;
    public TestCaseDevelopment() {
        super();
    }

    @BeforeMethod
    public void setup() {
        initialization();
        homePage = new HomePage();
        ****homePage.HeaderHomePage.l****
    }
}

标签: javaseleniumheaderpom.xmlfooter

解决方案


根据标准做法,您必须创建一个名为的类,该类BaesPage.java在应用程序中保留所有常见的定位器。所有页面对象都将扩展此类,因此所有常见定位器都可用于所有页面对象。请看一看。

BasePage.java

public class BasePage{

    @FindBy(xpath="//a[normalize-space()='Enroll']")
    WebElement LinkEnroll;


@FindBy(xpath="//footer//a[text()='Careers']")
    public WebElement LinkCareers;

    public BasePage() {
        PageFactory.initElements(driver, this);
    }
public void clickEnroll()
{

LinkEnroll.click();
}
}

现在您不需要 Header.java 和 Footer.java 两个单独的类,因为它们在应用程序中都有公共定位器,因此它在 BasePage.java 中移动

主页.java

public class HomePage extends BasePage {

    @FindBy(xpath = "//header//div[@id='masthead']")
    public Header HeaderHomePage;

    public HomePage() {
        PageFactory.initElements(driver, this);
    }
}

TestBase.java

public class TestBase {

    public static WebDriver driver;
    public static Properties prop;
    Header header = new Header();

    public TestBase(){

        try {
            prop = new Properties();
            FileInputStream istream = new FileInputStream("C:\\QA -Selenium\\WS\\WellsFargoTest\\src\\main\\java\\com\\wellsfargo\\qa\\config\\config.properties");
            prop.load(istream);
        }
        catch(FileNotFoundException e) {
            e.printStackTrace();
        }
        catch(IOException e)
        {
            e.printStackTrace();
        }
    }

    public static void initialization() {
        String browserName = prop.getProperty("browser");
        if(browserName.equals("chrome")){
            System.setProperty("webdriver.chrome.driver","C:\\Selenium\\chromedriver");
            driver =new ChromeDriver();
        }
        if(browserName.equals("FF")){
            System.setProperty("webdriver.gecko.driver","C:\\Selenium\\geckodriver");
            driver = new FirefoxDriver();
        }
        driver.manage().window().maximize();
        driver.manage().deleteAllCookies();
        driver.manage().timeouts().pageLoadTimeout(TestUtility.PAGE_LOAD_TIMEOUT, TimeUnit.SECONDS);        
        driver.manage().timeouts().implicitlyWait(TestUtility.IMLICIT_WAIT, TimeUnit.SECONDS);  
        driver.get(prop.getProperty("url"));
    }
}

测试用例开发

public class TestCaseDevelopment extends TestBase{

    HomePage homePage;
    public TestCaseDevelopment() {
        super();
    }

    @BeforeMethod
    public void setup() {
        initialization();
        homePage = new HomePage();
//Now you can call clickEnroll from any page object
        homePage.clickEnroll();
    }
}

推荐阅读