Kirpidon
Kirpidon

Reputation: 19

WebDriver. Not able to run tests after using parametrized class

I need to implement one thing: Every time when I start my tests I have to Login into the system. I created a class that should run this Login procedure. I'm able to send proper values to this class and it is able to login, but after that, I'm receiving NullPointerException on my second class (which is running tests itself). It looks like it is not able to see browser window at all. Help me please to write this classes in a way that would allow my to re-use the Login class in as many classes as I want.

One for Login:

public class Login {

    private static WebDriver driver;
    public static void Find_Fields (String path,String login, String password) {
        driver = Driver.get(path);
        WebElement login_field = driver.findElement(By.id("abc"));
        login_field.sendKeys(login);
        //Find Password Field on Start page
        WebElement password_field = driver.findElement(By.id("abc"));
        password_field.sendKeys(password);
        //Find Login Button
        WebElement login_button = driver.findElement(By.xpath("abc"));
        login_button.click();
    }
}

public class Testing {

    private static WebDriver driver;
    @BeforeClass
    public static void a_setup(){
        //here I'm trying to run Login with parameters
        Login fields = new Login();
        fields.Find_Fields(LOGIN_PATH, LOGIN, PASSWORD);
    }

    @Test
    public void b_Press_Login_Button(){
        //Here I'm trying to start testing in session started from a_setup()
        WebElement keyword = driver.findElement("..."));
        keyword.sendKeys("...");
    }

    @AfterClass
    public static void Close(){
        driver.quit();
        driver.close();
    }
}

Upvotes: 0

Views: 197

Answers (2)

JacekM
JacekM

Reputation: 4099

You have two independent WebDriver fields, one for each class. You've initialized the one in Login, but you didn't pass the reference back to Testing class. One of the solutions would be to simply pass WebDriver as parameter to Login constructor:

public class Login {

    private WebDriver driver;

    public void Find_Fields (String path,String login, String password) {
        driver.get(path);
        //(...) rest of this method is unchanged
    }

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

public class Testing {

    private static WebDriver driver;
    @BeforeClass
    public static void a_setup(){
        driver = new FirefoxDriver();        //initialize the driver
        Login fields = new Login(driver);    //pass the instance of WebDriver to login class
        fields.Find_Fields(LOGIN_PATH, LOGIN, PASSWORD);
    }

    //(...) rest of the class remains unchanged
}

Upvotes: 1

Richard
Richard

Reputation: 9029

You need to initialize your driver with a browser object. Something like this should work:

public static void Find_Fields (String path,String login, String password)
{
    driver = new FirefoxDriver();
    driver.get(path);
    WebElement login_field = driver.findElement(By.id("abc"));
    login_field.sendKeys(login);
    //Find Password Field on Start page
    WebElement password_field = driver.findElement(By.id("abc"));
    password_field.sendKeys(password);
    //Find Login Button
    WebElement login_button = driver.findElement(By.xpath("abc"));
    login_button.click();
}

Upvotes: 1

Related Questions