我正在使用Selenium进行自动化。挑战是这样的。
FirefoxDriver driver = new FirefoxDriver();
InternetExplorerDriver driver = new InternetExplorerDriver();
ChromeDriver driver = new ChromeDriver();
所有这三个都创建了驱动程序对象。并且所有驱动程序对象都有类似的方法。有一段代码将在实例化这些对象后运行。在程序的一次运行期间,只使用其中一个驱动程序。我需要一些逻辑来做到这一点。由于所有这些都没有超级类别。我正在寻找其他方法。
答案 0 :(得分:2)
Factory方法模式可以快速解决您的问题。
//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();
答案 1 :(得分: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);
}
}
答案 2 :(得分: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
}
答案 3 :(得分:0)
策略设计模式是解决这个问题的方法, 它会将对象的实例化延迟到运行时。
答案 4 :(得分: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;
}
}
}