类型中不存在C#类型名称。公共方法似乎不可见

时间:2016-12-29 15:09:25

标签: c# selenium

我是C#的新手,对于我的第一个项目,我尝试使用网络界面自动执行某些任务。

我对C#中的公共类和方法的理解是它应该对我正在进行的项目的其余部分可见,但在这种情况下似乎并非如此。

我收到错误"类型名称' ClearAllUnraisedAlerts'类型' ClearAlerts'"中不存在我希望有人能告诉我我在这里做出的错误假设或新手错误。

我的Program.cs是

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ManagedWorkspaceAutomator
{
    class Program
    {
        static void Main(string[] args)
        {
            AutomationFunctions.AlertsBoard.ClearAlerts test = new AutomationFunctions.AlertsBoard.ClearAlerts.ClearAllUnraisedAlerts();
        }
    }
}

试图在AutomationFunctions \ AlertsBoard \ ClearAlerts.cs中调用ClearAllUnraisedAlerts方法

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;

namespace ManagedWorkspaceAutomator.AutomationFunctions.AlertsBoard
{
    public class ClearAlerts
    {
        private IWebDriver _driver;

        public void ClearAllUnraisedAlerts()
        {
            _driver = new OpenQA.Selenium.Firefox.FirefoxDriver();

            PageModels.LoginPage login = new PageModels.LoginPage(_driver);

            login.EnterUsername = "testuser";
            login.EnterPassword = "testpass";
            login.ClickLogin();

        }
    }
}

为了完整性,这是我的Pagemodel,在PageModels \ LoginPage.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenQA.Selenium;

namespace ManagedWorkspaceAutomator.PageModels
{
    public class LoginPage
    {
        protected readonly IWebDriver WebDriver;

        public LoginPage(IWebDriver webdriver)
        {
            this.WebDriver = webdriver;
            string title = WebDriver.Title;

            if(!title.Equals("Managed Workspace Login"))
            {
                throw new InvalidOperationException("This is not the Login Page. Current page is: " + title);
            }

        }

        /*
          A property to enter username on login page
        */
        public string EnterUsername
        {
            set
            {
                IWebElement usernameField = WebDriver.FindElement(By.Id("txbUserName"));
                usernameField.Clear();
                usernameField.SendKeys(value);
            }
        }

        /*
          A property to enter password on login page
        */

        public string EnterPassword
        {
            set
            {
                IWebElement passwordField = WebDriver.FindElement(By.Id("txbPassword"));
                passwordField.Clear();
                passwordField.SendKeys(value);
            }
        }

        /*
          A method to click the login button on the page 
        */

        public void ClickLogin()
        {
            IWebElement loginButton = WebDriver.FindElement(By.Id("SubmitB"));
            loginButton.Click();
        }
    }
}

1 个答案:

答案 0 :(得分:0)

初始化对象的方式是错误的。请这样做

AutomationFunctions.AlertsBoard.ClearAlerts test = new ManagedWorkspaceAutomator.AutomationFunctions.AlertsBoard.ClearAlerts();
test.ClearAllUnraisedAlerts();