Getting Null pointer exception when running Selenium through TestNG - java

I have Test base class which calls a method from LandingPage class which has the locator for the page under test. When I execute this project, I'm getting Null pointer exception. I'm sure it has something to do with testNG annotations, but I'm unable to find out the reason.
package com.xyz.tests;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeSuite;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
import com.xyz.pageObjects.LandingPage;
import com.xyz.pageObjects.LoginPage;
import com.xyz.utils.Utils;
import engine.Engine;
public class LandingPageTest {
public WebDriver driver;
//Engine engine = new Engine(driver);
LandingPage landingPage;
LoginPage loginPage;
#BeforeTest
public void setUp(){
driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.get("https://www.go.com");
driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
}
#Test
public void clickOnSignInLink() {
landingPage.SignIn().click();
}
Below is my LandingPage class
package com.xyz.pageObjects;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class LandingPage {
private WebDriver driver;
public LandingPage(WebDriver driver) {
this.driver = driver;
}
//By signIn= By.xpath(".//*[#id='pageContainerInner']/div[2]/div[1]/div/div[2]/div[1]/a");
By signInBtn = By.linkText("Sign In or Create Account");
//By signInBtn2 = By.cssSelector("css=a.signIn");
By closeCrisisMessage = By.xpath(".//*[#id='closeCrisisMessageBtn']");
public WebElement SignIn(){
return driver.findElement(signInBtn);
}
}
Following is the error message
C:\Users\ad\AppData\Local\Temp\testng-eclipse--957796922\testng-customsuite.xml
FAILED: clickOnSignInLink
java.lang.NullPointerException
at com.disney.tests.LandingPageTest.clickOnSignInLink(LandingPageTest.java:36)
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:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Any suggestions will be appreciated.

I'm not sure on what line your NullPointerException is thrown, but I would expect this to fail because your landingPage has not been initialized when the #Test method is run:
landingPage = new LandingPage(driver);

I see, you didn't instantiate landingPage & loginPage anywhere. That's why you are getting a null pointer exception. To get around this issue, you have to instantiate those references (mainly, landingPage here). Instantiate it either in place of declaration or in #BeforeClass annotation.

Related

Second class is throwing Null Pointer exception for TestNG

I've created different classes for each webpage using TestNG. My first class is working fine but the second class is throwing Null Pointer Exception. The same script if I combine in only one class It's working without any issues.
Below is my baseclass
package Test;
import org.testng.annotations.Test;
import Pages.FM_login;
import org.testng.annotations.BeforeTest;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterClass;
import org.testng.annotations.AfterTest;
public class Baseclass {
WebDriver driver;
FM_login objLogin;
#BeforeTest
public void setup() {
System.setProperty("webdriver.chrome.driver", "C:\\Users\\****\\Downloads\\chromedriver.exe");
String url = "http://vatlookup.cloudfront.net";
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.get(url);
driver.manage().timeouts().implicitlyWait(60, TimeUnit.SECONDS);
String Title = driver.getTitle();
System.out.println(Title);
System.out.println("Application Launch successful");
// Accept the cookie policy
driver.findElement(By.xpath("//a[#class='cc-btn cc-allow']")).click();
}
#Test
public void Login() {
objLogin = new FM_login(driver);
objLogin.GoToLoginScreen();
objLogin.loginToFleetMatch("abc", "123");
System.out.println("Login Successful");
}
#AfterTest(enabled=false)
public void teardown() {
driver.close();
}
This is another class
package Test;
import org.openqa.selenium.WebDriver;
import org.testng.annotations.Test;
import Pages.FM_Profile;
public class Profile {
WebDriver driver;
FM_Profile objProfile;
#Test
public void Skip_Tutorial() {
objProfile = new FM_Profile(driver);
driver.getCurrentUrl();
objProfile.SkipTutorial();
}
#Test(enabled=false)
public void edit_profile() {
objProfile.edit_profile();
}
}
below is the page object for a second class
package Pages;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class FM_Profile {
WebDriver driver;
By company_name = By.xpath("//p[#id='company-name']");
By plan_details = By.xpath("//p[#id='current-plan']");
By End_tour = By.xpath("//button[#class='driver-close-btn']");
By edit_profile = By.xpath("//span[contains(text(),'Edit')]");
By Description = By.xpath("//textarea[#id='mat-input-10']");
By Upload_image = By.xpath("//div[#class='upload-btn continental-book ng-star-inserted']");
By Save_profile = By.xpath("//button[#class='save-btn continental-book mat-stroked-button mat-button-base mat-primary editMode cdk-focused cdk-mouse-focused']");
public FM_Profile(WebDriver driver) {
this.driver = driver;
}
public void SkipTutorial() {
driver.findElement(End_tour).click();
}
after executing it's showing java.lang.NullPointerException for second class
error
java.lang.NullPointerException
at Test.Profile.Skip_Tutorial(Profile.java:16)
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:567)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:135)
at org.testng.internal.TestInvoker.invokeMethod(TestInvoker.java:598)
at org.testng.internal.TestInvoker.invokeTestMethod(TestInvoker.java:174)
at org.testng.internal.MethodRunner.runInSequence(MethodRunner.java:46)
at org.testng.internal.TestInvoker$MethodInvocationAgent.invoke(TestInvoker.java:821)
at org.testng.internal.TestInvoker.invokeTestMethods(TestInvoker.java:147)
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:1507)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:588)
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:1214)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1136)
at org.testng.TestNG.runSuites(TestNG.java:1066)
at org.testng.TestNG.run(TestNG.java:1034)
at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:115)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:251)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:77)
In your Profile class the driver object is not initialized. The driver object that you are initializing is in BaseClass and instance of that class. You need to initialize driver object in Profile class as well
WebDriver is never initialised.
WebDriver driver = new WebDriver();

Selenium java.lang.NullPointerException with PageFactory

I'm trying to get a hang of PageFactory POM, however something is not working and I can not understand what is wrong.
This is my first POM class for Home Page:
package PageFactory;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class Home_Page_POF {
public WebDriver driver;
#FindBy(css = "div#header-profile a#header-profile-toggle")
public WebElement profileToggleButton;
#FindBy(css = "form#loginUserdataForm div.footer div.add-footer a.btn.btn-link.linkicon")
public WebElement newRegistrationButton;
public Home_Page_POF(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
}
This is the second POM class for the Reg. page
package PageFactory;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.openqa.selenium.support.ui.Select;
public class Registration_Page_POF {
public WebDriver driver;
//Personal details WebElements
#FindBy(css = "form#personalDetailsForm div.profile.pe div.profile-block.simple.first fieldset#pefield-title select#pesalutation")
public WebElement titleDropdown;
public Select titleSelect = new Select(titleDropdown);
public Registration_Page_POF(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
}
}
And this is the test case:
package Tests;
import PageFactory.Home_Page_POF;
import PageFactory.Registration_Page_POF;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.io.TemporaryFilesystem;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import java.util.concurrent.TimeUnit;
public class Test_POF {
public WebDriver driver;
Home_Page_POF objHomePage;
Registration_Page_POF objRegPage;
#BeforeClass
public void browserSetUp() {
System.setProperty("webdriver.chrome.driver", "D:/Install/selenium-2.53.0/drivers/chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
driver.navigate().to("http://www.lufthansa.com/");
}
#AfterClass
public void broserCleanUp() {
if (driver != null) {
TemporaryFilesystem.getDefaultTmpFS().deleteTemporaryFiles();
driver.close();
driver.quit();
}
}
#Test
public void Test0001() {
objHomePage = new Home_Page_POF(driver);
objHomePage.profileToggleButton.click();
objHomePage.newRegistrationButton.click();
objRegPage = new Registration_Page_POF(driver);
Select titleSelect = new Select(objRegPage.titleDropdown);
titleSelect.selectByVisibleText("Mr.");
}
}
So the HomePage objects are working fine, I click on two buttons and proceed to registration page.
After that I want to select a value from dropdown, but it gives me NullPointerException:
java.lang.NullPointerException
at org.openqa.selenium.support.ui.Select.<init>(Select.java:44)
at PageFactory.Registration_Page_POF.<init>(Registration_Page_POF.java:17)
at Tests.Test_POF.Test0001(Test_POF.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:85)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:639)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:816)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1124)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:108)
at org.testng.TestRunner.privateRun(TestRunner.java:774)
at org.testng.TestRunner.run(TestRunner.java:624)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:359)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:354)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:312)
at org.testng.SuiteRunner.run(SuiteRunner.java:261)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1215)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1048)
at org.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:74)
at org.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:121)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
The Problem is you are initializing the varaible titleSelect in class itself. Just Initialize inside constructor or after finding the element;
public class Registration_Page_POF {
public WebDriver driver;
//Personal details WebElements
#FindBy(css = "form#personalDetailsForm div.profile.pe div.profile-block.simple.first fieldset#pefield-title select#pesalutation")
public WebElement titleDropdown;
public Select titleSelect; //Dont initialize here
public Registration_Page_POF(WebDriver driver) {
this.driver = driver;
PageFactory.initElements(driver, this);
titleSelect = new Select(titleDropdown);//initialize here
}
}

Selenium Java, class to run same setup across tests

I had started at this original post
This has been very enlightening to read. I am in the same situation and have been trying to use this as a guide. Unfortunately, I am getting a java.lang.NullPointerException which I know why, its because the page isn't getting loaded but I am not sure why.
Here is my base test class:
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import org.junit.After;
import org.junit.Before;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
public class IRIS_Firefox_Test_Base {
static WebDriver driver;
static String baseURL;
#Before
public void setup() throws IOException{
baseURL = "http://localhost:3006";
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.get(baseURL);
}
#After
public void closeFireFox(){
driver.quit();
}
}
And my test:
import java.io.IOException;
import org.junit.Test;
import pageObjects.Login_Page;
public class POM_TC extends IRIS_Firefox_Test_Base {
#Test
public void login() throws IOException{
super.setup();
Login_Page.loginInput(driver).sendKeys("scornell");
Login_Page.passwordInput(driver).sendKeys("password");
Login_Page.loginBtn(driver).click();
}
}
I'm not really a well versed java programmer, my background is more JavaScript and C#(not a lot there either though). Maybe I'm missing something simple.
Here is my stack trace:
FAILED: login
java.lang.NullPointerException
at pageObjects.Login_Page.loginInput(Login_Page.java:13)
at com.hiiq.qa.testing.gen2.Login_Tests.POM_TCTest.login(POM_TCTest.java:11)
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:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
thanks to all for their input.
I would add a page load time out:
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
This allows the page to load, giving the program time to catch up and verify that certain elements are available on the page. I would make your setup look like:
#Before
public void setup() throws IOException{
baseURL = "http://localhost:3006";
driver = new FirefoxDriver();
driver.get(baseURL);
driver.manage().window().maximize();
driver.manage().timeouts().pageLoadTimeout(30, TimeUnit.SECONDS);
}
Remove the implicit wait, and add the pageLoadTimeout, also I moved the maximize portion until after the URL is called.

How can I pass this code successfully? Java Webdriver TestNg

I want to pass this code to my other classes so I don't have to keep pasting it.
This is the class containing the code:
package utility;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
public class BrowserType {
public static WebDriver driver;
#Parameters("browser")
#Test
public static void CallBrowser(String browser) {
if(browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
// If browser is IE, then do this
}else if (browser.equalsIgnoreCase("chrome")) {
// Here I am setting up the path for my IEDriver
{System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");}
driver = new ChromeDriver();
driver.get(Constant.URL);
}
}
}
I want to pass CallBrowser to SetUp which is below:
package automationFramework;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Parameters;
import org.testng.annotations.Test;
import utility.Constant;
import appModule.SignIn_Action;
public class SignIn {
public WebDriver driver;
#BeforeMethod
#Parameters("browser")
public void SetUp(String browser) {
if(browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
// If browser is Chrome, then do this
}else if (browser.equalsIgnoreCase("chrome")) {
{ System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");}
driver = new ChromeDriver();
{ driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);}
{driver.manage().window().maximize();}
driver.get(Constant.URL);
}
}
#Test
public void signIn() {
SignIn_Action.Execute(driver, Constant.DevStudentUsername, Constant.DevStudentPassword);
}
#AfterMethod
public void Teardown() {
driver.quit();
}
}
I tried just calling the static class, but then #BeforeMethod is throwing a configuration error, I'm sure I am just doing it wrong with passing driver, and parameters.
Can someone please explain the changes I need to make to both classes to make it work correctly?
These are the errors:
FAILED CONFIGURATION: #AfterMethod Teardown
java.lang.NullPointerException
at automationFramework.SignIn.Teardown(SignIn.java:38)
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:84)
at org.testng.internal.Invoker.invokeConfigurationMethod(Invoker.java:564)
at org.testng.internal.Invoker.invokeConfigurations(Invoker.java:213)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:786)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
FAILED: signIn
java.lang.NullPointerException
at appModule.SignIn_Action.Execute(SignIn_Action.java:27)
at automationFramework.SignIn.signIn(SignIn.java:33)
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:84)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:714)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:901)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1231)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
at org.testng.TestRunner.privateRun(TestRunner.java:767)
at org.testng.TestRunner.run(TestRunner.java:617)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:334)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:291)
at org.testng.SuiteRunner.run(SuiteRunner.java:240)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1224)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1149)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
===============================================
Default test
Tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================
===============================================
Suite
Total tests run: 1, Failures: 1, Skips: 0
Configuration Failures: 1, Skips: 0
===============================================
This is how i am trying to call the code:
public WebDriver driver;
#BeforeMethod
#Parameters("browser")
public void SetUp(String browser) {
BrowserType.CallBrowser(browser);
}
Your issue is that your driver is not initialized when you call teardown. You need to make sure it is initialized in all cases. I would go through your code with a debugger and check to make sure that when you hit the setup and teardown method that the driver is in fact initialized at setup and exists at tear down. You also have two drivers present when things run. One is your static driver that you declare in the first part of your code, and then you have another non-static driver located with your actual tests. I would fix that as it may be causing issues.
The easiest way to solve your issue is to make sure the driver is ALWAYS initialized during setup, even if the string does not match either option.
public void SetUp(String browser) {
if(browser.equalsIgnoreCase("firefox")) {
driver = new FirefoxDriver();
// If browser is Chrome, then do this
}else {
System.setProperty("webdriver.chrome.driver","C:/Users/elsid/Desktop/Eclipse/Selenium/chromedriver.exe");
driver = new ChromeDriver();
{ driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);}
{driver.manage().window().maximize();}
driver.get(Constant.URL);
}
}

Cannot instantiate class

iam facing a problem i have written a method navback, which i need to use regularily to navigate back. when iam running it is throwing a error.
below is the Code.
package Examples;
import java.util.concurrent.TimeUnit;
//import org.junit.BeforeClass;
import org.testng.annotations.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class Flipkart {
public static WebDriver driver;
Actions action = new Actions(driver);
//String ddd;
// public Example2() {
// super();
// }
#BeforeClass
public void beforeClass()
{
driver = new FirefoxDriver();
driver.manage().timeouts().implicitlyWait(30000, TimeUnit.MILLISECONDS);
}
#Test
public void mailSend() throws InterruptedException
{
driver.get("https://www.flipkart.com/");
driver.manage().window().maximize();
navback();
driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/ul/li/div/div[2]/div/ul/li[2]/a")).click();
driver.navigate().back();
navback();
driver.findElement(By.xpath("/html/body/div/div/div[2]/div/div/ul/li/div/div[2]/div/ul/li[3]/a")).click();
driver.navigate().back();
driver.navigate().refresh();
}
public void navback()
{
WebElement we = driver.findElement(By.xpath("//html/body/div/div/div[2]/div/div/ul/li/a/span"));
action.moveToElement(we).build().perform();
}
#AfterClass
public void tear()
{
// driver.quit();
}
}
Below is the Error.
org.testng.TestNGException:
Cannot instantiate class Examples.Flipkart
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:38)
at org.testng.internal.ClassHelper.createInstance1(ClassHelper.java:387)
at org.testng.internal.ClassHelper.createInstance(ClassHelper.java:299)
at org.testng.internal.ClassImpl.getDefaultInstance(ClassImpl.java:110)
at org.testng.internal.ClassImpl.getInstances(ClassImpl.java:186)
at org.testng.internal.TestNGClassFinder.<init>(TestNGClassFinder.java:120)
at org.testng.TestRunner.initMethods(TestRunner.java:409)
at org.testng.TestRunner.init(TestRunner.java:235)
at org.testng.TestRunner.init(TestRunner.java:205)
at org.testng.TestRunner.<init>(TestRunner.java:160)
at org.testng.remote.RemoteTestNG$1.newTestRunner(RemoteTestNG.java:141)
at org.testng.remote.RemoteTestNG$DelegatingTestRunnerFactory.newTestRunner(RemoteTestNG.java:271)
at org.testng.SuiteRunner$ProxyTestRunnerFactory.newTestRunner(SuiteRunner.java:561)
at org.testng.SuiteRunner.init(SuiteRunner.java:157)
at org.testng.SuiteRunner.<init>(SuiteRunner.java:111)
at org.testng.TestNG.createSuiteRunner(TestNG.java:1299)
at org.testng.TestNG.createSuiteRunners(TestNG.java:1286)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1140)
at org.testng.TestNG.run(TestNG.java:1057)
at org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111)
at org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204)
at org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
Caused by: java.lang.reflect.InvocationTargetException
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at org.testng.internal.ObjectFactoryImpl.newInstance(ObjectFactoryImpl.java:29)
... 21 more
Caused by: java.lang.NullPointerException
at org.openqa.selenium.interactions.Actions.<init>(Actions.java:41)
at Examples.Flipkart.<init>(Flipkart.java:18)
... 26 more
Please some one help me, iam not able to proceed further.
Thanks
You don't initialize driver, so it is null, but you pass it to Actions.
public static WebDriver driver;
Actions action = new Actions(driver);
That throws a NullPointerException.
Caused by: java.lang.NullPointerException
at org.openqa.selenium.interactions.Actions.<init>(Actions.java:41)
Initialize driver.
Note the lifecycle. Before JUnit runs your #BeforeClass or #Before methods, it has to create the Flipkart instance. The instance field initialization expression runs at that point.
Rethink your design. Initialize action after driver has been initialized.

Categories

Resources