TestStack / TestStack.Seleno

Seleno helps you write automated UI tests in the right way by implementing Page Objects and Page Components and by reading from and writing to web pages using strongly typed view models.
http://teststack.github.com/TestStack.Seleno/
MIT License
180 stars 60 forks source link

Broken tests: help with logic #216

Open sirj77 opened 9 years ago

sirj77 commented 9 years ago

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?

robdmoore commented 9 years ago

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.