2016-12-30 142 views

回答

0

在下面的例子中,我們已經創建了一個登錄類家庭類測試類 ...

這是沒有必要創建一個類爲一個頁面可能是你可以組的功能或模塊根據您的方便......

摘要: -

  • 創建登錄類具有所有需要的對象......和loginas方法應該返回主頁類.....

    public class LoginPage { 
        private final WebDriver driver; 
    
        public LoginPage(WebDriver driver) { 
         this.driver = driver; 
    
         // Check that we're on the right page. 
         if (!"Login".equals(driver.getTitle())) { 
          // Alternatively, we could navigate to the login page, perhaps logging out first 
          throw new IllegalStateException("This is not the login page"); 
         } 
        } 
    
        // The login page contains several HTML elements that will be represented as WebElements. 
        // The locators for these elements should only be defined once. 
         By usernameLocator = By.id("username"); 
         By passwordLocator = By.id("passwd"); 
         By loginButtonLocator = By.id("login"); 
    
        public LoginPage typeUsername(String username) { 
         driver.findElement(usernameLocator).sendKeys(username); 
    
         // Return the current page object as this action doesn't navigate to a page represented by another PageObject 
         return this;  
        } 
    
        public LoginPage typePassword(String password) { 
         driver.findElement(passwordLocator).sendKeys(password); 
    
         // Return the current page object as this action doesn't navigate to a page represented by another PageObject 
         return this;  
        } 
    
        public HomePage submitLogin() { 
         // This is the only place that submits the login form and expects the destination to be the home page. 
         // A seperate method should be created for the instance of clicking login whilst expecting a login failure. 
         driver.findElement(loginButtonLocator).submit(); 
    
         // Return a new page object representing the destination. Should the login page ever 
         // go somewhere else (for example, a legal disclaimer) then changing the method signature 
         // for this method will mean that all tests that rely on this behaviour won't compile. 
         return new HomePage(driver);  
        } 
    
    
    
        // Conceptually, the login page offers the user the service of being able to "log into" 
        // the application using a user name and password. 
        public HomePage loginAs(String username, String password) { 
         // The PageObject methods that enter username, password & submit login have already defined and should not be repeated here. 
         typeUsername(username); 
         typePassword(password); 
         return submitLogin(); 
        } 
    } 
    

2.製作的網頁類,我在這裏只添加了一個樣品驗證方法,可能是您可以根據您的應用需求添加方法...

class HomePage { 
    private final WebDriver driver; 

    public HomePage(WebDriver driver) { 
     this.driver = driver; 

     // Check that we're on the right page. 
     if (!"HOME".equals(driver.getTitle())) { 
      // Alternatively, we could navigate to the login page, perhaps 
      // logging out first 
      throw new IllegalStateException("This is not the home page"); 
     } 
    } 

    // below method is just a sample method 
    public HomePage verifyRecords() { 

     /// your homepage validation 
     return null; 
    } 
} 
  1. 現在我們來看看如何把上面的類聚,並使其按您的要求工作...

    類測試{

    public static void main(String[] args) { 
    
         LoginPage login = new LoginPage(new FirefoxDriver()); 
    
         // login and verify records in home page 
         login.loginAs("myname", "[email protected]@").verifyRecords(); 
    
        } 
    } 
    

在這一行login.loginAs("", "").verifyRecords();我們調用一個方法從登錄類和正在調用的方法將返回HomePage類....

您可以創建任何沒有POM類,並返回它像上面......像創建一些像儀表板和返回。從網頁類

如何返回頁面工作: - login.loginAs( 「MYNAME」, 「通@@」)verifyRecords();

在上面的代碼中,一旦這個login.loginAs("myname", "[email protected]@")片段執行時,它會完成登錄操作和一次login.loginAs("myname", "[email protected]@")執行完成後,將成爲新的Homepageobject返回此方法submitLogin();這又返回新的主頁...... 這裏..... Java編譯器是足夠聰明的操縱,並提供了一旦你把一個點後的網址對象的方法login.loginAs("myname", "[email protected]@").

請讓我知道如果你需要任何澄清..

謝謝,

TechDog

+0

我想知道一個網頁怎麼會返回一個指向另一個頁面在頁面對象的對象model.Suppose我們有2-3個網頁,根據POM,我們將爲所有這些頁面分別設置不同的類。現在如果第一頁上的點擊將我們帶到第二頁,則第一頁中的方法應該返回第二頁的對象按照POM.I想要如何完成,即按照POM建立不同頁面之間的關係。 –

+0

嘗試在您的系統中複製上述示例...在這裏,我們有登錄網頁的Login.java和主頁網頁的HomePage.java ..... Java方法返回類型功能允許返回新對象...返回新的主頁(驅動器); < - 這行創建主頁的新對象.....通過從loginAs方法返回submitLogin(),我們應該能夠返回登錄頁面..... – TechDog

+0

我試過使用相同的東西,但我得到的問題當我使用頁面工廠方法。 –

0

我將設法示出了使用3種不同的類類型PageFactory例子。

基地講座有如驅動聲明配置設置

POM講座包含用於單頁

含有

測試類級測試步驟

BaseClass的實施例

的POM對象
public class BaseClass { 
public WebDriver driver=null; 
public BaseClass() throws MalformedURLException { 
     driver=new firefoxDriver(); 
     driver.manage().window().maximize(); 
     driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS); 
     driver.get(baseURL); 
} 

POM等級示例

//Class1 
public class WebshopHomePage { 
WebDriver driver; 
public WebshopHomePage(WebDriver driver){ 
    this.driver=driver; 
} 

@FindBy(how=How.LINK_TEXT,using="Log in")//Identifying page elements 
WebElement loginLink; 

public void clickLoginLink(){ 
    loginLink.click(); 
} 
} 

//Class2 
public class SignInSignUpPage { 
WebDriver driver; 

public SignInSignUpPage(WebDriver driver){ 
    this.driver=driver;  
} 

@FindBy(how=How.ID,using="Email") 
WebElement emailID; 
} 

識別TestClass例

public class WebShopSignInTest extends BaseClass { 
@Test 
public void testSteps() { 

System.out.println("I'm in teststeps!!"); 
WebshopHomePage wshpObj=PageFactory.initElements(driver,WebshopHomePage.class);//Assigning POM class1 objects to driver 
wshpObj.clickLoginLink(); 


SignInSignUpPage signInSignUpPageObj=PageFactory.initElements(driver, SignInSignUpPage.class);//Assigning POM class2 objects to driver 
signInSignUpPageObj.enterCredsSubmit(userID, passw0rd); 
} 
+0

感謝Prithvi的上述例子。但是,這是基本的例子,它只涉及1頁,如果你看到,因爲我們只是做一個登錄應用程序..我問的是什麼如果主頁,下載等2-3頁有2-3個POM類。那麼,我們將如何建立不同頁面之間的關係。如果您看到我分享的鏈接http://stackoverflow.com/問題/ 34145422/selenium-framework-page-object-model-and-page-navigation/34147044#34147044,在這個相同的東西被提到。我想在這種情況下解決空指針異常。 –

+0

@dheerajkateja:我只是對示例做了一些編輯。注意2 POM類,只要您在整個測試中使用相同的驅動程序實例,就不需要建立關係,即不需要從POM類返回對象。希望這回答你的問題 – prithvi394

+0

相同的原則延伸到多個POM類 – prithvi394

0

包org.pom;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.FindBy;

import org.openqa.selenium.support.PageFactory;

公共類GoogleHomePageObjects {

public GoogleHomePageObjects(WebDriver driver) 

{ 
    PageFactory.initElements(driver, this); 
} 

@FindBy(name="q") 
public WebElement txtSearch; 

@FindBy(name="btnG") 
public WebElement btnSearch; 

@FindBy(linkText="Selenium - Web Browser Automation") 
public WebElement lnkSelenium; 


public void searchGoogle(String SearchText) 

{ 

    txtSearch.sendKeys(SearchText); 
    btnSearch.click(); 
} 
public SeleniumPageObjects clickSelenium() 
{ 
    lnkSelenium.click(); 
    return new SeleniumPageObjects(driver); //Here is the issue and i am  getting error 
} 

}

//測試類2

包org.pom;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.WebElement;

import org.openqa.selenium.support.FindBy;

import org.openqa.selenium.support.PageFactory;

公共類SeleniumPageObjects {

public SeleniumPageObjects(WebDriver driver) 

{ 

PageFactory.initElements(driver, this); 

} 




@FindBy(linkText="Download") 
public WebElement lnkDownload; 

@FindBy(xpath=".//*[@id='header']/h1/a") 
public WebElement lnkHome; 

public void clickDownloads() 
{ 

    lnkDownload.click();//Here it throws null pointer excception 
} 

public void clickHome() 
{ 

    lnkHome.click(); 
} 
} 

下面是我的主類:

包org.pom;

import org.openqa.selenium.WebDriver;

import org.openqa.selenium.firefox。FirefoxDriver;

公共類GoogleSearchTest {

public static void main(String[] args) throws InterruptedException 

{

System.setProperty("webdriver.gecko.driver","D:\\Desktop\\Selenium\\Jars\\geckodriver-v0.11.1-win64\\geckodriver.exe"); 

WebDriver driver=new FirefoxDriver(); 

    driver.get("http://www.google.com"); 

    GoogleHomePageObjects page=new GoogleHomePageObjects(driver); 


    page.searchGoogle("Selenium"); 

    Thread.sleep(4000); 

    SeleniumPageObjects selPage=page.clickSelenium(); 

    Thread.sleep(4000); 


    selPage.clickDownloads(); 

    Thread.sleep(4000); 

    selPage.clickHome(); 
    } 

}

+0

我有2個測試類和一個主類,我試圖進入搜索硒在谷歌,然後點擊第一個鏈接(Selenium網站)。一旦點擊第一個鏈接,頁面更改公共SeleniumPageObjects clickSelenium() { lnkSelenium.click( ); 返回新的SeleniumPageObjects(驅動程序); }它在這一點上顯示錯誤。請你解釋如何達到同樣的效果。 –

+0

我的問題已解決。感謝您的幫助和時間。 –

相關問題