-1

This question already has an answer here:

I have 2 classes in my project

First class contains all the webelements

Second one for method where i am using this elements

package facebook;

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

public class webelement {


    private static WebElement element;


    public static WebElement firstname(WebDriver driver) {
        driver.findElement(By.xpath("//input[@name='firstname']"));
        return element; 
    }
    public static WebElement lastname(WebDriver driver)
    {
        driver.findElement(By.xpath("//input[@name='lastname']"));
        return element;
    }
    public static WebElement email(WebDriver driver)
    {
        driver.findElement(By.xpath("//input[@name='reg_email__']"));
        return element;
    }
    public static WebElement newpassword(WebDriver driver)
    {
        driver.findElement(By.xpath("//input[@name='reg_passwd__']"));
        return element;
    }
    public static WebElement day(WebDriver driver)
    {
        driver.findElement(By.xpath("//select[@name='birthday_day']"));
        return element;
    }

    public static WebElement month(WebDriver driver)
    {
        driver.findElement(By.xpath("//select[@name='birthday_month']"));
        return element;

    }
    public static WebElement year(WebDriver driver)
    {
        driver.findElement(By.xpath("//select[@name='birthday_year']"));
        return element;

    }
    public static WebElement sex(WebDriver driver)
    {
    driver.findElement(By.xpath("(//input[@name='sex'])[2]"));
    return element;

    }

    public static WebElement signup(WebDriver driver) {
        driver.findElement(By.xpath("//button[@name='websubmit']"));
        return element;
    }
    }

And this is my second class where i am using this elements

package facebook;
import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
import org.testng.annotations.Test;

import facebook.facebook;
public class signup {
    static WebDriver driver;
    @Test
    public void login()

    {
        System.setProperty("webdriver.chrome.driver","C:\\Users\\5558\\eclipse-workspace\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.get("https://www.facebook.com");
        driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);
        driver.manage().window().maximize();
        webelement ele = new webelement();
        ele.firstname(driver).sendKeys("dixit");

        ele.lastname(driver).sendKeys("jani");
        ele.email(driver).sendKeys("test@gmail.com");

        ele.newpassword(driver).sendKeys("dixit1234");

        Select day = new Select(ele.day(driver));

        day.selectByIndex(24);

        Select month = new Select(ele.month(driver));

        month.selectByVisibleText("jul");

        Select year = new Select(webelement.year(driver));
        year.selectByVisibleText("1997");
        ele.sex(driver).click();

        ele.signup(driver).click();
        driver.close();




    }


}

marked as duplicate by Alexey R., Nitin Rastogi, Bharat Mane, Kate Paulk Apr 24 at 11:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • The way you have declear the methods is wrong please correct this, in your first class element have no value it's null. – Upkar Singh Apr 23 at 14:29
  • I wish we could have "Question about NPE" closure justification. We have dozens questions like this one here, but it seems that the answers do not introduce any value for other people. – Alexey R. Apr 23 at 16:27
  • Thanks you sir! sir i am learning from very basic thats why i think that you think that my question is sily. but for us this platerform is the way to learn. :) @alex – divyang jani Apr 24 at 17:23
0

Change your code like this:-
Your POM class-

public class webelement {
    static WebDriver driver;
    static WebElement element;

public webelement(WebDriver driver) {
        //super();
        this.driver = driver;
    }



        public static WebElement firstname() {
            element=driver.findElement(By.xpath("//input[@name='firstname']"));
            return element; 
        }
        public static WebElement lastname()
        {
            element=driver.findElement(By.xpath("//input[@name='lastname']"));
            return element;
        }
        public static WebElement email()
        {
            element=driver.findElement(By.xpath("//input[@name='reg_email__']"));
            return element;
        }
        public static WebElement newpassword()
        {
            element=driver.findElement(By.xpath("//input[@name='reg_passwd__']"));
            return element;
        }
        public static WebElement day()
        {
            element=driver.findElement(By.xpath("//select[@name='birthday_day']"));
            return element;
        }

        public static WebElement month()
        {
            element=driver.findElement(By.xpath("//select[@name='birthday_month']"));
            return element;

        }
        public static WebElement year()
        {
            element=driver.findElement(By.xpath("//select[@name='birthday_year']"));
            return element;

        }
        public static WebElement sex()
        {
            element=driver.findElement(By.xpath("(//input[@name='sex'])[2]"));
        return element;

        }

        public static WebElement signup() {
            element=driver.findElement(By.xpath("//button[@name='websubmit']"));
            return element;
        }

}  

Your Test class-

public class signup {
static WebDriver driver;
    @Test
    public void login()

    {
        System.setProperty("webdriver.chrome.driver","C:\\Users\\5558\\eclipse-workspace\\chromedriver.exe");
        driver=new ChromeDriver();
        driver.get("https://www.facebook.com");
        driver.manage().timeouts().implicitlyWait(2000, TimeUnit.MILLISECONDS);
        driver.manage().window().maximize();
        webelement ele = new webelement(driver);
        ele.firstname.sendKeys("dixit");

        ele.lastname.sendKeys("jani");
        ele.email.sendKeys("test@gmail.com");

        ele.newpassword.sendKeys("dixit1234");

        Select day = new Select(ele.day);

        day.selectByIndex(24);

        Select month = new Select(ele.month);

        month.selectByVisibleText("jul");

        Select year = new Select(webelement.year);
        year.selectByVisibleText("1997");
        ele.sex.click();

        ele.signup.click();
        driver.close();

    }  

Note: Just avoid the naming convention of class and also avoid using variable with same name for different purpose.

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