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

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).

Related

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

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()

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 ?

How to let placeManager.revealPlace open a request in a New Tab, in GWTP?

In GWTP, we can go into new url by:
PlaceRequest request = new PlaceRequest(NameTokens.myTok).with("param1","123");
placeManager.revealPlace(request);
However, these above codes will open a new url (ex: abc.com#myTok;param1=123) into the current tab. My question is how to let placeManager to open new url into a new tab?
There is other solution that can be found on internet but I am not sure it is the good one. We can
String url = Window.Location.createUrlBuilder()
.setHash("myTok;param1="+URL.encodeQueryString("123"))
.buildString();
Window.open(url, "_blank", null);
I think the 2nd solution is not elegant since we have to manually put the param into the url my ourselves, while in the 1st one, all the params was wrapped inside .with method.
So How to let placeManager.revealPlace open a request in a New Tab, in GWTP?
Why not just:
String url = Window.Location.createUrlBuilder().setHash(placeManager.buildHistoryToken(request)).buildString();
Window.open(url, "_blank", null);

Embedded browser control does not display

Basically I embedded a browser into my application. However the request I want to view (google) does not show in the application despite my code linking to it. Any ideas?
BrowserFieldConfig myBrowserFieldConfig = new BrowserFieldConfig();
myBrowserFieldConfig.setProperty(BrowserFieldConfig.NAVIGATION_MODE,BrowserFieldConfig.NAVIGATION_MODE_POINTER);
BrowserField browserSession = new BrowserField(myBrowserFieldConfig);
//browserSession.
browserSession.requestContent( "http://www.google.com" );
add(browserSession);**
It need MDS service to be running. Check for MDS. it will work.

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