-1

I am trying to design an automation framework, using the page object model, I am new to automation. I am stuck at this point, could not solve it. Kindly help I am getting Cannot invoke

"org.openqa.selenium.WebDriver.findElement(org.openqa.selenium.By)" because "this.driver" is null error

Base class:

package resources;

import java.io.FileInputStream;
import java.io.IOException;
import java.util.Properties;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;

import pageObjects.userDetails;

public class base {
    public WebDriver driver;
    public Properties property;
    @SuppressWarnings("deprecation")
    public WebDriver initializeDriver() throws IOException {
        
        
        property = new Properties();
        FileInputStream file = new FileInputStream("D:\\Automation-TestUni\\TestUni\\src\\main\\java\\resources\\data.properties");
        
        property.load(file);
        String BrowserName = property.getProperty("browser");
         
        if(BrowserName.equals("chrome")) 
        {
         System.setProperty("webdriver.chrome.driver", "D:\\TestUni-Framework\\Framework\\chromedriver.exe");   
            driver = new ChromeDriver();
            
        }
        else if(BrowserName.equals("firefox")) 
        {
            driver = new FirefoxDriver();
        }
        else if(BrowserName.equals("IE")) 
        {
            //Executes IE
        }
        
        driver.manage().timeouts().implicitlyWait(5, TimeUnit.SECONDS);
        return driver;
        
        
    }
public void user() throws IOException {
        
        
        driver.get("link");
        driver.manage().window().maximize();
        userDetails ud = new userDetails(driver);
        ud.getusername().sendKeys("xxxx");;
        ud.getpass().sendKeys("xxxx");
        ud.submit().click();
        
        JavascriptExecutor js = (JavascriptExecutor) driver;
        js.executeScript("window.scroll(0,1000)");
        ud.agreed().click();
        
        ud.getUniLink().click();
        ud.portal().click();
        
    }

}

User details page object class:

package pageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class userDetails {
    
    public WebDriver driver;
    
    By username = By.name("user");
    By pass = By.name("pass");
    By submit = By.name("submit");
    By agreed = By.name("agreed");
    By Uni = By.xpath("/html[1]/body[1]/div[1]/div[1]/div[2]/ul[1]/li[2]/a[1]");
    By portal = By.xpath("//a[@href='../../Key_Indicator/Criteria/Criteria.html#features-sec']");
    
    
    public userDetails(WebDriver driver) {
        
        this.driver=driver;
    }
    
    public WebElement getusername() 
    {
        
        return driver.findElement(username);
        
    }
    public WebElement getpass() 
    {
        
        return driver.findElement(pass);
        
    }
    public WebElement submit() 
    {
        
        return driver.findElement(submit);
    }
    public WebElement agreed() 
    {
        
        return driver.findElement(agreed);
    }
    public WebElement getUniLink() 
    {
        
        return driver.findElement(Uni);
        
    } 
    public WebElement portal() 
    {
        
        return driver.findElement(portal);
        
    } 

}

Curricular Aspects page object class:

package pageObjects;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;

public class CurricularAspects {
    

    public WebDriver driver;
    
    By CA = By.cssSelector("div.home-sec:nth-child(5) div.overlay div.container div.row.text-center div.col-lg-12.col-md-12.col-sm-12 div.flexslider.set-flexi:nth-child(1) ul.slides.move-me > a.btn.btn-info.btn-ll:nth-child(2)");
    
    public CurricularAspects(WebDriver driver) {
        
        this.driver=driver;
    }
    
    public WebElement curricular() {
        
        return driver.findElement(CA);
    }

}

Test class :

package Automation.TestUni;



import java.io.IOException;

import org.openqa.selenium.WebDriver;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import pageObjects.CurricularAspects;
import resources.base;

public class Criteria_1 extends base{
    static WebDriver driver;
    

    @BeforeTest
    public void LoginCriteria1() throws IOException {
        
    
        base b = new base();
        b.initializeDriver();
        b.user();
        
        
    }
    
    @Test
    public void curricular() throws InterruptedException {
        
        
        CurricularAspects ca = new CurricularAspects(driver);
        Thread.sleep(3000);
        ca.curricular().click();
        
        
    }

}
0

When Criteria_1 extends base, why you are instantiating base class? Your application needs advance code review. Why you are defining a static driver in your test class is another question...

Anyway you should use ITestContext to store driver in the context and then use that context with getCTX method with annotation @BeforeClass in the test class in order to get the value of the driver and use it in the factory page.

1
  • Thank you. It worked, removed base class instantiation. – Varun Feb 16 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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