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
变量。
关于java - 使用java的selenium代码中的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16589625/