java - 如何使用 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****
}
}
解决方案
根据标准做法,您必须创建一个名为的类,该类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();
}
}
推荐阅读
- java - Spring State Machine - 使用 StateMachineModelConfigurer 时定义 machineId
- android - 实现 MVVM LiveData RxJava Dagger Databinding 的正确结构?
- shell - 执行 shell 命令直到成功
- visual-studio - 如何在 C++ 中创建关于 MFC 或 MFC ActiveX 控件的 COM 对象?
- awk - 在找到包含模式的行后,我想使用 sed 或 awk 附加符合条件的多行
- postgresql - 如何在 postgresql 中创建函数以返回列值并更新表以及 x 锁定表?
- reactjs - 从 JSON 响应中获取 react js 中的数组数组
- influxdb - 在具有 HR 层次结构的用例上使用 influxdb
- asp.net-mvc - kendo ui mvc 编辑器“大于或等于”iTextSharp
- php - 我想知道是否有办法在android studio和使用mysql数据库的用户中显示当前登录的特定数据