<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                合規國際互聯網加速 OSASE為企業客戶提供高速穩定SD-WAN國際加速解決方案。 廣告
                # 頁面對象模型(POM)&頁面工廠:Selenium WebDriver 教程 > 原文: [https://www.guru99.com/page-object-model-pom-page-factory-in-selenium-ultimate-guide.html](https://www.guru99.com/page-object-model-pom-page-factory-in-selenium-ultimate-guide.html) ## 什么是頁面對象模型? **頁面對象模型(POM)**是一種設計模式,廣泛用于測試自動化中,可為 Web UI 元素創建**對象存儲庫**。 該模型的優點是它減少了代碼重復并改善了測試維護。 在此模型下,對于應用程序中的每個網頁,應有一個對應的**頁面類。** 此類將標識該網頁的 WebElement,并且還包含對這些 WebElement 執行操作的 **Page 方法**。 這些方法的名稱應根據它們正在執行的任務來指定,即如果加載程序正在等待支付網關的出現,則 POM 方法名稱可以是 waitForPaymentScreenDisplay()。 ![Page Object Model (POM) & Page Factory in Selenium: Complete Tutorial](https://img.kancloud.cn/47/34/4734345e16382220025c4d2103d5059f_553x254.png "Page Object Model (POM) & Page Factory in Selenium: Ultimate Guide") 在本教程中,您將學習- * [為什么選擇頁面對象模型?](#1) * [POM 的優點](#3) * [如何實現 POM?](#4) * [什么是頁面工廠?](#5) * [具有頁面工廠概念的 Guru99 TestCase](#6) * [AjaxElementLocatorFactory](#7) ## 為什么選擇頁面對象模型? 在 Selenium WebDriver 中啟動 UI 自動化并不是一項艱巨的任務。 您只需要查找元素并對其執行操作即可。 考慮這個簡單的腳本來登錄網站 ![Page Object Model (POM) & Page Factory in Selenium: Complete Tutorial](https://img.kancloud.cn/bc/53/bc53f6a1bbcf0b9961553ae99152a82e_624x297.png "Page Object Model (POM) & Page Factory in Selenium: Ultimate Guide") 如您所見,我們所做的只是查找元素并為這些元素填充值。 這是一個小腳本。 腳本維護看起來很容易。 但是隨著時間的推移,測試套件將不斷增長。 隨著在代碼中添加越來越多的行,事情變得越來越棘手。 腳本維護的主要問題是,如果 10 個不同的腳本使用相同的 page 元素,但對該元素進行了任何更改,則需要更改所有 10 個腳本。 這既費時又容易出錯。 一種更好的腳本維護方法是創建一個單獨的類文件,該文件將查找 Web 元素,填充它們或對其進行驗證。 可以使用該元素在所有腳本中重用該類。 將來,如果 web 元素發生更改,我們只需要在 1 個類文件而不是 10 個不同的腳本中進行更改。 這種方法稱為**頁面對象模型(POM)**。 它有助于使代碼**更具可讀性,可維護性**和**可重用。** ![Page Object Model (POM) & Page Factory in Selenium: Complete Tutorial](https://img.kancloud.cn/09/fa/09fa67a7490ba9fb5263abaf1f0530a5_464x294.png "Page Object Model (POM) & Page Factory in Selenium: Ultimate Guide") ## POM 的優點 1. 頁面對象模式表示,UI 中的操作和流程應與驗證分開。 這個概念使我們的代碼更加簡潔易懂。 2. 第二個好處是**對象庫獨立于測試用例**,因此我們可以使用不同的工具將同一對象庫用于不同的目的。 例如,我們可以將 POM 與 TestNG / JUnit 集成以進行功能[測試](/software-testing.html),同時與 JBehave / Cucumber 集成以進行驗收測試。 3. 由于 POM 類中的可重復使用的頁面方法,因此代碼變得更少并得到了優化。 4. **方法**獲得了**更現實的名稱**,可以通過 UI 中發生的操作輕松映射它們。 即,如果在點擊按鈕后我們進入首頁,則方法名稱將類似于“ gotoHomePage()”。 ## 如何實現 POM? 簡單的 POM: 這是頁面對象模型(POM)的基本結構,其中 **AUT** 的所有 Web 元素以及在這些 Web 元素上運行的方法都保存在類文件中。**驗證**之類的任務 作為測試方法的一部分,應將**分開**。 ![Page Object Model (POM) & Page Factory in Selenium: Complete Tutorial](https://img.kancloud.cn/62/f3/62f3ced18542cfc50dea309d828056a6_524x236.png "Page Object Model (POM) & Page Factory in Selenium: Ultimate Guide") **完整示例** **TestCase:**轉到 Guru99 演示站點。 <colgroup><col> <col></colgroup> | 步驟 1)前往 Guru99 示范網站 | ![Page Object Model (POM) & Page Factory in Selenium: Complete Tutorial](https://img.kancloud.cn/0f/ac/0fac926a0e377aa598eb8f188fd2d8ce_266x121.png "Page Object Model (POM) & Page Factory in Selenium: Ultimate Guide") | | 步驟 2)在首頁中,檢查文本**“ Guru99 Bank”** 存在 | ![Page Object Model (POM) & Page Factory in Selenium: Complete Tutorial](https://img.kancloud.cn/d6/ad/d6adc16b990c994685ec6046085688bb_329x202.png "Page Object Model (POM) & Page Factory in Selenium: Ultimate Guide") | | 步驟 3)登錄到應用程序 | ![Page Object Model (POM) & Page Factory in Selenium: Complete Tutorial](https://img.kancloud.cn/ad/73/ad7381ddd01c64b2d5db9c4234b203d2_325x186.png "Page Object Model (POM) & Page Factory in Selenium: Ultimate Guide") | | 步驟 4)確認首頁包含“ Manger ID:演示”文本 | ![Page Object Model (POM) & Page Factory in Selenium: Complete Tutorial](https://img.kancloud.cn/24/6e/246e9fdfaf13bcac4013c6b769b76e72_349x126.png "Page Object Model (POM) & Page Factory in Selenium: Ultimate Guide") | 這是我們正在處理的 2 頁 1. 登錄頁面 2. 主頁(登錄后顯示) 因此,我們創建了 2 個 POM 類 **Guru99 登錄頁面 POM** ``` package pages; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; public class Guru99Login { WebDriver driver; By user99GuruName = By.name("uid"); By password99Guru = By.name("password"); By titleText =By.className("barone"); By login = By.name("btnLogin"); public Guru99Login(WebDriver driver){ this.driver = driver; } //Set user name in textbox public void setUserName(String strUserName){ driver.findElement(user99GuruName).sendKeys(strUserName); } //Set password in password textbox public void setPassword(String strPassword){ driver.findElement(password99Guru).sendKeys(strPassword); } //Click on login button public void clickLogin(){ driver.findElement(login).click(); } //Get the title of Login Page public String getLoginTitle(){ return driver.findElement(titleText).getText(); } /** * This POM method will be exposed in test case to login in the application * @param strUserName * @param strPasword * @return */ public void loginToGuru99(String strUserName,String strPasword){ //Fill user name this.setUserName(strUserName); //Fill password this.setPassword(strPasword); //Click Login button this.clickLogin(); } } ``` **Guru99 主頁 POM** ``` package pages; import org.openqa.selenium.By; import org.openqa.selenium.WebDriver; public class Guru99HomePage { WebDriver driver; By homePageUserName = By.xpath("//table//tr[@class='heading3']"); public Guru99HomePage(WebDriver driver){ this.driver = driver; } //Get the User name from Home Page public String getHomePageDashboardUserName(){ return driver.findElement(homePageUserName).getText(); } } ``` **Guru99 簡單 POM 測試用例** ``` package test; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import pages.Guru99HomePage; import pages.Guru99Login; public class Test99GuruLogin { String driverPath = "C:\\geckodriver.exe"; WebDriver driver; Guru99Login objLogin; Guru99HomePage objHomePage; @BeforeTest public void setup(){ System.setProperty("webdriver.gecko.driver", driverPath); driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://demo.guru99.com/V4/"); } /** * This test case will login in http://demo.guru99.com/V4/ * Verify login page title as guru99 bank * Login to application * Verify the home page using Dashboard message */ @Test(priority=0) public void test_Home_Page_Appear_Correct(){ //Create Login Page object objLogin = new Guru99Login(driver); //Verify login page title String loginPageTitle = objLogin.getLoginTitle(); Assert.assertTrue(loginPageTitle.toLowerCase().contains("guru99 bank")); //login to application objLogin.loginToGuru99("mgr123", "mgr!23"); // go the next page objHomePage = new Guru99HomePage(driver); //Verify home page Assert.assertTrue(objHomePage.getHomePageDashboardUserName().toLowerCase().contains("manger id : mgr123")); } ``` ## 什么是頁面工廠? Page Factory 是 Selenium WebDriver 的內置頁面對象模型概念,但已進行了非常優化。 同樣,在這里,我們遵循頁面對象存儲庫和測試方法分離的概念。 另外,在 PageFactory 類的幫助下,我們使用批注 **@FindBy** 查找 WebElement。 我們使用 initElements 方法來初始化 Web 元素 ![Page Object Model (POM) & Page Factory in Selenium: Complete Tutorial](https://img.kancloud.cn/f1/ea/f1ea83930a36f553947b9e189d3b5c61_616x252.png "Page Object Model (POM) & Page Factory in Selenium: Ultimate Guide") **@FindBy** 可以接受 **tagName,partialLinkText,名稱,linkText,id,css,className,xpath** 作為屬性。 讓我們看看使用 Page Factory 的上述示例 **具有頁面工廠**的 Guru99 登錄頁面 ``` package PageFactory; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; public class Guru99Login { /** * All WebElements are identified by @FindBy annotation */ WebDriver driver; @FindBy(name="uid") WebElement user99GuruName; @FindBy(name="password") WebElement password99Guru; @FindBy(className="barone") WebElement titleText; @FindBy(name="btnLogin") WebElement login; public Guru99Login(WebDriver driver){ this.driver = driver; //This initElements method will create all WebElements PageFactory.initElements(driver, this); } //Set user name in textbox public void setUserName(String strUserName){ user99GuruName.sendKeys(strUserName); } //Set password in password textbox public void setPassword(String strPassword){ password99Guru.sendKeys(strPassword); } //Click on login button public void clickLogin(){ login.click(); } //Get the title of Login Page public String getLoginTitle(){ return titleText.getText(); } /** * This POM method will be exposed in test case to login in the application * @param strUserName * @param strPasword * @return */ public void loginToGuru99(String strUserName,String strPasword){ //Fill user name this.setUserName(strUserName); //Fill password this.setPassword(strPasword); //Click Login button this.clickLogin(); } } ``` **具有頁面工廠**的 Guru99 主頁 ``` package PageFactory; import org.openqa.selenium.WebDriver; import org.openqa.selenium.WebElement; import org.openqa.selenium.support.FindBy; import org.openqa.selenium.support.PageFactory; public class Guru99HomePage { WebDriver driver; @FindBy(xpath="//table//tr[@class='heading3']") WebElement homePageUserName; public Guru99HomePage(WebDriver driver){ this.driver = driver; //This initElements method will create all WebElements PageFactory.initElements(driver, this); } //Get the User name from Home Page public String getHomePageDashboardUserName(){ return homePageUserName.getText(); } } ``` ## 具有 Page Factory 概念的 Guru99 TestCase ``` package test; import java.util.concurrent.TimeUnit; import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver; import org.testng.Assert; import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; import PageFactory.Guru99HomePage; import PageFactory.Guru99Login; public class Test99GuruLoginWithPageFactory { String driverPath = "C:\\geckodriver.exe"; WebDriver driver; Guru99Login objLogin; Guru99HomePage objHomePage; @BeforeTest public void setup(){ System.setProperty("webdriver.gecko.driver", driverPath); driver = new FirefoxDriver(); driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS); driver.get("http://demo.guru99.com/V4/"); } /** * This test go to http://demo.guru99.com/V4/ * Verify login page title as guru99 bank * Login to application * Verify the home page using Dashboard message */ @Test(priority=0) public void test_Home_Page_Appear_Correct(){ //Create Login Page object objLogin = new Guru99Login(driver); //Verify login page title String loginPageTitle = objLogin.getLoginTitle(); Assert.assertTrue(loginPageTitle.toLowerCase().contains("guru99 bank")); //login to application objLogin.loginToGuru99("mgr123", "mgr!23"); // go the next page objHomePage = new Guru99HomePage(driver); //Verify home page Assert.assertTrue(objHomePage.getHomePageDashboardUserName().toLowerCase().contains("manger id : mgr123")); } } ``` 完整的項目結構將如下圖所示: ![Page Object Model (POM) & Page Factory in Selenium: Complete Tutorial](https://img.kancloud.cn/81/ea/81ead40865fa239eb2d56f681902358e_530x278.png "Page Object Model (POM) & Page Factory in Selenium: Ultimate Guide") ## AjaxElementLocatorFactory 使用 Page Factory 模式的主要優點之一是 AjaxElementLocatorFactory 類。 它正在處理延遲加載概念,即在 AjaxElementLocatorFactory 的幫助下將 WebElement 的超時分配給 Object 頁面類。 在此,當對元素執行操作時,等待其可見性僅從該時刻開始。 如果在給定的時間間隔內未找到該元素,則[測試用例](/test-case.html)執行將引發'NoSuchElementException'異常。 ![Page Object Model (POM) & Page Factory in Selenium: Complete Tutorial](https://img.kancloud.cn/d4/59/d4599fa7bead345fe025a422ce09916c_601x214.png "Page Object Model (POM) & Page Factory in Selenium: Ultimate Guide") ## 摘要 1. 頁面對象模型是 Selenium WebDriver 中的對象存儲庫設計模式。 2. POM 創建了可維護,可重用的測試代碼。 3. Page Factory 是在 POM 概念中創建對象存儲庫的一種優化方法。 4. AjaxElementLocatorFactory 是 Page Factory 模式中的惰性加載概念,用于僅在任何操作中使用 WebElement 時標識它們。 [在本教程中下載用于演示的 Selenium 項目文件](https://drive.google.com/uc?export=download&id=1RzLkWLBV3dpolQxcP5V2aJ5YmzJYJbTf)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看