[英]Not able to call method from class
我有一個名為test.cs的類:
using Microsoft.VisualStudio.TestTools.UnitTesting;
using OpenQA.Selenium;
using OpenQA.Selenium.Support.PageObjects;
using qa.WrapperFactory;
namespace Common.PageObjects
{
public class Test
{
[FindsBy(How = How.XPath, Using = "xpath")]
private IWebElement foundElement;
[FindsBy(How = How.XPath, Using = "xpath")]
private IWebElement EnvironmentLogoElement;
[FindsBy(How = How.XPath, Using = "xpath")]
private IWebElement UsernameElement;
[FindsBy(How = How.Id, Using = "xpath")]
private IWebElement PasswordElement;
public void Setup()
{
// Set window to full screen
BrowserFactory.Driver.Manage().Window.Maximize();
// Clear all cookies
BrowserFactory.Driver.Manage().Cookies.DeleteAllCookies();
}
public void CheckLoginPage ()
{
WaitMethods.WaitForShort(() => foundElement.Displayed);
Assert.IsTrue(UsernameElement.Displayed);
Assert.IsTrue(PasswordElement.Displayed);
}
}
}
我想從specflow步驟中調用方法public void CheckLoginPage()。 看起來像這樣:
using System.Configuration;
using Common.PageObjects;
using qa.WrapperFactory;
using TechTalk.SpecFlow;
namespace RegressionTest
{
[Binding]
public class SmokeTestSteps
{
[Given(@"I go to the HRControlnet login page")]
public void GivenIGoToTheHRControlnetLoginPage()
{
BrowserFactory.InitBrowser("Firefox");
var subDomain = ConfigurationManager.AppSettings["Environment"];
BrowserFactory.LoadApplication(subDomain);
}
[Then(@"the result should be on the screen")]
public void ThenTheResultShouldBeOnTheScreen()
{
Test.CheckLoginPage();
}
}
}
我現在在步驟ThenTheResultShouldBeOnTheScreen()上收到錯誤CS0120的錯誤。非靜態字段,方法或屬性需要對象引用。
我試圖將CheckLoginPage()設為靜態,但是所有xpath都給出了錯誤。
任何人都可以幫助我解決此問題?
您只需要初始化該類並調用以下方法即可:
public void ThenTheResultShouldBeOnTheScreen()
{
new Test().CheckLoginPage();
}
public void ThenTheResultShouldBeOnTheScreen()
{
Test test = new Test() // initialize new instance of class
test.CheckLoginPage() // call method
}
如果這不起作用,則需要添加參考
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.