[英]null pointer exception in selenium code using java
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.Selenium;
public class RedBus {
Selenium selenium;
@BeforeClass
public void base()
{
Selenium selenium = new DefaultSelenium("localhost",4444,"*firefox","http://www.redbus.in");
selenium.start();
selenium.windowMaximize();
selenium.open("/");
selenium.waitForPageToLoad("10000");
}
@Test
public void domain() throws InterruptedException
{
selenium.type("//input[@id='DDLSource']","hyde");
Thread.sleep(5000);
selenium.waitForPageToLoad("10000");
if(selenium.isTextPresent("//dt[@value='Hyderabad']"))
{
selenium.click("//dt[@value='Hyderabad']");
}
else{
System.out.println("ele not found");
}
/*
selenium.type("//input[@id='DDLDestination']","pune");
selenium.click("//img[@alt='Select your date of journey']");
*/
}
}
盡管在哪里可以獲得NullPointerException
尚不清楚,但我懷疑您需要更改以下行:
Selenium selenium = new DefaultSelenium("localhost",4444,"*firefox","http://www.redbus.in");
至
selenium = new DefaultSelenium("localhost",4444,"*firefox","http://www.redbus.in");
目前,您正在設置方法中初始化一個新的Selenium
對象,該對象僅在base
方法的作用域內,而不在類級Selenium
變量中。
您定義了Selenium selenium;
類字段Selenium selenium;
沒有初始化,因此為null
。 然后在base()
方法中創建另一個局部變量Selenium selenium
並將其初始化。 然后在您的測試中嘗試使用未初始化的硒字段。
為了使您的代碼正常工作,請從Selenium selenium = new DefaultSelenium...
行中刪除Selenium
,即:
@BeforeClass
public void base() {
selenium = new DefaultSelenium("localhost",4444,"*firefox","http://www.redbus.in");
selenium.start();
selenium.windowMaximize();
selenium.open("/");
selenium.waitForPageToLoad("10000");
}
嘗試這個
DefaultSelenium selenium = null;
//用它代替硒硒;
WebDriver驅動程序=新的FirefoxDriver(); selenium = new WebDriverBackedSelenium(驅動程序,“ http://www.redbus.in ”);
//無需使用selenium.start(); 或設置默認端口
//如果您使用的是Firefox 35或更高版本,則以下行selenium.type
..仍將返回空指針。 因此,請先嘗試使用firefox 15
,然后再檢查firefox 34
並在安裝34之后不要進行任何firefox更新。
我用過以下的罐子
selenium-server-coreless-1.0-20081010.060147.jar selenium-java-2.44.0.jar selenium-server-standalone-2.44.0.jar
//導入語句
import com.thoughtworks.selenium.DefaultSelenium;
import com.thoughtworks.selenium.webdriven.WebDriverBackedSelenium;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.WebDriver;
問候,拉維納斯·埃迪利辛格
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.