Tuesday, 7 March 2017

100 Day Deep Work - Day 5: Iterating Over Links



Day 5 of the 100 Day Deep Work challenge!

Today’s session was a bit all over the place. I spent most of the time imagining what I think the solution to my current problem could be, than actually solving the problem in code. Such is the way sometimes I guess but I certainly look forward to having mental models of code patterns in my mind to apply more readily.

Here’s the problem:
You have a website with 4 navigation links, 3 are text and 1 is a link under a company logo. The expectation is more links will be added in the future, they might be text or images. The test must check the known set of text links or linked images are present and if any new ones have been added. If new ones have been added, this should simply be reported and the known set tested anyway. The classes/methods for this must be reusable, to cover other sets of links across the website.

To do this I started splitting out the list of known links into a Dictionary

        public static IDictionary<stringstring> knownTextLinkAndURLList
            = new Dictionary<stringstring>
           {
                { "News","http://www.bbc.co.uk/news/" }
                { "Sport","http://www.bbc.co.uk/sport" }
            };


Then I created a method to loop over the links by looking for the link text

       public static void TopNavigationLinksCheck()
        {
            foreach (var navigationLink in knownTextLinkAndURLList.Key)
            {
                if (IsElementPresent(By.LinkText(navigationLink)))
                {
                    Console.WriteLine($"As expected, I saw: '{navigationLink}' link text");
                }
                else
                {
                    Console.WriteLine($"I could NOT locate the link: {navigationLink}");
                    TakeScreenshot.SaveScreenshot();
                    var pageNavigationElementException = $"I expected {navigationLink} but it was not located";
                    throw new Exception(pageNavigationElementException);
                }
            }
        }


Which used a reusable extension in the If statement I copied down from Stack Overflow:

        public static bool IsElementPresent(By by)
        {
            try
            {
                Driver.Instance.FindElement(by);
                return true;
            }
            catch (NoSuchElementException)
            {
                return false;
            }
        }

This seems fine for checking over text links, but we need to do that then check the actual link is correct. This is assuming we're concerned link text may be written incorrectly / against an agree style and that the link applied might vary, say news.bbc instead of bbc.co.uk/news for example.

Two things to investigate further then; 1) How to check both link text and link, 2) How to confirm there are no new links added (we can already check if any are removed or their links changed)

Day 6 here we come!

Mark.

(Be sure to have a look at the book - Deep Work)
-------------------------------------------------------------------------------------------------------------
Day 4: http://cyreath.blogspot.co.uk/2017/03/100-day-deep-work-day-4-configuration.html

0 comments: