0

I created a java class and written logic for password and confirm password in the class. while calling the class in another class by creating class object, it is showing null pointer exception.

public class Verify_password {
    WebDriver driver;
    String pwd = "password";
    String confirm_pwd = "password";
    String wrong_pwd="yogesh";
    public void password_match(){
        //Password = confirm password
        driver.findElement(By.id("encrypted_pwd")).clear();
        driver.findElement(By.id("confirm_pwd")).clear();
        driver.findElement(By.id("encrypted_pwd")).sendKeys(pwd);
        driver.findElement(By.id("confirm_pwd")).sendKeys(confirm_pwd);
        driver.findElement(By.id("submit-btn")).click();
        if(pwd ==confirm_pwd){
            System.out.println("Password Match");
        }
        else{
            System.out.println("Password doesn't Match");
        }
    }

Calling this class to another class by:

Verify_password password1 = new Verify_password();
password1.password_match1();

Can someone verify and confirm that is it okay or not?

Thanks

Yogesh Trivedi
  • 45
  • 1
  • 1
  • 10
  • public void password_match() this is your method in the class. So I think it should be password1.password_match(); – sebenalern Apr 26 '16 at 18:38
  • public void password_match1(){ //password & conform password <8 driver.findElement(By.id("encrypted_pwd")).sendKeys(wrong_pwd); driver.findElement(By.id("confirm_pwd")).sendKeys(wrong_pwd); driver.findElement(By.id("submit-btn")).click(); String error_msg = driver.findElement(By.id("encrypted_pwd1")).getText(); if(wrong_pwd.length() < 8){ System.out.println(error_msg); – Yogesh Trivedi Apr 26 '16 at 18:40
  • Please edit your post and update it and remove the comment above. – sebenalern Apr 26 '16 at 18:41
  • @YogeshTrivedi Please do not add code as a comment. As you can see, it's totally unreadable. Edit it into your question and format it. – tnw Apr 26 '16 at 18:42

2 Answers2

0

You have to create an instance of a driver before using password_match1() method, for example by creating a constructor of Verify_password and inside of it creating an instance of WebDriver class.

Additionally you should fix your class name to be compliant with the proper naming convention to VerifyPassword or PasswordVerifier. You can also work on your method name, to be just: match() - after all you know from a class name, that it concerns password operations.

Chris Jaga
  • 389
  • 4
  • 17
0

Edit:

First you have to get the dependencies:

<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
     <artifactId>selenium-java</artifactId>
      <version>current version</version>
</dependency>
<dependency>
    <groupId>org.seleniumhq.selenium</groupId>
    <artifactId>htmlunit-driver</artifactId>
    <version>current version</version>
</dependency>

Now you have to initialize a instance of an driver for example a firefox driver:

WebDriver driver = new FirefoxDriver();

After that you have to call the site you want to test:

driver.get("http://www.google.com");

Now you could find the elements and do what you want to do:

WebElement element = driver.findElement(By.id());

Additionally you should store the references of the elements in object. Every findElement call decrease the performance of your tests.

For more details see http://www.seleniumhq.org/docs/03_webdriver.jsp