Beispiel #1
0
        protected static void NagivateBrowserToUrlWithTimeout(BrowserSession.BrowserSession browser, string url, TimeSpan timeout)
        {
            AutoResetEvent ev = new AutoResetEvent(false);

            var thread = new Thread(new ThreadStart(() =>
            {
                try
                {
                    browser.GoTo(url);
                    ev.Set();
                }
                catch (ThreadAbortException)
                {
                }
            }));

            thread.Start();

            if (!ev.WaitOne(timeout))
            {
                browser.Stop();
                Thread.Sleep(5000);
                thread.Abort();
                throw new TimeoutException();
            }
        }
Beispiel #2
0
        public static void CheckoutAndSelectDomesticDelivery(BrowserSession.BrowserSession browser)
        {
#if kill
            browser.GoTo(URL.CheckoutUrl);
#else // kill
            NagivateBrowserToUrlWithTimeout(browser, URL.CheckoutUrl, TimeSpan.FromMinutes(1));
#endif // kill

            // select domestic delivery
            {
                var domestic_button = browser.DefaultFrame.FindElementById <BrowserSession.WebElement.Anchor>("delivery-type-domestic");
                if (domestic_button == null)
                {
                    throw new Exception("cant find #delivery-type-domestic");
                }
                domestic_button.ClickAsync().Wait();
                Task.Delay(1000).Wait();
            }
        }
Beispiel #3
0
        private void RemoveAllItemsInBag(BrowserSession.BrowserSession browser)
        {
            // open bag page
#if kill
            browser.GoTo(URL.BagUrl);
#else // kill
            NagivateBrowserToUrlWithTimeout(browser, URL.BagUrl, TimeSpan.FromMinutes(1));
#endif // kill

            DateTime start = DateTime.Now;
            while (DateTime.Now < start + TimeSpan.FromMinutes(5))
            {
                browser.ExecuteJavascriptAsync("document.body.scrollTop = document.documentElement.scrollTop = 0;").Wait();
                //var remove_button = browser.DefaultFrame.FindElementById("BasketRemove:0");
                var remove_button = browser.DefaultFrame.FindElementByXPath("//span[starts-with(@id, 'BasketRemove:')]");
                if (remove_button == null)
                {
                    return;
                }

                try
                {
                    //while (!remove_button.IsVisibleOnWindow)
                    //    browser.ExecuteJavascriptAsync("window.scrollTo(0, 10)").Wait();
                    remove_button.ClickWithoutScrollAsync().Wait();
                    //remove_button.ClickAsync().Wait();
                }
                catch (Exception ex)
                {
                    while (!browser.IsCompletedLoading)
                    {
                        System.Threading.Thread.Sleep(1000);
                    }
                }
            }

            throw new Exception("failed to clear item from bag");
        }
Beispiel #4
0
        /// <summary>
        /// get url of all items in this category page
        /// </summary>
        /// <param name="browser"></param>
        /// <param name="update_status"></param>
        /// <returns></returns>
        private string[] GetAllItemUrls(BrowserSession.BrowserSession browser, UpdateStatusDelegate update_status)
        {
            for (int trial = 0; trial < 10; trial++)
            {
                try
                {
                    // stop current loading on browser
                    browser.Stop();

                    // go to the category page
#if kill
                    browser.GoTo(this.Url);
#else // kill
                    NagivateBrowserToUrlWithTimeout(browser, this.Url, TimeSpan.FromMinutes(1));
#endif // kill

                    var item_urls = new HashSet <string>();

                    while (true)
                    {
                        var product_containers = browser.DefaultFrame.FindElementByClassName("hproduct");
                        if (product_containers == null)
                        {
                            break;
                        }

                        bool added = false;
                        foreach (var product_container in product_containers)
                        {
                            var anchors = product_container.FindElementByTagName <BrowserSession.WebElement.Anchor>("a");
                            if (anchors == null)
                            {
                                continue;
                            }
                            foreach (var anchor in anchors)
                            {
                                if (item_urls.Contains(anchor.Href))
                                {
                                    continue;
                                }
                                item_urls.Add(anchor.Href);
                                added = true;
                            }
                        }

                        if (!added) // no more
                        {
                            break;
                        }

#if future
                        // scroll to bottom 5 times to load more items
                        for (int i = 0; i < 5; i++)
                        {
                            browser.ExecuteJavascriptAsync("window.scrollTo(0, document.body.scrollHeight);").Wait();
                        }
#else // future
                        break;
#endif // future
                    }

                    if (item_urls.Count == 0)
                    {
                        throw new Exception("force retry");
                    }

                    return(item_urls.ToArray());
                }
                catch (Exception ex)
                {
                    System.Threading.Thread.Sleep(5000);
                }
            }

            update_status("category failed: " + this.Url);
            return(null);
        }
Beispiel #5
0
        public void GetProductDetailAndAddToBag(BrowserSession.BrowserSession browser, UpdateStatusDelegate update_status, out bool sold_out)
        {
            sold_out = false;

            // navigate to the item page
#if kill
            browser.GoTo(this.Url);
#else // kill
            NagivateBrowserToUrlWithTimeout(browser, this.Url, TimeSpan.FromMinutes(1));
#endif // kill

            // find the detail element
            BrowserSession.WebElement.WebElement product_detail_container;
            {
                var product_detail_containers = browser.DefaultFrame.FindElementByClassName("product-detail");
                if (product_detail_containers == null || product_detail_containers.Count < 1)
                {
                    throw new Exception("cant find .product-detail");
                }
                product_detail_container = product_detail_containers[0];
            }

            // time
            {
                this.TimeUTC = DateTime.UtcNow;
            }

            // title
            {
                var title_containers = product_detail_container.FindElementByTagName("h1");
                if (title_containers == null || title_containers.Count < 1)
                {
                    throw new Exception("cant find .product-detail h1");
                }
                this.Title = title_containers[0].Text.Trim();
            }

            // image
            {
                var image_container = browser.DefaultFrame.FindElementById <BrowserSession.WebElement.Image>("main-image");
                if (image_container == null)
                {
                    throw new Exception("cant find #image_container");
                }
                this.ImageUrl = image_container.Src;
            }

            // original price
            {
                var price_info_containers = product_detail_container.FindElementByClassName("price");
                if (price_info_containers == null || price_info_containers.Count < 1)
                {
                    throw new Exception("cant find .product-detail .price");
                }
                string txt = string.Concat(price_info_containers[0].Text.ToCharArray().Where(c => c == '.' || (c >= '0' && c <= '9')));
                this.OriginalPrice = float.Parse(txt);
            }

            // color
            {
                var color_container = product_detail_container.FindElementById("product-colour");
                if (color_container == null)
                {
                    throw new Exception("cant find .product-detail #product-colour");
                }
                this.Color = color_container.Text.Trim();
            }

            // sizes
            {
                List <string> options = new List <string>();

                var available_sizes_container = product_detail_container.FindElementById("available-sizes");
                if (available_sizes_container != null)
                {
                    var select_containers = product_detail_container.FindElementByTagName("select");
                    if (select_containers != null && select_containers.Count == 1)
                    {
                        var option_containers = available_sizes_container.FindElementByTagName <BrowserSession.WebElement.Option>("option");
                        if (option_containers != null)
                        {
                            foreach (var option_container in option_containers)
                            {
                                if (option_container.Value == "0")
                                {
                                    continue;
                                }

                                options.Add(option_container.InnerHTML.Trim().Replace("\"", "\'"));

                                // select this size for later stage of add-to-bag
                                browser.ExecuteJavascriptAsync("document.getElementById('product_id').value='" + option_container.Value + "';").Wait();
                            }
                        }
                    }
                }

                this.Sizes = options.ToArray();
            }

            // add to bag
            {
                var form = product_detail_container.FindElementById <BrowserSession.WebElement.Form>("add-to-bag");
                if (form == null)
                {
                    sold_out = true;
                    return;
                    //throw new Exception("cant find .product-detail #add-to-bag");
                }

                var cart_count_container = browser.DefaultFrame.FindElementById("minicart-count");
                if (cart_count_container == null)
                {
                    throw new Exception("cant find #minicart-count");
                }

                var original_count = cart_count_container.Text.Trim();

                form.SubmitAsync().Wait();

                // wait until cart is updated
                int      failed     = 0;
                bool     added      = false;
                DateTime start_wait = DateTime.Now;
                while (DateTime.Now < start_wait + TimeSpan.FromMinutes(1))
                {
                    try
                    {
                        cart_count_container = browser.DefaultFrame.FindElementById("minicart-count");
                        var new_count = cart_count_container.Text.Trim();
                        if (original_count != new_count)
                        {
                            added = true;
                            break;
                        }
                    }
                    catch
                    {
                        failed++;
                        if (failed >= 50)
                        {
                            throw new Exception("cant update cart");
                        }
                    }

                    Task.Delay(200).Wait();
                }

                if (!added)
                {
                    sold_out = true;
                    update_status(this.Title + " is sold out");
                    return;
                }
            }
        }