0
votes

I am breaking down the testing on this so that I have feature files for areas like Login, ResetPassword, ForgotPassword etc. Let's say I have the below example. I have an automation step creating a brand new user in CreateAccount.feature. That step is used multiple times within that Feature/Step Class without issue. But now I want the user to change their password so I create a new Feature File MyAccount.feature. When I copy the Given Statement in, it is found immediately. Then I add the code to click the reset password and continue on with the rest of the steps.

When I run the ResetPassword test, the automation creates the new user but when it get's to step 2, "When I Click Reset Password" it fails because it can't find the element. Since bindings are global, this strikes me odd. So what I did was take step "Given I have created my account" and renamed it and added to the other feature file/steps class and ran it again. It worked fine.

I am not sure why I can't share between steps. Any ideas?

Some updates showing more code...

 CreateAccount.feature
 scenario: Feature Create Account
 Given I have created my account 
 -----------

CreateAccountsteps.cs
namespace Project
{
[Binding]
public class CreateAccount:  BaseTestObject
{

[Given]
public void Given_I_have_created_my_account()
{
        ConfigProperties.Environment = "Test";
        TestDriver.goToUrl(ConfigProperties.StartUrl);
        TestDriver.goToUrl(ConfigProperties.StartUrl + "Create/Account");
        [followed by input for creating a user acct]

-------------------------------------------------


 MyAccount.feature
 scenario: Feature Change Password
 Given I have created my account 
 When I Click Reset Password
 ...........

 MyAccountSteps.cs

 namespace Project
 {
 [Binding]
 public class MyAccountSteps: BaseTestObject
 {

 [When]
 public void When_I_click_Reset_Password()
 {
 On.MyHeaderPage.BtnResetPassword.Click();
 }
 [followed by rest of steps to change password]

BaseTestObject.cs

    namespace Project
  {

 public class BaseTestObject
  {
     private IWebDriver seleniumDriver;
    private IDriver testDriver;

    [TestInitialize]
    public virtual void Setup()
    {
        TestDriver.goToUrl(ConfigProperties.StartUrl);
    }


    [AfterScenario]
    public void CleanUp()
    {
        if (seleniumDriver != null)
        {
            SeleniumDriver.Dispose();
            seleniumDriver = null;
        }
    }



    public IWebDriver SeleniumDriver
    {
        get
        {
            if (seleniumDriver == null)
            {
                seleniumDriver = GetDriver();
            }
            return seleniumDriver;
        }
    }

    public IDriver TestDriver
    {
        get
        {
            if (testDriver == null)
            {
                testDriver = new UiDriver(SeleniumDriver);
            }
            return testDriver;
        }
    }


    public CurrentPageObjectScope On
    {
        get
        {
            return new CurrentPageObjectScope(TestDriver);
        }
    }

    public static String GetTimestamp()
    {
        return DateTime.Now.ToString("yyyyMMddhhmmssfff");
    }

    public static String GetTimestamp2()
    {
        return DateTime.Now.ToString("M/d/yyyy");
    }

    private IWebDriver GetDriver()
    {
        switch (ConfigProperties.Browser.ToLower())
        {
            case "firefox":
                return new FirefoxDriver();
            case "chrome":



                ChromeOptions options = new ChromeOptions();
                ChromeDriverService service =               ChromeDriverService.CreateDefaultService(@"../Chrome/");
                service.SuppressInitialDiagnosticInformation = true;
                service.HideCommandPromptWindow = true;
                options.AddArguments("test-type");
                options.AddArgument("--start-maximized");
                return new ChromeDriver(service, options);

            case "ie":
            case "internetexplorer":

                return new InternetExplorerDriver(@"../IE/");


            default:
                throw new NotImplementedException("Unknown browser string in Config properties " + ConfigProperties.Browser);
        }


    }
2
please add a bit more detail about whats happening in your Given step. Also what exactly do you mean by 'it can not find the first element for feature 2'? What element? and what is the exception you get? SpecFlow will create a new instance of each class (MySteps and MyTestTest) for each scenario so any objects that they use may be differentSam Holder
your code snippet looks like you are actually copying c# code into your .feature file. we need to know what code is in what files. at present you haven't been clear enough for anyone to help answer your question.perfectionist
ok updated and added some more details. Thanks for the help all. The error is the typical, Message: no such elementDazed
Thanks Sam - That was a typo. The shared step is "Given I have created my account". The error message is no such element found. As I mentioned, if I add the "Given I have created my account" code into the steps of the other feature, the elements are found and it runs fine. I am ending up duplicating steps instead of reusing them due to this issue. I will add the baseTestObject into the top section here.Dazed

2 Answers

0
votes

Based on your updates it looks like you have named your when step incorrectly. You feature says:

scenario: Feature Change Password
Given I have created my account 
When I Click Reset Password

but you step has the name When_I_click_My_Account

This seems wrong to me.

Really though we need more details (like the actual exception message) and perhaps some indication of what BaseTestObject looks like.

0
votes

Resolved - In the BaseTestObject I changed the methods to static.