0

I have a class named 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);
        }

    }
}

I want to call the method public void CheckLoginPage () from the specflow steps. that looks like this:

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();
        }
    }
}

I get now the error on the step ThenTheResultShouldBeOnTheScreen() with Error CS0120 An object reference is required for the non-static field, method, or property.

I tried to make CheckLoginPage () a static but then all the xpaths give an error.

Anyone can help me out on how to fix this?

  • Possible duplicate [How to make method call another one in classes C#?](https://stackoverflow.com/questions/16226444/how-to-make-method-call-another-one-in-classes-c) – TheGeneral Jan 23 '19 at 07:59
  • `new Test().CheckLoginPage();` – SᴇM Jan 23 '19 at 08:07

2 Answers2

0

You just have to initialize the class and call the method something like below:

  public void ThenTheResultShouldBeOnTheScreen()
    {
        new Test().CheckLoginPage();
    }
FreakyAli
  • 13,349
  • 3
  • 23
  • 63
  • This works. But when I execute the test I get the following error in Tests.cs: object reference not set to an instance of an object. – Tester2019 Jan 23 '19 at 10:28
  • Either `WaitMethods` or `Assert` is null you might want to check this https://stackoverflow.com/questions/779091/what-does-object-reference-not-set-to-an-instance-of-an-object-mean – FreakyAli Jan 23 '19 at 10:54
0
public void ThenTheResultShouldBeOnTheScreen()
{
    Test test = new Test() // initialize new instance of class
    test.CheckLoginPage()  // call method
}

If this doesn't work, you need to add reference

Kajbo
  • 1,068
  • 3
  • 16
  • 31