时间:2016-12-30 04:39:39

标签: selenium nullpointerexception pom.xml

任何人都可以在下面的帖子中解决问题。

Selenium Framework Page Object Model and Page Navigation

当页面返回其他页面的对象时,我无法解决空指针异常问题。任何人都可以告诉我这样做的确切方法。正如上面的链接所解释的,它不清楚错误是怎么回事已经解决了。谢谢。

3 个答案:

答案 0 :(得分:0)

在下面的示例中,我们创建了登录类家庭类测试类 ...

没有必要为一个页面创建一个类,您可以根据自己的方便对功能或模块进行分组......

摘要: -

  • 创建包含所有必需对象的登录类... loginas 方法应返回主页类.....

    db.test.aggregate([
      {$unwind: "$member"},
      {'$project':{'member.name':1}},
      {'$group':{'_id':'$member.name','count':{'$sum':1}}},
      {'$sort':{'count':-1}},
      {'$limit':1}
    ]) 
    

<强> 2。创建了主页类,我在这里只添加了一个示例验证方法,可能是您可以根据应用需求添加方法...

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();
    }
}
  1. 现在我们将看看如何将上述课程放在一起,并按照您的要求使其工作......

    班级考试{

    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;
        }
    }
    
  2. 在这一行 public static void main(String[] args) { LoginPage login = new LoginPage(new FirefoxDriver()); // login and verify records in home page login.loginAs("myname", "pass@@").verifyRecords(); } } 中,我们从login类调用一个方法,被调用的方法将返回HomePage类....

    你可以创建任何没有pom类并像上面那样返回它......就像创建一些像dashboard这样的东西并从主页类中返回它

    返回页面的工作原理: - login.loginAs(&#34; myname&#34;,&#34;传递@@&#34;)。verifyRecords();

    在上面的代码中,一旦这个login.loginAs("", "").verifyRecords();代码段执行,它将完成登录操作并返回此方法login.loginAs("myname", "pass@@"),而这又返回新的HomePage ...... 这里一旦submitLogin();的执行完成,它将成为新的主页对象...... Javacompiler非常聪明,可以操纵它并在login.loginAs("myname", "pass@@")之后放置一个点后提供主题对象的方法

    如果您需要澄清,请告诉我。

    谢谢,

    TechDog

答案 1 :(得分:0)

我将尝试使用3种不同的类型来说明PageFactory示例。

基类 - 具有类似驱动程序声明的配置设置

POM Class-包含单页的POM对象

包含测试步骤的测试类 -

BaseClass示例

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);
}

答案 2 :(得分:0)

package 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
}

}

// test class 2

package 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();
}
}

以下是我的主要课程:

package 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();
    }

}