private async Task<string> FillInCities(IBrowser browser, Parcel parcel, CancellationToken ct) { var senderCity = parcel.SenderCity ?? "Йошкар-Ола"; if (string.IsNullOrWhiteSpace(await SelectCity(browser, senderCity, "getSenderCity", "senderContext", ct))) throw new InvalidOperationException("Can't find and choose sender city"); var receiverPossibleCities = parcel.TryParseReceiverAddress(); foreach (var receiverCity in receiverPossibleCities) { var deliveryPath = await SelectCity(browser, receiverCity, "getReceiverCity", "receiverContext", ct); if (!string.IsNullOrWhiteSpace(deliveryPath)) { return deliveryPath; } } throw new InvalidOperationException("Can't find and choose receiver city"); }
private async Task<string> SetDeliveryPath(Parcel parcel, CancellationToken ct) { while (!await _browser.Select("input[name='form.cityDeliveryName']", ct)) await Task.Delay(100, ct); var receiverPossibleCities = parcel.TryParseReceiverAddress(); var cities = new List<string>(); foreach (var receiverCity in receiverPossibleCities) { await _browser.Input(receiverCity, ct); if (!await _browser.WaitForAjax(5000, ct)) throw new TimeoutException("Failed to wait for sender city setup"); cities.Clear(); var icities = (await _browser.ExecuteJs("return fragma.modules.dpdTariffs.getCities()", ct) as IList); if (icities.Count == 0) continue; foreach (var c in icities) cities.Add(c.ToString()); if (cities.Count == 1) return cities.First(); var weight = 0; var index = 0; var i = 0; foreach (var city1 in cities) { var newWeight = receiverCity.GetLevelOfSimilarity(city1); if (newWeight > weight) { index = i; weight = newWeight; } ++i; } if (weight == 0) continue; await _browser.Input(cities[index], ct); if (!await _browser.WaitForAjax(5000, ct)) throw new TimeoutException("Failed to wait for sender city setup"); return cities[index]; } return null; }