gpt4 book ai didi

java - 在 Selenium 中获取函数 isElementPresent 的空指针异常...

转载 作者:行者123 更新时间:2023-12-01 14:48:46 25 4
gpt4 key购买 nike

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

header 验证类

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();

}

}

最佳答案

浏览器静态变量在 HeaderVerification 类中为 null。您应该添加

HeaderVerification.browser = browser;

StarstServer.startServer()方法中。

关于java - 在 Selenium 中获取函数 isElementPresent 的空指针异常...,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15104743/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com