hasRole() within th:remove is not working - java

I want to disable a particular link but retain its text, giving it the appearance of being disabled. I am trying to place a th:remove with a certain condition inside the anchor tag. I found this on the ThymeLeaf tutorial page:
Link text not to be removed
Based on that I'm trying to do this:
<li>
<a th:href="#{/config/mod/}"
th:remove="${#authorization.expression('hasRole(''VIEW_MODULE_STATUS'')')}? tag">
<i class="fa fa-gear"></i> [[#{webadmin.view.config.module.title.short}]]
</a>
</li>
where VIEW_MODULE_STATUS is the role. The condition does not seem to work and I can't understand why.
FYI: I have used sec:authorize="hasRole('VIEW_MODULE_STATUS')" in the anchor tag and it works fine. I want to avoid this approach because it completely removed the text and the link. Is there any other approach to have the link disabled and retain the text using ThymeLeaf?
(I am using ThymeLeaf 3.0)

There is a mistake in the ternary operator being used in the expressions th:remove="${#authorization.expression('hasRole(''VIEW_MODULE_STATUS'')')}? tag"
A ternary operator is of form condition? 'true' : 'false'. So you have to update your expression. Then you cannot disable an <a> tag, the only thing you can do is update its href attribute to be # or javascript:void(0); so that it doesn't have any action. You could do it as shown below:
th:href="${#authorization.expression('hasRole(''VIEW_MODULE_STATUS'')') ? '/something' : '#'}"

Related

Selenium tooltip text verification

I'm trying to verify the tool tip text using selenium for the following HTML code.But not sure how to proceed further.
HTML CODE:
<div id="divImgAnnualAllowanceType" class="imgHelp" _tooltip="If the client
<br>an arrangement
<br>a durable arrangement
<br>received a Lump Sum.">
</div>
Since the text is separated by <br> I don't know how to retrieve this text.
I tried using the following code but got null value.
driver.findElement(By.id("divImgAnnualAllowanceType")).getAttribute("value");
Thanks in Advance :)
<div> tags doesn't (necessarily) has value attribute, use getText() instead
driver.findElement(By.id("divImgAnnualAllowanceType")).getText();
You should specify the attribute which value you need.
driver.findElement(By.id("divImgAnnualAllowanceType")).getAttribute("_tooltip");
You have to use proper attribute name with getAttribute() method and there is no attribute as value in your targeted <div> element, you can try below code:
String tooltip = driver.findElement(By.id("divImgAnnualAllowanceType")).getAttribute("_tooltip");
System.out.println(tooltip.replace("<br>", ""));
In above code, we are storing the tooltip value in a variable then printing the same after replacing all <br>.
This code is written in Java.

Using Thymeleaf to create links that open in a new window/tab

In basic HTML, a link can have the target="_blank" property to force it to open in a new window or tab. But if when I put that in an <a href> tag that also includes a th:href for Thymeleaf, Thymeleaf overwrites the whole tag and wipes out my target="_blank.
I've considered the brute force method of adding target="_blank" to every link as it is stored in my database so that it is already part of the link when Thymeleaf writes it out. But I would prefer a way to have Thymeleaf write the target="_blank" property as it is writing the <a> tag.
Thymeleaf overwrites the a tag. Use th:target="_blank".

How to use anchor tag to find a fixed position in the current jsp in Spring MVC?

I just want to location my html tag in my current jsp page,it means that I do not need to jump to the controller.Now I use the code,but it always jump to the controller,how to change my code?
My website url is http://localhost:8888/MySpringMVC/,and I have a div called headdiv in the top.When user click the link,I want to locate it to the headdiv.My current web page url is http://localhost:8888/MySpringMVC/main/index. I want to jump to http://localhost:8888/MySpringMVC/main/index#top-container
<!--both of ways jump to http://localhost:8888/MySpringMVC/#top-container-->
<a href="javascript:void(window.location.href='#top-container')">
<a href="#top-container">
The second thing you tried should work (TEST), works in my tests which means there is probably more to it than you wrote in your question.
Anyway, here is a small javascript solution that should work
TEST
Try
<a href="#top-container" onClick="return false;">

Xpath for checkbox element by locating it's following label element by value

I have been working for quite a while on this and still haven't found an answer specific to my problem in stackoverflow or from experimenting with the Xpath myself. I am quite inexperienced so I alpogise if this is a simple problem but I would really appreciate any help.
I am working with Selenium to test a web app that uses Wicket. I need the Xpath to the checkbox that correlates to the respective label. This is because I need to be able to enter the value shown on the label and for it to find the relevant checkbox based on the label text such as "001", as the checkbox ids do not match the values.
Mockup below shows the checkboxes and their corresponding labels;
The corresponding HTML is show below;
<span wicket:id="excludeDepotCheckBox" id="excludeDepotCheckBox5">
<input name="adminPreferenceSection:excludeDepotCheckBox" type="checkbox" value="0" id="excludeDepotCheckBox5-adminPreferenceSection:excludeDepotCheckBox_0">
<label for="excludeDepotCheckBox5-adminPreferenceSection:excludeDepotCheckBox_0">001</label>
<br>
<input name="adminPreferenceSection:excludeDepotCheckBox" type="checkbox" value="1" id="excludeDepotCheckBox5-adminPreferenceSection:excludeDepotCheckBox_1">
<label for="excludeDepotCheckBox5-adminPreferenceSection:excludeDepotCheckBox_1">009</label>
<br>
</span>
Another problem I also face is that the Xpath must include the fact that it is inside the span shown in the html as there are 3 other groups of checkboxes on the page with the same values so it must be specific for each span for example:
id="excludeDepotCheckBox5"
I have tried the following Xpaths to no avail;
//span[#id='excludeDepotCheckBox5' and contains(., '009')]"
"//*[#id='excludeDepotCheckBox5' and ./label/text()='009']/preceding-sibling::*[#name='adminPreferenceSection:excludeDepotCheckBox']
//*[#id='excludeDepotCheckBox5' and ./label/text()='009']/preceding-sibling::input[1]"
Again I aplogise if it is a simple syntax/understanding problem but I would really appreciate any help.
Since "preceding-sibling" is so error-prone (it will break as soon as the HTML structure changes a little bit), here's a more stable variant (wrapped for legibility):
//span[#id = 'excludeDepotCheckBox5']//input[
#id = //span[#id = 'excludeDepotCheckBox5']//label[normalize-space() = '001']/#for
]
You can use the below xpaths:
1- For checking the checkbox related to label '001':
//span[#id='excludeDepotCheckBox5']/label[.='001']/preceding-sibling::input[1]
2- For checking the checkbox related to label '009':
//span[#id='excludeDepotCheckBox5']/label[.='009']/preceding-sibling::input[1]
NOTE: It will check for the 'input' element which is the first preceeding sibling of label element with exact innerHTML/text as '001' or '009' under a span element with id='excludeDepotCheckBox5'.
//*[#id='excludeDepotCheckBox5']/label[contains(text(),'001')]//preceding-sibling::input[1]

Writing cssselector expression for webDriver using attribute matching

I am a newbie to css, html, etc and thus cssselectors. I have been messing around with selenium webdriver and am trying to .click() the following button:
<button title="" class="btn addWidgButt bt-block" href="" role="button" type="button" context="UNIQUE_THING">
<span class="btn-text">UNIQUE THING</span></button>
According to http://www.w3.org/TR/css3-selectors/#selectors shouldn't the following selector work? (Rule is E[foo="bar"] for element E
By.cssSelector("button title[context=UNIQUE_THING]")
By.cssSelector("btn.addWidgButt.bt-block[context=UNIQUE_THING]")
By.cssSelector("span[btn-text=UNIQUE THING]")
I think the last one is my best bet, as span is an element and btn-text is an attribute value. Any help is appreciated, thanks.
One thing I noticed is that all of your selectors in [] are missing single quotes.
By.cssSelector("button[context='UNIQUE_THING']")
By.cssSelector(".btn.addWidgButt.bt-block[context='UNIQUE_THING']")
By.cssSelector("span.btn-text")
Your second selector is missing the leading .
Your last selector will not work, as you cannot match text with CSS Selectors. You can match a class with ., or id with #.
For more information, this is an excellent reference: http://www.w3schools.com/cssref/css_selectors.asp

Categories

Resources