Right click in selenium webdriver problems - scala

I've found this code here: https://stackoverflow.com/a/22376627/4165083
Actions oAction = new Actions(driver);
oAction.moveToElement(Webelement);
oAction.contextClick(Webelement).build().perform(); /* this will perform right click */
WebElement elementOpen = driver.findElement(By.linkText("Open")); /*This will select menu after right click */
elementOpen.click();
But I'm having problems with driver: "Cannot resolve symbol driver". I can't import anything. What should I do to get it working in my tests in Scala?

I believe you are referencing to wrong element. You have to reference to the element you are trying to right click on;
WebElement elementToRightClickOn = driver.findElement(By.id("something"));
Actions oAction = new Actions(driver);
oAction.moveToElement(elementToRightClickOn);
oAction.contextClick(elementToRightClickOn).build().perform(); /* this will perform right click */
WebElement elementOpen = driver.findElement(By.linkText("Open")); /*This will select menu after right click */
elementOpen.click();

"Cannot resolve symbol driver" - Check have you created the instance of the WebDriver and is accessible.
For Actions class - import org.openqa.selenium.interactions.Actions;
Provide complete code to assist.

Related

Is it possible to use CMD + CLICK Element A + CLICK Element B

I cannot use the Katalon recorder to click on the Element with CMD Key. Is it possible to click on two different items while the CMD key is pressed?
I tried to do that with sendKeys, controlKeyDown, keyDown Commands and then call Click Command on first element and then call Click Command on second element. But nothing happens.
Also, if we can simulate with CLICK Element A then CMD + CLICK Element B it can be ok for me.
In Katalon Studio (not the recorder) switch to script mode:
Now you can write scripts in Groovy!
Next, try using Actions class, like this:
WebUI.openBrowser("url of the page you are testing")
WebDriver driver = DriverFactory.getWebDriver()
WebElement targetA = driver.findElement(By.cssSelector("element-a-selector"))
WebElement targetB = driver.findElement(By.cssSelector("element-b-selector"))
targetA.click()
Actions actions = new Actions(driver)
actions.keyDown(Keys.CONTROL).click(targetB).keyUp(Keys.CONTROL).perform()
Note that this is your second scenario, first clicking element A (you will need to add the CSS selector or any other way of identifying it) and then holding CTRL and clicking element B.
The first one (press CTRL, click element a, click element b, release CTRL) is similar, just change the last line to:
actions.keyDown(Keys.CONTROL).click(targetA).click(targetB).keyUp(Keys.CONTROL).perform()
and remove targetA.click().
You will need to import the following Selenium helper classes:
import org.openqa.selenium.By
import org.openqa.selenium.Keys
import org.openqa.selenium.WebDriver
import org.openqa.selenium.WebElement as WebElement
import org.openqa.selenium.interactions.Actions

how to fetch value of gwt combobox using selenium web driver?

I am trying to test GWT web application using selenium webdriver. I want to fetch the value of selected option of the combobox of gwt. Can please someone tell me how can i do that.
Thanks
You can view value selected, as given below -
Select combo1 = new Select(driver.findElement(By.xpath("xpath of combobox")));
WebElement comboOptionSelected = combo1.getFirstSelectedOption();
System.out.println("Selected option is " + comboOptionSelected.getText());
If you want to fetch the selected option from the combo box follow the below one.
Select dropdown = new Select(driver.findElement(By.id("dropDownElementID")));
WebElement option = dropdown.getFirstSelectedOption();
option.getText();
If you want to select a value use the below one.
dropdown.selectByVisibleText("text");
Try the code and let me know the result.

Eclipse: Perform an action any time an editor receives input

I'm wondering if it is possible to determine what input was just entered inside of an editor in Eclipse - I'm currently working off of the example JDT editor - and then perform an action based on that input.
e.g.: I have a file example.jav open in my editor window. I push the 'a' key. 'a' would appear in the editor window per normal, but 'a' would also print out to the console.
Obviously the operation I'll be performing will be more complicated than a System.out.println() statement, but if someone could help show me where the change gets detected by the editor itself, I can take it from there.
A few notes:
I'm working in Eclipse 3.7.2 with Java 1.7
If you cannot find the JDT example editor, go to Help > Welcome > Samples and click on "Java Editor".
Thanks in advance!
Figured it out!
As the Editor API is so vast in eclipse that it is difficult to know where to start, I focused on adding a KeyListener to my Shell. Turns out that is slightly problematic in SWT, as when an item inside the Shell gains focus, the Shell itself looses focus. After a bit of searching though, I stumbled across someone else who had the same problem. By adding a filter to the Shell's display, you can add a Listener object which works for the entire application. Such as:
IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
Shell shell = window.getShell();
shell.getDisplay().addFilter(SWT.KeyDown, new Listener()
{
public void handleEvent(Event event)
{
System.out.println("" + event.character);
}
});
To further this and only worry about keys pressed in a specific non-widget part (otherwise you could just add a KeyListener to that part) you can add a check to make sure that the currently active part is the same as whatever part you wish to perform the actions for by using a simple if check.
final IWorkbenchWindow window = PlatformUI.getWorkbench().getActiveWorkbenchWindow();
Shell shell = window.getShell();
shell.getDisplay().addFilter(SWT.KeyDown, new Listener()
{
public void handleEvent(Event event)
{
IWorkbenchPage page = window.getActivePage();
IWorkbenchPart part = page.getActivePart();
IEditorPart editor = page.getActiveEditor();
if(part.equals(editor))
{
System.out.println("" + event.character);
}
}
});
Here is hoping that this helps someone else have an easier time than I had finding the solution!

Eclipse plugin RCP question

I am creating new Eclipse RCP application. I used "Hello RCP" as template project to create a new project. It works fine. Then I tried adding new menu. I used two extension points "org.eclipse.ui.commands" and "org.eclipse.ui.menu". I created handler class and also defined the menucontribution location uri(). But my menu is not showing up. I strongly suspect my location uri is wrong. But I dont know how to correct it. I have pasted my plugin.xml contents here. Let me know if anyone has a solution. I am following the steps given here http://zenit.senecac.on.ca/wiki/index.php/Add_Menu_to_RCP_Application
You can use ApplicationActionbarAdvisor class to define menus and commands for toolbar as well as menubar.
Firstly you have to declare the commands you want to add in your app like below:
private IWorkbenchAction newAction
Then with the help of ActionFactory class, you have to define the commands in makeActions() method like below:
newAction = ActionFactory.NEW_WIZARD_DROP_DOWN.create(window);
register(newAction);
newAction.setText("New");
Now, after declaration you have to add the commands in menus by using fillMenuBar(IMenuManager menuBar) method :
MenuManager filemenu = new MenuManager("&File", "file");
filemenu.add(newAction);
and if you want to add this command in toolbar, then you have to use following method:
protected void fillCoolBar(ICoolBarManager coolBar) {
IToolBarManager toolbar = new ToolBarManager(coolBar.getStyle());
coolBar.add(toolbar);
toolbar.add(newAction);
Sometimes, you have to use IContributionIem class for declaring commands because all the commands are not in IWorkbenchAction class.
Note: If you are using iContributionItem class then declaration and defination code will be replaced as follws:
IContributionItem show_view;
show_view = ContributionItemFactory.VIEWS_SHORTLIST.create(window);
and rest will be same.

How to identify the source of a text selection event coming from a CompareEditorInput in eclipse?

In my eclipse plugin I have the following code:
public class MyHandler extends AbstractHandler {
#Override
public Object execute( ExecutionEvent event ) throws ExecutionException {
ISelection sel = HandlerUtil
.getActiveWorkbenchWindowChecked( event )
.getSelectionService()
.getSelection();
if( sel instanceof TextSelection ) {
IEditorPart activeEditor = PlatformUI
.getWorkbench()
.getActiveWorkbenchWindow()
.getActivePage()
.getActiveEditor();
IEditorInput editorInput = activeEditor.getEditorInput();
if( editorInput instanceof CompareEditorInput ) {
// here are two possible sources of the text selection, the
// left or the right side of the compare editor.
// How can I find out, which side it is from?
}
}
return null;
}
}
Here I'm handling a text selection event coming from an CompareEditorInput, i.e. the result of comparing two remote revisions of a file with subclipse.
Now I want to handle the text selection properly. For that I have to know if it selects some text inside the left side editor or inside the right side editor.
How can I find that out?
EDIT 2010-04-10:
The concrete instance of CompareEditorInput is org.tigris.subversion.subclipse.ui.compare.SVNCompareEditorInput.
The issue is: org.eclipse.compare.CompareEditorInput (which have its java sources here) is an abstract class which has not always "left" or "right" pane:
/**
* The most important part of this implementation is the setup
* of the compare/merge UI.
* The UI uses a simple browser metaphor to present compare results.
* The top half of the layout shows the structural compare results
* (e.g. added, deleted, and changed files),
* The bottom half the content compare results
* (e.g. textual differences between two files).
* A selection in the top pane is fed to the bottom pane.
* If a content viewer is registered for the type of the selected object,
* this viewer is installed in the pane.
* In addition if a structure viewer is registered for the selection type,
* the top pane is split horizontally to make room for another pane
* and the structure viewer is installed in it.
* When comparing Java files this second structure viewer would show
* the structural differences within a Java file,
* e.g. added, deleted or changed methods and fields.
*/
The question is: do you know the exact implementation type of this CompareEditorInput object?
I.e: It is interesting to see how concrete classes deal with selection:
The org.eclipse.compare.internal.ResourceCompareInput for instance deals with org.eclipse.jface.viewers.IStructuredSelection, and comes with an utility function to get left and right IResource based on the selection.