java - 使用java的selenium代码中的空指针异常

标签 java javascript css string selenium

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/

相关文章:

javascript - 从接口(interface)声明中访问静态属性

javascript - 如何显示先前从一个网页执行到另一个网页的 .js 文件的值?

java - 在线程 "main"java.lang.VerifyError : class com. google.gwt.dev.HostedModeBase$ArgHandlerNoServerFlag 中获取异常

java - 使用 PowerMock 添加测试后 Spring Web 应用程序无法启动

javascript - 在模板内使用 Vuex 存储中对象的嵌套属性

javascript - Snap.svg 缩放和动画 SVG

html - 创建一个简单的登录表单

javascript - 如何建立类似gmail的菜单标题

java - 过滤器,在 Controller 之前获取 url (Spring MVC)

java - 立方体渲染: Textures displaying incorrectly