0

I read a lot about this problem but i can not find answer for my case.

I have class with Selenium method

public class PrzesylkiPrzygotowane implements Tools{
private WebDriver driver;
private StringBuffer verificationErrors = new StringBuffer();

//Override
public Narzedzia getNarzedzia() {
    return new Narzedzia();  
  }

public void setUp() throws Exception {
  StartEN start = new StartEN(GetParams.getUser(),
            GetParams.getPassword());
    this.driver = start.getDriver();
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}

@Test
public void testPrzesylkiPrzygotowane() throws Exception {
setUp();
driver.findElement(By.cssSelector("a[href*='?action=GetZbior&arg1=220170'")).click();
driver.findElement(By.cssSelector("button.widgetButton")).click();
driver.findElement(By.id("nazwa")).clear();
Thread.sleep(1000);
driver.findElement(By.id("nazwa")).sendKeys("Mar");
driver.findElement(By.xpath("//body[@id='Body']/div[4]/ul/li/strong[3]")).click();
driver.findElement(By.id("submit_button")).click();
Thread.sleep(1000);
//NPE throw here    
getNarzedzia().logout();
}

... rest code.

I made interface for this class

public interface Tools {
public Narzedzia getNarzedzia();
}

"Narzedzia" is a class with group of methods which i use like tools for aplication.

public class Narzedzia{

public WebDriver driver;
boolean acceptNextAlert = true;

public void logout() throws InterruptedException{
      //Ustawienia driv = new Ustawienia();
      driver.findElement(By.linkText("Wyloguj")).click();
      Thread.sleep(1000);
      assertTrue(closeAlertAndGetItsText(driver).matches("^Czy na pewno chcesz wyjść z Elektronicznego Nadawcy[\\s\\S] Sprawdź czy wszystkie dane zostały przekazane do placówki\\.$"));
      driver.close();
  }
  public String closeAlertAndGetItsText(WebDriver driv) {
        try {
          Alert alert = driv.switchTo().alert();
          String alertText = alert.getText();
          if (acceptNextAlert) {
            alert.accept();
          } else {
            alert.dismiss();
          }
          return alertText;
        } finally {
          acceptNextAlert = true;
        }
      }

...rest code

When i run test rest od method in "Narzedzia" works fine but logout throw error: java.lang.NullPointerException

  • 1
    Where is the NPE thrown? Is it your WebDriver? Can't see your setUp method called. – Maze Apr 21 '16 at 08:45
  • NPE throw in line getNarzedzia().logout(); i will mark this line now. Alse I think that driver in logout method is empty but i don't have idea how i can solve this problem. – Marek Derdzinski Apr 21 '16 at 08:49
  • If you call `getNarzedzia().logout();` a new object is returned (also a new WebDriver which is not initialized). Correct me if I'm wrong, but I think you want to implement some kind of Singleton Pattern, but it must not deliver a new object. Please have a look at the Singleton. – Maze Apr 21 '16 at 08:58
  • Yes exactly because when i start @test, driver from class PrzesylkiPrzygotowane start to work and i want to sent him and us in method logout. because i want to use class Narzędzia in many testy with same structure. – Marek Derdzinski Apr 21 '16 at 09:05

2 Answers2

0

With Google you should find many hints for implementing a Singleton in Java. It should be something like this:

public class SingleObject {

   //create an object of SingleObject
   private static SingleObject instance = new SingleObject();

   //make the constructor private so that this class cannot be
   //instantiated
   private SingleObject(){}

   //Get the only object available
   public static SingleObject getInstance(){
      return instance;
   }
}

Source: Design Pattern - Singleton Pattern (my first search result with Google 'singleton pattern java')

Basically the Constructor has to be private and your getter method has to return a field which holds the object. There are some variants around the tutorial sites. Some of them are thread safe. Choose what you need.

Maze
  • 156
  • 10
0

I solved problem.

Made constructor with parameter in "Narzedzia" class

public Narzedzia (WebDriver wd){
    this.driver = wd;
}

And call constructor like this in PrzesylkiPrzygorowane

 public Narzedzia getNarzedzia() {
        return new Narzedzia(this.driver);  
      }

Test is done and logout doesn't throw NPE.

  • 1
    Remember, this is not a Singleton. You create a new object every single call of `getNarzedzia()`. It is not very efficient and if your class get more fields then only a WebDriver, you have to change your constructor. Might be not the best solution. – Maze Apr 21 '16 at 10:45
  • I will remember yout advice, and i will try improve code when my skill will be higher :) – Marek Derdzinski Apr 21 '16 at 11:30