Common question: Is it possible to wait for an application window to appear til i go on with the code? The application was invoked by Java Process/ProcessBuilder.
you can try this :
ProcessBuilder builder = new ProcessBuilder(command);
Process shell=builder.start();
shell.waitFor();
Related
Selenium webdriver | driver().switchTo().defaultContent() method is not switching the control back to parent window from multiple child windows.
I am facing problem with respect to no of windows and not frames. When I click a button on parent window say wp, a new web window say w1 is getting generated and eventually one more window gets generated say w2 , I want to switch to control to wp so I am using
driver.switchTo.defaultContent()
but its not switching control to parent window.
i have this problem to.
and i find answer
try this, and give me feedback - maybe i can help you
const number1 = await driver.wait(until.elementLocated(By.xpath('your xpath')))
await driver.executeScript("arguments[0].click()", number1).then(() =>{
console.log(driver.executeScript, 'Click done');
})
are you looking for switching to window tabs or iframe ?
for frames : we need to do,
driver.switchTo().defaultContent();
driver.switchTo.parentFrame();
for windows :
String parentwindow = driver.getWindowHandle();
List<String> allwindows = new ArrayList<>(driver.getWindowHandles());
driver.switchTo().window(allwindows.get(1));
driver.switchTo().window(parentwindow);
There are two similar methods that can be used while switching to parent frames, but there is a slight difference between them.
driver.switchTo().defaultContent()
driver.switchTo().parentFrame()
E.g.: You have three frames i1,i2,i3 and you are on frame i3,
when you use driver.switchTo.defaultContent(), it will take you to i1, whereas with
driver.switchTo.parentFrame() control will switch to i2 i.e. immediate parent frame (parent frame of the current frame).
If the process opens multiple browser tabs, we need to use switchTo().window(window reference); to switch focus between the tabs.
// Save the initial tab as parentwindow
String parentwindow = driver.getWindowHandle();
// Collect the tabs opened in a list
List<String> windows = new ArrayList<>(driver.getWindowHandles());
// Switch to window other than parentwindow
driver.switchTo().window(windows.get(1));
// Switch back to parentwindow
driver.switchTo().window(parentwindow);
I was using RemoteWebDriver for my cloud session where VM were running tests. Locally, it worked for me using $ driver.switchTo().defaultContent() $, but all of a sudden, it did not work with RemoteWebDriver. I resolved my issue using:
driver.switchTo().parentFrame();
I have a test that always fail when running inside Jenkins.
My project includes Selenium webdriver, JAVA, Maven, TestNG, Jenkins, Allure (reports).
I have a few suites of tests with 100+ test cases, and I iterate them through 3 different browsers (the tests run in parallel using TestNG). They all run (using maven command line) and pass in my development laptop, and on the test server when using a command line.
I have 2 problems regarding Jenkins and separated them into 2 questions- one of them is described in this question, and the other (IE11 issue) is here.
The problem starts when running inside Jenkins in the test server!
The test fail in mobile emulator (Chrome browser) - in the test I click on a link to verify that a new window was opened with the correct url.
I tried 3 types of clicks (Selenium click, Actions, JS) and all returned a null handle.
The code:
Here I create the main window handle and click the link:
String mwh = driver.getWindowHandle();
WebElement poweredBy = (new WebDriverWait(driver,10).until(ExpectedConditions.visibilityOfElementLocated(By.xpath(Consts.POWERED_BY_XPATH_1000))));
poweredBy.click();
And this is, part of, the method that gets the handle and verify the new window:
public boolean closePopupWindow(String mwh, String mTitle, String layoutNumber) {
// For IE11- make sure popup blocker is turned off in the options. else it will have only one window handle and fail
boolean isOpenedWindowCorrect = false;
String newWindow = null;
Set<String> handlers = driver.getWindowHandles();
for (String window : handlers) {
if (!window.equals(mwh)) {
newWindow = window;
}
}
// the focus is on the main page. need to switchTo new page and close it
driver.switchTo().window(newWindow);
System.out.println("The focus now is on the NEW window");
String newTitle = driver.getTitle();
System.out.println(newTitle);
This is the error I'm getting:
java.lang.NullPointerException: null value in entry: handle=null
at com.google.common.collect.CollectPreconditions.checkEntryNotNull(CollectPreconditions.java:34)
at com.google.common.collect.SingletonImmutableBiMap.(SingletonImmutableBiMap.java:42)
at com.google.common.collect.ImmutableBiMap.of(ImmutableBiMap.java:73)
at com.google.common.collect.ImmutableMap.of(ImmutableMap.java:123)
at org.openqa.selenium.remote.RemoteWebDriver$RemoteTargetLocator.window(RemoteWebDriver.java:995)
at il.carambola.pages.Page.closePopupWindow(Page.java:786)
Do you think there is a security issue that Jenkins wont open new windows in the browser? Is it VERY slow to open the window?
The same tests PASS when not using mobile emulator. (I have the same test in Chrome and Firefox and it succeed to click and pass the verification).
JDK 1.8.0_162
Jenkins V 2.121.1
Server- AWS t2.large - 8GB RAM, Windows server 2016 Data center, 64bit
It's clear that when your program gets to this line
driver.switchTo().window(newWindow);
that newWindow is still null. The way this is written, it could be a timing issue, and it could be another issue causing the popup to not show up. To make sure it's not a timing issue, I would suggest adding some kind of wait for there to be multiple window handles before you try to switch windows. Something like
(new WebDriverWait(driver,10)).until(d -> d.getWindowHandles().size() == 2);
If that wait fails, then you know the popup is being blocked and can go from there.
I use a java program for communication between a arduino board and a scratch file. The communication happen well. I use a user interface to start the communication where i have buttons called
connect
close and minimize
When the user clicks the connect button code will check the value in combo box and accordingly it opens the scratch file.
Once the connect button is clicked the control moves to the scratch application. After completing my work when i tried closing the scratch. My scratch application closes as expected but the control does not return to the user interface because of which i am not able to close the application and i close it in net beans forcefully. In the output screen i don't see build successful and instead i get build stopped. That is my process works perfectly until i give connect but once the button is pressed it is hanged up some where.
I tried making it as a jar file and running it in a different machine at that time i use end task in task manager to close the application.
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
if("Disconnect".equals(jButton1.getText()))
{
System.exit(0);
}
if(jComboBox2.getSelectedItem()==null)
{
System.out.println("Select one port");
}
else
{
Runtime r = Runtime.getRuntime();
try {
//this.hide();
//p = r.exec("C:\\Program Files\\Scratch 2\\Scratch 2.exe C:\\Users\\Admin\\Desktop\\fwdbckpwm12.sb2");
p = Runtime.getRuntime().exec("C:\\Program Files\\Scratch 2\\Scratch 2.exe C:\\Users\\Admin\\Desktop\\scratch files new.sb2");
//Runtime.getRuntime().exec("taskkill /F /IM <p>.exe");
//p.destroy();
//r.exec("C:\\Windows\\notepad.exe C:\\Windows\\ss.txt");
//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
A4S a4sObj = new A4S(new String[] {jComboBox2.getSelectedItem().toString()}); //defaultline
//A4S a4sObj = new A4S(new String[]{"COM16"}); //addedline
//r.gc();
//this.setDefaultCloseOperation(EXIT_ON_CLOSE);
} catch (IOException ex) {
Logger.getLogger(serialportselection.class.getName()).log(Level.SEVERE, null, ex);
}
finally{
//p.destroy();
//System.gc();
// }
}
Here is the code i tried. But none seems to work.
Move all Process related work into separate Thread.
Use waitFor method to recognise Process end - then you are free to
exit your app.
As I can understood you used SWING for creating UI.
You can set
yourFrame.setDefaultCloseOperation(EXIT_ON_CLOSE);
for your frame. This must help.
I have a c++ application which runs for a long time and I want to track the progress of it. I have a user interface for this application in java. Can anyone please tell me how to track the progress of the native application using JProgressBar. My native application returns an integer when it is done and I have a "Done" button that shows up when this integer is returned. But I want to have a progress bar that shows that the native application is running.
The only ways would be to either create a callback in java (meaning the code in c++ would call the java function) to inform of the current progress or create a function that the java code calls to get the current progress of your task.
http://www.codeproject.com/Articles/22881/How-to-Call-Java-Functions-from-C-Using-JNI
The java code would then use the value to update the jprogress bar.
Another possible simpler solution from Adam's is to have your C++ program output an update of progress to standard output, perhaps a String representation of the percent of progress. The Java program's Process would then have its OutputStream monitored by calling getInputStream() on the Process (yeah, it's not a typo; it's getInputStream()) in a thread background to the Swing thread, and use the information for updating the JProgressBar.
As others have said you need to have a way for your java application to know how far alone your c++ application is.
If you want a quick fix, use a JProgressBar and set it to interdeminate. It will animate the bar to show you something is happening. Once your process returns set the bar to 100%.
Easy way:
ProcessBuilder processBuilder = new ProcessBuilder(command);
JProgressBar progressBar = new JProgressBar();
progressBar.setIndeterminate(true);
Process p = processBuilder.start();
p.waitFor();
progressBar.setIndeterminate(false);
progressBar.setValue(progressBar.getMaxValue());
Better way:
ProcessBuilder processBuilder = new ProcessBuilder(command);
JProgressBar progressBar = new JProgressBar();
processBuilder.redirectErrorStream(true);
Process p = processBuilder.start();
InputStream is = p.getInputStream();
try {
in = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((line = in.readLine()) != null) {
//error checking
int progressValue = Integer.parseInt(line);
progressBar.setValue(progressValue);
} finally {
in.close();
}
}
progressBar.setValue(progressBar.getMaxValue());
I want to fire a command line executible with the parameters entered in GUI.
Process class can be used to fork my required command line process from the Java application, and I used the getInputStream() method of Process object to get the result and got it displayed in the GUI.
private void confirmActionPerformed(java.awt.event.ActionEvent evt) {
String output;
try {
Process p = Runtime.getRuntime().exec("my command line exe with parameters");
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
while ((output = stdInput.readLine()) != null) {
TextField.setText(TextField.getText()+output+"\n");
}
} catch (IOException e) {
System.out.println("Exception in Process");
e.printStackTrace();
}
}
This is my code which is an event listener of a button pressed event and I attempted to display the result of the process in the text field (java swing component).
This process actually runs for quite a long time and shows some statistics as and when it runs when run in command line, but when i attempt it in GUI with the above code I'm getting the entire consolidated result only after the process finish running. I'm not getting the TextField updated as and when it is executing.
What would be the cause of this issue?
This is because the whole thing is done by the Swing event-handling thread.
Perhaps should you consider creating a separate thread and update the TextField with a SwingUtilities.invokeLater().
As Maurice already pointed out, you shouldn't be doing intensive processing on the UI thread. The SwingWorker class helps with that. It's part of the standard API since 1.6. There is also a backport to 1.5 if you can't use 1.6.
This class makes it easier to put some processing task on another thread and display the result later so that your GUI doesn't block in the meantime. You can also use SwingUtilities.invokeLater() directly, of course, as suggested by Maurice, but for me SwingWorker is the way to go as it is quite easy and apparently the new standard way of doing this kind of thing.