Null pointer exception on testng - selenium

I have written the below code and getting the java null pointer exception.
please help what is the problem.
public class testngbasics {
WebDriver driver;
#BeforeMethod
public void setbrowser() {
System.setProperty("webdriver.chrome.driver","C:\\Users\\chromedriver.exe");
WebDriver driver= new ChromeDriver();
driver.manage().timeouts().pageLoadTimeout(70, TimeUnit.SECONDS);
driver.get("http://demo.guru99.com/v4/");
driver.manage().deleteAllCookies();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(70, TimeUnit.SECONDS);
}
#Test
public void login1() {
driver.findElement(By.xpath("//input[#name='uid']")).sendKeys("mngr212595");
driver.findElement(By.xpath("//input[#name='password']")).sendKeys("EgebYpy");
driver.findElement(By.xpath("//input[#value='LOGIN']")).click();
System.out.println(driver.getTitle());
}
#AfterMethod
public void closebrowser()
{
driver.close();
}
}
I am getting the error:
java.lang.NullPointerException it is only going to #beforemethod
annotation and not going to #test and #aftermethod

As you have already defined a global instance of WebDriver as driver in the line:
WebDriver driver;
You don't have to create another instance of WebDriver again as in:
WebDriver driver= new ChromeDriver();
And you need to replace this line as:
driver = new ChromeDriver();

Related

Error: Session ID is null. Using WebDriver after calling quit()? - Parallel execution - AfterMethod driver.quit/close

Find below my code
TestBase.java
protected WebDriver driver;
#BeforeMethod
public void setUpDriver() {
System.setProperty("webdriver.chrome.driver", getConfigProp.getChromeDriverPath());
driver = new ChromeDriver();
}
#AfterMethod
public void quitDriver() {
driver.close();
driver.quit();
}
MyTests.java extends TestBase.java
#Test
Test1 (){
driver.doSomething()
}
#Test
Test2 (){
driver.doSomething()
}
Error:
org.openqa.selenium.NoSuchSessionException: Session ID is null. Using WebDriver after calling quit()?
I understand it is because of driver.quit(). So I used only driver. close() in AfterMethod. Still failed. I am not using static as well. What is the Solution.
I got it working using Threadlocal driver. Thanks

Unable to Launch browser

public class LaunchBrowser {
public static void main(String[] args) {
WebDriver driver = new FirefoxDriver();
System.setProperty("webdriver.gecko.driver", "/Users/spectra/Drivers/geckodriver");
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
driver.get("https://www.seleniumhq.org");
}
}
Exception in thread "main" java.lang.Error: Unresolved compilation problems:
WebDriver cannot be resolved to a type
FirefoxDriver cannot be resolved to a type
at com.simplilearn.day2.oops.LaunchBrowser.main(LaunchBrowser.java:16)
setProperty before driver initialization which should be like
public static void main(String[] args) {
System.setProperty("webdriver.gecko.driver", "/Users/spectra/Drivers/geckodriver");
WebDriver driver = new FirefoxDriver();
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(40,TimeUnit.SECONDS);
driver.manage().timeouts().pageLoadTimeout(90, TimeUnit.SECONDS);
driver.get("https://www.seleniumhq.org");
}

Getting Null pointer exception when declaring Webdriver as null outside the method and also declaring webdriver inside the method

Can some one explain why i am getting Null pointer exception when code execution reaches login() method in below script
public class TC_01_CreateEmployee {
WebDriver driver=null;
public void launchBrowser() throws Exception
{
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
}
public void login()
{
driver.get("******");
driver.findElement(By.id("txtUsername")).sendKeys("****");
driver.findElement(By.id("txtPassword")).sendKeys("****");
driver.findElement(By.id("btnLogin")).click();
}
WebDriver driver=new ChromeDriver(); - This driver has a scope only within the method. The driver object which is used on the login method is still null. I am not sure why you would need 2 driver objects. you have 2 options to solve this,
public void launchBrowser() throws Exception
{
WebDriver driver=new ChromeDriver();
driver.manage().window().maximize();
this.driver = driver;
Thread.sleep(2000);
}
Or
public void launchBrowser() throws Exception
{
driver=new ChromeDriver();
driver.manage().window().maximize();
Thread.sleep(2000);
}
It is likely that the launchBrowser() method is not being called before the login() method is being called.
An easy way around this is to define a getDriver() method that calls launchBrowser if driver is null.
private WebDriver getDriver() {
if (driver == null) {
launchBrowser();
}
return driver;
}
Then your login method looks like:
WebDriver driver = getDriver();
driver.get("******");
driver.findElement(By.id("txtUsername")).sendKeys("****");
driver.findElement(By.id("txtPassword")).sendKeys("****");
driver.findElement(By.id("btnLogin")).click();

Even after adding annotations in JUnit it says no test cases with testrunner.How to handle?

I have downloaded all the JUnit jar files and installed them.When I run my package as JUnit,it says no tests found with TestRunner 'JUnit 4'.What am I missing
*public class AmazonTestFactory{
static WebDriver driver;
#Before
public void setUp() throws Exception {
driver= new FirefoxDriver();
driver.get("http://www.amazon.co.uk");
Thread.sleep(5000);
driver.manage().window().maximize();
}
#Test
public void testHomePage(){
AmazonHomePageFactory Page = PageFactory.initElements(driver, PageFactoryModelExercise.AmazonHomePageFactory.class);
Page.enterProductInSearchBox();
Page.clickSearchButton();
}
#After
public void closeBrowser(){
driver.close();
}*

Facing Issue "Cannot find class in classpath" in selenium

I am learning selenium, as part of i was trying to execute the below program. But i was getting the error " Cannot find class in classpath:practiseAutomation". The code is below:
public class practiseAutomation {
public WebDriver driver;
String baseurl="http://www.ticketnew.com/";
#BeforeTest
public void beforeTest() {
System.out.println("Executing Before Test Block");
System.setProperty("webdriver.ie.driver", "c://IEDriverServer.exe");
WebDriver driver=new InternetExplorerDriver();
driver.get(baseurl);
driver.manage().window().maximize();
driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);
}
#Test
public void test()
{
System.out.println("Executing Test Block");
System.out.println("The Page titile is "+ driver.getTitle());
}
}
#AfterTest
public void afterTest()
{
System.out.println("Executing After Test Block");
driver.close();
}
}
Kindly help me in resolving the above issue.
In your BeforeTest method, you are creating webdriver instance which has scope in beforeTest method only. It will throw null pointer in #Test and #AfterTest methods.
As you have already declared driver globally, so just initialize it in BeforeTest method like below:-
driver=new InternetExplorerDriver();
and not WebDriver driver=new InternetExplorerDriver();