0
package Annotaion_testng;

import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

public class Login_case {

  public String baseUrl = "https://www.eventerprise.com";
  String driverpath = "C://Users//fizan//Documents//testing//webdriver_soft//fire_fox_driver//geckodriver.exe";
  public WebDriver driver;

  @BeforeMethod
  public void f() throws InterruptedException {

    System.out.println("opening firefox");
    System.setProperty("webdriver.gecko.driver", driverpath);
    WebDriver driver = new FirefoxDriver();
    driver.get(baseUrl);
    Thread.sleep(10000);
  }

  @Test
  public void login(){
    System.out.println("click on login link from header");

    //code is failing from here
    driver.findElement(By.xpath(".//*[@id='app']/div/div/div/header/div[1]/div[1]/div[2]/ul/li[4]/a")).click();

    driver.findElement(By.id("email")).sendKeys("fijan@atyantik.com");

    driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
    //enter  password

    driver.findElement(By.id("password")).sendKeys("atyantik@1234");

    driver.manage().timeouts().implicitlyWait(1, TimeUnit.SECONDS);
    //click on login button

    driver.findElement(By.xpath(".//*[@id='eve-modal']/div/div[1]/div/div/div[2]/div/div/div/div[1]/div[2]/form/div[4]/button")).click();

    driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);

    //click on log out from header

    System.out.println("logging out");

    driver.findElement(By.xpath(".//*[@id='app']/div/div/header/div[1]/div[1]/div[2]/div[1]/ul/li[7]/a")).click();  
  }
}
Kevin Cruijssen
  • 9,153
  • 9
  • 61
  • 135
Fijan Kaji
  • 25
  • 4
  • 1
    Which exception are you getting, which line? Show us relevant HTML block, provide more information, so we can help you – Andrei Suvorkov Jul 24 '18 at 06:44
  • Ignore my previous (now deleted) comment. As provided in the answer of @ScaryWombat, you aren't using the class-variable `driver` but instead create a new local variable in your method `f`. So the class-variable `driver` is still null, causing the NPE at `driver.findElement(...)`. – Kevin Cruijssen Jul 24 '18 at 06:48

1 Answers1

2

You are shadowing driver in f WebDriver driver = new FirefoxDriver(); As you have already correctly declared this as field, removed the new redeclaration

i.e.

driver = new FirefoxDriver(); 
Scary Wombat
  • 44,617
  • 6
  • 35
  • 64