如何全局初始化 Web 驱动程序? [英] How can I initialize web driver globally?

查看:57
本文介绍了如何全局初始化 Web 驱动程序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 selenium webdriver 进行自动化,java.获得 nullpointerexception 并且它说驱动程序为空.

I am working on automation using selenium webdriver , java. Getting nullpointerexception and it says driver is null.

我的代码结构如下:

包实用程序

  • 基类
  • 登录类
  • App_constant 类

包添加用户

  • 添加用户类

实用程序包代码:

 package Utility;

 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.chrome.ChromeDriver;

public class Base {

public static WebDriver driver = null;


//CALL WEB BROWSER AND OPEN WEBSITE
public static void openURL()
{

    try{

    System.setProperty("webdriver.chrome.driver", "D:\\chromedriver.exe");
    driver = new ChromeDriver();
    driver.get(Constant_value_utility.URL);
    }catch(Exception E)

    {
        E.printStackTrace();


    }
}

 }

<小时>

package Utility;

public class Constant_value_utility {


//OPEN URL
public static final String URL = "Site URL";


//LOGIN FIELDS
public static final String loginbox = "UserName";
public static final String passbox = "Password";

//LOGIN DATA
public static final String username = "test";
public static final String password = "test";
public static final String loginbt = "btnsubmit";

  }

<小时>

 package Utility;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;


 public class Login_Page {

public static WebDriver driver;


public static void login()
{

       Base.openURL();


       driver.manage().window().maximize();

       driver.findElement(By.id(Constant_value_utility.loginbox)).sendKeys(Constant_value_utility.username);
       driver.findElement(By.id(Constant_value_utility.passbox)).sendKeys(Constant_value_utility.password);
       driver.findElement(By.id(Constant_value_utility.loginbt)).click();

  }

}

添加用户包代码

package Adduser;
import Utility.Base;
 import Utility.Login_Page;

 import org.openqa.selenium.WebDriver;
 import org.openqa.selenium.chrome.ChromeDriver;

 public class Add_User {

  public static void main(String[] args){
   {


    //LOGIN TO SITE
     Base.openURL();
     Login_Page.login();
    }}}

现在我的问题是我已经在基类中创建了公共静态方法 openurl() 并且在那里初始化了 webdriver.但是当我在同一个包和其他包的其他类中调用相同的方法时,为什么它给我 webdriver 的 nullpointerexception?

Now My question is I have already created public static method openurl() in base class and webdriver is initialized there. But When I call same method in other class of same package and other packages , Why it is giving me nullpointerexception for webdriver?

是否有必要在每个类中编写代码来初始化 webdriver 并调用浏览器.如何全局初始化 Web 驱动程序,以便我声明一次并可以调用项目中的任何位置.

Is that necessary to write code to initialize webdriver and call browser in every class. How can I initialize web driver globally so I declare it once and can call any where in my project.

推荐答案

你的 webDriver 也在另一个类中声明,所以

your webDriver is declared in the other class too, so

   Base.openURL();


   driver.manage().window().maximize();

此处未初始化驱动程序.

driver is not initialized here.

尝试重写您的 Base.openUrl() 方法以返回 webDriver

try to rewrite your Base.openUrl() method in order to return the webDriver

您的类字段对其他类可见,这是真的.但为了从正确的类中获得一个,你应该尝试类似 Base.driver因为 Base.driver != Login_Page.driver

edit: your class fields are visible to other classes, thats true. but in order to get the one from the correct class you should try something like Base.driver because Base.driver != Login_Page.driver

edit2:这是一个工人阶级可能是什么样子的例子

edit2: here is one example of how a working class could look like

   package Utility;

   import org.openqa.selenium.By;
   import org.openqa.selenium.WebDriver;


   public class Login_Page {

   //public static WebDriver driver;


   public static void login()
   {

   Base.openURL();

   //note the change from driver  to Base.driver
   Base.driver.manage().window().maximize();
   Base.driver.findElement(By.id(Constant_value_utility.loginbox)).sendKeys(Constant_value_utility.username);
   Base.driver.findElement(By.id(Constant_value_utility.passbox)).sendKeys(Constant_value_utility.password);
   Base.driver.findElement(By.id(Constant_value_utility.loginbt)).click();

}

这篇关于如何全局初始化 Web 驱动程序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆