Showing menu when right clicking on a item of a tree view - java

I have a treeview and I wanted to add right click support for the different items. This is how I did it:
http://jsfiddle.net/doonot/xWjSz/
The menu is shown only for the first root module (after right click), but not for the rest of the root modules. Can you tell me what I have to change, in order to get the menu for all root modules?
Thanks a lot, I much appreciate your answer.

Hiya Please see this demo http://jsfiddle.net/hYJPv/1/ or http://jsfiddle.net/hYJPv/ (Fixed the issue) or diff approach here http://jsfiddle.net/UeqBk/for_Dooonot_from_Tats_innit/
on rightclick you will get an alert.
code
$(document).ready(function()
{
// If you want to disable showing the context menu when right clicking
// on the document, the code below would do the trick.
$(document).bind("contextmenu",function(e)
{
alert('right click capture');
return false;
});
var $tree = $("#tree").kendoTreeView(
{
select: function (event)
{
var $item = $(event.node);
console.log( $item );
alert( "selected" );
}
});
// Find the item you want to select...
var $selected = $('#selected');
var $treePath = $selected.parentsUntil($tree, "li");
var treeView = $tree.data('kendoTreeView');
// Expand the tree in order to show the selected item
treeView.expand( $treePath );
// Gotta make both calls...
treeView.select( $selected );
treeView.trigger( 'select', {node: $selected} );
});
​

Related

Error trying to close a tab and switching to the other tab using selenium / java

I have a window with two tabs. I am trying to close the tab with a specific title and switch the control to the other tab.
Here is my code:
public static void closeTheWindowWithTitle(String title) {
ArrayList<String> tabs = new ArrayList<String> (driver.getWindowHandles());
String mainWindow = driver.getWindowHandle();
for(int i = 0; i < tabs.size(); i++) {
log.debug("switched to " + driver.getTitle() + " Window");
if(driver.getTitle().contains(title))
{
driver.switchTo().window(tabs.get(i));
driver.close();
log.debug("Closed the " + driver.getTitle() + " Window");
}
}
driver.switchTo().window(mainWindow);
}
When I run my code, I am getting the following exception:
org.openqa.selenium.NoSuchWindowException: no such window: target window already closed
from unknown error: web view not found
I am unable to figure out what is the problem. Please help.
I'm guessing, that the WindowHandle of your Main-Window got changed, somewhere along the way. You should be able to get your problem solved by doing something similar to the suggested solution here, i.e. getting all WindowHandles, and iterating over them and in the end switching to [0], which should be the only one left, after closing the second one.
I hope this will help you to fix your issue, i dont want to provide code fix and i want to explain you the details process step by step.
Open firefox/IE/Chrome browser and Navigate to https://www.bbc.co.uk
WebDriver driver = new AnyDriveryourusing();
// set implicit time to 30 seconds
driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
// navigate to the url
driver.get("https://www.bbc.co.uk");
Get the GU ID of the current (parent) window using getWindowHandle() method present in the webdriver and store the value in a String
// get the Session id of the Parent
String parentGUID = driver.getWindowHandle();
Click on the Open New Window button, application open new window with google page.
// click the button to open new window
driver.findElement(By.id("two-window")).click();
Thread.sleep(5000);
Get the GU IDs of the two windows (parent + google), using getWindowHandles() method present in the webdriver. Store the GU IDs in a Set Collection, this Set will have GU IDs of both parent and Child Browsers
// get the All the session id of the browsers
Set allGUID = driver.getWindowHandles();
iterate the Set of GUID values, and if the value is parent value skip it if not switch to the new window
// iterate the values in the set
for(String guid : allGUID){
// one enter into if block if the GUID is not equal to parent window's GUID
if(! guid.equals(parentGUID)){
//todo
}
}
Switch to window using switchTo().window() method, pass the GU ID of the child browser to this method.
// switch to the guid
driver.switchTo().window(guid);
Find the search bar in Google.com and search for "success"
driver.findElement(By.name("q")).sendKeys("success");
Close the Google tab/Window and return to parent tab/browser window
// close the browser
driver.close();
// switch back to the parent window
driver.switchTo().window(parentGUID);
You were close but you weren't switching windows before checking the title. I've updated the code to something that should work.
public static void closeTheWindowWithTitle(String title)
{
Set<String> tabs = driver.getWindowHandles();
String mainWindow = driver.getWindowHandle();
for(int i = 0; i < tabs.size(); i++)
{
// you need to switch to the window before checking title
driver.switchTo().window(tabs.get(i));
log.debug("switched to " + driver.getTitle() + " Window");
if(driver.getTitle().contains(title))
{
driver.close();
log.debug("Closed the " + driver.getTitle() + " Window");
break; // this breaks out of the loop, which I'm assuming is what you want when a match is found
}
}
driver.switchTo().window(mainWindow);
}

Selenium - Clicking a link opens up a new tab

I have seen many threads about how to open a link in a new tab, but what about the case when you have a link that creates a new tab and you need to verify the title? All I need to do is click the link --> verify that the new tab has the correct title --> close the tab and continue on the original tab. Thanks in advance for the help!
//Get Current Page
String currentPageHandle = driver.getWindowHandle();
linkToClick.click();
//Add Logic to Wait till Page Load
// Get all Open Tabs
ArrayList<String> tabHandles = new ArrayList<String>(driver.getWindowHandles());
String pageTitle = "ThePageTitleIhaveToCheckFor";
boolean myNewTabFound = false;
for(String eachHandle : tabHandles)
{
driver.switchTo().window(eachHandle);
// Check Your Page Title
if(driver.getTitle().equalsIgnoreCase(pageTitle))
{
// Report ur new tab is found with appropriate title
//Close the current tab
driver.close(); // Note driver.quit() will close all tabs
//Swithc focus to Old tab
driver.switchTo().window(currentPageHandle);
myNewTabFound = true;
}
}
if(myNewTabFound)
{
// Report page not opened as expected
}

Click on a button not working

I'm using selenium webdriver with java.
I have encountered a situation where i am able to locate & click on a button,but nothing happens after this.
The HTML code for the said button is->
<div id="divAllButtons" class="UCButtonMainCSS" style="display: none;">
<div>
<div id="OtherActionParent" class="mT8">
<div id="btnSave" class="btn fLt mR20">
<span>
<a onclick="Save_onclick()" href="javascript:void(0)">
<span id="Label24">Save</span>
</a>
</span>
</div>
The button when clicked, should redirect to the confirmation page, or show an alert message if mandetory fields are not filled.
I have tried few thing,
1
Button = driver.findElement(By.id("btnSave"));
Button.click();
2
Button = driver.findElement(By.xpath("//div[#id='dataContainer']/div[2]/div/div/div[4]/div/div[1]/div[2]/span/a"));
Button.click();
3
Actions action = new Actions(driver);
WebElement we = driver.findElement(By.xpath("//div[#id='dataContainer']/div[2]/div/div/div[4]/div/div[1]/div[2]/span/a"));
action.moveToElement(we).click().build().perform();
4
Point coordinates = driver.findElement(By.xpath("//div[#id='dataContainer']/div[2]/div/div/div[4]/div/div[1]/div[2]/span/a")).getLocation();
Robot robot = new Robot();
robot.mouseMove(coordinates.getX()+40,coordinates.getY()+30);
robot.mousePress(InputEvent.BUTTON1_MASK);
robot.mouseRelease(InputEvent.BUTTON1_MASK);
Each of the way appears to work fine to click on the button as message javascript:void(0) is displayed in the bottom corner of the browser.
Try this code to click on the Save button and see if it works:
WebElement Button = driver.findElement(By.xpath("//div[#id='btnSave]//a"));
Button.click();
OR
WebElement Button = driver.findElement(By.xpath("//div[#id='btnSave]//span[.='Save']"));
Button.click();
Try a text based xpath search
text based xpath solved a lot of issues for me. You just need to make sure you have enough wait time before clicking the element
EDIT: try using action
By saveButton = By.xpath("//*[.='Save']");
WebElement element = driver.findElement(saveButton);
Actions action = new Actions(driver);
action.moveToElement(element).build().perform();
driver.findElement(saveButton).click();
Note: untested code written in java
Just try with javascript executor. it may work....
WebElement save = driver.findElement(By.id("btnSave"));
JavascriptExecutor executor = (JavascriptExecutor)driver;
executor.executeScript("arguments[0].click();", save);
The problem is solved. The issue was not to locate the element but the onClick() event was not firing. Then i found out that something else was there which stopped from the event to fire. I had used java script to enable the date picker box & did this,
((JavascriptExecutor)driver).executeScript ("document.getElementById('txtOriginDate').removeAttribute('readonly',0);");
WebElement originDateBox= driver.findElement(By.xpath(prop.getProperty("originDateBox")));
originDateBox.clear();
originDateBox.sendKeys("9-Dec-2014"); //Enter date
Developer designed this in such a way that if you don't use date picker to select date, a specific variable was not set. Which eventually made the onclick event not to fire.
The date picker code was something like this,
var jsoncustdate = "";
var jsonorigindate = "";
function onSelectCalender( StrDt, obj )
{
if ( !varReadonly )
{
if ( $( "#txtCustDescisionDate" ).attr( "IsDisable" ) == "FALSE" )
{
if ( obj.id == "txtCustDescisionDate" )
{
custobjDt = new Date( obj.selectedYear, obj.selectedMonth,obj.selectedDay, 0, 0, 0, 0 );
jsoncustdate = custobjDt.getTime();
jsoncustdate = "\/Date(" + jsoncustdate + ")\/";
DisabledBtnStage();
// $("#txtFromDate").datepicker("option", "maxDate", objDt);
}
if ( obj.id == "txtOriginDate" )
{
var objDt = new Date( obj.selectedYear, obj.selectedMonth,obj.selectedDay,0, 0,0,0 );
jsonorigindate = objDt.getTime();
jsonorigindate = "\/Date(" + jsonorigindate + ")\/";
DisabledBtnStage();
// $("#txtToDate").datepicker("option", "minDate", objDt);
}
}
elogCommon.CheckMandatory();
}
}
I finally used date picker in normal way & the event fired smoothly.
Thank you guys for help . .cheers !!!

Javascript on a icon that expands and collapse a table not working with the enter button

I have on this page a clickable +/- icon that will expand and collapse a table of information when the user clicks on it in the jsp page, but the issue is that if the user tabs to this icon and try to expand/collapse it by pressing the enter button, the javascript won't run; it only works with mouse click for some reason. Here's what I have on the jsp page:
<td>
<a onclick="hideShowTable(${count}, this.id)" style="cursor:hand"
title="Expand/Collapse Table" tabindex="40"
id="eCTable${count}"
>+
</a>
</td>
That executes this function in the js file:
function hideShowTable(tableCounter, id)
{
//Loop through all rows of the month
for(i=1; i<=12; i++)
{
var tableElm = document.getElementById("tableMonth"+ i +"_"+tableCounter);
//Hide or show the div tag
if (tableElm .style.display == "block"){
tableElm .style.display = "none";
document.getElementById(id).innerText="+";
}
else{
tableElm .style.display = "block";
document.getElementById(id).innerText="-";
}
}
}
The description says:
The onclick event occurs when the pointing device button is clicked over an element. This attribute may be used with most elements.
I think you can try using form's onsubmit() rather than onclick() . Give a try..

Programatically open a menu from menubar(Simulate the menu click programmatically for UI automation test)

MenuManager and MenuContribution items has been already created.
For the input Menu Item id/label, I need to problematically drop-down/open/display a menu item from menubar in Eclipse. I think I may need to fire some event.
This is requirement for UI Automation that Menu should be drop down automatically.
Can you please help at the earliest. I'm trying following, but here not sure how to set the x & y co-ordinates where mouse click event should be fired.
Code:
String toCompare = "File";
Menu menu = window.getShell().getMenuBar();
if(menu!=null && !menu.isDisposed()){
MenuItem[] items = menu.getItems();
for(int i=0;i<items.length;i++){
String menuText = LegacyActionTools.removeMnemonics(items[i].getText());
if(toCompare.equalsIgnoreCase(menuText)){
Event event = new Event();
event.doit = true;
event.widget = items[i];
event.type = SWT.MouseDown;
event.button = 1;
boolean success = items[i].getDisplay().post(event);
System.out.println("Could we generate the event ? "+success);
}
}
}
Why don't you use dedicated tools for UI testing, such as SWTBot. It seems typicall matche what you would do

Categories