0

我已经声明了属性文件并在一个类中初始化了 Webdriver 对象。现在我想在同一个包或另一个包中的任何地方使用这个 Webdriver 对象。如何?

请在下面找到代码:

public class Config
{
public static Properties config =null;
public static Properties OR = null;
public static WebDriver driver = null ;
public static Logger APPLICATION_LOGS = Logger.getLogger("devpinoyLogger");

@SuppressWarnings("unused")
public void initialization() throws IOException
{
    // creating properties files storing the ID's and xpaths
    APPLICATION_LOGS.debug("Starting the test suite");
    APPLICATION_LOGS.debug("Loading config files");
    config = new Properties();
    //FileInputStream fp = new FileInputStream("./config.properties");
    FileInputStream fp = new FileInputStream(System.getProperty("user.dir")+"\\src\\com\\ode\\utility\\config.properties");
    config.load(fp);
    APPLICATION_LOGS.debug("Loading Object XPATHS");
    OR = new Properties();
    //FileInputStream fp1 = new FileInputStream("./OR.properties");
    FileInputStream fp1 = new FileInputStream(System.getProperty("user.dir")+"\\src\\com\\ode\\utility\\OR.properties");
    OR.load(fp1);

    APPLICATION_LOGS.debug("Starting the driver");
    driver = new InternetExplorerDriver();

    driver.get(config.getProperty("Testwebsite"));
    driver.manage().window().maximize();
    driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
}
}

我不知道如何使用这个...帮我解决这个问题...

帮助将不胜感激..

4

2 回答 2

0

由于您可能会在相同和不同包中的不同类中使用驱动程序,如何在一个类中初始化驱动程序,以及哪个类需要驱动程序,只需扩展该类,以便在测试用例运行时访问来自不同的不同方法类,那里会使用相同的驱动程序引用吗?

public class A {

public static WebDriver driver = new ......();

}

public class B extends A {
  //members and attributes of class B
}

public class C extends A {
  //members and attributes of class C

}
于 2013-03-19T16:16:46.593 回答
0

我在我正在开发的测试框架中所做的是我创建了基类:

BasePage.java
BaseTest.java
BaseElement.java

并在 BaseTest 类中初始化 webdriver 对象。我正在遵循 PageObject 模式。因此,我创建的任何 pageobject 类都在其中扩展 BasePage 类。我编写的任何测试类(带有注释的 TestNG 测试方法),我都扩展了 BaseTest 类。因此,在 BaseTest 类中初始化的驱动程序对象可以通过它们的构造函数在子类和 PageObject 类中传递。那可行。

于 2013-03-27T09:28:02.817 回答