* First draft

* Geolocation tests

* cr
This commit is contained in:
Darío Kondratiuk 2020-02-03 08:22:27 -03:00 коммит произвёл GitHub
Родитель e05caebf6b
Коммит 06e8079cb8
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
5 изменённых файлов: 267 добавлений и 3 удалений

Просмотреть файл

@ -1,4 +1,6 @@
namespace PlaywrightSharp
using System.Collections.Generic;
namespace PlaywrightSharp
{
/// <summary>
/// <see cref="IBrowser.NewContextAsync(BrowserContextOptions)"/> options.
@ -33,6 +35,16 @@
/// <summary>
/// Changes the timezone of the context. See <see href="https://cs.chromium.org/chromium/src/third_party/icu/source/data/misc/metaZones.txt?rcl=faee8bc70570192d82d2978a71e2a615788597d1">ICUs metaZones.txt</see> for a list of supported timezone IDs.
/// </summary>
public string TimezoneId { get; set; }
public string TimezoneId { get; set; }
/// <summary>
/// Changes the Geolocation of the context.
/// </summary>
public GeolocationOption Geolocation { get; set; }
/// <summary>
/// A <see cref="Dictionary{TKey, TValue}"/> from origin keys to permissions values. See <see cref="IBrowserContext.SetPermissionsAsync(string, ContextPermission[])"/> for more details.
/// </summary>
public Dictionary<string, ContextPermission[]> Permissions { get; set; }
}
}

Просмотреть файл

@ -0,0 +1,73 @@
namespace PlaywrightSharp
{
/// <summary>
/// Options for <see cref="IBrowserContext.SetPermissionsAsync(string, ContextPermission[])"/>.
/// </summary>
public enum ContextPermission
{
/// <summary>
/// Geolocation.
/// </summary>
Geolocation,
/// <summary>
/// MIDI.
/// </summary>
Midi,
/// <summary>
/// Notifications.
/// </summary>
Notifications,
/// <summary>
/// Push.
/// </summary>
Push,
/// <summary>
/// Camera.
/// </summary>
Camera,
/// <summary>
/// Microphone.
/// </summary>
Microphone,
/// <summary>
/// Background sync.
/// </summary>
BackgroundSync,
/// <summary>
/// Ambient light sensor, Accelerometer, Gyroscope, Magnetometer
/// </summary>
Sensors,
/// <summary>
/// Accessibility events.
/// </summary>
AccessibilityEvents,
/// <summary>
/// Clipboard read.
/// </summary>
ClipboardRead,
/// <summary>
/// Clipboard write.
/// </summary>
ClipboardWrite,
/// <summary>
/// Payment handler.
/// </summary>
PaymentHandler,
/// <summary>
/// MIDI sysex.
/// </summary>
MidiSysex,
}
}

Просмотреть файл

@ -0,0 +1,45 @@
using System;
namespace PlaywrightSharp
{
/// <summary>
/// Geolocation option.
/// </summary>
/// <seealso cref="IBrowserContext.SetGeolocationAsync(GeolocationOption)"/>
public class GeolocationOption : IEquatable<GeolocationOption>
{
/// <summary>
/// Latitude between -90 and 90.
/// </summary>
/// <value>The latitude.</value>
public decimal Latitude { get; set; }
/// <summary>
/// Longitude between -180 and 180.
/// </summary>
/// <value>The longitude.</value>
public decimal Longitude { get; set; }
/// <summary>
/// Optional non-negative accuracy value.
/// </summary>
/// <value>The accuracy.</value>
public decimal Accuracy { get; set; }
/// <inheritdoc cref="IEquatable{T}"/>
public bool Equals(GeolocationOption other)
=> other != null &&
Latitude == other.Latitude &&
Longitude == other.Longitude &&
Accuracy == other.Accuracy;
/// <inheritdoc cref="IEquatable{T}"/>
public override bool Equals(object obj) => Equals(obj as GeolocationOption);
/// <inheritdoc cref="IEquatable{T}"/>
public override int GetHashCode()
=> (Latitude.GetHashCode() ^ 2014) +
(Longitude.GetHashCode() ^ 2014) +
(Accuracy.GetHashCode() ^ 2014);
}
}

Просмотреть файл

@ -66,6 +66,21 @@ namespace PlaywrightSharp
/// Clears the context's cookies.
/// </summary>
/// <returns>A <see cref="Task"/> that completes when the cookies are cleared.</returns>
Task ClearCookiesAsync();
Task ClearCookiesAsync();
/// <summary>
/// Grants permissions to an URL.
/// </summary>
/// <param name="url">The origin to grant permissions to, e.g. "https://example.com".</param>
/// <param name="permissions">An array of permissions to grant.</param>
/// <returns>A <see cref="Task"/> that completes when the message was confirmed by the browser.</returns>
Task SetPermissionsAsync(string url, params ContextPermission[] permissions);
/// <summary>
/// Sets the page's geolocation.
/// </summary>
/// <param name="geolocation">Geolocation.</param>
/// <returns>A <see cref="Task"/> that completes when the message was confirmed by the browser.</returns>
Task SetGeolocationAsync(GeolocationOption geolocation);
}
}

Просмотреть файл

@ -0,0 +1,119 @@
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Threading.Tasks;
using PlaywrightSharp.Tests.Attributes;
using PlaywrightSharp.Tests.BaseTests;
using Xunit;
using Xunit.Abstractions;
namespace PlaywrightSharp.Tests.BrowserContext
{
///<playwright-file>geolocation.spec.js</playwright-file>
///<playwright-describe>Overrides.setGeolocation</playwright-describe>
public class GeolocationTests : PlaywrightSharpPageBaseTest
{
/// <inheritdoc/>
public GeolocationTests(ITestOutputHelper output) : base(output)
{
}
///<playwright-file>geolocation.spec.js</playwright-file>
///<playwright-describe>Overrides.setGeolocation</playwright-describe>
///<playwright-it>should work</playwright-it>
[SkipBrowserAndPlatformFact(skipFirefox: true)]
public async Task ShouldWork()
{
await Context.SetPermissionsAsync(TestConstants.ServerUrl, ContextPermission.Geolocation);
await Page.GoToAsync(TestConstants.EmptyPage);
await Context.SetGeolocationAsync(new GeolocationOption
{
Longitude = 10,
Latitude = 10
});
var geolocation = await Page.EvaluateAsync<GeolocationOption>(
@"() => new Promise(resolve => navigator.geolocation.getCurrentPosition(position => {
resolve({latitude: position.coords.latitude, longitude: position.coords.longitude});
}))");
Assert.Equal(new GeolocationOption
{
Latitude = 10,
Longitude = 10
}, geolocation);
}
///<playwright-file>geolocation.spec.js</playwright-file>
///<playwright-describe>Overrides.setGeolocation</playwright-describe>
///<playwright-it>should throw when invalid longitude</playwright-it>
[SkipBrowserAndPlatformFact(skipFirefox: true)]
public async Task ShouldThrowWhenInvalidLongitude()
{
var exception = await Assert.ThrowsAsync<ArgumentException>(() =>
Context.SetGeolocationAsync(new GeolocationOption
{
Longitude = 200,
Latitude = 100
}));
Assert.Contains("Invalid longitude '200'", exception.Message);
}
///<playwright-file>geolocation.spec.js</playwright-file>
///<playwright-describe>Overrides.setGeolocation</playwright-describe>
///<playwright-it>should throw with missing latitude</playwright-it>
[Fact(Skip = "We don't this test")]
public void ShouldThrowWithMissingLatitude() { }
///<playwright-file>geolocation.spec.js</playwright-file>
///<playwright-describe>Overrides.setGeolocation</playwright-describe>
///<playwright-it>should not modify passed default options object</playwright-it>
[SkipBrowserAndPlatformFact(skipFirefox: true)]
public async Task ShouldNotModifyPassedDefaultOptionsObject()
{
var geolocation = new GeolocationOption { Latitude = 10, Longitude = 10 };
BrowserContextOptions options = new BrowserContextOptions { Geolocation = geolocation };
var context = await NewContextAsync(options);
await Page.GoToAsync(TestConstants.EmptyPage);
await Context.SetGeolocationAsync(new GeolocationOption
{
Longitude = 20,
Latitude = 20
});
Assert.Equal(options.Geolocation, geolocation);
}
///<playwright-file>geolocation.spec.js</playwright-file>
///<playwright-describe>Overrides.setGeolocation</playwright-describe>
///<playwright-it>should throw with missing longitude in default options</playwright-it>
[Fact(Skip = "We don't this test")]
public void ShouldThrowWithMissingLongitudeInDefaultOptions() { }
///<playwright-file>geolocation.spec.js</playwright-file>
///<playwright-describe>Overrides.setGeolocation</playwright-describe>
///<playwright-it>should use context options</playwright-it>
[SkipBrowserAndPlatformFact(skipFirefox: true)]
public async Task ShouldUseContextOptions()
{
var options = new BrowserContextOptions
{
Geolocation = new GeolocationOption
{
Latitude = 10,
Longitude = 10
},
Permissions = new Dictionary<string, ContextPermission[]>
{
[TestConstants.ServerUrl] = new[] { ContextPermission.Geolocation }
}
};
var context = await NewContextAsync(options);
var page = await context.NewPageAsync();
await page.GoToAsync(TestConstants.EmptyPage);
var geolocation = await page.EvaluateAsync<GeolocationOption>(@"() => new Promise(resolve => navigator.geolocation.getCurrentPosition(position => {
resolve({latitude: position.coords.latitude, longitude: position.coords.longitude});
}))");
Assert.Equal(options.Geolocation, geolocation);
}
}
}