1

我正在使用 Selenium 进行自动化。挑战是这样的。

FirefoxDriver driver = new FirefoxDriver();
InternetExplorerDriver driver = new InternetExplorerDriver();
ChromeDriver driver = new ChromeDriver();

所有这三个创建驱动程序对象。并且所有驱动程序对象都有类似的方法。有一段代码将在这些对象实例化后运行。在程序的一次运行期间,仅使用其中一个驱动程序。我需要一些逻辑来做到这一点。因为没有所有这些 3 的超类。我正在寻找其他出路。

4

5 回答 5

2

工厂方法模式可以快速解决您的问题。

//Define the interface with the common methods
Interface ISuperDriver
{
void run();
}

//implement the interface on firefox
class FireFoxDriver:ISuperDriver{
void run(){
//firefox driver
}
}

//implement the interface on IE
class InternetExplorerDriver:ISuperDriver{
void run(){
//ie driver
}
}

//chrome
class ChromeDriver:ISuperDriver{
void run(){
//chrome
}
}

//create reference for the interface
ISuperDriver Driver;

if(<input 1>){
 Driver= new FireFoxDriver();}//instantiate firefox
else if(<input 2>){
 Driver= new InternetExplorerDriver();}//IE
else if(<input 3>){
 Driver= new ChromeDriver();}//Chrome

//Finally invoke your method
Driver.Run();
于 2012-09-30T19:15:35.870 回答
1

只需创建一个包含这 3 个类的功能的超类。

public abstract class Browser {
    public abstract Navigate(string link);
}

public class Firefox : Browser {
    FirefoxDriver driver;
    public Firefox(){
        driver = new FirefoxDriver();
    }

    public abstract Navigate(string link){
        driver.GoTo(link);
    }
}

public class Chrome : Browser {
    ChromeDriver driver;
    public Chrome (){
        driver = new ChromeDriver();
    }

    public abstract Navigate(string link){
        driver.FollowLink(link);
    }
}
于 2012-09-30T19:06:23.710 回答
0

我知道这已经晚了。但这就是我最终为自己工作的东西

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;

using OpenQA.Selenium.Internal;
using OpenQA.Selenium.IE;
using OpenQA.Selenium.Chrome;
using OpenQA.Selenium.Firefox;
using OpenQA.Selenium.Remote;
using OpenQA.Selenium;
using System.Collections.Specialized;

enum Browser { ie, firefox, chrome };


namespace SearchandClickAds
{
    class Program
    {


        public static RemoteWebDriver getRemoteDriver(Browser b)
        {

            switch (b)
            {
                case Browser.ie:
                    return new InternetExplorerDriver(
                                    InternetExplorerDriverService.CreateDefaultService(),
                                    new InternetExplorerOptions(),
                                    TimeSpan.FromMinutes(10)
                    );

                case Browser.firefox:

                    return new FirefoxDriver(
                                new FirefoxBinary(),
                                new FirefoxProfile(),
                                TimeSpan.FromMinutes(10)
                    );

                case Browser.chrome:

                    return new ChromeDriver(
                                                ChromeDriverService.CreateDefaultService(),
                                                new ChromeOptions(),
                                                TimeSpan.FromMinutes(10)
                    );
            }

            return null;
        }


        static void Main(string[] args)
        {

            string browser_s = args[0].ToLower();
            Browser browser;

            if (browser_s == "ie")
            {
                browser = Browser.ie;
            }
            else if (browser_s == "chrome")
            {
                browser = Browser.chrome;
            }
            else if (browser_s == "firefox")
            {
                browser = Browser.firefox;
            }
            else
            {
                Console.WriteLine("Unknown browser. Must be ie, chrome, or firefox");
                return;
            }

            RemoteWebDriver driver = null;

            while (true)
            {
                try
                {

                    driver = getRemoteDriver(browser);
                    /* Do navigation here */
                 }
            }

            Console.WriteLine("Done.");
            Environment.Exit(0);
            return;

        }
    }
}
于 2013-02-23T20:43:29.723 回答
0

策略设计模式就是解决这个问题的一种,它将对象的实例化延迟到运行时。

点我

于 2012-09-30T20:51:59.977 回答
0

您可以使用以下类型的类:

public abstract class BrowserDriver { ... }

public class InternetExplorerDriver : BrowserDriver { ... }
public class FirefoxDriver : BrowserDriver { ... }
public class ChromeDriver : BrowserDriver { ... }

用法:

BrowserDriver driver = null;

switch (BrowserType) // assuming BrowserType is a property of type Browser enum, holding the value from the set {InternetExplorer, Firefox, Chrome}
{
    case Browser.InternetExplorer:
        driver = new InternetExplorerDriver();
        break;
    case Browser.Firefox:
        driver = new FirefoxDriver();
        break;
    case Browser.Chrome:
        driver = new ChromeDriver();
        break;
}

SomeFunction(driver); 

// ...

public void SomeFunction(BrowserDriver driver)
{
    //... your code here
}
于 2012-09-30T19:17:04.600 回答