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']");
*/
}
}
3 回答
虽然不清楚你在哪里得到 a 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;
,因此是null
. 然后在base()
方法中创建另一个局部变量Selenium selenium
并对其进行初始化。然后在您的测试中尝试使用未初始化的 selenium 字段。
要使您的代码Selenium
从 line中删除Selenium selenium = new DefaultSelenium...
,即:
@BeforeClass
public void base() {
selenium = new DefaultSelenium("localhost",4444,"*firefox","http://www.redbus.in");
selenium.start();
selenium.windowMaximize();
selenium.open("/");
selenium.waitForPageToLoad("10000");
}
尝试这个
默认硒硒=空;
// 用这个代替 Selenium selenium;
WebDriver driver = new FirefoxDriver(); selenium = new WebDriverBackedSelenium(driver, " 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;
问候, 拉维纳特·埃迪林赫