0

要素が存在するにもかかわらず、TestDriver クラスのメイン関数から headerVerification メソッドを呼び出すと、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();

    }

}
4

1 に答える 1

1

ブラウザの静的変数はnullクラスにありHeaderVerificationます。追加する必要があります

HeaderVerification.browser = browser;

StarstServer.startServer()メソッドで。

于 2013-02-27T05:45:11.687 に答える