コード例 #1
0
        public async Task Start(IWebDriver _driver)
        {
            var sample       = new Sample();
            var testSettings = await GetSettings();

            var startPage = testSettings.StartPage.ToString();


            long startWeb = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;

            _driver.Navigate().GoToUrl(startPage);
            long endWeb       = DateTime.Now.Ticks / TimeSpan.TicksPerMillisecond;
            var  mainPageTime = endWeb - startWeb;


            var mainPageElement = new CheckedElement(startPage, "Strona startowa", mainPageTime, sample);

            sample.AddElement(mainPageElement);

            var mainCategory = _driver.FindElement(By.XPath("//div[contains(@class, 'block-categories')]"));
            var categories   = mainCategory.FindElements(By.XPath("//*[contains(@class, 'tree')]/child::li"));

            Random rmd    = new Random();
            var    rIndex = rmd.Next(0, categories.Count - 1);

            var rCategory          = categories[rIndex];
            var subcategoryElement = CheckElement(rCategory, _driver, sample);

            sample.AddElement(subcategoryElement);

            var SubCategories = _driver.FindElements(By.XPath("//*[@id='left-column']/div[1]/div[2]/ul/li[" + (rIndex + 1) + "]/ul/child::li"));

            if (SubCategories.Count > 0)
            {
                var rIndex2          = rmd.Next(0, SubCategories.Count - 1);
                var rNextSubcategory = SubCategories[rIndex2];
                var nextSubcategory  = CheckElement(rNextSubcategory, _driver, sample);
                sample.AddElement(nextSubcategory);
                Thread.Sleep(2000);

                var anotherElements = _driver.FindElements(By.XPath("//*[@id='left-column']/div[1]/div[2]/ul/li[" + (rIndex + 1) + "]/ul/li[" + (rIndex2 + 1) + "]/ul/child::li"));

                if (anotherElements.Count > 0)
                {
                    var rIndex3              = rmd.Next(0, anotherElements.Count - 1);
                    var anotherElement       = anotherElements[rIndex3];
                    var anotherRandomElement = CheckElement(anotherElement, _driver, sample);
                    sample.AddElement(anotherRandomElement);
                }
            }
            try
            {
                await SaveSample(sample);
            }
            catch (Exception ex)
            {
                throw ex;
            }

            var sendMail = false;

            foreach (var element in sample.Elements)
            {
                if (element.LoadTime >= testSettings.MaxLoadTime * 1000)
                {
                    sendMail = true;
                    break;
                }
            }
            if (sendMail)
            {
                var emailMessage = $"Sprawdź raport - jedna ze stron ładuje się dłużej niż {testSettings.MaxLoadTime} s";
                SendEmail(emailMessage, testSettings.Email);
            }
            Thread.Sleep(5000);
        }