![](/img/trans.png)
[英]Unable to use driver object of webdriver class inside class and outside method but able to access it inside a method of same classCan anyone explain?
[英]Declared the Properties files and initialized the Webdriver object in a class. I want to use this Webdriver object in the same package or another
我已經聲明了Properties文件並在一個類中初始化了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);
}
}
我不知道該如何使用...在這個問題上幫助我...
幫助將不勝感激..
由於您可能會在同一類和不同包中的不同類中使用該驅動程序,因此如何在一個類中初始化該驅動程序,以及哪個類需要該驅動程序,只需擴展該類,以便在測試用例運行時從不同的方法訪問不同的方法類,是否將使用相同的驅動程序引用?
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
}
我在開發的測試框架中所做的就是創建了基類:
BasePage.java
BaseTest.java
BaseElement.java
並在BaseTest類中初始化webdriver對象。 我正在遵循PageObject模式。 因此,我創建的任何pageobject類都將在其中擴展BasePage類。 我編寫的任何測試類(帶有注釋的TestNG測試方法)都將擴展BaseTest類。 因此,可以在BaseTest類中初始化的驅動程序對象通過其構造函數在子類以及PageObject類中傳遞。 那可行。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.