JSTree Generating Links that are not usable in JSP page w/ Struts2 - java

I have a tree generated with JSTree in my JSP page (part of a Struts2 webapp) as follows:
<div class="panel">
<div id="demo1" class="demo">
<ul>
<li id="node"><s:property value="product"/>
<ul>
<li id="node">
Dependents
<ul>
<s:iterator value="dependentsList" id="dependent">
<li id="node">
<a href="#">
<s:property value="productName"/></a>
<ul>
<li>
<a href="#">
Version Number: <s:property value="version" />
</a>
</li>
<s:if test="documentationLink != ''">
<li>
<a href="<s:property value="documentationLink" />">
Link to Product Documentation
</a>
</li>
</s:if>
</ul>
</li>
</s:iterator>
</ul>
</li>
</ul>
</li>
</ul>
</div>
<script type="text/javascript" >
$(function () {
$("#demo1").jstree(
{
"core" :
{
"initially_open" : [ "#node" ]
},
"themes" :
{
"theme" : "default",
"icons" : false
},
"plugins" : [ "themes", "html_data", "ui"]
});
});
</script>
Thing is the links to product documentation show up on my browser (I tried using both Firefox and Internet Explorer) on the bottom status bar, but when I click on them, nothing pops up (I checked my HTML source code, and the anchor tags are showing the correct URL links). Could anyone figure out why my links aren't working? I am using both the latest versions of JQuery and JSTree. Thanks!

I got the links to work after removing the UI plugin, I guess the UI plugin doesn't like to work with HTML links...

Related

Is there a way in Selenium to overcome Safari browser's same origin policy in click on a button within iFrame?

I'm trying to click on a Submit button within an iFrame. Trying to switch with iFrame id gives "Blocked a frame with origin from accessing a cross-origin frame" error.
<iframe id="challengeFrame" name="challengeFrame" width="250" height="400">
<html>
<body>
<form id="capture" class="challenge-form" action="capture" method="POST">
<div class="column">
<ul class="options">
<li>
....
</li>
<li>
...
</li>
</ul>
</div>
<div class="column">
<input type="submit" class="button" value="Submit">
</div>
</form>
</body>
</html>
</iframe>
In the above iFrame I need to click on the submit button. Please suggest any method with example to achieve this in Selenium Java/JS framework.
Be sure to include in your chrome options this argument:
options.add_argument("--disable-web-security")
This should disable the CORS check.

How to hide a tab in java based on the user role

In my application I have three listitems File Upload,View Reports and Change Password. I need to hide View Reports if the login is by normal user and i need to display View Reports if the login is by manager. Below is the code of my tabs
Code in master page-
<div class="wrapper">
<div class="menu">
<div class="menu">
<ul id="drop-nav">
<li id="MenuFileUpload" runat="server">File Upload
<ul>
<li>View Uploaded Data</li>
</ul>
</li>
<li id="MenuReports" runat="server">View Reports<ul>
<li>Report1</li>
<li>Reports2</li>
<li>Report3</li>
</ul>
</li>
<li id="lichangepwd" runat="server"><a id="A1" href="ChangePassword.jsp" runat="server">Change Password</a></li>
<div style="width:auto;float:right;font-weight:bold;padding-right:0cm;">
<li id="lilogout"> Logout</li></div>
<div style="float: left;text-align:right; width: 64%;">
<asp:Label ID="lblmsg" runat="server" ForeColor="Red" BorderColor="White" Text="Label" Font-Size="Small"></asp:Label> </div>
</ul>
</div>
</div>
code in login page-
if(role.equals("Supervisor"))
{
reports.visible=false;
}
I had tried by making reports.visible=false; which we will do in .net but its not working in java
Well basically, you pass some dto that represents logged user into the view.
Dto (Data transfer object) in most cases contains (login, firstname, lastname and role). Then in the code you do some thing like this:
<c:if test="${loggedUserDto.getRole() eq 'ADMIN'}">
<li id="MenuReports" runat="server">View Reports<ul>
....
</li>
</c:if>
If you use spring security :
<%# taglib prefix="sec" uri="http://www.springframework.org/security/tags" %>
<sec:authorize access="hasRole('ADMIN')">
<li ....>
</sec:

How to print the all li items text of dropdown using Selenium WebDriver?

How I print the all li items text using selenium webdriver.
Here I want to some type of single line code that print all li items text like
Jackets,
protective etc. May by using for loop
<div class="dropdown change_right">
<ul>
<li>
<a class="" title="Jackets" href="/collections/jackets">Jackets</a>
</li>
<li>
<a title="Jackets protective" href="/collections/jackets/jackets-protective" class="sub-link"> protective</a>
</li>
<li>
<a title="casual leather" href="/collections/jackets/casual-leather" class="sub-link">casual leather</a>
</li>
<li>
<a class="" title="T-shirts & shirts " href="/collections/t-shirts-shirts">T-shirts & shirts </a>
</li>
<li>
<a title="shirts" href="/collections/t-shirts-shirts/shirts" class="sub-link">shirts</a>
</li>
<li>
<a title="crew-neck" href="/collections/t-shirts-shirts/crew-neck" class="sub-link">crew-neck</a>
</li>
<li>
<a title="polo tshirts" href="/collections/t-shirts-shirts/polo-tshirts" class="sub-link">polo tshirts</a>
</li>
<li>
<a class="" title="Trousers" href="/collections/trousers">Trousers</a>
</li>
<li>
<a title="Trousers Protective" href="/collections/trousers/trousers-protective" class="sub-link"> Protective</a>
</li>
</ul>
Can be done with a simple findElements(). With the help of xpath to grab the collection of elements and a simple iterate through the collection and print the value with the help of getText()which returns the string value of the element.
List<WebElement> allText = driver.findElements(By.xpath("//*[contains(#class,'dropdown')]//a"));
for ( WebElement element: allText) {
System.out.println(element.getText());
}
Note: untested code. You may need to make sure click and open the list happens before executing this code and some wait might also be necessary as well

How to retrieve atomic values inside tags in HTMLUnit

I am new to HtmlUnit and I don't know how to get the text inside the [...]
A part of my html file:
<ul ......somethin....>
<li data-role="list-divider" role="heading" style="font-size:16px;" class="ui-bar-f">
INFORMATION_LINE_1
</li>
<li data-theme="d" class="ui-li ui-btn-icon-right ui-btn-up-d ui-odd-match-column ">
<div class="ui-btn-inner ui-li">
<div class="">
<div class="ui-btn-text">
<a href="/x/cxntay/13113/ndzvsssl/g1" class=" ui-link-inherit ui-link-hover">
<h3 class="ui-li-heading">
<span class="xheader">INFORMATION_LINE_2</span>
<span class="label live">INFORMATION_LINE_3</span>
</h3>
<div class="ui-live-scores">
<span class="team1-scores">
<span class="ui-team-name">INFORMATION_LINE_4</span>
<span style="font-weight:bold">INFORMATION_LINE_5</span>
</span>
<span>INFORMATION_LINE_6</span>
</div>
</a>
</div>
</div>
</div>
</li>
</ul>
Now, I want to retrieve "INFORMATION_LINE_X"(1,2...6) in between these tags..
This is what I tried:
List<HtmlUnorderedList> ls = (List<HtmlUnorderedList>) page.getByXPath("/ul");
List<DomNode> dls = ls.get(0).getChildNodes();
System.out.println(dls.get(0).getFirstByXPath("//li[#data-role='list-divider']/text()");
I just tried to get INFORMATION_LINE_1
But it printed null. I need to get all the INFORMATION_LINES.
It is better to use just XPath rather than mixing it with HTMLUnit methods. Something like this should work to get you the first information line:
HtmlElement e = page.getFirstByXPath("//li[#data-role='list-divider']");
System.out.println(e.asText());
In order to fetch the other information lines you should follow the same approach but changing the XPath string.
Bear in mind you should always debug the page by taking a look at the code by printing the output of page.asXml(). If you use a real browser you are not actually seeing exactly the same as HTMLUnit is seeing. You can stumble with differences particularly if the page executes JavaScript.

if else statement not working jsp

Is there anything wrong with my code? My if else statement is not working.
<%
String pages = request.getParameter("page");
%>
<input type="hidden" id="page" value="<%=pages%>"/>
<div class="navbar navbar-fixed-bottom">
<div class="navbar-inner">
<a class="brand" href="index.jsp">PeachMangoPie</a>
<ul class="nav pull-right">
<li <%if(pages=="papoy"){out.write("class='active' ");}%>>Papoy</li>
<li class="divider-vertical"></li>
<li <%if(pages=="chuckie"){out.write("class='active' ");}%>>Chuckie</li>
<li class="divider-vertical"></li>
<li <%if(pages=="dutchmill"){out.write("class='active' ");}%>>Dutch Mill</li>
<li class="divider-vertical"></li>
<li <%if(pages=="icecream"){out.write("class='active' ");}%>>Ice Cream</li>
<li class="divider-vertical"></li>
<li <%if(pages=="chicken"){out.write("class='active' ");}%>>Chicken</li>
<li class="divider-vertical"></li>
<li <%if(pages=="straberrycake"){out.write("class='active' ");}%>>Strawberry-Cake</li>
</ul>
</div>
</div>
You need to use the .equals method to test for string equality.
Try this instead:
<li <%if(pages.equals("papoy")){out.write("class='active' ");}%>>Papoy</li>
To fix your code: use equals to compare Strings (as shown in Harshal Pandya's answer).
To really fix your code: stop using scriptlets as explained here: How to avoid Java code in JSP files?. Rewrite all this using EL, JSTL and JavaScript. I wrote a basic example on this to omit all that hard-to-maintain scriptlet and HTML code:
<script type="text/javascript">
window.onload = function() {
var page = '<c:out value="${param.page}" />';
var ulOptions = document.getElementById('ulOptions');
var liItems = ulOptions.getElementsByTagName("li");
for(var i = 0; i < liItems.length; i++) {
var liItem = liItems[i];
var a = liItem.getElementsByTagName("a")[0];
if (a !== undefined && a.innerText.toLowerCase() === page) {
liItem.setAttribute("class", "brand");
}
}
};
</script>
<input type="hidden" id="page" value="${param.page}"/>
<div class="navbar navbar-fixed-bottom">
<div class="navbar-inner">
<a class="brand" href="index.jsp">PeachMangoPie</a>
<%--
provided id for ul HTML component in order to ease
JavaScript onload function development
--%>
<ul id="ulOptions" class="nav pull-right">
<li>Papoy</li>
<li class="divider-vertical"></li>
<li>Chuckie</li>
<li class="divider-vertical"></li>
<li>Dutch Mill</li>
<li class="divider-vertical"></li>
<li>Ice Cream</li>
<li class="divider-vertical"></li>
<li>Chicken</li>
<li class="divider-vertical"></li>
<li>Strawberry-Cake</li>
</ul>
</div>
</div>
Explanation of the provided code:
${param.page} replaces request.getParameter("page");
window.onload = function() { ... }; defining a JavaScript function to execute after loading the page (more info: onload Event).
<c:out> will print text directly on the generated HTML. More info: <c:out>.
document.getElementById returns an HTML element by its id (the function name explains this pretty straightforward).
getElementsByTagName returns a NodeList that contains all the inner HTML elements with the corresponding tag name (the function name explains this pretty straightforward).
for(var i = 0; i < liItems.length; i++) traverse all the <li> elements inside ulOptions.
liItem.getElementsByTagName("a")[0] as explained before, get the <a> element in position 0.
a !== undefined && a.innerText.toLowerCase() === page verifying that a exists (this for those <li> without the <a> element inside) and verifying the inner text against the page request parameter.
This is better approach (without scriptlets):
<li <c:out value="${param.page=='papoy'?'class=active':''}"/>">
Papoy
</li>

Categories