Assert + Selenium 4 relative locators - java

I wonder if I can use the new Selenium 4 relative locators in my assertions.
For example something like that:
Assert.assertTrue("", myWonderfulLogo.above(myWonderfulNavBar));
There's a crooked way to do this but I wonder if there's a more straightforward way to do it: https://github.com/angiejones/selenium-relative-locators/blob/master/src/test/java/books/RelativeLocatorsTests.java
Thanks in advance!

If your goal is to write a test to see if your myWonderfulLogo is above myWonderfulNavBar, then relative locators can help you, although they will need to be converted to web elements first. All relative locators are doing is further constraining regular locations (xpaths, by id, by name, css selectors etc).
For example if I wanted to look for an element with a certain id, but I only wanted to look for elements that were above a certain other element (instead of searching the entire document) relative locators are good for that.
Say your myWonderfulLogo has the following html:
<img id="logo" ... />
And your myWonderfulNavBar has the following html:
<nav id="nav" ... />
Then to write a test to make sure your logo is above your nav bar, you could write something like this:
var myWonderfulLogo = driver.findElements(By.id("logo").above(By.id("nav")));
Assert.assertTrue(myWonderfulLogo.size() > 0);
If your logo isn't above the nav bar, findElements will have found no elements, so comparing if it's greater than 0 means that at least one thing was found. Hope that helps explain things.
Bonus food for thought: how will your test perform on different devices/screen sizes? With this new addition test developers will have even more to think about when testing responsive websites :)

Related

Selecting NOT the iframe

I have an element to find:
//div[contains(#class, 'bPageBlock')][.//*[.='Agreement Documents']]//div[#class='pbBody']//tr[contains(#class,'dataRow')]//a[text()='View']
The problem is, this finds two elements and I want only one. One is under an iframe and one isn't. It would be easy if I wanted the one under the iframe. I could do
//iframe///div[contains(#class, 'bPageBlock')][.//*[.='Agreement Documents']]//div[#class='pbBody']//tr[contains(#class,'dataRow')]//a[text()='View']
However, as you probably guessed, I want the one that is NOT under the iframe. I don't know how to specify something like //not iframe//. And even if I could, the // would find something one step lower or higher that was not an iframe.
Any suggestions how to find the one NOT under the iframe. The roots and paths other than that all seem to be the same with each other. I have tried lots of different things.
If you want to see the specific iframe:
<iframe frameborder="0" id="RLPanelFrame" name="RLPanelFrame" src="/emptyHtmlDoc.html" title="Hidden Frame - Ignore" style="height: 176px;"></iframe>
Then there are many sub-units under it with auto-generated meaningless IDs.
I don't think I can find each one using driver.find() and get the full xpath and check for "iframe" because selenium does not have an xpath extractor. I guess for each one I could keep getting the parent until I got to //html or //iframe but that would take a long time.
Any suggestions? This with Java and Selenium

Selenium findElement(s) issues when search for anchor tag

I have a few nav bar items that I am trying to find with driver.findElement(by.id("menu-news-menu-item")) and driver.findElements(by.id("menu-news-menu-item")). It can't find them for some reason. I have verified that the id is correct on the site but it still can't be found. I know there are other ways to get to the info, but it is my understanding that using the id is the best way to go about finding elements. Below I have included an HTML snippet of what I am trying to search for. If I need to provide any more information please let me know.
<div class="navbar-collapse collapse">
<li>
<a id="menu-news-menu-item" href="/novus/news">News</a>
</li>
</div>
From looking at your HTML I see one potential problem. There may be more.
The top level DIV you posted has a class navbar-collapse collapse. That indicates to me that that DIV is collapsible and is currently collapsed which means that any of its children will be hidden. Selenium was designed to allow the user to only interact with visible elements. This means that if you search for your A tag by ID and it's a child of the DIV that is currently collapsed, Selenium won't find it. What you need to do before you search for the A tag is to unhide it. I don't know for sure how to do this but it probably involves clicking the collapsible DIV.
With this info, try to figure the rest out on your own. You should be able to investigate the page HTML, try some code, and see what happens. If it doesn't work and you get stuck. Come back and post some more of the surrounding HTML, the code you tried, and the result (error messages, etc.) and we'll try to help you more.

Best way to find element with the same signature in Selenium?

I've been asked this question on a interview. What is the best way to find particular button on a page, from two identical buttons?
Two buttons like this on a page, i need to find the second.
button class="button-signin" name="btnlogin_login" value="Login" type="submit"> Sign In /button>
I've answered that I'd locate both and choose what I need from the list - they said that it's no good, because page can be changed. Can you suggest me the good way to do this?
If they said to find always the second element I would go for a solution like this:
List<WebElement> buttons = driver.findElements(By.class("button-signin"));
if(buttons.size() > 1){
buttons.get(1);
}
But what dont make sense for me is what they told you, than the page can change...Technically if the page change you should modify yours tests to make them easier to find elements save time and make them more readable, you cant do a test forever as a page is not forever.
Basically, the buttons must be child elements of some HTML elements.
Case 1: Parent are different, you can refer to each button by specify different parent e.g. cssSelector("#component1 > button") and cssSelector("#component2 > button").
Case 2: They have the same parent or path are identical, there are a couple options.
Use index such as XPath //button[2] or CssSelector button:nth-child(2)
Change the application, add something to differentiate the buttons
For case 2, I do think there is no point to have the 2 buttons which have the exactly same properties in the application. Personally, I prefer the option#2

selenium very similar xpaths?

I have two buttons on a page that have really similar xpaths -
the button im trying to click -
/html/body/div[#id='wrapper']/div[#id='content']/div[#id='contentarea']/div[#id='votecontent']/div[#id='votetext']/div[#id='voteboxes']/div[#id='votenow'][2]/form/input[2]
and the other button im trying to ignore -
/html/body/div[#id='wrapper']/div[#id='content']/div[#id='contentarea']/div[#id='votecontent']/div[#id='votetext']/div[#id='voteboxes']/div[#id='votenow'][1]/form/input[2]
the only difference between the two is the
[#id='votenow'][1]
and
[#id='votenow'][2]
but I can't figure out how to interact with the one that has the votenow[2], whichever way I go about it, it always seems to interact with the first one because that's the first one it finds
this is for java using the firefox driver, any suggestions would be great :)
Just find them both and get the desired one by index:
List<WebElement> buttons = driver.findElements(By.xpath("your xpath"));
WebElement secondButton = buttons.get(1);
First
Please talk to your developers! It is really bad practice to assign the same id to two different elements (in your case buttons) on the same page! It makes life for DEV and QA unnecessarily harder than it need be!
Second
The xpath-expressions you posted already contain the differentiation between these two buttons. So you just need to find the first one and click it.
via xpath:
You can use xpath - should be enough to search for the elements with id="votenow". As said before, you can be pretty precise in this case and already filter for the 2nd button:
WebElement button02 = driver.findElement(By.xpath("//div[#id='votenow'][2]/form/input[2]"));
button02.click();
via id:
As #alecxe already pointed out, you can also first go for a more general search of several elements and then filter the right one out. Personally I would use the id in this case:
List<WebElement> buttonWrappers = driver.findElements(By.id("votenow"));
// you want the button-input-element in the 2nd wrapper element, indexing starts at 0, so do this:
WebElement button02 = buttonWrappers.get(1).findElement(By.xpath("//input[2]"));
// since it seems there are several input elements below the desired div, you can use xpath again

Selenium selection By.Xpath in between a div

I am sure this question has been asked many times, and there are several examples of it. However, with there being so many different selection options, Id like the most clear cut way to handle selections of this sort. If the easiest way is not to use an xpath that is fine too.. Here is the puny HTML segment...
<div class="atcui-text atcui-align-right">SAVED DOCUMENTS</div>
I want to be able to identify it by the 'SAVED DOCUMENTS'.. what would be the easiest way to do this? Thank you!
You can do it with Xpath:
driver.FindElement(By.Xpath("//div[contains(#class, 'atcui-text') and contains(text(), 'SAVED DOCUMENTS')]"));
or if class is going to be dynamic then search only by text:
driver.FindElement(By.Xpath("//div[contains(text(), 'SAVED DOCUMENTS')]"));
This is c# hope you can translate it to java...

Categories

Resources