I am new to Selenium and just started to use it. I want to open a new browser session in a different window from my script and do not know how to do it.
I tried using the open command and gave the Firefox Url but it opened in the same window.
Any ideas?
Try using openWindow instead of open. If you get a message that Firefox blocked a popup, allow popups. This will probably open a tab instead of a window, but maybe that will suit your needs.
#Test
public void Test01() throws Exception {
openTab("http://www.xyz.com");
}
This will open a different Firefox window. And then Handle to switch the new window.
public void trigger(String script, WebElement element) {
((JavascriptExecutor) driver).executeScript(script, element);
}
public Object trigger(String script) {
return ((JavascriptExecutor) driver).executeScript(script);
}
public void openTab(String url) {
String script = "var d=document,a=d.createElement('a');a.target='_blank';a.href='%s';a.innerHTML='.';d.body.appendChild(a);return a";
Object element = trigger(String.format(script, url));
if (element instanceof WebElement) {
WebElement anchor = (WebElement) element;
anchor.click();
trigger("var a=arguments[0];a.parentNode.removeChild(a);", anchor);
} else {
throw new JavaScriptException(element, "Unable to open Window", 1);
}
}
Related
On Eclipse Luna, I select a server and click the Start button on Servers view, then the server (for example Tomcat8) will get started. If something is wrong during the start-up process, a dialog will be populated to display the error messages (for example time-out). The dialog is modeless in this test case.
Now I need to start the server programmatically from a plugin. In case that errors occur, how could I programmatically detect that a dialog has been opened and how to close it?
You could use the Display.addFilter method to listen for all SWT.Activate events which will tell you about all Shells (and other things) being activated. You can then detect the shells you want to close.
Something like:
Display.getDefault().addFilter(SWT.Activate, new Listener()
{
#Override
public void handleEvent(final Event event)
{
// Is this a Shell being activated?
if (event.widget instanceof Shell)
{
final Shell shell = (Shell)event.widget;
// Look at the shell title to see if it is the one we want
if ("About".equals(shell.getText()))
{
// Close the shell after it has finished initializing
Display.getDefault().asyncExec(new Runnable()
{
#Override
public void run()
{
shell.close();
}
});
}
}
}
});
which closes a dialog called 'About'.
In more recent versions of Java the above can be simplified to:
Display.getDefault().addFilter(SWT.Activate, event ->
{
// Is this a Shell being activated?
if (event.widget instanceof Shell shell)
{
// Look at the shell title to see if it is the one we want
if ("About".equals(shell.getText()))
{
// Close the shell after it has finished initializing
Display.getDefault().asyncExec(shell::close);
}
}
});
This uses Java 8 lambdas and method references and Java 16 instanceof type patterns.
I am trying to open a marker, while double-clicking on an entry from a TableViewer, inside an eclipse plug-in. I am able to get the associated resource from the marker, however nothing is happening while the openEditor method is executed.
The code is as below:
viewer.addDoubleClickListener(new IDoubleClickListener() {
public void doubleClick(DoubleClickEvent event) {
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
try {
IStructuredSelection sel = (IStructuredSelection) event.getSelection();
ReviewIssue reviewIssue = (ReviewIssue) sel.getFirstElement();
if(reviewIssue != null){
MessageDialog.openError(window.getShell(), "Insta Review", reviewIssue.getMarker().getResource());
try {
IDE.openEditor(window.getActivePage(), reviewIssue.getMarker(), true);
} catch (PartInitException e) {
MessageDialog.openError(window.getShell(), "Insta Review", e.getMessage());
}
}
} catch (Exception e) {
MessageDialog.openError(window.getShell(), "Insta Review", e.getMessage());
}
}
});
Please let me know, if am missing something here. Thanks in advance.
Also ignore the message dialogs, as I plan to implement the logging functionality later.
UPDATE:
Even though I created the marker on IFile, I was getting the same behaviour. I was finally able to open the editor by using the IFile, instead of the marker.
IFile iFile = markerProject.getFile(path);
//IMarker marker = iFile.createMarker("id.myMarker");
.....
IDE.openEditor(window.getActivePage(), reviewIssue.getiFile(), true);
//IDE.openEditor(window.getActivePage(), reviewIssue.getMarker()), true);
For this to work the IMarker.getResource() method must return an IFile. The code in IDE.openEditor is:
// get the marker resource file
if (!(marker.getResource() instanceof IFile)) {
IDEWorkbenchPlugin
.log("Open editor on marker failed; marker resource not an IFile"); //$NON-NLS-1$
return null;
}
so look in the .log file in the workspace .metadata directory to see if you are getting that log message.
Normally you would create a marker for a file using the IFile.createMarker method (createMarker is actually an IResource method).
I have developed an application using net bean,
1) I'm making project in java API 6. I'm using "Net Beans 7.1".
2) I want to use JInternalFrame in my project
3) I made another package and made "JInternalFrame" there. And then call it in my main application window by firing action performed event on "JMenuItem".
4) It works fine but only one problem occurs that is, if i click on "JMenuItem" again and again, new "JInternalFrame" of same instance are opening, How can i stop that?
5) I want that, if I open "JInternalFrame" once and then i again click on "JMenuItem" to open the same "JInternalFrame", it Should do nothing or it shows the window which already opened and minimized
sample code:
<code>
private void empDataActionPerformed(java.awt.event.ActionEvent evt) {
Emps employees = new Emps();
desktop.add(employees);
employees.setVisible(true);
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
employees.setBounds(230, 40, screenSize.width / 2 - 80, screenSize.height / 2 + 105);
}
<code>
please I need help.
Here is may sample code. hope this help.
Menu action to call internal frame in main application where JdesktopPane in it.
private void YourJinternalFrameMenuItemActionPerformed(java.awt.event.ActionEvent evt) {
YourJinternalFrame nw = YourJinternalFrame.getInstance();
nw.pack();
//usefull part for you.. if open shows, if not creates new one
if (nw.isVisible()) {
} else {
desktopPane.add(nw);
nw.setVisible(true);
}
try {
nw.setMaximum(true);
} catch (PropertyVetoException ex) {
Logger.getLogger(MainApplication.class.getName()).log(Level.SEVERE, null, ex);
}
}
put this inside of your YourJinternalFrame
private static YourJinternalFrame myInstance;
public static YourJinternalFrame getInstance() {
if (myInstance == null) {
myInstance = new YourJinternalFrame();
}
return myInstance;
I have a strange problem which puzzles me. I open a file in an editor in Eclipse with this code:
final IWorkbench wb = PlatformUI.getWorkbench();
IWorkbenchWindow window = wb.getActiveWorkbenchWindow();
final IWorkbenchPage page = window.getActivePage();
wb.getProgressService().runInUI( window, new IRunnableWithProgress() {
#Override
public void run( IProgressMonitor monitor ) throws InvocationTargetException, InterruptedException {
if( null == monitor ) {
monitor = new NullProgressMonitor();
}
try {
monitor.beginTask( "Append to file", 2 );
ITextEditor editor = (ITextEditor) IDE.openEditor( page, file );
monitor.worked( 1 );
// TODO Bug: Editor is active, it has the focus but doesn't process keypress events :-(
} catch( Exception e ) {
throw new InvocationTargetException( e, "Error appending to file " + file );
} finally {
monitor.done();
}
}
}, null );
I collected the bits and pieces from several sources on the Internet.
The strange problem is that the editor seems to have the focus (the tab is highlighted and I see the blue border around it)
But there is no cursor visible in the editor and when I type something, nothing happens (also nothing happens elsewhere in the workbench).
I also tried ITextEditor editor = (ITextEditor) IDE.openEditor( page, file, true ); but with the same result.
When I click on the tab, the cursor appears and I can use the editor. Pressing F12 has no effect, though.
And ideas?
Try page.activate(editor); (even though it should already be active).
I have created a Dialog with two buttons Yes, No, and then I have add action listener to them, my problem is that I want no button to hide the Dialog that I have created
the code is looks like:
dialog = new Dialog(title);
dialog.setDialogType(Dialog.TYPE_CONFIRMATION);
ta = new TextArea(text);
ta.getStyle().setBorder(Border.createEmpty());
ta.setEditable(false);
yesCommand = new Button("YES");
noCommand = new Button("NO");
yesCommand.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
LGBMainMidlet.getLGBMidlet().notifyDestroyed();
}
});
noCommand.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent ae) {
Logger.Log("Bye Bye");
dialog = null;
System.gc();
}
});
dialog.addComponent(ta);
dialog.addComponent(yesCommand);
dialog.addComponent(noCommand);
dialog.show();
the code is not working for me, can anyone told me what is the problem?
B.N. I have used dialog.dispose(), but it exit the whole application
It is better to use
dialog.setTimeout(1000); the number show the time limit the dialog box wait in milliseconds. So by doing this you can exit the dialog form automatically.
Dialog.dispose() does not exit the whole application, it just closes the dialog.
If you have nothing in your application you might see nothing if you dispose the dialog.