Beispiel #1
0
        public async Task UpdatePageAndTaskResult(AppPage page, AppTask task, CbtScreenshot result)
        {
            // update page
            UpdateDefinition <AppPage> pageUpdate = null;
            var resultId = result.screenshot_test_id.ToString();

            if (task.Type == AppTaskType.Desktop)
            {
                if (page.DesktopResultId != resultId)
                {
                    pageUpdate = Builders <AppPage> .Update.Set(d => d.DesktopResultId, resultId);
                }
            }
            else
            {
                if (page.MobileResultId != resultId)
                {
                    pageUpdate = Builders <AppPage> .Update.Set(d => d.MobileResultId, resultId);
                }
            }

            if (pageUpdate != null)
            {
                var pageFilter = Builders <AppPage> .Filter.Eq(d => d.Id, page.Id);

                await pageCollection.UpdateOneAsync(pageFilter, pageUpdate);
            }

            // update task
            var filter = Builders <AppTask> .Filter.Eq(d => d.Id, task.Id);

            var update = Builders <AppTask> .Update.Set(d => d.State, AppTaskState.Executed)
                         .Set(d => d.FinishedAt, DateTime.UtcNow);

            await taskCollection.UpdateOneAsync(filter, update);
        }
        public async Task <CbtScreenshot> TakeScreenshot(AppTask task)
        {
            var resultId = task.Type == AppTaskType.Desktop ? page.DesktopResultId : page.MobileResultId;
            var browsers = task.Type == AppTaskType.Desktop ? project.DesktopBrowsers : project.MobileBrowsers;

            string apiUri;

            if (string.IsNullOrWhiteSpace(resultId))
            {
                // new one
                apiUri = CbtAPIBaseUri;
            }
            else
            {
                // update one
                var info = await GetScreenshotInfo(resultId);

                var version = info.versions.First();

                apiUri = Url.Combine(CbtAPIBaseUri, resultId, version.version_id.ToString());
            }

            var targetUrl = "https://" + Url.Combine(project.Domain, page.Path);
            var body      = JsonConvert.SerializeObject(new {
                format   = "json",
                url      = targetUrl,
                browsers = browsers
            });

            var content = new StringContent(body);

            content.Headers.ContentType = MediaTypeHeaderValue.Parse("application/json");

            var tryCount = 0;

            LastError = null;
            Logger.Log($"Start taking {task.Type} screenshot of {targetUrl}");

START:
            HttpResponseMessage res = await client.PostAsync(apiUri, content);

            if (tryCount > 3)
            {
                Logger.Log($"Stop {task.Type} screenshot of {targetUrl} by retry 3 times");

                return(null);
            }

            if (res.StatusCode != HttpStatusCode.OK)
            {
                LastError = await res.Content.ReadAsStringAsync();

                Logger.Log($"{task.Type} screenshot of {targetUrl} failed, Error: {LastError}");

                Thread.Sleep(10_000);
                tryCount++;
                goto START;
            }

            var json = await res.Content.ReadAsStringAsync();

            var result = JsonConvert.DeserializeObject <CbtScreenshot>(json);

            return(result);
        }