public void DownloadPocketQueryIntegrationTest()
        {
            WebExtractorPocketQuery test = new WebExtractorPocketQuery();

            System.Object      websiteLock = new System.Object();
            List <PocketQuery> queries     = test.ExtractPocketQueries(websiteLock).ToList();

            Assert.NotNull(queries);
            Assert.NotZero(queries.Count());

            foreach (PocketQuery pq in queries)
            {
                Assert.NotNull(pq.DateGenerated);
                Assert.NotNull(pq.EntryCount);
                Assert.NotNull(pq.FileSize);
                Assert.NotNull(pq.Name);
                Assert.NotNull(pq.Url);
                Assert.NotNull(pq.HttpClient);
            }

            //Also do integration tests unzipping and file stuff.
            //Possibly not the ideal place to put these..
            // Only need to test one link..
            PocketQuery query = queries.ElementAt(0);

            Assert.NotNull(query.Zip);
            Assert.NotZero(query.Zip.Entries.Count);

            Assert.NotNull(query.GpxGeocaches);
            Assert.IsNotEmpty(query.GpxGeocaches);

            Assert.NotNull(query.GpxWaypoints);
            Assert.IsNotEmpty(query.GpxWaypoints);
        }
        public IEnumerable <PocketQuery> ExtractPocketQueries(Object websiteLock)
        {
            WebExtractor webExtractor = new WebExtractor();
            HtmlDocument result       = webExtractor.GetPage("/pocket/");

            //Determine if log in was successful
            //<div class="validation-summary-errors">

            var loginResult = result.DocumentNode.Descendants("div").FirstOrDefault(d => d.Attributes.Contains("class") &&
                                                                                    d.Attributes["class"].Value.Equals("validation-summary-errors"));

            if (loginResult != null)
            {
                //TODO When GUI is implemented, need to write this error to GUI message, or debug.
                Debug.WriteLine("Password or Email incorrect!");
                return(new List <PocketQuery>());
            }

            //Iterate over each Pocket Query to get the download strings. The fourth one should have the "<a href"
            Debug.WriteLine("Extracting PocketQueries");
            var table = result.GetElementbyId("uxOfflinePQTable");

            if (table == null)
            {
                //TODO send web extractor status message according to website errors.
                return(null);
            }


            var rows = table.ChildNodes.Where(row => row.Name.Equals("tr"));

            List <PocketQuery> pocketQueries = new List <PocketQuery>();

            foreach (var row in rows)
            {
                PocketQuery pocketQuery = new PocketQuery()
                {
                    HttpClient = webExtractor.Client
                };
                pocketQuery.WebsiteLock = websiteLock;
                var columns = row.ChildNodes.Where(c => c.Name.Equals("td"));

                if (columns.Count() < 6)
                {
                    // Last line in table is not a PocketQuery entry, but a option to delete selected Queries from website.
                    continue;
                }

                for (int column = 0; column < columns.Count(); column++)
                {
                    switch (column)
                    {
                    case 2:
                        pocketQuery.Url  = columns.ElementAt(column).ChildNodes.First(n => n.Name.Equals("a")).Attributes.First(n => n.Name.Equals("href")).Value;
                        pocketQuery.Name = columns.ElementAt(column).ChildNodes.First(n => n.Name.Equals("a")).InnerText.Trim();
                        break;

                    case 3:
                        pocketQuery.FileSize = columns.ElementAt(column).InnerText.Trim();
                        break;

                    case 4:
                        pocketQuery.EntryCount = Convert.ToInt16(columns.ElementAt(column).InnerText.Trim());
                        break;

                    case 5:
                        /* Date last generated
                         * Strings will be of the form: "31 Oct 16 (last day available)" or "01 Nov 16 (1 days remaining)"
                         */
                        pocketQuery.DateGenerated = DateTime.Parse(columns.ElementAt(column).InnerText.Trim().Substring(0, 9));
                        break;
                    }
                }
                pocketQueries.Add(pocketQuery);
            }
            return(pocketQueries);
        }