Open a new window and get its location after loading - dom

I am using this code to open a new window:
Window w = window.open('example2.com', 'example2'); // Consisder than my domain
// is example1.com
This part of code works ok and succesfully opens a new window. Than i am trying to call my function after loading finishes.
w.onLoad.listen(locationGetter);
This is a code of locationGetter() function:
locationGetter(Event e) {
Location currentLocation = w.location;
currentHref = currentLocation.href; // currentHref is var defined
} // in main() function
But this code doesn't work well. Every time when I run my script both currentLocation and currentHref is null. At the beginning i thought that problem was in onLoad event, so i tried to call w.location exacly after opening a window:
Window w = window.open('example2.com', 'example2');
Location currentLocation = w.location; // still null
I am pretty sure that both Window and WindowBase has location property. Please help me with my problem or provide alternative solution of this task.

I don't think this is a Dart issue.
Seems you run into this
http://blog.carbonfive.com/2012/08/17/cross-domain-browser-window-messaging-with-html5-and-javascript/
CORS prevents accessing windows of other domains, you can use postMessage instead.

Related

How can i handle popups in playwright-java?

How can i handle alerts and popups in playwright-java?
there are different methods in API like page.onDialog(), page.onPopup(), what is the difference between them and how can i generate a handle?
//code to launch my browser and url
Playwright playwright = Playwright.create();
Browser browser = playwright.chromium().launch(new LaunchOptions().withHeadless(false).withSlowMo(50));
BrowserContext context = browser.newContext();
Page page = context.newPage();
page.navigate("http://myurl.com");
//had to switch to iframe to click on upload button
Frame mypage = page.frameByName("uploadScreenPage");
//below line is triggering the alert
mypage.setInputFiles("//*[#id='fileUpload']",Path.of("C:\\myFile.jar"));
//using this code to handle alert, which is not working
page.onDialog(dialog -> {dialog.accept();});
unable to accept alert using the above code. also alert has occurred after clicking an element that is inside an iframe. how can i handle such alerts?
Dialogs are messages like an alert or a confirm, whereas popups are new pages, like the one you would get by calling window.open.
This is how you can use it :
page.onDialog(dialog -> {
assertEquals("alert", dialog.type());
assertEquals("", dialog.defaultValue());
assertEquals("yo", dialog.message());
dialog.accept();
});
page.evaluate("alert('yo')");
I am happy to answer this question. I encountered the same situation and find the solution. Please see below:
//Handling Pop-up or new page
Page pgdriver1 = pagedriver.waitForPopup(new Runnable()
{
public void run() {
pagedriver.click("text=\"Analiza\"");
}
});
pgdriver1.click("//button[normalize-space(#aria-label)=\"Close dialog\"]/nx-icon");
I hope this answer your question.
//listening to the alert
page.onDialog(dialog -> {dialog.accept();});
//next line will be action that triggers your alert
page.click("//['clickonsomethingthatopensalert']")

Winum can not locate elements on windows 10

I am newbie using Winium and installed a sample test - steps are only to open Notepad and click on the File button/menu item. The launching of the application (Notepad) works but it seems it can not locate the button. I have tried to locate using both name and id attributes without any luck. I am running on Windows 10 so my guess is it has something to do with this..Any tips or workarounds highly appriciated - i will pase my simple code below
Thanks!
public static void main(String[] args) throws MalformedURLException, InterruptedException {
DesktopOptions option = new DesktopOptions();
option.setApplicationPath("C:\\Windows\\System32\\notepad.exe");
WiniumDriver driver = new WiniumDriver(new URL("http://localhost:9999"), option);
Thread.sleep(2000);
WebElement el = driver.findElement(By.name("File"));
el.click();
}
You could try getting a reference to the window first, and then looking within that for an element called 'File'.
This works for me.
var window = driver.FindElementByClassName("Notepad");
var fileMenuHeader = window.FindElement(By.Name("File"));
fileMenuHeader.Click();
I'm not sure how you get the next level of menus though - it doesn't appear to be part of the window.
Try catching the menu-bar first with its ID.
Then with that element try catching the menu options like file, edit,etc.
Below code works fine on Windows 10.
var menubar = Driver.FindElementById("MenuBar");
var editMenu = menubar.FindElement(By.Name("Edit"));
var FileMenu = menubar.FindElement(By.Name("File");
editMenu.Click();

Vala force refresh progressbar

I've made an aplication with vala where at some point I have to process a lot of files. I've created a window to choose a folder and then I get the paths of files and make some proces on them.
I've added a progress bar to this window to show how many files have been processed but for some reason it remains always empty.
Code about window:
this.files_window = new Gtk.Window();
this.files_window.window_position = Gtk.WindowPosition.CENTER;
this.files_window.destroy.connect (Gtk.main_quit);
// VBox:
Gtk.Box vbox = new Gtk.Box (Gtk.Orientation.VERTICAL, 5);
this.files_window.add (vbox);
// Buttons to open and close
Gtk.Button cancel = new Gtk.Button.with_label ("Cancel");
Gtk.Button select = new Gtk.Button.with_label ("Select");
vbox.add (select);
vbox.add (cancel);
// proogress bar
this.progress_bar = new Gtk.ProgressBar();
vbox.add(this.progress_bar);
// conect select to method do_stuff
select.clicked.connect (do_stuff);
this.files_window.show_all ();
As you can see, I connect the button "select" to the method "do_stuff" where I get the paths of selected files and make some process.
I update correctlly the fraction of the progres bar because I've added some prints to know if the value is correct and it is. It's just that the windows is not refreshing, possibly because all the work it is doing with the process of the files. Here is the code about do_stuff() method:
// some proces to get paths of files in the list sfiles
double fraction = 0.0;
this.progress_bar.set_fraction (fraction);
int processed_files = 0;
foreach (string sfile in sfiles) {
do_some_proces_to_file(sfile);
processed_files += 1;
fraction = (double)processed_files/(double)sfiles.length;
this.progress_bar.set_fraction (fraction);
stdout.printf("Real fraction: %f\n", this.progress_bar.get_fraction());
}
The printf shows that the value of the progres bar is being updated but in the window the bar is always empty.
Am I missing something? Is it the correct way to do the progres bar? Should I made another thread to do the stuff?
As #nemequ says, your code is blocking the main loop thread (which handles both user input and scheduling/drawing widget updates), hence it the progress bar is not updated until the method completes.
Using a thread is one way solve the problem, however using threads can lead to a lot of bugs however since it can be difficult to make even simple interactions between threads safe.
An async method avoids this by interleaving the code with the other work being done by the main loop. An async version of your do_stuff() would be pretty straight-forward to write, simply declare it async and put a yield in the for loop somewhere:
public async void do_stuff() {
...
foreach (string sfile in sfiles) {
// all of this is as before
do_some_proces_to_file(sfile);
processed_files += 1;
fraction = (double)processed_files/(double)sfiles.length;
this.progress_bar.set_fraction (fraction);
// Schedule the method to resume when idle, then
// yield control back to the caller
Idle.add(do_stuff.callback);
yield;
}
}
You can then kick it off from your click handler by calling: do_stuff.begin().
Unless there is some relevant code you're not showing, you're blocking the main loop. One option would be to do everything in a thread, and use an idle callback to update the UI. The basic idea is something like:
new GLib.Thread<void*>("file-processor", () => {
foreach (string sfile in sfiles) {
/* do stuff */
GLib.Idle.add(() => {
/* Update progress */
return false;
});
}
return null;
});
Depending on your application you may need to add a mutex to avoid race conditions. You may also need to add some logic for canceling the operation.
A better option might be to use a GLib.ThreadPool. You'd still want to update the UI from an idle callback, but this would allow each task to execute in parallel, which could provide a significant speed-up.
If I were you I'd probably wrap it all up in an async function to keep the API tidy, but you don't really have to.

Crossrider : Using mouse position in context menu

We would like to use appAPI.openURL but in place of sending the data.selectedText I woould like to send the text of the element under the mouse. But I can't find the way of getting the mouse position. My idea was to add in the appAPI.ready the following
$().mousemove(function(event) {
myPositionX = event.pageX ;
myPositionY = event.pageY ;
}
And to have two global variable myPositionX and myPositionY which I could access in my background code to transmit as parameters of my URL.
But this doesn't seem to work.
Is what I'm doing crazy?
You'll be pleased to note that your are not crazy but simply missed the selector required to attach the handler to the page. Hence, to make you code work, bind the mousemove handler to the document object per the following tried and tested code:
$(document).mousemove(function(event) {
myPositionX = event.pageX ;
myPositionY = event.pageY ;
});

Convert GdkX11Window to VTE Terminal class in an focus event

I need to receive a GTK+ focus in event on a Terminal (VTE), but the event returns EventFocus which holds Gtk.Window reference:
http://www.valadoc.org/gdk-3.0/Gdk.EventFocus.html
How can I get the Terminal from the Window reference? I cannot retype it, it looks like it is a container. But I am unable to find which method to call to get the Terminal.
Terminal terminal = new Terminal();
// ...
terminal.focus_in_event.connect((event) =>
{
the_terminal = event.window; // DOES NOT WORK > invalid cast from `GdkX11Window' to `Terminal'
return false;
});
Thanks for pointing out I dont need it. Yeah, my real code is:
for (int i = 0; i < terminal.length; i++) {
this.terminal[i].focus_in_event.connect((event) =>
{
GLib.stdout.printf("Focus event terminal %p\n", this.terminal[i]);
return false;
});
}
Unfortunately it always prints null :-(
Thanks!
I'm not sure there is an easy way to convert a Gdk.Window to a Gtk.Widget as not all widgets have an associated GDK window, necessarily. As I see it, there's no compelling reason to try to extract the terminal from the event. In the context of the callback, you can simply reference the outer variable terminal and Vala will lift it into the callback.
Terminal terminal = new Terminal();
// ...
terminal.focus_in_event.connect((event) =>
{
terminal.queue_draw();
return false;
});