2017-03-19 12 views
-2

基本的なPOMとTestngフレームワークを使用して記述したこのコードをこのデバッグに手伝ってもらえますか?テストケースで「ClickJoin」に関連するメソッドが呼び出されない理由を理解できません。PageオブジェクトモデルとTestNGフレームワークを使用してWebページ上のリンクをクリックすることはできません

私は基本的に、そのページに関連するすべての要素とメソッドと、メソッドを呼び出そうとしている別の「ログイン確認」ページを持つ「ログイン」2ページの1つを持っています。

エラー:

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

あなた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); 
     } 
     ........ 
} 
+0

おかげで、それがおくる..働きました! –

+0

助けになるのはうれしいです。この場合、回答として受け入れてください。 – Cosmin

関連する問題