当我试图在开放的网站上执行操作时,我得到空指针异常

时间:2017-08-30 10:01:54

标签: selenium

大家好,我创建了一个实用程序包,我已经创建了一个下面的类

utility package    

public class Utils           
{
    public static  void  openBrowser(WebDriver driver,String url)            
    {            
        System.out.println("Internet Explorer is selected");         

        System.setProperty("webdriver.ie.driver","D:\\Requirede for Automation\\IEDriverServer.exe");          
        driver = new InternetExplorerDriver();            
        driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS);       
        driver.manage().window().maximize();           
        driver.get(url);      
    }      
}                         

现在上面的类我试图在我的主要自动化软件包中访问并且能够这样做但是当我尝试在开放网站上执行操作时我得到空指针异常所以有些请告诉我是什么调用类并对其进行操作的正确方法。

public class dropdown 
{           

    WebDriver driver;                

    @Test       
    public void openBrowser() {        

        String url = ("https://loadfocus.com/blog/2016/06/13/how-to-select-adropdown-in-selenium-webdriver-using-java");                                            

        WebDriver driver = null;                                   
        utility.Utils.openBrowser(driver, url);                                                
    }                         

    @Test                                      

    public void open()  
    {                                                 

        Select dropdown = new Select(driver.findElement(By.id("mySelect")));                                          
        dropdown.selectByIndex(2);                                                        
    }                                                  
}                                                      

错误收到

  

选择Internet Explorer启动InternetExplorerDriver服务器   (32位)   2.39.0.0侦听端口18171 log4j:WARN找不到logger(org.apache.http.client.protocol.RequestAddCookies)的appender。   log4j:WARN请正确初始化log4j系统。 log4j:警告请参阅   http://logging.apache.org/log4j/1.2/faq.html#noconfig了解更多信息。   2017年8月30日下午1:54:35 org.openqa.selenium.remote.ProtocolHandshake   createSession INFO:检测到的方言:OSS PASSED:openBrowser FAILED:   打开java.lang.NullPointerException   automationFramework.dropdown.open(dropdown.java:45)at   sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)at   sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)     在   sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)     在

的java.lang.reflect.Method.invoke(Method.java:498)

3 个答案:

答案 0 :(得分:0)

您看到的是NullPointerException,因为当您调用openBrowser()方法时,您将driver作为null发送,因为您保留了WebDriver driver = null

您的问题的解决方案是初始化WebDriver实例并将其转换为测试类中的InternetExplorerDriver,即dropdown类。

您需要的更改是:

  • 班级dropdown

    WebDriver driver = new InternetExplorerDriver(); //add the cast                                  
    utility.Utils.openBrowser(driver, url);
    
  • 班级Utils

    System.setProperty("webdriver.ie.driver","D:\\Requirede for Automation\\IEDriverServer.exe");          
    // remove "driver = new InternetExplorerDriver();"            
    driver.manage().timeouts().implicitlyWait(20, TimeUnit.SECONDS); 
    

答案 1 :(得分:0)

It looks like what you really want to do based on your code snipped is have your openBrowser() method be a setup method. Currently you have a local instance of driver initialized by the openBrowser method which the open() method does not use hence NPE. Try the following

included

This would also require that you openBrowser() in your Utils class returns the instance of the browser. I would actually consider having a static method in your Utils class that simply initializes the driver and returns the instance but that's all in how you want to design your classes.

Also keep in mind @Before executes before every test, perhaps you may want to use @BeforeMethod or @BeforeClass depending on what your goals. It also depends on whether you're using TestNG or JUnit but the documentation for both is worth looking at.

答案 2 :(得分:-1)

中添加IEDriverServer.exe
  

C:\ Windows \ System32下

然后重新启动计算机。它会成功运行。