2013-02-27 4 views
0

TestDriver 클래스의 main 함수에서 headerVerification 메서드를 호출 할 때 null 포인터 예외가 발생합니다 (요소가 있지만).Selenium에서 함수 isElementPresent에 대한 Null 포인터 예외 가져 오기 ...

서버 시작 클래스 :

package WebTesting; 

import com.thoughtworks.selenium.DefaultSelenium; 
import com.thoughtworks.selenium.Selenium; 

public class StartServer { 

    private String host; 
    private String nameOfBrowser; 
    private int port; 
    private String url; 

    public StartServer(String host, int port, String nameOfBrowser, String url){ 
     this.host=host; 
     this.port=port; 
     this.nameOfBrowser=nameOfBrowser; 
     this.url=url; 
    } 

    public static Selenium browser; 

    public void startServer(){ 
     browser = new DefaultSelenium(host, port, nameOfBrowser, url); 
     browser.start(); 
     browser.open("/"); 
     System.out.println("Browser Started !!!"); 
    } 
} 

헤더 검증 클래스

package WebTesting; 

import com.thoughtworks.selenium.Selenium; 

public class HeaderVerification { 

    private String elementPath; 
    private String linkPath; 
    private String testLink; 

    public HeaderVerification(String elementPath, String linkPath, String testLink){ 
     this.elementPath=elementPath; 
     this.linkPath=linkPath; 
     this.testLink=testLink; 
    } 

    public static Selenium browser; 

    public void headerVerification() throws InterruptedException{ 
     System.out.println(elementPath); 
     if(browser.isElementPresent(elementPath)){ 
      Thread.sleep(5000); 
      System.out.println("Header is Present"); 
      browser.click(linkPath); 
      Thread.sleep(5000); 

      if(browser.getLocation().equals(testLink)){ 
       System.out.println("Correct Location!!!"); 
      } 
      else 
       System.out.println("Incorrect Location!!!"); 

      browser.close(); 
      System.out.println("Browser Closed!!!"); 
     } 
    } 
} 

TestDriver 클래스

package WebTesting; 

public class TestDriver { 

    /** 
    * @param args 
    */ 

    static StartServer ss = new StartServer("localhost", 4444, "*firefox", "http://docs.seleniumhq.org/"); 
    static HeaderVerification hv = new HeaderVerification ("//div[@id='header']", "//a[@title='Overview of Selenium']", "http://docs.seleniumhq.org/about/"); 

    public static void main(String[] args) throws InterruptedException { 
     // TODO Auto-generated method stub 
     ss.startServer(); 
     hv.headerVerification(); 

    } 

} 

답변

1

브라우저 정적 변수는 HeaderVerification 클래스에 null입니다.

HeaderVerification.browser = browser; 

StarstServer.startServer() 방법으로 추가해야합니다.

+0

Benoit, 정말 고맙습니다 !!! 그것은 일했다. .. :) – Paras