user3360542
user3360542

Reputation: 11

Getting selenium support pagefactory DefaultElementLocator findElement nullpointerexception for driver

I am creating page object model plus data driver framework. I am writing Testcase for logIn but getting pagefactory nullpointerexception. 1. How I can initialise my driver in order to avoid this error? 2. Again How I can Sccreenshot page class in my test script i have given code below.

FAILED: Log("[email protected]", "sw45") java.lang.NullPointerException at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)

TestBase class

public class TestBase { public WebDriver driver;

public void initialize() throws InterruptedException, IOException {
    System.out.println("Launching browser");
    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\admin\\eclipse-workspace\\SampleProject\\src\\main\\java\\selenium\\org\\sample\\SampleProject\\data\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver.navigate().to("http://automationpractice.com/index.php?controller=authentication&back=my-account");
    Thread.sleep(6000);
}

}

AppTest java

public class AppTest extends ExcelReader { TestBase TB = new TestBase();

@BeforeTest
void browserlaunch() throws InterruptedException, IOException

{
    TB.initialize();
}

@Test(dataProvider = "testdata")
public void LogIn(String email, String pwd) throws IOException, InterruptedException {
    System.out.println("Sign in page1");
    SignIn loginpage = PageFactory.initElements(driver, SignIn.class);
    loginpage.setUserName(email);// email entered
    loginpage.setPwd(pwd);// password entered
    loginpage.Sign_In_btn();
    driver.manage().window().maximize();
    try {
        Assert.assertEquals(driver.getTitle(), "My account - My Store");
        System.out.println("Log  IN successfull1");

    } catch (AssertionError E) {
        System.out.println("Log  IN un-successfull" + E);
    }
    Thread.sleep(8000);
    System.out.println("after click");
}

}

Screenshotpage java

public class ScreenshotPage extends TestBase { private WebDriver driver = new ChromeDriver();

public void ScreenshotPage1() throws InterruptedException, IOException {
    Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000))
            .takeScreenshot(driver);
    ImageIO.write(fpScreenshot.getImage(), "PNG", new File("D:/selenium/" + System.currentTimeMillis() + ".png"));
}

}

Upvotes: 0

Views: 1124

Answers (2)

sunit
sunit

Reputation: 1

package com.xyz33;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class DemoPOMclass {
    
    //static WebDriver driver;
        static String url1="https://www.facebook.com/";
        static WebDriver driver;
    
    
    
      public DemoPOMclass(WebDriver driver) {
      
      this.driver=driver; PageFactory.initElements(driver, this);// initElements() method will create all WebElements
      
      }
     
    
    
@FindBy(name="email")
WebElement userID;

@FindBy(id="pass")
WebElement passwrd;


@FindBy(id="u_0_d_Ek")
WebElement btn_login;


public   WebDriver launchBrowser() {//just open chrome browser using automation
    
    System.setProperty("webdriver.chrome.driver" ,"C:/Users/tgt193/Desktop/traningAll/AllDrivers/chromedriver.exe" );
    
    System.out.println("browser is launch successfully");
    
      driver=new ChromeDriver();//type casting 
    driver.manage().window().maximize();    //maximizes the open browser
    System.out.println("browser maximize sussfully");
    return driver;
    
    }

public  WebDriver openUrl(String url1) {//open URL using automation
    driver.get(url1);
    System.out.println("URL is launch successfully");
    return driver;
        }



public void enterUserName(String uName){
    driver.manage().timeouts().implicitlyWait(3, TimeUnit.SECONDS);
    System.out.println("wait 3 sec ");
    System.out.println("enter username at FB page");
    userID.sendKeys(uName);   
    System.out.println("username enter  successfully at FB page");
}

public void enterPassword(String uPasswd){


    passwrd.sendKeys(uPasswd);     
    System.out.println("enter password at FB page");
    }
    


public void click_onLogInButton(){

    btn_login.click();

}  

public void login_ToFB() {

    
    this.launchBrowser();
    this.openUrl("https://www.facebook.com/");
    
    this.enterUserName("sunit");
    this.enterPassword("sunit123@");
    this.click_onLogInButton();
    
}
public static void main(String[] args) {
    
    DemoPOMclass objPOM =new DemoPOMclass(driver);
    
    objPOM.launchBrowser();
    objPOM.openUrl(url1);
    objPOM.enterUserName("sunit");
    objPOM.enterPassword("sunit123");
    //objPOM.login_ToFB();
    //objPOM.login_ToFB();
    
}
}

Upvotes: 0

dangi13
dangi13

Reputation: 1275

Make this method to return WebDriver :

public WebDriver initialize() throws InterruptedException, IOException {
    System.out.println("Launching browser");
    System.setProperty("webdriver.chrome.driver",
            "C:\\Users\\admin\\eclipse-workspace\\SampleProject\\src\\main\\java\\selenium\\org\\sample\\SampleProject\\data\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);
    driver.navigate().to("http://automationpractice.com/index.php?controller=authentication&back=my-account");
    Thread.sleep(6000);

return driver;
}

and in AppTest class use it as :

public class AppTest extends ExcelReader {

    public WebDriver driver;
    TestBase TB = new TestBase();

    @BeforeTest
    void browserlaunch() throws InterruptedException, IOException

    {
        driver = TB.initialize();
    }

For screenshot make your method as :

public static void ScreenshotPage1(WebDriver driver) throws InterruptedException, IOException {
    Screenshot fpScreenshot = new AShot().shootingStrategy(ShootingStrategies.viewportPasting(1000))
            .takeScreenshot(driver);
    ImageIO.write(fpScreenshot.getImage(), "PNG", new File("D:/selenium/" + System.currentTimeMillis() + ".png"));
}

and after @Test use in AppTest class :

@AfterMethod
    public void takeScreenShot() throws InterruptedException, IOException {
        ScreenshotPage.ScreenshotPage1(driver);
    }

Upvotes: 0

Related Questions