First, how to create object repository
. 1 Package pageclasses; 2 . 3 Import org.openqa.selenium.WebDriver; . 4 Import org.openqa.selenium.WebElement; . 5 Import org.openqa.selenium.support.FindBy; . 6 Import org.openqa.selenium.support.PageFactory; . 7 8 public class SearchPageFactory { 9 10 WebDriver Driver; 11 12 13 // how to create an object factory 14 // create object repository findBy need to use this component, findBy selenium can tell what way to find elements 15 @FindBy (the above mentioned id = "the Tab the Tab-the HP--flight " ) 16 WebElement fly; 17 18 @FindBy(xpath="//input[@id='flight-origin-hp-flight']") 19 WebElement startText; 20 21 @FindBy(xpath="//input[@id='flight-destination-hp-flight']") 22 WebElement endText; 23 24 @FindBy(xpath="//input[@id='flight-departing-hp-flight']") 25 WebElement startData; 26 27 @FindBy(xpath="//input[@id='flight-returning-hp-flight']") 28 WebElement returnData; 29 30 @FindBy (XPath = "// div [@ class = 'nested ab25184-Submit-cols'] // Button [@ class = 'Primary BTN-BTN-Action GCW-Submit']" ) 31 is WebElement Searchbutton; 32 33 is public SearchPageFactory (the WebDriver Driver) { 34 is the this .driver = Driver; 35 // to find elements is initialized, then we can use the well 36 // the this represents the object of the present type 37 [ PageFactory.initElements (Driver, the this ); 38 is } 39 40 // operation warehouse element 41 is public void clickFly () { 42 is fly.click (); 43 is } 44 45 public void writStartText(String text) { 46 startText.sendKeys(text); 47 } 48 49 public void writEndText(String text) { 50 endText.sendKeys(text); 51 } 52 53 public void writStartData(String text) { 54 startData.sendKeys(text); 55 } 56 57 public void writReturnData(String text) { 58 returnData.sendKeys(text); 59 } 60 61 public void clickSearchButton() { 62 searchButton.click(); 63 } 64 }
1, how to create object repository:
@FindBy(id="tab-flight-tab-hp")
WebElement fly;
Id: express way finding elements (also XPath , name , etc.)
tab-flight-tab-hp: represent elements need to find
Fly : representation is WebElement type of variable name
2, the elements to find the object repository is initialized, so that we can use to: . PageFactory initElements (Driver, the this );
Driver : represents the browser object
The this : This indicates the type of the object
Second, the reference to an instance object repository
package pageclasses; import java.util.concurrent.TimeUnit; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.runner.notification.RunListener.ThreadSafe; import org.openqa.selenium.WebDriver; import org.openqa.selenium.chrome.ChromeDriver; class FarmeworkTestCase { WebDriver driver; String url; SearchPageFactory search; @BeforeEach void setUp() throws Exception { driver = newChromeDriver (); Search = new new SearchPageFactory (Driver); URL = "https://www.expedia.com/" ; driver.manage () window () Maximize ();.. . Driver.manage () Timeouts () .implicitlyWait ( 10 , TimeUnit.SECONDS); driver.get (url); } @Test void the Test () throws InterruptedException { // click on the ticket to enter the ticket booking page search.clickFly (); // comfortable departure search. writStartText ( "Changsha, China (CSX- Huanghua international Airport)" ); // enter the destination Thread.sleep (2000 ); search.writEndText ( "Shanghai, China (PVG- Pudong International Airport)" ); // enter a starting date search.writStartData ( "08/01/2019" ); // input return date search.writReturnData ( "08/03 / 2019 " ); // point search button search.clickSearchButton (); } @AfterEach void the tearDown () throws Exception { the Thread.sleep ( 2000 ); driver.quit (); } }