Selenium

I started using Selenium with my current client .. on the side, mostly because logging into the website involved: “wait click text text click click text click wait click wait click”. Additionally, any time I compile, it wipes my logged in session, and thus repeat. 

Last night at my son’s soccer game, I was chatting with another technical parent.. he was starting to use Selenium also!  In his case, Selenium IDE and converting that into code for tests.   Having played a bit with that side of things, and ending up with Selenium WebDriver + some ideas of how to do it nicely, I decided to compose this post to share my experiences so far. 

  • Code on Github (learning github for windows);
  • VS2012, Nuget package restore enabled;
  • Total time taken to write code (from scratch) = Less than taken to write this post talking about the code.  

Targeting NerdDinner

My client would have a cow (and a chicken, and maybe several other barnyard animals) if I put any of their code outside their network.  And I’d get fired.  So I recreated the pattern I am using against www.nerddinner.com.  A certain dude whom I respect created it as a demo website. It had about the right mix of complexity – it was not just searching on google  – but it wasn’t navigating gmail either.  Thank you Scott.

First Attempt at Code.

See github branch here; Excerpt from here

        [Test]
        public void MainPage_CanSearchForDinners_ButNeverFindsAnyInKY()
        {
            using (IWebDriver driver = new FirefoxDriver())
            {
                driver.Navigate().GoToUrl("http://www.nerddinner.com");
                var input = driver.FindElement(By.Id("Location"));
                input.SendKeys("40056");
                var search = driver.FindElement(By.Id("search"));
                search.Click();
                var results = driver.FindElements(By.ClassName("dinnerItem"));
                // at this point, i don't know what to do, as there's never any search results.   
                Assert.AreEqual(0, results.Count,
                                "No dinners should be found.. omg, if this works, then its worth it to change the test");
            }
        }
  • This code is using NUnit (out of scope for this post)
  • This is how you navigate
  • This is how you find things
  • This is how you send text
  • This is how you click things
  • Every time you run, you’re starting of with no cookies, so you’ll have to log in every time.

Whee.  Good start.  Okay, now lets get serious[er]. 

Second Pass At Code

Per the recommendations of Jim Holmes (@aJimHolmes, http://frazzleddad.blogspot.com/), whom I met in person at http://www.codepalousa.com/ and whom I have also derived a great deal of respect, I know what smells funny:

  • The first pass is very brittle.  If somebody changes an ID or a classname, you have a LOT of tests to go change.   Solution: Page Pattern
  • What does one do when one does not have a second bullet, but there seems like there should be one?

What I accomplish with all this Jazz:

image

The overall branch is here; the rewritten test here:

        [Test]
        public void MainPage_CanSearchForDinners_ButNeverFindsAnyInKY()
        {
            MainPage.LocationToSearch.SendKeys("40056");
            MainPage.SearchButton.Click();
            var results = MainPage.PopularDinnerSearchResults;

            Assert.AreEqual(0, results.Count,
                            "No dinners should be found.. omg, if this works, then its worth it to change the test");

        }

Much simpler.  The Page Helper Class is here, and partially looks like:

    public class MainPage : PageBase
    {
        public MainPage(IWebDriver driver)
            : base(driver)
        {
            Wait.Until(d => d.Title == "Nerd Dinner");
        }

        public static MainPage NavigateDirectly(IWebDriver driver)
        {
            driver.Navigate().GoToUrl("http://www.nerddinner.com");
            return new MainPage(driver);
        }

        public IWebElement LocationToSearch
        {
            get { return Driver.FindElement(By.Id("Location")); }
        }

        public IWebElement SearchButton
        {
            get { return Driver.FindElement(By.Id("search")); }
        }

  • The constructor takes and stores a reference to the Driver.  The calling program is responsible for creating and disposing the driver (as its an expensive resource)
  • It uses a PageBase class which creates an additional WebDriverWait (the Wait variable)
  • The constructor waits until we know we’re on the right page.  This allows us to click something somewhere else, and new up this object, and then wait till the page actually loads. 
  • Because this is the root of the website, I include a NavigateDirectly() routine which says “I don’t care where you were, now go to this page”.  I only do this on overview or login pages, the kind not derived from a click.
  • It exposes IWebElements to the callers (tests) so they don’t need to know where on the page various things are located.
  • In WebForms – I have to implement extension methiods which search IWebDriver and IWebElement for id’s Ending In Text, because ID=”btnSearch” ends up being “ctl00.BoogerBooger_04.HumDeDum_03.YabbaDabba.WickedBackbtnSearch”

Another excerpt (here):

public List UpcomingDinners
        {
            get
            {
                var upcomingDinnersUl = (from e in Driver.FindElements(By.ClassName("upcomingdinners"))
                                         where e.TagName == "ul"
                                         select e).FirstOrDefault();
                if (upcomingDinnersUl == null) throw new NotFoundException("could not find ul.upcomingdinners");
                return upcomingDinnersUl.FindElements(By.TagName("a")).ToList(); 
            }
        }
  • When doing complicated finds, I usually search By.ClassName or By.Id or whatever first, and end up with e.TagName second, because they do NOT provide an e.Id or e.ClassName routine.   (You can do e.GetAttribute(“id”) but I’m not sure if that returns null or empty, and nulls can be a bummer).
  • new ByChained(x,y,z) does not mean an element which matches x,y and z, but instead an element x which contains an element y which contains an element z.   

Third Pass – the Developer Helper

If I run the Console App which is the library that has all the page helpers, I get something like this:

image

  • The purpose of this app is to let me quickly get to sections of my website that I need to play around in.
  • The keywords yield a more detailed list; the more detailed list is used in navigation; using the old trick of:
    delimited = “|“+delimited+”|“; if (delimited.contains(“|xxx|“) … 

    which is what we did before arrays #GetOffMyLawn #WheresMyTeeth

  • Once the site is up, it stays there (until I choose somewhere else to go)

Additionally, in tests, I redid the creation/deletion of the driver as follows:

        [TestFixtureSetUp]
        public void FixtureSetup()
        {
            
        }

        [TestFixtureTearDown]
        public void FixtureTeardown()
        {
            AbandonDriver();
        }

        [SetUp]
        public void TestSetup()
        {
            if (Driver == null) Driver = new FirefoxDriver();
            MainPage = MainPage.NavigateDirectly(Driver);
        }

        [TearDown]
        public void TestTeardown()
        {
            
        }

        private void AbandonDriver()
        {
            if (Driver != null) Driver.Dispose();
            Driver = null;
        }

  • I am not creating a driver for every test; however if a test is going to mess up the relative state of a driver, it can be abandoned and a new one started.
  • This saves several seconds per test, which helps once you break the 5-6 test mark.  Especially if what is saved is not as much “start the driver”, but more “start the driver, go to the web site, log in, and get to the page of interest”.

Conclusion

Once you’ve done it once or twice, its remarkably easy to write some pretty neat tests.   This may not be the perfect way to do it, but its something that works for me; hope it works for you!

Author: sunnywiz

Me.

Leave a Reply

Your email address will not be published. Required fields are marked *