Example Page Object (II) - with the use of PageFactory

Before the introduction of the concept of Page Object and basic use ( here ), although to achieve a separation of the separation pages and pages, and test pages, but sometimes may just change the individual elements of the id or something, do not need to change more, PageFactory ( Wiki Selenium ) Selenium is used to help funny easier to achieve a Page Object tools. 

In this paper, the article gives the difference between using and not using PageFactory:

PageFactory: LoginPage.java:

. 1  Import org.openqa.selenium.WebDriver;
 2  Import org.openqa.selenium.WebElement;
 . 3  Import org.openqa.selenium.support.CacheLookup;
 . 4  Import org.openqa.selenium.support.FindBy;
 . 5  // need to import PageFactory packet 
. 6  Import org.openqa.selenium.support.PageFactory;
 . 7  Import java.util.List;
 . 8  Import java.util.concurrent.TimeUnit;
 . 9  
10  public  class the LoginPage {
 . 11      the WebDriver Driver;
 12 is  
13 is      // Find all elements on put the front page, if the latter easily modified UI
14      // @FindBy is a feature that will be called when using the actual findElement
 15      // If it is determined this will not change in the current elements of the page, you can add @CacheLookup, looked for the first time after this element does not Find again 
16      @FindBy (ID = "lbNormal" )
 . 17      @CacheLookup
 18 is      Private WebElement accoutLogin;
 . 19      @FindBy (tagName = "iframes" )
 20 is      Private List <WebElement> iframes to;
 21 is      @FindBy (name = "In Email" )
 22 is      Private loginEmail WebElement;
 23 is      @FindBy (name = "password" )
 24      Private WebElement loginPassword;
 25     @FindBy(id = "dologin")
26     private WebElement loginButton;
27 
28     public LoginPage(WebDriver driver) {
29         this.driver = driver;
30     }
31 
32     public HomePage login(String user, String pwd) {
33         driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
34         accoutLogin.click();
35         String id = iframes.get(0).getAttribute("id");
36         if (id != null) {
37             driver.switchTo().frame(id);
38 is          }
 39          loginEmail.sendKeys (User);
 40          loginPassword.sendKeys (pwd);
 41 is          loginButton.click ();
 42 is          driver.switchTo () defaultContent ();.
 43 is          .. Driver.manage () Timeouts () implicitlyWait (10 , TimeUnit.SECONDS);
 44 is  
45          // case of using driver instance initialization @FindBy PageFactory transfer element and instantiates a jump is page 
46 is          the homePage homePage = new new the homePage ();
 47          PageFactory.initElements (driver, homePage);
 48          return homePage;
 49      }
 50 }

PageFactory: LoginTest.java

 1 import org.openqa.selenium.WebDriver;
 2 import org.openqa.selenium.support.PageFactory;
 3 import org.testng.Assert;
 4 import simple.com.faye.selenium.pages.HomePage;
 5 import simple.com.faye.selenium.pages.LoginPage;
 6 import simple.com.faye.selenium.utils.SeleniumInit;
 7 
 8 public class LoginTest {
 9     public static void main(String[] args) {
10         SeleniumInit seleniumInit = new SeleniumInit();
11         Driver = the WebDriver seleniumInit.getFFDriver ();
 12 is          String the URL = "https://mail.163.com/" ;
 13 is          String User = "Mailbox" ;
 14          String pwd = "password" ;
 15          the try {
 16              driver.get ( the URL of);
 17              LoginPage the LoginPage = new new LoginPage (Driver);
 18              // Note to initialize LoginPage page (homePage does not need to because the login method has been initialized) 
19              PageFactory.initElements (Driver, the LoginPage);
 20              the homePage homePage = loginPage.login (User, pwd);
 21 is             Assert.assertTrue(homePage.idExists(), "login failed");
22         } catch (Exception e) {
23             e.printStackTrace();
24         } finally {
25             driver.quit();
26         }
27     }
28 }

 LoginPage.java comparison:

. 1  class LoginPagePageObejectOnly {
 2      the WebDriver Driver;
 . 3  
. 4      public the HomePage loginOperation (User String, String pwd) {
 . 5          // into the frame a little, if any change need not PageFactory have access to all elements of this modification method 
. 6          WebElement loginEmail = driver.findElement (By.name ( "In Email" ));
 . 7          loginEmail.sendKeys (User);
 . 8  
. 9          // returns directly to the new page, no need to call PageFactory.initElements, but to pass driver example 
10          return  new new the HomePage (driver);
 . 11      }
 12 is  }
 13 is  
14  class LoginPageWithPageFactory {
 15     ; WebDriver Driver
 16      // subject to change just change this one all the other elements are using this method without modifying
 17      // will not find again if coupled @CacheLookup, first looked for this element 
18      @FindBy (name = "In Email" )
 . 19      Private WebElement loginEmail;
 20 is  
21 is      public the HomePage loginOperation (User String, String pwd) {
 22 is          // into the frame slightly, when used to call findElement 
23 is          loginEmail.sendKeys (User);
 24  
25          // use examples of the lower driver element PageFactory transmitting initialization @FindBy and instantiate a page jump is 
26 is          the homePage homePage = new new the homePage ();
 27          PageFactory.initElements (driver, homePage);
28         return homePage;
29     }
30 }

 Use the above code you can see the difference and how to use, PageFactory there @FindBys to Find List <WebElement>, and the like in the following ways, but personally do not recommend, because this method applies only to find by id and name, and the element variable name and need to find the value of the same page elements, or a change-all change, not worth:

. 1  public  class the HomePage {
 2      // the first element id or name defined 
. 3      Private WebElement spnUid;
 . 4  
. 5      public  Boolean idExists () {
 . 6          // performed first driver.findElement (By.id ( "spnUid") ) find use elements
 7          // if not then perform driver.findElement (By.name ( "spnUid") ) to find elements
 8          // no will throw NoSuchElementException, all common way of comparison @FindBy can use findByElement is not enough 
9          System.out.println (spnUid.getText ());
 10          return spnUid.isDisplayed ();
 . 11      }
 12 is }

 

Guess you like

Origin www.cnblogs.com/fayez/p/11725051.html