1
我在Eclipse中使用Selenium Web Driver和JUnit。我希望通過爲可複製步驟創建新類來減少我的代碼。爲了方便,我希望將這些類存儲在單獨的文件中。例如,這一個這樣的類:如何在硒測試中正確調用外部類(包含硒命令)?
import org.openqa.selenium.ie.InternetExplorerDriver;
//Login
public class Login {
private InternetExplorerDriver driver;
String url;
String name;
String password;
String language;
public Login (String ur, String nam, String pass, String lang){
url=ur;
name=nam;
password=pass;
language=lang;
}
public void log_in(){
driver.get(url);
driver.findElement(By.name("username")).sendKeys(name);
driver.findElement(By.name("password")).sendKeys(password);
driver.findElement(By.name("language")).sendKeys(language);
driver.findElement(By.name("logon_action")).click();
}
}
這是我的主要測試:
package bobsworld;
import init.Login;
import org.junit.Assert;
import org.junit.Test;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.ie.InternetExplorerDriver;
public class WebTest {
private WebDriver driver;
@Test
public void testUnit() throws Exception {
//Open new window
System.setProperty("webdriver.ie.driver", "IEDriverServer.exe");
driver = new InternetExplorerDriver();
//Login
init.Login login = new init.Login ("myurl", "log","pas","English");
login.log_in();
}
問題是與對象的驅動程序。我得到java.lang.NullPointerException
並且無法執行測試。我應該如何組織調用登錄和代碼來使我的測試工作?