无法使用Page对象模型和TestNG框架

时间:2017-03-19 19:14:15

标签: selenium firefox webdriver testng pom.xml

任何人都可以帮我调试这段代码,我用基本的POM和Testng框架编写了这段代码。我无法弄清楚为什么在我的测试用例中没有调用与“ClickJoin”相关的方法。

我基本上有2个页面,一个“登录”,其中包含与该页面相关的所有元素和方法,以及另一个“登录验证”页面,我试图调用这些方法。

错误:

    java.lang.NullPointerException
        at com.gptoday.pages.Login.ClickJoin(Login.java:20)
        at com.gptoday.com.gptoday.testcases.LoginVerification.f(LoginVerification.java:40)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:108)
        at org.testng.internal.Invoker.invokeMethod(Invoker.java:661)
        at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:869)
        at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1193)
        at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:126)
        at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
        at org.testng.TestRunner.privateRun(TestRunner.java:744)
        at org.testng.TestRunner.run(TestRunner.java:602)
        at org.testng.SuiteRunner.runTest(SuiteRunner.java:380)
        at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:375)
        at org.testng.SuiteRunner.privateRun(SuiteRunner.java:340)
        at org.testng.SuiteRunner.run(SuiteRunner.java:289)
        at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
        at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
        at org.testng.TestNG.runSuitesSequentially(TestNG.java:1301)
        at org.testng.TestNG.runSuitesLocally(TestNG.java:1226)
        at org.testng.TestNG.runSuites(TestNG.java:1144)
        at org.testng.TestNG.run(TestNG.java:1115)
        at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:132)
        at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:230)
        at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:76)

**My code for Login Page:**
package com.gptoday.pages;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
public class Login {

        public WebDriver driver;
        By join = By.xpath("//*[@id='members']/a[1]");
        By username = By.id("username");
        By password = By.id("password");
        By loginButton = By.name("Login");

        /*public Login(WebDriver driver){
            this.driver=driver;
        }*/

        public void ClickJoin(){
            driver.findElement(join).click();
            System.out.println("Clicked Join");
        }

        public void EnterUsername(){
            driver.findElement(username).clear();
            driver.findElement(username).click();
            System.out.println("Username Entered");
        }

        public void EnterPassword(){
            driver.findElement(password).clear();
            driver.findElement(password).click();
            System.out.println("Password Entered");
        }

        public void ClickButton(){
            driver.findElement(loginButton).click();
            System.out.println("Login Button Clicked");
        }

}

**My Code for "Verification Testcase":**
package com.gptoday.com.gptoday.testcases;

import org.testng.annotations.Test;
import com.gptoday.pages.Login;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.AfterTest;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.firefox.FirefoxProfile;
import org.openqa.selenium.firefox.internal.ProfilesIni;

public class LoginVerification {

    public WebDriver driver;
    Login obj = new Login();

    /*LoginVerification(WebDriver driver){
        this.driver =driver;
    }*/

@BeforeTest
    public void amg() {
        ProfilesIni prof = new ProfilesIni();
        FirefoxProfile ffProfile= prof.getProfile ("vishvesh");
        ffProfile.setAcceptUntrustedCertificates(true);
        ffProfile.setAssumeUntrustedCertificateIssuer(false);

        String base_url = "https://www.gptoday.com/";
        System.setProperty("webdriver.gecko.driver", "G:/Workplace/AutomationSetupFiles/Geckdriver1/geckodriver.exe");
        driver = new FirefoxDriver(ffProfile);
        driver.get(base_url);
      }
@Test
    public void f() {
      driver.manage().window().maximize();
      obj.ClickJoin();
      //driver.findElement(By.xpath("//*[@id='members']/a[1]")).click();
      obj.EnterUsername();
      obj.EnterPassword();
      //obj.ClickButton();
      //driver.navigate().refresh();
      //WebDriverWait wait = new WebDriverWait(driver,10);
      //wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id='fpBox1']/a/div")));
      }

@AfterTest
    public void just() {
      System.out.println("Success");
  }

}

    enter code here
    @AfterTest
    public void just() {
      System.out.println("Success");
  }

}

1 个答案:

答案 0 :(得分:1)

您的driver对象无法初始化,因此null

public class Login {

        public WebDriver driver;
        By join = By.xpath("//*[@id='members']/a[1]");
        By username = By.id("username");
        By password = By.id("password");
        By loginButton = By.name("Login");

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

然后你需要:

public class LoginVerification {

    public WebDriver driver;
    Login obj = null;

    @BeforeTest
    public void amg() {
        ProfilesIni prof = new ProfilesIni();
        FirefoxProfile ffProfile= prof.getProfile ("vishvesh");
        ffProfile.setAcceptUntrustedCertificates(true);
        ffProfile.setAssumeUntrustedCertificateIssuer(false);

        String base_url = "https://www.gptoday.com/";
        System.setProperty("webdriver.gecko.driver", "G:/Workplace/AutomationSetupFiles/Geckdriver1/geckodriver.exe");
        driver = new FirefoxDriver(ffProfile);
        driver.get(base_url);
        this.driver = driver;
        obj = new Login(driver);
      }
      ........
}