0

我需要知道我们如何在不同的类中使用相同的 Selenium Driver 实例?所以我在一个类中创建了一个驱动程序的实例,使用代码:

IWebDriver driver = new ChromeDriver("C:/Users/Krishna/source/repos/G1ANT.Addon.YouTube/packages/Selenium.Chrome.WebDriver.83.0.0/driver");

所以我需要在新类中使用这个实例,我该怎么做?

谢谢你

4

2 回答 2

0
You need to create utils class for driver instance so you can ignore nullpoint exception.  i will share my utils class for driver instance you can use it (create package for utils classes inside you project folder)

    package utils;

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.File;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Paths;

public class TestApp {
    private WebDriver driver;
    private static TestApp myObj;
    // public static WebDriver driver;
    utils.PropertyFileReader property = new PropertyFileReader();

    public static TestApp getInstance() {
        if (myObj == null) {
            myObj = new TestApp();
            return myObj;
        } else {
            return myObj;
        }
    }

    //get the selenium driver
    public WebDriver getDriver()
    {
        return driver;
    }

    //when selenium opens the browsers it will automatically set the web driver
    private void setDriver(WebDriver driver) {
        this.driver = driver;
    }

    public static void setMyObj(TestApp myObj) {
        TestApp.myObj = myObj;
    }

    public void openBrowser() {
        //String chromeDriverPath = property.getProperty("config", "chrome.driver.path");
        //String chromeDriverPath = property.getProperty("config", getChromeDriverFilePath());
        System.setProperty("webdriver.chrome.driver", getChromeDriverFilePath());
        ChromeOptions options = new ChromeOptions();
        options.addArguments("--disable-notifications");
        options.addArguments("disable-infobars");
        driver = new ChromeDriver(options);
        driver.manage().window().maximize();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    public void navigateToURL() {
        String url =property.getProperty("config","url");
        driver.get(url);
    }

    public void closeBrowser()
    {
        driver.quit();
    }

    public WebElement waitForElement(By locator, int timeout)
    {
        WebElement element = new WebDriverWait(TestApp.getInstance().getDriver(), timeout).until
                (ExpectedConditions.presenceOfElementLocated(locator));
        return element;
    }

    private String getChromeDriverFilePath()
    {
        // checking resources file for chrome driver in side main resources
        URL res = getClass().getClassLoader().getResource("chromedriver.exe");
        File file = null;
        try {
            file = Paths.get(res.toURI()).toFile();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
        return file.getAbsolutePath();
    }
}
Note if you are using mac mention above chromedriver only 
Keep chrome driver in resource folder 

TestApp.getInstance().getDriver() -This is way use driver.


the 2 nd option is you can create global variable for driver instance 
 ---**static private WebDriver driver;**
于 2020-07-17T05:29:45.270 回答
0

最简单的方法是将驱动程序放在另一个类的静态字段中,如下所示。或者您可以使用单例设计模式在整个应用程序中只返回一次驱动程序实例

public class DriverHelper
{
    public static readonly IWebDriver Driver = new ChromeDriver("C:/Users/Krishna/source/repos/G1ANT.Addon.YouTube/packages/Selenium.Chrome.WebDriver.83.0.0/driver");   

}

然后像这样访问驱动程序

var _driver = DriverHelper.Driver;
于 2020-07-17T05:25:15.300 回答