0

I am new to selenium. I tried to create a automate testing project. In there I created class for every web pages and in my test packages I created a baseTest class where I initiate browser and open the home page and another class for other methods.I also created test classes for every pages. Now when I move into homepages the driver is null.but every testclass extends baseTest.Why is it happening?

package utilities;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chromium.ChromiumDriver;

public class utility {
    public WebDriver udriver;
    
    public void setDriver(WebDriver driver)
    {
        udriver =driver;
    }
    
    public String getTitle() {
        return udriver.getTitle();
        
    }
    
    public void clickEl(WebElement element) {
        element.click();
    }

}

homePageClass

package Pages;

import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;

import utilities.utility;

public class HomePage extends utility {

    @FindBy(xpath = "//a[normalize-space()='Gmail']")
    WebElement gmailBtn;

    public String homePageTitle() {
        return getTitle();
    }

    public void clickOnGmailBtn() {
        clickEl(gmailBtn);
    }

}

baseTest

package tests;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;

import utilities.utility;

public class BaseTest {
    protected WebDriver driver;
    utility utility =new utility();
    
    @BeforeTest
    public void initializeBrowser()
    {
        driver= new ChromeDriver();
        driver.manage().window().maximize();
        driver.get("https://www.google.com/");
        utility.setDriver(driver);
        
    }
    @AfterTest
    public void tearDown(){
        driver.quit();
    
    }
}

TestHomePage

package tests;

import org.testng.annotations.Test;

import Pages.HomePage;
import utilities.utility;

public class TestHomePage extends utility{
    HomePage homePage= new HomePage();
    @Test
    public void TilteTest() {
    
        System.out.println(homePage.homePageTitle());
    }

}

I tried with create a utility instance before every method ,but didn't work. I hope that the driver form crated in baseTest class will be passed to utility class and evry webPage class will get access to it.

1 Answer 1

0

To make this work, you need to make the driver static in the Utility class.

public class Utility {
   public static WebDriver udriver;

   public static void setDriver(WebDriver driver) {
      udriver = driver;
   }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.