Skip to content

Broken tests: help with logic #216

Open

Description

@sirj77

Hi all,

I've bumped into some not really good logic in my objects. I tried to change it, but the results were pretty much the same.
A problem: when I run my tests for some functional part on the site and if one of the tests is failed, then the others probably will be also failed, because of depending.
How to re-write this piece of code to eliminate an issue?

       public HomePage LogInUser(string name, string password)
        {
            if (!LoginLink) return this;
            Find.Element(By.CssSelector(LoginLinkCss)).Click();
            Name = name;
            Password = password;
            SignIn();
            return this;
        }

        public bool LoginLink
        {
            get { return Find.Element(By.CssSelector(LoginLinkCss)).Displayed; }
        }

So, an example what I need to handle:
1st test is failed for some reason (LogInUser method is processed successfully and on some step it is failed, user is still logged in)
2nd test is running (but on the LogInUser method the test is becoming failed, because it gets LoginLink first and then it's trying to find en element By.CssSelector(LoginLinkCss) and:
TestStack.Seleno.Configuration.Interceptors.SelenoReceivedException : Unable to locate element: {"method":"css selector","selector":"[href='https:/my_site/#login']"}, because the user is already logged in)

How to re-write it that even if one test is failed for some reason it will not break the others?

Activity

  1. robdmoore commented on Aug 26, 2015

    @robdmoore
    Member

    You have two options.

    The general way to handle this is to not make assumptions in your tests. If there is a particular context that matters for the test when starting then you should do that (e.g. if the user should start logged off then send them to the log out link as the first thing then perform login).

    The second option is to use the Find.OptionalElement method, which will return null if something doesn't exist. You can use that to perform conditional logic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions