简体   繁体   中英

Recieving a java.lang.NullPointerException when running my project in Page object model with selenium webdriver

I have an issue when creating page object model with selenium webdriver and using testng to create my tests. I Instasiated the webdriver in my web_elements.java page and run my tests in Testing1.java on both these classes I have added the webdriver. But when I run the test I get an error.

web_element.java - page

package Pages;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.util.concurrent.TimeUnit;

public class web_Elements {
private final WebDriverWait wait;
WebDriver driver;


public void LoginTest() throws InterruptedException {
    driver.findElement(By.xpath("//body[@class='ng-scope']/div[@class='ng- scope']/div[@class='container-fluid ng-scope']/div[@class='ng-scope']//button[.='Customer Login']")).click();

}

public void SelectYourName() {
    driver.findElement(By.cssSelector("#userSelect > option:nth-child(3)")).click();
}


public web_Elements(WebDriver driver) {
    this.driver = driver;
    long timeOutInSeconds = 10; // just an arbitrary value example
    this.wait = new WebDriverWait(driver, timeOutInSeconds);

    // the rest of your constructor as in the original code
    driver.manage().timeouts().pageLoadTimeout(10, TimeUnit.SECONDS);
    driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);


}}

Testing1.java - page

package Test_Cases;

import Pages.web_Elements;
import Utilities.DriverFactory;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;



public class Testing1 {


public WebDriver driver;
web_Elements PageTesting;

@BeforeClass
public void Initialize_driver() throws InterruptedException {
    ///Initialize diver
    System.setProperty("webdriver.chrome.driver", 
   "C:\\Users\\rassools\\Documents\\ChromeD\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.way2automation.com/angularjs-protractor/banking/#/login");
    driver.manage().window().maximize();


}

@Test(priority = 0)
public void other() throws InterruptedException {
    PageTesting = new web_Elements(driver);
    PageTesting.LoginTest();
    PageTesting.SelectYourName();

}


public void wait(int seconds) {
    try {
        Thread.sleep(2000);
    } catch (final InterruptedException e) {
        e.printStackTrace();

    }
}
}

Error Message

java.lang.NullPointerException
at com.google.common.base.Preconditions.checkNotNull(Preconditions.java:882)
at org.openqa.selenium.support.ui.FluentWait.<init>(FluentWait.java:104)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:71)
at org.openqa.selenium.support.ui.WebDriverWait.<init>(WebDriverWait.java:45)
at Pages.web_Elements.<init>(web_Elements.java:27)
at Test_Cases.Testing1.other(Testing1.java:32)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:564)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:134)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:597)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:173)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:816)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:146)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:146)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:128)
at java.base/java.util.ArrayList.forEach(ArrayList.java:1510)
at org.testng.TestRunner.privateRun(TestRunner.java:766)
at org.testng.TestRunner.run(TestRunner.java:587)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:384)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:378)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:337)
at org.testng.SuiteRunner.run(SuiteRunner.java:286)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:53)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:96)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1187)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1109)
at org.testng.TestNG.runSuites(TestNG.java:1039)
at org.testng.TestNG.run(TestNG.java:1007)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
 at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110)


 ===============================================
 Default Suite
 Total tests run: 1, Passes: 0, Failures: 1, Skips: 0
 ===============================================


 Process finished with exit code 0

Here

@BeforeClass
public void Initialize_driver() throws InterruptedException {
    ///Initialize diver
    System.setProperty("webdriver.chrome.driver", 
   "C:\\Users\\rassools\\Documents\\ChromeD\\chromedriver.exe");
    WebDriver driver = new ChromeDriver();
    driver.get("http://www.way2automation.com/angularjs-protractor/banking/#/login");
    driver.manage().window().maximize();
}

You're not initializing the field of your class. You just create a new variable and initialize it. It is not visible from the outside of Initialize_driver() method.

To initialize your field you need to change WebDriver driver = new ChromeDriver(); to driver = new ChromeDriver();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM