How to check page in new tab in Playwright (C#) - playwright

I want to create the test
Click on element with middle button on the mouse
Verify content of page in new tab
I know how to create new Browser context, but I don't know how to switch to new page.
Thank you for any help in advance.

you can use the ElementHandle.click() method to simulate a mouse click on a particular element.
const browser = await playwright.chromium.launch();
const page = await browser.newPage();
// Navigate to a web page
await page.goto('https://www.example.com');
// Find the element that links to a new webpage
const linkElement = await page.$('a[href="https://www.example.com/new-page"]');
// Simulate a mouse click on the element
await linkElement.click()

Related

Cannot open new tab in the same context in Playwright (c#)

I'm trying to open new tab in the same browser session and switch context to it.
When I do it with following code:
await BrowserSession.Browser.Contexts[0].NewPageAsync();
I got the error: Please use Browser.NewContextAsync()
await BrowserSession.Browser.NewPageAsync();
from other hand create new BrowserContext and open new page in separate window (not new tab).

Open link in a new window in electron

I am using the below code to open a link in a new electron window:
$('body').on('click', '.openlink', function () {
var url = 'https://www.google.com' + this.id;
window.open(url,"_blank","height=650,width=1000,frame=true,show=true");
});
However, when I click the button again, the other page opens in the same window that google.com opened. How can I open new button clicks on new windows every time ?

Xamarin.Auth Google Presenter not closing in Android Xamarin.Forms App

I am using Xamarin.Form to write a Android app. I have successfully implemented the OAuth and I can sign in and get the user information using OAuth2Authenticator.
When the user clicks signup/Login I show the OAuthLoginPresenter as follows:
var oAuthLoginPresenter = new Xamarin.Auth.Presenters.OAuthLoginPresenter();
oAuthLoginPresenter.Login(App.OAuth2Authenticator);
This works great and the user sees the login page.
When the user clicks Allow the completed Event on the OAuth2Authenticator instance fires as expected and the user is once again back looking at the app.
However this is where my problem is - I get a notification:
If CustomTabs Login Screen does not close automatically close
CustomTabs by Navigating back to the app.
Thing is though I am back at the app. If I look at all my open apps I can see the login screen is still running in the back ground so that presenter has not closed.
In my intercept Activity which gets the redirect looks like this:
[Activity(Label = "GoogleAuthInterceptor")]
[IntentFilter
(
actions: new[] { Intent.ActionView },
Categories = new[]
{
Intent.CategoryDefault,
Intent.CategoryBrowsable
},
DataSchemes = new[]
{
// First part of the redirect url (Package name)
"com.myapp.platform"
},
DataPaths = new[]
{
// Second part of the redirect url (Path)
"/oauth2redirect"
}
)]
public class GoogleAuthInterceptor: Activity
{
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
Android.Net.Uri uri_android = Intent.Data;
// Convert Android Url to C#/netxf/BCL System.Uri
Uri uri_netfx = new Uri(uri_android.ToString());
// Send the URI to the Authenticator for continuation
App.OAuth2Authenticator?.OnPageLoading(uri_netfx);
Finish();
}
}
Am I missing a step in here to close that presenter? Any other ideas please?
UPDATE:
I have now found that using Chrome as the default browser works fine and presenter is closed. But if I use a Samsung browser as my default browser - it does not close.
So I need a way to close it manually.
Just set property to null when initializing Xamarin.Auth in your main activity:
//don't show warning message when closing account selection page
CustomTabsConfiguration.CustomTabsClosingMessage = null;

jquery mobile page dialog: tell dialog close from opening a new dialog

Open page as dialog. For example,
page1 -> page2(dialog) -> page3(dialog).
when opening a dialog, a dialog page is created in DOM by ajax.
<div data-role="page" id="dialog1">
....
</div>
$(":mobile-pagecontainer").pagecontainer("change", "#dialog1", { role: "dialog" } );
Opening dialog using page by ajax: works.
when a dialog closes, a code is executed to remove the page dialog element from DOM.
PageContainer hide event is registered as:
$( document).on( "pagecontainerhide", function( event, ui ) {
if (ui.prevPage) {
ui.prevPage.remove();
}
});
Issue: when a page dialog close, the ui.prevPage is not defined in the code above. ui.nextPage is correctly defined to point to the next page. How to catch event in order to remove the DOM element of a hidden page.
I tried navigation between two existing pages, both ui.prevPage and ui.nextPage are defined for pagecontainerhide event. what is the difference for hiding pages created by ajax (add page elements to DOM).
Note that: pagecontainerhide event can not be bound to page. pagehide event that is bound to page is deprecated.
Thanks.

Optionally open a new window in controller Action

In a ASP.Net MVC 2 application I want to do the following: in the action that handles a form post I want to:
Redirect the user to other view in the current browser window
Open a new window showing other info (other view)
That can be done easily setting the target="_blank" attribute in the form element and adding the following jQuery script:
$(function () {
$("form").submit(function () {
window.location = "...";
});
});
The View returned by the action handler will be rendered in the new window where the form is posted to.
But, let's make it a little trickier:
If there are no service layer errors when executing the action, then do the above.
If there's any service layer error when executing the action, then do not open the new window and the view returned by the action must be shown in the same window where the form was in the first place.
E.g.: Imagine that the service layer generates a pdfDocument to show to the user if everything is ok, and that pdf must be shown in a new window.
[HttpPost]
public ActionResult SomeAction(FormCollection form)
{
var serviceMethodParams = ... // convertion from the form data somehow
MemoryStream pdfDocument = null;
if (!serviceLayer.DoSomething(serviceMethodParams, out pdfDocument))
{
// Something went wrong, do not redirect, do not open new window
// Return the same view where error should be displayed
return View(...);
}
// The service method run ok, this must be shown in a new window and the origal window must be redirected somewhere else
return File(pdfDocument.ToArray(), "application/pdf");
}
Note that the original solution work fine when the service returns true, but if the service returns false the view showing the errors is shown in a new window and the original window is redirected somewhere else.
In this situation, the better option would be to simply return a URL specific to that document for the user to point to, and your ajax call, when successful, takes the URL returned from your action method, and then opens a new window itself pointing to that URL. On error, you can display errors or some such - basically, that data is folded in a Json return value.
Outside of an ajax call, the most acceptable pattern to me is to have the view re render, then attach some startup javascript to open the new window pointing to that specific URL.

Resources