Skip to content

Commit

Permalink
Merge pull request #286 from Lombiq/issue/OSOE-629
Browse files Browse the repository at this point in the history
OSOE-629: Adding FillInCodeMirrorEditorWithRetriesAsync() and SetTextContentWithScript(), renaming ChangeCurrentTenantToDefault(), using current tenant in shortcuts
  • Loading branch information
Psichorex authored Jun 14, 2023
2 parents c0d153f + d35fd45 commit 6c490d2
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 150 deletions.
2 changes: 1 addition & 1 deletion Lombiq.Tests.UI.Samples/Tests/TenantTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ await context.CreateAndSwitchToTenantAsync(
(await context.GetCurrentUserNameAsync()).ShouldBe(tenantAdminName);
context.GetCurrentUri().AbsolutePath.ShouldStartWith($"/{TestTenantUrlPrefix}");
context.ChangeCurrentTenantToDefault();
context.SwitchCurrentTenantToDefault();
(await context.GetCurrentUserNameAsync()).ShouldBe(DefaultUser.UserName);
context.GetCurrentUri().AbsolutePath.ShouldNotStartWith($"/{TestTenantUrlPrefix}");
},
Expand Down
56 changes: 44 additions & 12 deletions Lombiq.Tests.UI/Extensions/FormUITestContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -77,25 +77,25 @@ public static Task ClickAndFillInTrumbowygEditorWithRetriesAsync(
}

/// <summary>
/// Uses Javascript to reinitialize the given field's EasyMDE instance and then access the internal CodeMirror
/// Uses JavaScript to reinitialize the given field's EasyMDE instance and then access the internal CodeMirror
/// editor to programmatically change the value. This is necessary, because otherwise the editor doesn't expose the
/// CodeMirror library globally for editing the existing instance and this editor can't be filled using regular
/// Selenium interactions either.
/// </summary>
public static void SetMarkdownEasyMdeWysiwygEditor(this UITestContext context, string id, string text)
{
var script = $@"
/* First get rid of the existing editor instance. */
document.querySelector('#{id} + .EasyMDEContainer').remove();
/* Create a new one using the same call found in OC's MarkdownBodyPart-Wysiwyg.Edit.cshtml */
var mde = new EasyMDE({{
element: document.getElementById('{id}'),
forceSync: true,
toolbar: mdeToolbar,
autoDownloadFontAwesome: false,
}});
/* Finally set the value programmatically. */
mde.codemirror.setValue({JsonConvert.SerializeObject(text)});";
/* First get rid of the existing editor instance. */
document.querySelector('#{id} + .EasyMDEContainer').remove();
/* Create a new one using the same call found in OC's MarkdownBodyPart-Wysiwyg.Edit.cshtml */
var mde = new EasyMDE({{
element: document.getElementById('{id}'),
forceSync: true,
toolbar: mdeToolbar,
autoDownloadFontAwesome: false,
}});
/* Finally set the value programmatically. */
mde.codemirror.setValue({JsonConvert.SerializeObject(text)});";

context.ExecuteScript(script);
}
Expand Down Expand Up @@ -165,6 +165,38 @@ public static Task FillInWithRetriesUntilNotBlankAsync(
timeout,
interval));

/// <summary>
/// Fills a CodeMirror editor with the given text, and retries if the value doesn't stick.
/// </summary>
public static Task FillInCodeMirrorEditorWithRetriesAsync(
this UITestContext context,
By by,
string text,
TimeSpan? timeout = null,
TimeSpan? interval = null) =>
context.ExecuteLoggedAsync(
nameof(FillInCodeMirrorEditorWithRetriesAsync),
$"{by} - \"{text}\"",
() => context.DoWithRetriesOrFailAsync(
() =>
{
// Approach taken from https://stackoverflow.com/a/57621266/220230.
// Getting CodeMirror element.
var codeMirrorEditor = context.Get(by);
// Clicking the first line of code inside CodeMirror to bring it in focus.
codeMirrorEditor.Get(By.ClassName("CodeMirror-line")).Click();
// Sending keystrokes to textarea once CodeMirror is in focus.
IWebElement GetTextArea() => codeMirrorEditor.Get(By.CssSelector("textarea").OfAnyVisibility());
GetTextArea().SendKeys(text);
return Task.FromResult(GetTextArea().GetValue() == text);
},
timeout,
interval));

/// <summary>
/// Returns a value indicating whether the checkbox of <paramref name="by"/> is checked or not.
/// </summary>
Expand Down
13 changes: 10 additions & 3 deletions Lombiq.Tests.UI/Extensions/ScriptingUITestContextExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ public static object ExecuteAsyncScript(this UITestContext context, string scrip
context.ExecuteLogged(nameof(ExecuteAsyncScript), script, () => context.Driver.ExecuteAsyncScript(script, args));

/// <summary>
/// Uses Javascript to set form inputs to values that are hard or impossible by normal means.
/// Uses JavaScript to set form inputs to values that are hard or impossible by normal means.
/// </summary>
public static void SetValueWithScript(this UITestContext context, string id, object value) =>
ExecuteScript(
context,
$"document.getElementById({JsonConvert.SerializeObject(id)}).value = " +
$"{JsonConvert.SerializeObject(value)};");
$"document.getElementById({JsonConvert.SerializeObject(id)}).value = {JsonConvert.SerializeObject(value)};");

/// <summary>
/// Uses JavaScript to set textarea values that are hard or impossible by normal means.
/// </summary>
public static void SetTextContentWithScript(this UITestContext context, string textareaId, object value) =>
ExecuteScript(
context,
$"document.getElementById({JsonConvert.SerializeObject(textareaId)}).textContent = {JsonConvert.SerializeObject(value)};");
}
Loading

0 comments on commit 6c490d2

Please sign in to comment.