简体   繁体   中英

Building a selenium chrome webdriver with parameters and passing it to other methods in C#

I am new to Visual Studio 2015 and C#, my last coding exposure was about 15 years ago in a high school visual basic class. I have been asked to create a desktop application for someone else that goes to a webpage and performs some tasks. They want to click a couple buttons in the application instead of going to the page and don't want to see the browser or a command prompt.

I chose Selenium and C# in Visual Studio 2015 ent because it works with my needs for headlessly opening a chrome browser without the command prompt, also for office integration later. I did it like this:

    //Sets up chrome webdriver with hidden console and headless mode.
    ChromeOptions option = new ChromeOptions();
    option.AddArguments("--headless");
    var driverService = ChromeDriverService.CreateDefaultService();
    driverService.HideCommandPromptWindow = true;
    //Add "option" in ChromeDriver() to activate headless chrome \/
    IWebDriver driver = new ChromeDriver(driverService);

"option" left out of ChromeDriver() for testing, I need to see what the browser is doing at this step.

And it works, but it was created in a button_click event. My problem now is that I don't know the correct way to establish the driver with all the parameters needed outside of this method. I'd also like to use that same driver's session again without starting over.

My end goal is to have that button populate the form with the contents of a CheckedListBox (the only part of the page that changes and needs direct input) then another button sending the user's selection to the page and generating a report.

Here is the relevant part:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using OpenQA.Selenium;
    using OpenQA.Selenium.Chrome;
    using OpenQA.Selenium.Support.UI;

namespace Report_Tool
{

public partial class ReportToolUI : Form
{

    //Sets the chrome webdriver.
    public static IWebDriver Driver { get; set; }

    public ReportToolUI()
    {
        InitializeComponent();
    }

    public void ReportToolUI_Load(object sender, EventArgs e)
    {

    }

    public void getListButton_Click(object sender, EventArgs e)
    {

        //Sets up chrome webdriver with hidden console and headless mode.
        ChromeOptions option = new ChromeOptions();
        option.AddArguments("--headless");
        var driverService = ChromeDriverService.CreateDefaultService();
        driverService.HideCommandPromptWindow = true;
        //Add "option" in ChromeDriver() to activate headless chrome \/
        IWebDriver driver = new ChromeDriver(driverService);

        //Goes to obfuscated site.
        driver.Navigate().GoToUrl("obfuscated");

    }}}

What would be the correct way to create that webdriver once and call it in the different button_click methods?

Use a singleton :

public class Utils
{
   private static IWebDriver _driver;

   public static IWebDriver Driver {
      get {
         if (_driver == null) {

            var service = ChromeDriverService.CreateDefaultService();
            service.HideCommandPromptWindow = true;

            var options = new ChromeOptions();
            options.AddArguments("--headless");

            var commandTimeout = TimeSpan.FromSeconds(30);

            _driver = new ChromeDriver(service, options, commandTimeout);
         }

         return _driver;
      }
   }
}

Then access the driver anywhere with Utils.Driver .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM