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");
目前你正在你的setup方法中初始化一个新的Selenium
对象,它只在你的base
方法的范围内,而不是你的类级Selenium
变量。
您定义了未初始化的类字段Selenium selenium;
,因此是null
。然后在base()
方法中创建了另一个本地变量Selenium 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;
WebDriver = new FirefoxDriver();selenium = new webdriverbackkedselenium (driver, "http://www.redbus.in");
//不需要使用selenium.start();或者设置一个默认端口
//下面一行selenium.type
. .如果您使用的是firefox 35或以上版本,则仍将返回空指针。所以首先尝试在firefox 15
,然后检查firefox 34
和请不要采取任何firefox更新后,你安装34。
我使用了以下jar
硒- server -空心1.0 20081010.060147.jarselenium-java-2.44.0.jarselenium-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;
问候,Ravinath Edirisinghe