【问题标题】:Selenium Driver - How to use the same new driver in different classes in C#Selenium 驱动程序 - 如何在 C# 的不同类中使用相同的新驱动程序
【发布时间】:2020-07-17 05:10:14
【问题描述】:

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

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

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

谢谢

【问题讨论】:

  • 创建一个辅助类并将IWebDriver 作为该类的静态属性。在其他地方使用它

标签: c# selenium-webdriver c#-4.0 c#-3.0 g1ant


【解决方案1】:

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

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;

【讨论】:

    【解决方案2】:
    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;**
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-28
      相关资源
      最近更新 更多