Example #1
0
        public void Should_clear_cookies()
        {
            using (var ie = new IE())
            {
                // Clear all cookies.
                ie.ClearCookies();

                // Ensure our test cookies don't exist from a previous run.
                Assert.IsNull(ie.GetCookie("http://1.watin.com/", "test-cookie"), "Expected no cookies for 1.watin.com");
                Assert.IsNull(ie.GetCookie("http://2.watin.com/", "test-cookie"), "Expected no cookies for 2.watin.com");

                // Create cookies for a pair of domains.
                ie.SetCookie("http://1.watin.com/", "test-cookie=abc; expires=Wed, 01-Jan-2020 00:00:00 GMT");
                ie.SetCookie("http://2.watin.com/", "test-cookie=def; expires=Wed, 01-Jan-2020 00:00:00 GMT");

                // Clear cookies under one subdomain.
                ie.ClearCookies("http://1.watin.com/");

                // Ensure just the cookie of the first subdomain was deleted.
                Assert.IsNull(ie.GetCookie("http://1.watin.com/", "test-cookie"), "Expected no cookies for 1.watin.com after clear");
                Assert.AreEqual("test-cookie=def", ie.GetCookie("http://2.watin.com/", "test-cookie"), "Expected cookie for 2.watin.com");

                // Clear cookies under master domain.
                ie.ClearCookies("http://watin.com/");

                // Ensure the second subdomain's cookie was deleted this time.
                Assert.IsNull(ie.GetCookie("http://2.watin.com/", "test-cookie"), "Expected no cookies for 2.watin.com after ClearCookies");
            }
        }
Example #2
0
        public void login()
        {
            browser = new IE();
            browser.ClearCache();
            browser.ClearCookies();
            Utils.GoTo(browser, "http://www.tribalwars.net/sv-se/page/logout");
            Utils.GoTo(browser, "http://www.tribalwars.net");
            //browser.Body.Focus();
            browser.TextField(Find.ById("user")).TypeText(username);
            browser.TextField(Find.ById("password")).TypeText(password);
            System.Console.WriteLine("Clicking login.");
            browser.Link(Find.ByClass("btn-login")).Click();
            System.Console.WriteLine("Waiting");
            Div wContainer = browser.Div(Find.ByClass("worlds-container"));

            wContainer.WaitUntilExists();
            System.Console.WriteLine("Going to world");
            Utils.GoTo(browser, "http://www.tribalwars.net/sv-se/page/play/" + world);
            System.Console.WriteLine("Logged in");
            while (browser.Html == null)
            {
                System.Threading.Thread.Sleep(100);
            }
            Utils.CheckBotProtection(browser.Html);
            isLoggedIn = true;
            // Give browser object to villages.
            foreach (Village village in villages)
            {
                village.SetBrowser(ref browser);
                village.SetWorld(world);
            }
        }
Example #3
0
        public void TestSmokePageAskForLogin()
        {
            ie.GoTo("http://localhost:13164/Photo/PhotoAlbumManager.aspx");
            ie.ClearCookies();
            TextField tf = ie.TextField(Find.ByName(t => t.EndsWith("$UserName")));

            Assert.That(tf.Exists);
        }
Example #4
0
 public void TearDownTest()
 {
     browser.ClearCookies(rootUrl);
     browser.ClearCache();
     browser.Close();
     browser.Dispose();
     browser = null;
 }
Example #5
0
        void Close()
        {
            ie.ClearCache();
            ie.ClearCookies();
            ie.Close();
            ie.Dispose();

            complete();
        }
Example #6
0
 private void initialize()
 {
     data = new Data();
     ie   = new IE();
     ie.ClearCookies();
     ie.ClearCache();
     ie.WaitForComplete();
     ie.Visible             = data.BrowserVisible;
     accountRegistrationUrl = getOpenAccountUrl();
     navigateToRegistrationPage();
     fillAccountForm();
 }
Example #7
0
        void Open()
        {
            try
            {
                ie = new IE();

                ie.ShowWindow(NativeMethods.WindowShowStyle.Hide);
                ie.ClearCache();
                ie.ClearCookies();
            }
            catch { }
            //ie.ShowWindow(NativeMethods.WindowShowStyle.Hide);
        }
Example #8
0
        public LambdAssert GoToUrlInFreshBrowser(string url)
        {
            using (IE ie = WebBrowser as IE)
            {
                ie.ClearCookies();
                ie.ClearCache();
                ie.Close();
            }

            WebBrowser.Reopen();
            WebBrowser.GoTo(url);
            WebBrowser.WaitForComplete();
            return(LambdAssert);
        }
Example #9
0
 private void navigateToUrl(string url)
 {
     while (true)
     {
         ie.ClearCache();
         ie.ClearCookies();
         ie.GoTo(url);
         Thread.Sleep(6000);
         ie.WaitForComplete();
         if (ie.Url == url)
         {
             break;
         }
     }
 }
Example #10
0
        /// <summary>
        /// Method to clear the cache of a browser
        /// will clear cache of IE or FireFox browser
        /// </summary>
        /// <param name="browser"></param>
        /// <param name="browsertype">IE or FireFox</param>
        public void ClearCache(Browser browser, string browsertype)
        {
            switch (browsertype)
            {
            case "IE":

                IE ie = (IE)browser;

                ie.ClearCache();
                ie.ClearCookies();
                break;

            case "FireFox":

                FireFox ff = (FireFox)browser;

                Console.Write("Unsure how to clear cache in FireFox");

                ff.Refresh();


                break;
            }
        }
Example #11
0
        public void Should_set_and_get_cookies()
        {
            using (var ie = new IE())
            {
                // Clear all cookies.
                ie.ClearCookies();

                // Ensure our test cookies don't exist from a previous run.
                Assert.IsNull(ie.GetCookie("http://1.watin.com/", "test-cookie"));
                Assert.IsNull(ie.GetCookie("http://2.watin.com/", "test-cookie"));

                // Create cookies for a pair of domains.
                ie.SetCookie("http://1.watin.com/", "test-cookie=abc; expires=Wed, 01-Jan-2020 00:00:00 GMT");
                Assert.AreEqual("test-cookie=abc", ie.GetCookie("http://1.watin.com/", "test-cookie"));

                ie.SetCookie("http://2.watin.com/", "test-cookie=def; expires=Wed, 01-Jan-2020 00:00:00 GMT");
                Assert.AreEqual("test-cookie=def", ie.GetCookie("http://2.watin.com/", "test-cookie"));

                CookieCollection collection = ie.GetCookiesForUrl(new Uri("http://2.watin.com/"));
                Assert.AreEqual(1, collection.Count);
                Assert.AreEqual(collection[0].Name, "test-cookie");
                Assert.AreEqual(collection[0].Value, "def");
            }
        }
Example #12
0
 public void SetUpTest()
 {
     browser = new IE(rootUrl);
     browser.ClearCookies(rootUrl);
     browser.ClearCache();
 }
Example #13
0
        public void Should_set_and_get_cookies()
        {
            using (var ie = new IE())
            {
                // Clear all cookies.
                ie.ClearCookies();

                // Ensure our test cookies don't exist from a previous run.
                Assert.IsNull(ie.GetCookie("http://1.watin.com/", "test-cookie"));
                Assert.IsNull(ie.GetCookie("http://2.watin.com/", "test-cookie"));

                // Create cookies for a pair of domains.
                ie.SetCookie("http://1.watin.com/", "test-cookie=abc; expires=Wed, 01-Jan-2020 00:00:00 GMT");
                Assert.AreEqual("test-cookie=abc", ie.GetCookie("http://1.watin.com/", "test-cookie"));

                ie.SetCookie("http://2.watin.com/", "test-cookie=def; expires=Wed, 01-Jan-2020 00:00:00 GMT");
                Assert.AreEqual("test-cookie=def", ie.GetCookie("http://2.watin.com/", "test-cookie"));

                CookieCollection collection = ie.GetCookiesForUrl(new Uri("http://2.watin.com/"));
                Assert.AreEqual(1, collection.Count);
                Assert.AreEqual(collection[0].Name, "test-cookie");
                Assert.AreEqual(collection[0].Value, "def");
            }
        }
Example #14
0
        public void Should_clear_cookies()
        {
            using (var ie = new IE())
            {
                // Clear all cookies.
                ie.ClearCookies();

                // Ensure our test cookies don't exist from a previous run.
                Assert.IsNull(ie.GetCookie("http://1.watin.com/", "test-cookie"), "Expected no cookies for 1.watin.com");
                Assert.IsNull(ie.GetCookie("http://2.watin.com/", "test-cookie"), "Expected no cookies for 2.watin.com");

                // Create cookies for a pair of domains.
                ie.SetCookie("http://1.watin.com/", "test-cookie=abc; expires=Wed, 01-Jan-2020 00:00:00 GMT");
                ie.SetCookie("http://2.watin.com/", "test-cookie=def; expires=Wed, 01-Jan-2020 00:00:00 GMT");

                // Clear cookies under one subdomain.
                ie.ClearCookies("http://1.watin.com/");

                // Ensure just the cookie of the first subdomain was deleted.
                Assert.IsNull(ie.GetCookie("http://1.watin.com/", "test-cookie"), "Expected no cookies for 1.watin.com after clear");
                Assert.AreEqual("test-cookie=def", ie.GetCookie("http://2.watin.com/", "test-cookie"), "Expected cookie for 2.watin.com");

                // Clear cookies under master domain.
                ie.ClearCookies("http://watin.com/");

                // Ensure the second subdomain's cookie was deleted this time.
                Assert.IsNull(ie.GetCookie("http://2.watin.com/", "test-cookie"), "Expected no cookies for 2.watin.com after ClearCookies");
            }
        }
Example #15
0
        public static void ReportProcessRenew()
        {
            CloseInternetExplorers();

            var totaltodayReportList = MySqlHelper.PopulateTodayRenewAdsForReport();

            ServiceLog.CraigslistErrorLog("*****************************************NUMBEROFADTORUN = " + totaltodayReportList.Count + "*******************************************");

            int showedAds = 0;

            int flagedAds = 0;

            foreach (var vehicle in totaltodayReportList)
            {
                try
                {
                    if (_browser == null)
                    {
                        _browser =
                            new IE(vehicle.CraigslistUrl);
                    }
                    else
                    {
                        _browser.GoToNoWait(vehicle.CraigslistUrl);
                    }
                    _browser.ShowWindow(NativeMethods.WindowShowStyle.Hide);

                    System.Threading.Thread.Sleep(1000);


                    if (_browser.Html.Contains("This posting has been flagged for removal"))
                    {
                        MySqlHelper.NonShowAds(vehicle.AdTrackingId);

                        ServiceLog.CraigslistErrorLog(vehicle.AdTrackingId.ToString(CultureInfo.InvariantCulture) + " == FLAGGED");
                        flagedAds++;
                    }
                    else
                    {
                        MySqlHelper.ShowAds(vehicle.AdTrackingId);
                        ServiceLog.CraigslistErrorLog(vehicle.AdTrackingId.ToString(CultureInfo.InvariantCulture) + " == SHOWED");
                        showedAds++;
                    }



                    System.Threading.Thread.Sleep(200);
                }
                catch (Exception ex)
                {
                    ServiceLog.CraigslistErrorLog(ex.Message);
                }
            }

            if (_browser != null)
            {
                _browser.ClearCache();

                _browser.ClearCookies();

                _browser.Close();

                _browser = null;
            }

            var percentage =
                Math.Ceiling(((double)showedAds /
                              totaltodayReportList.Count()) * 100);

            var builder = new StringBuilder();

            builder.Append("NUMBER OF RENEW ADS TODAY = <b>" + totaltodayReportList.Count + ".....");

            builder.Append("NUMBER OF RENEW ADS SHOWED = " + showedAds + ".....");

            builder.Append("NUMBER OF RENEW ADS FLAGGED = " + flagedAds + "....");

            builder.Append("PERCENTAGE = <b>" + percentage.ToString(CultureInfo.InvariantCulture) + "%" + ".......");

            SmsHelper.SendMessageToAtt("Craigslist Report", builder.ToString());

            ServiceLog.CraigslistErrorLog("*****************************************SHOWED = " + showedAds + "*******************************************");

            ServiceLog.CraigslistErrorLog("*****************************************FLAGGED = " + flagedAds + "*******************************************");

            ServiceLog.CraigslistErrorLog("*****************************************DONE*******************************************");
        }
Example #16
0
        public static void RenewAdsProcess(int splitPart)
        {
            var totalrenewList = MySqlHelper.PopulateAvailableRenewAds();

            var renewList = totalrenewList.Skip(totalrenewList.Count * splitPart / 2).Take(totalrenewList.Count / 2).ToList();

            var ieProcesses = Process.GetProcessesByName("iexplore");

            if (ieProcesses.Any() && ieProcesses.Count() > 5)
            {
                foreach (Process ie in ieProcesses)
                {
                    ie.Kill();
                }
            }

            if (_browser != null)
            {
                _browser.ClearCache();

                _browser.ClearCookies();

                _browser.Close();

                _browser = null;
            }


            if (_browser != null && IsAlreadyLogin())
            {
                System.Threading.Thread.Sleep(2000);

                _browser.Span(Find.ById("ef")).Links.First().Click();

                System.Threading.Thread.Sleep(3000);
            }



            foreach (var vehicle in renewList)
            {
                try
                {
                    if (_browser == null)
                    {
                        _browser = new IE(vehicle.CraigslistCityUrl + "search/ctd?zoomToPosting=&query=" + vehicle.CLPostingId + "&srchType=A&minAsk=&maxAsk=");
                    }
                    else
                    {
                        _browser.GoToNoWait(vehicle.CraigslistCityUrl + "search/ctd?zoomToPosting=&query=" + vehicle.CLPostingId + "&srchType=A&minAsk=&maxAsk=");
                    }

                    _browser.ShowWindow(NativeMethods.WindowShowStyle.ShowMaximized);

                    System.Threading.Thread.Sleep(2000);

                    if (_browser.Html.Contains("Found: "))
                    {
                        if (vehicle.AutoID == 1 || (renewList.Any(x => x.AutoID == vehicle.AutoID - 1) && !renewList.First(x => x.AutoID == vehicle.AutoID - 1).CraigslistAccountName.Equals(vehicle.CraigslistAccountName)))
                        {
                            if (_browser == null)
                            {
                                _browser = new IE("https://accounts.craigslist.org/");
                            }
                            else
                            {
                                _browser.GoTo("https://accounts.craigslist.org/");
                            }

                            if (_browser != null && IsAlreadyLogin())
                            {
                                System.Threading.Thread.Sleep(2000);

                                _browser.Span(Find.ById("ef")).Links.First().Click();

                                System.Threading.Thread.Sleep(3000);

                                _browser.ClearCache();

                                _browser.ClearCookies();

                                _browser.Close();

                                _browser = null;
                            }


                            if (_browser == null)
                            {
                                _browser = new IE("https://accounts.craigslist.org/");
                            }
                            else
                            {
                                _browser.GoTo("https://accounts.craigslist.org/");
                            }


                            _browser.TextField(Find.ByName("inputEmailHandle")).TypeText(vehicle.CraigslistAccountName);

                            _browser.TextField(Find.ByName("inputPassword")).TypeText(vehicle.CraigslistAccountPassword);

                            System.Threading.Thread.Sleep(2000);

                            _browser.Buttons.First().Click();
                        }
                        else
                        {
                            if (_browser != null)
                            {
                                _browser.GoTo("https://accounts.craigslist.org/");
                            }
                            else
                            {
                                _browser = new IE("https://accounts.craigslist.org/");
                            }

                            if (!IsAlreadyLogin())
                            {
                                _browser.ClearCache();

                                _browser.ClearCookies();

                                _browser.Close();

                                _browser = null;

                                _browser = new IE("https://accounts.craigslist.org/");

                                _browser.TextField(Find.ByName("inputEmailHandle")).TypeText(vehicle.CraigslistAccountName);

                                _browser.TextField(Find.ByName("inputPassword")).TypeText(vehicle.CraigslistAccountPassword);

                                System.Threading.Thread.Sleep(2000);

                                _browser.Buttons.First().Click();
                            }
                        }

                        System.Threading.Thread.Sleep(2000);

                        _browser.GoTo("https://post.craigslist.org/manage/" + vehicle.CLPostingId);


                        if (_browser.Buttons.Any(x => x.Value == "Renew this Posting"))
                        {
                            System.Threading.Thread.Sleep(1000);

                            _browser.Button(Find.ByValue("Renew this Posting")).ClickNoWait();

                            System.Threading.Thread.Sleep(4000);

                            MySqlHelper.ShowAdsButEligibleForRenew(vehicle.AdTrackingId);

                            //ServiceLog.CraigslistErrorLog(vehicle.AdTrackingId.ToString(CultureInfo.InvariantCulture) + " == RENEWED");
                        }
                        else
                        {
                            MySqlHelper.ShowAdsButIneligibleForRenew(vehicle.AdTrackingId);

                            _browser.GoTo("https://accounts.craigslist.org/");

                            System.Threading.Thread.Sleep(2000);

                            _browser.Span(Find.ById("ef")).Links.First().Click();

                            System.Threading.Thread.Sleep(3000);

                            _browser.ClearCache();

                            _browser.ClearCookies();

                            _browser.Close();

                            _browser = null;

                            //.CraigslistErrorLog(vehicle.AdTrackingId.ToString(CultureInfo.InvariantCulture) + " == NOT ELIGIBLE TO RENEW");
                        }
                    }
                    else
                    {
                        MySqlHelper.DeleteTracking(vehicle.AdTrackingId);
                    }

                    System.Threading.Thread.Sleep(10000);
                }
                catch (Exception ex)
                {
                    //throw new Exception(ex.Message);
                }
            }
        }
Example #17
0
        public static void RenewAdsProcessByEmailAccount(int computerId)
        {
            var totalrenewList = SQLHelper.PopulateEmailHavingRenewAds(computerId);

            foreach (var eAccount in totalrenewList)
            {
                if (_browser != null && IsAlreadyLogin())
                {
                    System.Threading.Thread.Sleep(2000);

                    _browser.Span(Find.ById("ef")).Links.First().Click();

                    System.Threading.Thread.Sleep(6000);

                    _browser.ClearCache();

                    _browser.ClearCookies();
                }

                var ieProcesses = Process.GetProcessesByName("iexplore");

                if (ieProcesses.Any())
                {
                    foreach (Process ie in ieProcesses)
                    {
                        ie.Kill();
                    }
                }

                System.Threading.Thread.Sleep(2000);

                //var networkManagement = new NetworkManagement();

                //networkManagement.SetIP(eAccount);

                System.Threading.Thread.Sleep(8000);

                try
                {
                    if (_browser == null)
                    {
                        _browser = new IE("https://accounts.craigslist.org/");
                    }
                    else
                    {
                        _browser.GoTo("https://accounts.craigslist.org/");
                    }


                    _browser.ShowWindow(NativeMethods.WindowShowStyle.ShowMaximized);

                    System.Threading.Thread.Sleep(2000);

                    _browser.TextField(Find.ByName("inputEmailHandle")).TypeText(eAccount.CraigslistAccount);

                    _browser.TextField(Find.ByName("inputPassword")).TypeText(eAccount.CraigsListPassword);

                    System.Threading.Thread.Sleep(2000);

                    _browser.Buttons.First().Click();

                    int numberofPages = 1;

                    int numberofFilterpage = 0;

                    foreach (var tmp in _browser.Links)
                    {
                        if (!String.IsNullOrEmpty(tmp.Url) && tmp.Url.Contains("filter_page"))
                        {
                            numberofFilterpage++;
                        }
                    }
                    numberofPages = numberofPages + numberofFilterpage / 2;

                    if (numberofPages > 0)
                    {
                        for (int i = 1; i <= numberofPages; i++)
                        {
                            if (i == 1)
                            {
                                var allDeleteTab = _browser.TableCells.ToArray();

                                var listofDeleted = new List <long>();

                                foreach (var deleteTmp in allDeleteTab)
                                {
                                    if (!String.IsNullOrEmpty(deleteTmp.GetAttributeValue("style")) &&
                                        deleteTmp.GetAttributeValue("style").Contains("border: 1px solid pink") &&
                                        !String.IsNullOrEmpty(deleteTmp.Text))
                                    {
                                        long clpostingId = 0;
                                        Int64.TryParse(deleteTmp.Text, out clpostingId);

                                        if (clpostingId > 0)
                                        {
                                            listofDeleted.Add(clpostingId);
                                        }
                                    }
                                }

                                System.Threading.Thread.Sleep(2000);

                                if (_browser.Buttons.Any(x => x.Value == "renew"))
                                {
                                    do
                                    {
                                        var tmp = _browser.Buttons.First(x => x.Value == "renew");

                                        System.Threading.Thread.Sleep(3000);

                                        tmp.Click();

                                        var clPostingIdString =
                                            _browser.Url.Substring(
                                                _browser.Url.LastIndexOf("/", System.StringComparison.Ordinal) +
                                                1);

                                        long clPostingId = 0;

                                        Int64.TryParse(clPostingIdString, out clPostingId);

                                        if (clPostingId > 0)
                                        {
                                            SQLHelper.ShowAdsButEligibleForRenewFromClAccountManagement(
                                                clPostingId);
                                        }

                                        System.Threading.Thread.Sleep(8000);

                                        _browser.GoTo("https://accounts.craigslist.org/");

                                        System.Threading.Thread.Sleep(3000);
                                    } while (_browser.Buttons.Any(x => x.Value == "renew"));
                                }
                            }
                            else
                            {
                                _browser.GoTo("https://accounts.craigslist.org/?filter_page=" + i +
                                              "&filter_cat=0&filter_date=0&filter_active=0&show_tab=postings");

                                var allDeleteTab = _browser.TableCells.ToArray();

                                var listofDeleted = new List <long>();

                                foreach (var deleteTmp in allDeleteTab)
                                {
                                    if (!String.IsNullOrEmpty(deleteTmp.GetAttributeValue("style")) &&
                                        deleteTmp.GetAttributeValue("style").Contains("border: 1px solid pink") &&
                                        !String.IsNullOrEmpty(deleteTmp.Text))
                                    {
                                        long clpostingId = 0;
                                        Int64.TryParse(deleteTmp.Text, out clpostingId);

                                        if (clpostingId > 0)
                                        {
                                            listofDeleted.Add(clpostingId);
                                        }
                                    }
                                }
                                SQLHelper.PinkAdInClAccountManagement(
                                    listofDeleted);


                                System.Threading.Thread.Sleep(2000);

                                if (_browser.Buttons.Any(x => x.Value == "renew"))
                                {
                                    do
                                    {
                                        var tmp = _browser.Buttons.First(x => x.Value == "renew");

                                        System.Threading.Thread.Sleep(3000);

                                        tmp.Click();

                                        var clPostingIdString =
                                            _browser.Url.Substring(
                                                _browser.Url.LastIndexOf("/", System.StringComparison.Ordinal) +
                                                1);

                                        long clPostingId = 0;

                                        Int64.TryParse(clPostingIdString, out clPostingId);

                                        if (clPostingId > 0)
                                        {
                                            SQLHelper.ShowAdsButEligibleForRenewFromClAccountManagement(
                                                clPostingId);
                                        }

                                        System.Threading.Thread.Sleep(8000);

                                        _browser.GoTo("https://accounts.craigslist.org/?filter_page=" + i +
                                                      "&filter_cat=0&filter_date=0&filter_active=0&show_tab=postings");


                                        System.Threading.Thread.Sleep(3000);
                                    } while (_browser.Buttons.Any(x => x.Value == "renew"));
                                }
                            }
                        }
                    }
                }

                catch
                (Exception ex)
                {
                    if (_browser != null)
                    {
                        _browser.ClearCache();

                        _browser.ClearCookies();

                        _browser = new IE("https://accounts.craigslist.org/");
                    }
                    else
                    {
                        _browser.GoTo("https://accounts.craigslist.org/");
                    }


                    throw new Exception(ex.Message);

                    System.Threading.Thread.Sleep(2000);
                }
            }
        }
Example #18
0
        /// <summary>
        /// Performs authentication.
        /// </summary>
        private void EnsureAuth()
        {
            if (this.session != null && !this.session.Expired)
            {
                return;
            }

            try
            {
                using (var browser = new IE())
                {
                    var timer = new System.Diagnostics.Stopwatch();
                    timer.Start();

                    browser.AutoClose = true;
                    browser.GoTo(this.AuthUrl);

                    // Do autologin if needed.
                    if (this.AutoLogin && !browser.Url.ToLowerInvariant().Contains("access_token=") &&
                        !browser.Url.ToLowerInvariant().Contains("error="))
                    {
                        // Fill email password.
                        if (browser.TextField(Find.ByName("email")).Exists&&
                            browser.TextField(Find.ByName("pass")).Exists)
                        {
                            browser.TextField(Find.ByName("email")).Value = this.userEmail;
                            browser.TextField(Find.ByName("pass")).Value  = this.userPassword;
                            browser.Button("install_allow").Click();
                        }

                        // Accept application permissions.
                        if (browser.Button("install_allow").Exists)
                        {
                            browser.Button("install_allow").Click();
                        }

                        if (!browser.Url.ToLowerInvariant().Contains("access_token=") &&
                            !browser.Url.ToLowerInvariant().Contains("error="))
                        {
                            throw new AuthenticationException("Most likely wrong email\\password.");
                        }
                    }

                    // Wait for user to login.
                    if (!this.AutoLogin)
                    {
                        while (!browser.Url.ToLowerInvariant().Contains("access_token="))
                        {
                            System.Threading.Thread.Sleep(3000);
                        }
                    }

                    if (browser.Url.ToLowerInvariant().Contains("error="))
                    {
                        var error = Regex.Match(browser.Url, "error=(?<error>[^&$]*)", RegexOptions.IgnoreCase).Groups["error"].Value;
                        var desc  = Regex.Match(browser.Url, "error_description=(?<desc>[^&$]*)", RegexOptions.IgnoreCase).Groups["desc"].Value;
                        throw new AuthenticationException(
                                  string.Format(
                                      "Error: {0}\nDescription: {1}",
                                      HttpUtility.UrlDecode(error),
                                      HttpUtility.UrlDecode(desc)));
                    }

                    // Create auth session.
                    var token   = Regex.Match(browser.Url, "access_token=(?<token>[^&$]*)", RegexOptions.IgnoreCase).Groups["token"].Value;
                    var expires = int.Parse(Regex.Match(browser.Url, "expires_in=(?<expires>\\d+)", RegexOptions.IgnoreCase).Groups["expires"].Value);
                    var uid     = uint.Parse(Regex.Match(browser.Url, "user_id=(?<uid>\\d+)", RegexOptions.IgnoreCase).Groups["uid"].Value);

                    timer.Stop();
                    this.session = new AuthSession(uid, token, expires - (int)timer.Elapsed.TotalSeconds);

                    // Clears IE cache, all cookies (!) and kills IE process. Weird, I know :)
                    browser.ClearCache();
                    browser.ClearCookies();
                    browser.ForceClose();
                }
            }
            catch (AuthenticationException)
            {
                // WTF?! Browser closed? Wrong auth url?!
                throw;
            }
            catch (Exception ex)
            {
                throw new InvalidOperationException("Authentication failed.", ex);
            }
        }