如何使用页面对象隐藏我的主要测试方法中的所有WebElements

时间:2017-05-13 17:25:30

标签: c# selenium

所以我想在几个网站邮件网站上使用selenium执行一些测试,所以我构建了这个:

public abstract class AbstractInboxPage<T> where T : AbstractPageEmenetsMap, new()
{
   protected T Map
   {
        get { return new T(); }
   }

    public abstract void Login(string user, string pawwrod);

    public abstract void OpenContacts();

    public abstract void OpenRequireEmail();

    public abstract void OpenComposeMessageWindow();
}

public abstract class AbstractInboxPage<M, V> : AbstractInboxPage<M>
    where M : AbstractPageEmenetsMap, new()
    where V : AbstractPageValidator<M>, new()
{
    public V Validate()
    {
        return new V();
    }
}

public class GmailInboxPage : AbstractInboxPage
{
    public override void Login(string user, string pawwrod)
    {
        throw new NotImplementedException();
    }

    public override void OpenComposeMessageWindow()
    {
        throw new NotImplementedException();
    }

    public override void OpenContacts()
    {
        throw new NotImplementedException();
    }

    public override void OpenRequireEmail()
    {
        throw new NotImplementedException();
    }
}

驱动程序

public class WebDriver
    {
        private static IWebDriver _driver;
        private static WebDriverWait _webDriverWait;
        private static IJavaScriptExecutor _javaScriptExecutor;
        private static Actions _actions;
        private static BrowserType _browserType;

        public static IWebDriver Driver
        {
            get
            {
                if (_driver == null)
                    throw new NullReferenceException("The WebDriver browser instance was not initialized. You should first call the method Start.");
                return _driver;
            }

            private set
            {
                _driver = value;
            }
        }

        public static WebDriverWait WebDriverWait
        {
            get
            {
                if (_webDriverWait == null || _driver == null)
                    throw new NullReferenceException("The WebDriver 'Driver' wait instance was not initialized. You should first call the method StartBrowser.");
                return _webDriverWait;
            }

            private set
            {
                _webDriverWait = value;
            }
        }

        public static IJavaScriptExecutor JavaScriptExecutor
        {
            get
            {
                if (_javaScriptExecutor == null || _driver == null)
                    throw new NullReferenceException("The WebDriver 'JavaScriptExecutor' instance was not initialized. You should first call the method StartBrowser.");
                return _javaScriptExecutor;
            }
        }

        public static Actions Actions
        {
            get
            {
                if (_actions == null || _driver == null)
                    throw new NullReferenceException("The WebDriver 'Actions' instance was not initialized. You should first call the method StartBrowser.");
                return _actions;
            }
        }

        public static BrowserType BrowserType
        {
            get
            {
                return _browserType;
            }
        }

        public static void StartBrowser(BrowserType browserType = BrowserType.Chrome, int defaultTimeOut = 120)
        {
            _browserType = browserType;
            switch (browserType)
            {
                case BrowserType.Firefox:
                    WebDriver.Driver = new FirefoxDriver();
                    break;

                case BrowserType.InternetExplorer:
                    break;

                case BrowserType.Chrome:

                    ChromeOptions chromeOptions = new ChromeOptions();
                    chromeOptions.AddArguments("--disable-notifications");
                    chromeOptions.AddArguments("--disable-infobars");
                    chromeOptions.AddArguments("start-maximized");

                    DesiredCapabilities desiredCapabilities = DesiredCapabilities.Chrome();
                    desiredCapabilities.SetCapability(ChromeOptions.Capability, chromeOptions);
                    WebDriver.Driver = new ChromeDriver(Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location), "chrome_driver"), chromeOptions);
                    break;

                default:
                    break;
            }

            _webDriverWait = new WebDriverWait(_driver, TimeSpan.FromSeconds(defaultTimeOut));
            _javaScriptExecutor = (IJavaScriptExecutor)_driver;
            _actions = new Actions(_driver);
        }

        public static void StopBrowser()
        {
            _driver.Quit();
            _driver = null;
            _webDriverWait = null;
        }
    }

所以我想创建单独的类,例如AbstractInboxPageElementsMap和另一个儿子(在我的情况下GmailInboxPageElementsMap),在这里我将使用简单的get找到我的所有元素,但我想要从我的主要隐藏这个,所以所有的元素只能从儿子看到,在我的情况下GmailInboxPageElementsMap

public class AbstractPageEmenetsMap
{

}

page validator

public class AbstractPageValidator<T> where T:AbstractPageEmenetsMap, new()
{

}

现在这是我的Gmail实施:

我的页面元素:

public class GmailInboxPageEmenetsMap : AbstractPageEmenetsMap
{
    public IWebElement ComposeButton
    {
        get { return WebDriver.WebDriverWait.Until(ExpectedConditions.ElementToBeClickable(By.XPath("//div[normalize-space(text())='COMPOSE']"))); }
    }
}

一些验证员:

public class GmailInboxPageValidator:AbstractPageValidator<AbstractPageEmenetsMap>
{

}

收件箱页面:

public class GmailInboxPage: AbstractInboxPage<GmailInboxPageEmenetsMap, GmailInboxPageValidator>
{
    public override void Login(string user, string pawwrod)
    {

    }

    public override void OpenComposeMessageWindow()
    {
        throw new NotImplementedException();
    }

    public override void OpenContacts()
    {
        throw new NotImplementedException();
    }

    public override void OpenRequireEmail()
    {
        throw new NotImplementedException();
    }
}

问题:

在这个课程中我得到了这个错误:

  

严重级代码描述项目文件行抑制状态   错误CS0311“GmailInboxPageValidator”类型不能用作类型   泛型类型或方法'AbstractInboxPage'中的参数'V'。   没有隐式引用转换   'GmailInboxPageValidator'来   'AbstractPageValidator'。

1 个答案:

答案 0 :(得分:0)

您可以将元素保存为“GmailInboxPage.cs”文件中的私有get属性。但你的追求是将元素保存在一个单独的文件中并将其隐藏在主文件中,你可以使用“部分类”概念。

将所有项目保存在GmailInboxPage.cs文件的单独“patial类”中作为私有get属性。 在另一个部分类的GmailInboxPage.cs文件中,可以访问这些元素,您可以将其从其他文件中隐藏。

文件1

您可以将文件名保留为“GamilInboxElements”,但类名应为“GmailInboxPage”。请参阅附图

partial class GmailInboxPage
{
    private IWebElement ToButton { get { return xxxxx; }}
}

文件2

 partial class  GmailInboxPage
    {
        public void setToAddress()
        {
            ToButton.SendKeys("xxxx");
        }
    }

enter image description here