* Dialog tests

* cr
This commit is contained in:
Darío Kondratiuk 2020-01-30 09:39:22 -03:00 коммит произвёл GitHub
Родитель 8a23861ec6
Коммит 5e835afb32
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
8 изменённых файлов: 207 добавлений и 12 удалений

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

@ -0,0 +1,51 @@
using System.Threading.Tasks;
namespace PlaywrightSharp
{
/// <summary>
/// <see cref="IDialog"/> objects are dispatched by page via the <see cref="IPage.Dialog"/> event.
/// </summary>
/// <example>
/// An example of using Dialog class:
///<code>
///<![CDATA[
/// Page.Dialog += async (sender, e) =>
/// {
/// await e.Dialog.AcceptAsync();
/// }
/// await Page.EvaluateAsync("alert('yo');");
/// ]]>
/// </code>
/// </example>
public interface IDialog
{
/// <summary>
/// Dialog's type, can be one of alert, beforeunload, confirm or prompt.
/// </summary>
/// <value>The type of the dialog.</value>
DialogType DialogType { get; set; }
/// <summary>
/// If dialog is prompt, returns default prompt value. Otherwise, returns empty string.
/// </summary>
/// <value>The default value.</value>
string DefaultValue { get; set; }
/// <summary>
/// A message displayed in the dialog.
/// </summary>
/// <value>The message.</value>
string Message { get; set; }
/// <summary>
/// Accept the Dialog.
/// </summary>
/// <param name="promptText">A text to enter in prompt. Does not cause any effects if the dialog's type is not prompt.</param>
/// <returns>A <see cref="Task"/> that completes when the dialog has been accepted.</returns>
Task AcceptAsync(string promptText = "");
/// <summary>
/// Dismiss the dialog.
/// </summary>
/// <returns>A <see cref="Task"/> that completes when the dialog has been dismissed.</returns>
Task DismissAsync();
}
}

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

@ -0,0 +1,22 @@
using System;
namespace PlaywrightSharp
{
/// <summary>
/// <see cref="IPage.Dialog"/> arguments.
/// </summary>
public class DialogEventArgs : EventArgs
{
/// <summary>
/// Dialog data.
/// </summary>
/// <value>Dialog data.</value>
public IDialog Dialog { get; }
/// <summary>
/// Initializes a new instance of the <see cref="DialogEventArgs"/> class.
/// </summary>
/// <param name="dialog">Dialog.</param>
public DialogEventArgs(IDialog dialog) => Dialog = dialog;
}
}

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

@ -0,0 +1,29 @@
using System.Runtime.Serialization;
namespace PlaywrightSharp
{
/// <summary>
/// Dialog type.
/// </summary>
/// <seealso cref="IDialog"/>
public enum DialogType
{
/// <summary>
/// Alert dialog.
/// </summary>
Alert,
/// <summary>
/// Prompt dialog.
/// </summary>
Prompt,
/// <summary>
/// Confirm dialog.
/// </summary>
Confirm,
/// <summary>
/// Before unload dialog.
/// </summary>
[EnumMember(Value = "beforeunload")]
BeforeUnload,
}
}

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

@ -61,6 +61,11 @@ namespace PlaywrightSharp
/// </summary>
Viewport Viewport { get; }
/// <summary>
/// Raised when a JavaScript dialog appears, such as <c>alert</c>, <c>prompt</c>, <c>confirm</c> or <c>beforeunload</c>. PlaywrightSharp can respond to the dialog via <see cref="Dialog"/>'s <see cref="IDialog.AcceptAsync(string)"/> or <see cref="IDialog.DismissAsync"/> methods.
/// </summary>
event EventHandler<DialogEventArgs> Dialog;
/// <summary>
/// Executes a script in browser context
/// </summary>

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

@ -5,14 +5,6 @@
<ReleaseVersion>0.0.0</ReleaseVersion>
<LangVersion>8.0</LangVersion>
</PropertyGroup>
<ItemGroup>
<None Remove="testCert.cer" />
</ItemGroup>
<ItemGroup>
<Content Include="testCert.cer">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</Content>
</ItemGroup>
<PropertyGroup Condition="'$(TargetFramework)'=='net471'">
<RuntimeIdentifier>win7-x64</RuntimeIdentifier>
</PropertyGroup>

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

@ -16,7 +16,7 @@ namespace PlaywrightSharp.Tests.BrowserContext
///<playwright-file>browsercontext.spec.js</playwright-file>
///<playwright-describe>BrowserContext</playwright-describe>
///<playwright-it>should have default context</playwright-it>
///<playwright-it>should create new incognito context</playwright-it>
[Fact]
public async Task ShouldHaveDefaultContext()
{

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

@ -0,0 +1,99 @@
using System;
using System.Threading.Tasks;
using PlaywrightSharp.Tests.BaseTests;
using Xunit;
using Xunit.Abstractions;
namespace PlaywrightSharp.Tests.Page.Events
{
///<playwright-file>browsercontext.spec.js</playwright-file>
public class DialogTests : PlaywrightSharpPageBaseTest
{
/// <inheritdoc/>
public DialogTests(ITestOutputHelper output) : base(output)
{
}
///<playwright-file>dialog.spec.js</playwright-file>
///<playwright-describe>Page.Events.Dialog</playwright-describe>
///<playwright-it>should fire</playwright-it>
[Fact]
public async Task ShouldFire()
{
Page.Dialog += async (sender, e) =>
{
Assert.Equal(DialogType.Alert, e.Dialog.DialogType);
Assert.Equal(string.Empty, e.Dialog.DefaultValue);
Assert.Equal("yo", e.Dialog.Message);
await e.Dialog.AcceptAsync();
};
await Page.EvaluateAsync("alert('yo');");
}
///<playwright-file>dialog.spec.js</playwright-file>
///<playwright-describe>Page.Events.Dialog</playwright-describe>
///<playwright-it>should allow accepting prompts</playwright-it>
[Fact]
public async Task ShouldAllowAcceptingPrompts()
{
Page.Dialog += async (sender, e) =>
{
Assert.Equal(DialogType.Prompt, e.Dialog.DialogType);
Assert.Equal("yes.", e.Dialog.DefaultValue);
Assert.Equal("question?", e.Dialog.Message);
await e.Dialog.AcceptAsync("answer!");
};
string result = await Page.EvaluateAsync<string>("prompt('question?', 'yes.')");
Assert.Equal("answer!", result);
}
///<playwright-file>dialog.spec.js</playwright-file>
///<playwright-describe>Page.Events.Dialog</playwright-describe>
///<playwright-it>should dismiss the prompt</playwright-it>
[Fact]
public async Task ShouldDismissThePrompt()
{
Page.Dialog += async (sender, e) =>
{
await e.Dialog.DismissAsync();
};
string result = await Page.EvaluateAsync<string>("prompt('question?')");
Assert.Null(result);
}
///<playwright-file>dialog.spec.js</playwright-file>
///<playwright-describe>Page.Events.Dialog</playwright-describe>
///<playwright-it>should accept the confirm prompt</playwright-it>
[Fact]
public async Task ShouldAcceptTheConfirmPrompts()
{
Page.Dialog += async (sender, e) =>
{
await e.Dialog.AcceptAsync();
};
bool result = await Page.EvaluateAsync<bool>("confirm('boolean?')");
Assert.True(result);
}
///<playwright-file>dialog.spec.js</playwright-file>
///<playwright-describe>Page.Events.Dialog</playwright-describe>
///<playwright-it>should dismiss the confirm prompt</playwright-it>
[Fact]
public async Task ShouldDismissTheConfirmPrompt()
{
Page.Dialog += async (sender, e) =>
{
await e.Dialog.DismissAsync();
};
bool result = await Page.EvaluateAsync<bool>("prompt('boolean?')");
Assert.False(result);
}
}
}

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

@ -31,7 +31,4 @@
<ProjectReference Include="..\PlaywrightSharp\PlaywrightSharp.csproj" />
<ProjectReference Include="..\PlaywrightSharp.TestServer\PlaywrightSharp.TestServer.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="BrowserContext\" />
</ItemGroup>
</Project>