简体   繁体   中英

How to call a java class in selenium webdriver?

just started learning selenium-webdriver and trying too....soo here is my dout?

below created a function navigate to google home page

package UtilityGoogle;

import org.openqa.selenium.WebDriver; import org.openqa.selenium.firefox.FirefoxDriver;

public class HomePage { WebDriver WD=null;

public static void main(WebDriver WD) { // TODO Auto-generated method stub WD = new FirefoxDriver(); WD.navigate().to("https://www.google.co.in"); WD.manage().window().maximize(); return; }

and below code calling Homepage function..

 package GoogleMain; import org.openqa.selenium.WebDriver; import org.testng.AssertJUnit; import org.testng.annotations.Test; import UtilityGoogle.HomePage; public class Google_Tc1 { private static WebDriver XP = null;@ Test public void Open() { HomePage HP = new HomePage(); String actual = XP.getTitle(); String expected = "Google"; AssertJUnit.assertEquals(expected, actual); } } 

Getting below error .... please do help me to fix this

 FAILED: Open java.lang.NullPointerException at GoogleMain.Google_Tc1.Open(Google_Tc1.java:13) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) 

You set the WebDriver to null here:

private static WebDriver XP = null;

Then in your Open() method try to use it:

String actual = XP.getTitle();

The stack trace informs you of this:

at GoogleMain.Google_Tc1.Open(Google_Tc1.java:13)

So to fix it assign XP

Your WebDriver XP instrance variable is never initialized. The main method in HomePage is never called, and even if it was it wouldn't matter since it doesn't manipulate the XP variable you're trying to use on your test.

I believe this is what you want to achieve:

public class HomePage {

    public HomePage(WebDriver driver) {
        driver = new FirefoxDriver();
        driver.navigate().to("https://www.google.co.in");
        driver.manage().window().maximize();
    }

}

And the test class:

public class GoogleTest {

    private static WebDriver driver;

    @Test
    public void Open() {
        HomePage homePage = new HomePage(driver);
        String actual = driver.getTitle();
        String expected = "Google";
        AssertJUnit.assertEquals(expected, actual);
    }

}

Now when you create an instance of HomePage passing a driver as an argument, the driver is initialized and can be used.

I changed the variable names because the names you used weren't good. You should read this . I also think there are room for a lot of other improvements. You should probably read about the Page Object Model here and here and try to stick with it.

Just like you have initialized WD in main method of Homepage class, WD = new FirefoxDriver();
similarly you have to initialize XP before using it.

 package GoogleMain; import org.openqa.selenium.WebDriver; import org.testng.AssertJUnit; import org.testng.annotations.Test; import UtilityGoogle.HomePage; public class Google_Tc1 { private static WebDriver XP = new FirefoxDriver();@ Test public void Open() { HomePage HP = new HomePage(); String actual = XP.getTitle(); String expected = "Google"; AssertJUnit.assertEquals(expected, actual); } } 

public class GoogleTest {

    private static WebDriver driver;

    @Test
    public void Open() {
        HomePage homePage = new HomePage(driver);
        String actual = driver.getTitle();
        String expected = "Google";
        AssertJUnit.assertEquals(expected, actual);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM