-1

Base class:

public class Base {

  public WebDriver driver ;

   @BeforeMethod
  public void initialize() {
     System.setProperty("webdriver.chrome.driver", "/Users/apple/eclipse-workspace/Selenium/src/Google/chromedriver");

    ChromeDriver driver = new ChromeDriver() ;
    driver.manage().window().maximize();

    driver.get("https://google.com");
  }

   @AfterMethod
   public void exitdriver() {
      driver.close();
   }
}

Page Class:

public class Pages {

  WebDriver driver ;

   public Pages( WebDriver driver)
   {
        this.driver = driver ;
   }

    @FindBy(how = How.XPATH, using = ".//*[@id='lst-ib']")
    WebElement search ;

    public void Googletext() throws InterruptedException
    {
        search.sendKeys("Test");
    }
}

Main Class

 public class Front extends Base {


  @Test
  public void Login() throws InterruptedException {

      Pages PagesObj = new Pages(driver);
      PagesObj.Googletext();  
  }
}
7
  • What do you expect? Your code does not make sense. Try to use debugging before asking for assistance.
    – Alexey R.
    Commented Mar 6, 2018 at 14:54
  • Sorry , some mistake in copying code here , please check now. I want to initialize & quit driver in my base class which i want to use as inheritance in my main class( through extend ). there is one more class which has all the functions, which i want to call at my main function
    – Batu-QA
    Commented Mar 6, 2018 at 18:11
  • what driver are you sending as a parameter for the Pages Constructor?
    – Malachi
    Commented Mar 6, 2018 at 18:31
  • WebDriver driver
    – Batu-QA
    Commented Mar 6, 2018 at 18:33
  • 2
    StackOverFlow is the good resource of knowledge and professional advise. But prior going here and posting questions, make sure you've tried everything and made a research on the other forums. If you don't understand the principles of OOP, it's better to read a book / take a course, because explaining this topic over here is just impossible. Commented Mar 6, 2018 at 18:51

1 Answer 1

0

You were getting NullpointerException because you are declaring another ChromeDriver instance inside the initialize() method and creating an instance of ChromeDriver object whose scope is limited to initialize() method itself.

Make the following corrections in the code:

In Base class:

public void initialize() {
 ..some code

  driver = new ChromeDriver() ;

..some code 
}

And you don't need another WebDriver driver declaration in Front class.( you can remove it).

In Front class:

public class Front extends Base {

  @Test
  public void Login() throws InterruptedException {
      Pages PagesObj = new Pages(driver);
      PagesObj.Googletext();  
  }
}

So, now the driver gets initialized in initialize() method of Base class and the same driver instance is passed through to your Page object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.