0

当我从 TestDriver 类的主函数调用 headerVerification 方法时,我得到空指针异常,尽管该元素存在。

启动服务器类:

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!!!");
        }
    }
}

测试驱动程序类

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

浏览器静态变量nullHeaderVerification类中。你应该添加

HeaderVerification.browser = browser;

StarstServer.startServer()方法。

于 2013-02-27T05:45:11.687 回答