这是我的代码。无法弄清楚为什么我会得到例外。
import net.sf.sahi.client.Browser;
import org.testng.Assert;
import org.testng.annotations.Test;
public class browser {
@Test
public void function2(){
System.out.println("Starting");
String browserName = "firefox"; // default values are "ie", "firefox", "safari", "chrome", "opera" - specified in userdata/config/browser_types.xml
Browser browser = new Browser(browserName);
browser.open();
browser.navigateTo("http://www.google.com");
browser.textbox("q").setValue("sahi forums");
browser.submit("Google Search").click();
browser.link("Sahi - Web Automation and Test Tool").click();
browser.link("Login").click();
Assert.assertTrue(browser.textbox("req_username").exists());
browser.close();
System.out.println("Ending");
}
}
import org.testng.annotations.Test;
public class trial {
@Test
public void function2(){
browser.function1();
}
}
我的 pom.xml 如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0
http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>rashmi</groupId>
<artifactId>sahi</artifactId>
<version>1.0-SNAPSHOT</version>
<repositories>
<repository>
<id>clojars</id>
<name>Clojars Repository</name>
<url>http://clojars.org/repo</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>sahi</groupId>
<artifactId>sahi</artifactId>
<version>3.5</version>
</dependency>
<dependency>
<groupId>org.testng</groupId>
<artifactId>testng</artifactId>
<version>6.8.8</version>
</dependency>
</dependencies>
</project>
当我右键点击我的测试并运行它时,我得到的异常如下:
java.lang.NullPointerException
at net.sf.sahi.config.Configuration.getUserProperty(Configuration.java:694)
at net.sf.sahi.config.Configuration.getCommonDomain(Configuration.java:780)
at net.sf.sahi.client.Browser.openURL(Browser.java:265)
at net.sf.sahi.client.Browser.open(Browser.java:241)
at browser.function1(browser.java:15)
at trial.function2(trial.java:12)
有人可以帮我弄清楚为什么我会得到例外吗?
答案 0 :(得分:3)
正如Browser的javadoc所示,应在open
之前首先完成以下操作。
String sahiBasePath = "D:\\path\\to\\sahi_dir";
Sting userDataDirectory = "D:\\path\\to\\userdata_dir";
net.sf.sahi.config.Configuration.initJava(sahiBasePath, userDataDirectory);