RFT click on second named node - rft

I have a JTree that has two same-named nodes.
I am writing an RFT script that needs to click the second-named node but it will only click the first entry it finds.
enter image description here
By utilising ITestDataTree, I have managed to retrieve the ITestDataTreeNode I need but can't figure out how to click it.
Is there any way to convert an ITestDataTreeNode object into a GuiTestObject?
Thanks in advance,
Steven.

Finally found a solution.
simple as:
List list = new List();
list.append(new Index(0)); // root
list.append(new Index(0)); // suite
list.append(new Index(0)); // scenario
list.append(new Index(0)); // testcase
list.append(new Index(1)); // action
jTree().click(list); // expands second action node

Related

how can I click only one of the elements in a class with multiple elements?

ok so I cannot share the website I'm trying to automate but I'll share a screen shots of the inspect view.
ill add the code i used and the log i got from it
as you can see the class: data-command has three elements within in the number is dynamic but I need to click on the last one, i do not want to use absolut xpath as the class: data-command is dynamic.
ill add the code i used and the log i got from it
how do i click the last element
##{element_value}= Get WebElements class:data-value
#{elements_name}= Get WebElements class:data-label
#{element_commands}= Get WebElements class:data-command
WHILE ${i} < 5
#Log To Console ${element_commands[${i}]}
Click Element ${element_commands[${i}]}
Sleep 5s
#Capture Page Screenshot
Run Keyword And Warn On Failure Page Should Contain ${graph}
${i}= Evaluate ${i} + ${one}
END
I'm not familiar with robot framework, but is case data-command class is a unique locator, the following XPath will give you the last child inside that element:
"(//div[#class='data-command']//*)[last()]"
You can get elements store it in collection/list and then get the last one and click it:
List<WebElement> elements= driver.findElements(By.css(".data-command"));
element = elements.get(list.size() - 1); //Click only the last in the list

Difference between 3 different Xpaths producing same results

I am trying to locate the element <a>.
HTML:
<html><head></head><body>
Click Here!
</body></html>
Below is the Selenium code using Xpath producing same results:
driver.findElement(By.xpath("self::node()/child::node()/child::body/child::a")).click(); //Statement1
driver.findElement(By.xpath("/child::node()/child::body/child::a")).click(); //Statement2
driver.findElement(By.xpath("child::node()/child::body/child::a")).click(); //Statement3
My understanding:
In Statement1, it mentions the initial context node as the self::node. Here, we are passing our entire HTMLDocument as the self::node(). So, our initial context node is set to entire HTMLDocument(which is also called as document root node(/)). So, our initial context node is set to document root node(/)
In Statement2, it mentions the initial context node absolutely as the document root node(/).
But in Statement3, the initial context node is not mentioned.
So, does it mean that if we do not mention the initial context node, then will it be set to document root node(/) by default ?
Please help to improve my understanding.
Your 1 and 3 statements are the same from the "context" standpoint (self and child are both xpath axes). Both have root of the document as the context (in Selenium it is called SearchContext).
The context is root in that case because you lookup elements right within a driver. If you will have a WebElement and try to look up elements inside that element your context won't be root for 1 and 3 statements.
Below is some more detailed explanation..
Assume we have test.html with the following content:
<A val="success A">
<B val="success B"/>
</A>
And the test like this:
#Test
public void test(){
driver.get("file:///path_to_page/test.hml");
WebElement a = driver.findElement(By.xpath("html/body/A")); // (1, 2)
System.out.println(a.getAttribute("val"));
try{
System.out.println(driver.findElement(By.xpath("body/A/B")).getAttribute("val")); // (3)
}catch (NoSuchElementException e){
System.out.println("body/A/B cannot be found as the context is root");
}
WebElement b = a.findElement(By.xpath("B")); // (4)
System.out.println(b.getAttribute("val"));
System.out.println(b.findElement(By.xpath("/html/body/A")).getAttribute("val")); // (5)
}
Here are few remarkable points:
Despite our file has the root A, browser automatically adds html tags to make the html file valid. So it adds html node and body node
Considering the previous point we start from accessing html/body/A which has the root context
Then we make sure that we cannot find B using the path body/A/B since the context is still root.
Then we can see that we can find B in the context of previously located A.
The final thing is that we are looking up /html/body/A within the context of B. Despite we use the B as search context we still can find the element because we start the path from / which means root and ignores any search context.

Making dynamic code in Cucumber with the Page Object Model

Is there a way perform actions dynamically using Cucumber?
Example:
Feature File:
Scenario: Click all the boxes
Given On the checkbox page
When Click checkboxA
And Click checkboxB
Step Definition:
#When("Click checkboxA")
public void clickCheckBoxA()
{
pageObject.checkBoxA.click();
}
#And("Click checkboxB")
public void clickCheckBoxB()
{
pageObject.checkBoxB.click();
}
In this scenario, there are two very similar step definitions. The reason why there are two different definitions is because each WebElement is defined in the pageObject Class. Is there a way to dynamically pass which checkbox we want to click, rather than having two separate methods performing the same action?
The only way I can think to do this is by passing a selector as a parameter in the feature step and instantiating the webElement within the step definition method. But that seems like bad practice to me.
Click is your step. It should not be unique for each item you may want to click. You should match what you want to click with a regular expression. The following combines your two steps into one.
#When("^Click (.*)$")
public void clickElement(String elementToClick) {
switch (elementToClick) {
case "checkBoxA":
pageObject.checkBoxA.click();
case "checkBoxB":
pageObject.checkBoxB.click();
}
}
I'd suggest using a smarter regex match (this one is lazy and sloppy) and you could also create a new variable for the element to be clicked, assign its value to your existing element in each case and have a single call to click() after the switch statement.
Regarding your question of doing this "dynamically," you cannot do so in Java (I think you were thinking of having a single line in the example I gave above of pageObject.elementToClick.click();?) because it's a compiled programming language; your code can't be altered at runtime.
I would prefer to use #mike's solution, but in some cases I tend to create a locator as string in POM so that it reduces lines of code in the step definition.
Feature File:
Scenario: Click all the boxes
Given On the checkbox page
When Click checkbox A
And Click checkbox B
POM:
private String checkBox = "//input[text()='checkBox%s']";
Step Definition:
#When("Click checkbox (.*)")
public void handleCheckBox(String checkBoxName) {
driver.findElement(By.xpath(String.format(checkBox, checkBoxName))).click();
}

How to get the clicked element in selenium when we don't know what are we clicking

I am clicking with the help of following lione oc code->
actions.moveToElement(objDriver.findElement(By.id("id_popcode")),coordinates.getX(),coordinates1.getY()-1).doubleClick().build().perform();
Basically i double click at a position(x,y) in our application. Individually we cannot click that particular element bcoz it has to be clicked at particular (x,y) itself. So i want to get the properties of that clicked element(which i click using actions command which i mentioned above) liked id, classname. Can some one help me with this...kinda stuck here..........
edit:
try execute.elementFromPoint() with JavascriptExecutor to get element by coordinates
JavascriptExecutor js = (JavascriptExecutor)driver;
WebElement theElement = (WebElement)js.executeScript("return document.elementFromPoint(arguments[0], arguments[1])", coordinates.getX(), coordinates1.getY()-1);
System.out.println(theElement.getAttribute("tagName"));
System.out.println(theElement.getAttribute("class"));
old:
you are using negative value in getY()-1 which mean coordinates above the element, it maybe the parent or preceding-sibling of element try to select it using xpath
WebElement popcodeBefore = objDriver.findElement(By.xpath("//*[#id='id_popcode']/parent::*"));
// Or
// WebElement popcodeBefore = objDriver.findElement(By.xpath("//*[#id='id_popcode']/preceding-sibling::*"));
System.out.println(popcodeBefore.getAttribute("class"));
actions.moveToElement(popcodeBefore).doubleClick().build().perform();
If you have any specific text at that particular coordinates make use of it. I too had the same issue like this where I need to double click on a cell which had text 0.00%. I have done hovering action first using the text and then performed the double-click
Ignore the syntax issues since I am working on the protractor these days
browser.driver.actions().mouseMove(driver.findElement(by.xpath("//*[text()='00')]").build().perform();
and then perform the click
Still, you have issues, check if you have any attribute like ng-click which can be helpful to get the coordinates for that particular location. please always share the HTML code so that It may help us to check more deeply

netbeans rcp adding to topcomponent at runtime

I am working on a netbeans RCP desktop application and have a need to add components dynamically. For example, I have a button which if I click on the menu should add components to the window at runtime. I have an actionlistener for the button and I added the following code in the action performed, but am not seeing the new component added. Any help is appreciated.
TopComponent editorTopComponent = WindowManager.getDefault().findTopComponent("componentId");
editorTopComponent.add(new JButton("TEST"));
editorTopComponent.validate();
editorTopComponent.repaint();
editorTopComponent.updateUI();
Thanks
If you want create now instance (more then one), then you can use :
MyTopComponent my = new MyTopComponent();
my.open();
my.requestActive();
if you want open TC in one instance (only), then you can use:
TopComponent editor= WindowManager.getDefault().findTopComponent("componentId");
if(editor!=null){
JPanel x =editor.getMyPanel();
x.setVisible(false);
//some changes
x.setVisible(true);
if(!editor.isOpened())editor.open();
}
Jirka