Property Not Found On Type - java

I'm trying to use this in JSF to dynamically show the title of a page.
<h:panelGroup rendered="#{not empty searchBean.fsResultsTitleOne}"><h2>#{msgs.fireStudySearchTitle}</h2></h:panelGroup>
And I'm getting this error:
rendered="#{not empty searchBean.fsResultsTitleOne}": Property 'fsResultsTitleOne' not found on type
However, I did define it in on the type like this:
private String fsResultsTitleOne;
public String getFSResultsTitleOne(){return fsResultsTitleOne;}
public void setFSResultsTitleOne(String newValue){fsResultsTitleOne = newValue;}
And set it to something:
setFSResultsTitleOne("I'm not Empty!");
And even used this to make sure it would be set:
System.out.println("This is the FS Results Page Title: " + fsResultsTitleOne);
And it seems to be working:
This is the FS Results Page Title: I'm not Empty!
Am I setting it wrong someplace?

Change
getFSResultsTitleOne
setFSResultsTitleOne
to
getFsResultsTitleOne
setFsResultsTitleOne

The way how JSF accesses the properties in your code is it adds a "get" to the property name with the first letter of the property in caps.
E.g. If you write in the xhtml page as -
value="#{myBean.name}"
Good coding style says you must have private property with respective getters and setters. So JSF parser in order to access the property will convert the request as following-
value = myBean.getName()
Note that the N is in caps.
So if you mess with the name of the property as you did, the parser will be happy enough to throw a PropertNotFoundException.

Related

tag <c:forEach/> si not doing anything

I am trying to show through JSP code the value of some variables stored in in ArrayList money.
I am using Java for this project.
The thing is that when I try to do:
<c:forEach var="pos" items="${yourGame.money}">
<c:out value="${pos.nombre}"/>
${pos.nombre}: <b>${pos.precio}</b>
<br/>
</c:forEach>
It shows nothing, and I have made sure that yourGame.money isn't empty so I don't know what's going on. I am new to JSP and have run out of ideas; could anyone please help me?
This are the structures I am using:
public class Juego{
String nombre;
String plataforma;
String edicion;
ArrayList<Precios> money = new ArrayList();
}
public class Precios{
String precio;
String url;
String nombre;
}
The JSP class recieves a "Juego" object, and I am trying to print the values of the "money" atribute. I know the class JSP is recieving the "Juego" object fine since I have already printed some other variables of this class such as :
<h3>The ultimate edition: ${yourGame.edicion}</h3>
And it works just fine ...enter code here
Please modify your variables to use private access and generate get and set methods for all of them. The issue will be resolved.
Can you cross check your controller/Action classes and ensure you are setting money attribute in this yourGame bean and this bean is stored in request or session properly.
This is the only place where I suspect, you might have missed something. Apart from this you jsp looks fine to me.
One more point to check, can you try to print like this
<c:out value="${yourGame.edicion}"/>
just wanted to make sure the core tag is working fine in your jsp.

Getting value of a class with getAttribute method

I'm looking for a way to get the value of the class (for my example "AAABC") stored in a variable. I tried different key words with the getAttribute method, but none were successful. Key word "class" obviously gave me "gwt-Label", all the other key words gave me "null".
Using getAttribute is not necessary, if you can think of an other elegant way.
Example:
<div class="gwt-Label">AAABC</div>
driver.findElement(By.xpath("//div[#class='gwt-Label']")).getText();
This is solve your issue.
First you need to do the following to get the string from your class object:-
String example = object.toString();
// here in msg you will get the whole string < div class="gwt-Label"> AAABC< /div>
Now you can use approach like below to get your string:-
example = example.substring(example.indexOf(">") + 1);
As per the HTML to retrieve the class attribute you can use the following line of code :
String myClass = driver.findElement(By.xpath("//div[text()='AAABC']")).getAttribute("class");

Selenium/ Java how to verify the this complex text on page

I want to verify below text(HTML code) is present on page which as // characters , etc using selenium /jav
<div class="powatag" data-endpoint="https://api-sb2.powatag.com" data-key="b3JvYmlhbmNvdGVzdDErYXBpOjEyMzQ1Njc4" data-sku="519" data-lang="en_GB" data-type="bag" data-style="bg-act-left" data-colorscheme="light" data-redirect=""></div>
Appreciate any help on this
I believe you're looking for:
String textToVerify = "some html";
boolean bFoundText = driver.getPageSource.contains(textToVerify)
Assert.assertTrue(bFoundText);
Note, this checks the page source of the last loaded page as detailed here in the javadoc. I've found this to also take longer to execute, especially when dealing with large source codes. As such, this method is more prone to failure than validating the attributes and values and the answer from Breaks Software is what I utilize when possible, only with an xpath selector
As Andreas commented, you probably want to verify individual attributes of the div element. since you specifically mentioned the "//", I'm guessing that you are having trouble with the data-endpoint attribute. I'm assuming that your data-sku attribute will bring you to a unique element, so Try something like this (not verified):
String endpoint = driver.findElement(
new By.ByCssSelector("div[data-sku='519']")).getAttribute("data-endpoint");
assertTrue("https://api-sb2.powatag.com", endpoint);

How to convert string to By type

How to convert String to By type.
Following is my scenario:
Keep object identification in Properties file in below manner
username=By.id("username")
password=By.id("password")
In the application i would like to retrieve the values like
Properties prop=new Properties();
prop.load("above properties file path")
driver.findelement(prop.getProperty("username")) //Here in eclipse it is complaining saying "The method findElement(By) in the type WebDriver is not applicable for the arguments (String)"
So can somebody help me in this?
I can use like below or some other format, but i want solution for the above
username="//*[#id='username']"
username="username"
driver.findElement(By.xpath(prop.getProperty("username"))
driver.findElement(By.id(prop.getProperty("username"))
You can create one parser method which will return desired locator object something like below:
public static By locatorParser(String locator) {
By loc = By.id(locator);
if (locator.contains("id"))
loc = By.id(locator.substring(locator.indexOf("\"") + 1,
locator.length() - 2));
else if (locator.contains("name"))
loc = By.name(locator.substring(locator.indexOf("\"") + 1,
locator.length() - 2));
if (locator.contains("xpath"))
loc = By.xpath(locator.substring(locator.indexOf("\"") + 1,
locator.length() - 2));
return loc;
}
Can be called in your code in the following way:
driver.findElement(locatorParser(prop.getProperty("username")));
Added logic for id, name, xpath. You can modify the same method to add all the available locators. Hope this helps!
The WebDriver.findElement method accepts only an object parameter of the type By.
The Property.getProperty method returns only a String typed object.
Therefore, this may be what fits your need:
WebElement element = driver.findElement(By.name(prop.getProperty("username")));
You can't force a String typed object into a method that accepts only a By typed object. When you ask Selenium to find a String "username" you have to tell it more than just the string's value.
The method By.[method] you choose all depends on what you are looking for in the page that Selenium is searching. "username" is most likely the "name" (By.name) or "id" (By.Id) of the field you are looking for. The By class refines the search to where you expect the String "username" to be: in a name, id, tag, class, etc. See the By class definition.
Also, take caution as the getProperty method could return a null, and the By methods with throw an IllegalArgumentException if you pass it a null string. So providing a default return value ("") for getProperty is usually safer.
WebElement element = driver.findElement(By.name(prop.getProperty("username", "")));
You have to evaluate the entire expression within the context of existing code. You should framework such as JEXL or expressionoasis
Code below uses JEXL
// Create JexlEngine instance
JexlEngine jexl = new JexlEngine();
// Create Expression object for the string
Expression e = jexl.createExpression(prop.getProperty("username"));
// Create a context. You can pass reference to any object you want to use inside the expression
JexlContext jc = new MapContext();
// Now evaluate the expression, getting the result
driver.findElement((By)e.evaluate(jc));
I think you are trying to implement page object model using properties file. What I would suggest here to use xml file instead of java properties file. for example sample xml for page elements would look like below.
<Pages>
<LoginPage>
<txtUserName by="id">username</txtUserName>
<txtPassword by="id">password</txtPassword>
</LoginPage>
</Pages>
Now you can write methods retrieve nodes from the xml file and node attribute as id,xpath etc... further write methods to find elements using xml node value and attribute. I have used same method in my project as it works great for me.

Unable to bind value from java bean to jsp using jstl

I have a String getter and setter that i am setting in my bean.
I am trying to get the value in my jsp using jstl like this :
<jsp:useBean class="com.test.MyBean" id="results" scope="request"/>
<script type="text/javascript">
function setMyFields(){
var flag="<c:out value='${results.sdateFlag}'/>";
alert(flag);
var text_box = document.getElementById('mySelectedDate');
if(flag=="true"){
text_box.setAttribute('disabled', 'disabled');
}
}
window.onload = setMyFields;
</script>
I have imported jstl core as well in my jsp.
But when i do this i get an error like this :
javax.servlet.ServletException: Unable to find a value for "sdateFlag" in object of class "com.test.MyBean" using operator "."
at org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:660)
at com.ibm._jsp._pageMyAmount._jspService(_pageMyAmount.java:306)
at com.ibm.ws.jsp.runtime.HttpJspBase.service(HttpJspBase.java:87)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.service(ServletWrapper.java:1101)
at com.ibm.ws.webcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:569)
at com.ibm.ws.wswebcontainer.servlet.ServletWrapper.handleRequest(ServletWrapper.java:478)
at com.ibm.wsspi.webcontainer.servlet.GenericServletWrapper.handleRequest(GenericServletWrapper.java:122)
at com.ibm.ws.jsp.webcontainerext.AbstractJSPExtensionServletWrapper.handleRequest(AbstractJSPExtensionServletWrapper.java:226)
at com.ibm.ws.webcontainer.webapp.WebAppRequestDispatcher.forward(WebAppRequestDispatcher.java:321)
But ihave been debugging the java class using the java debugger and it seems the value is getting set. Then why is it not getting the value ?
EDIT : These are the setter and getter of my bean :
public void setDateFlag(String b) { sDateFlag = b; }
public String isDateFlag() { return sDateFlag; }
What's wrong here ? Am i missing something ?
You're treating the dateFlag as a boolean property, but it is in fact a String. So you "getter" should be called getDateFlag, not isDateFlag. As mentioned, the isPropertyName syntax is only applicable to properties of type boolean.
Also, <c:out value='${results.dateFlag}'/> isn't really needed. You should be able to simply do ${results.dateFlag}.
So , i got the problem fixed and it was a bizarre solution. The problem it seems is, any variable declared must follow java standards. And my problem was , i had given the variable name as sDateFlag in my original proprietary code, which does not follow the java naming conventions. When i removed it and gave it as flag , it started working. I R&D ed the answer using this link here : JSTL . Though it's not fully correct, it led me to the root cause of my problem. And i will like to convey my thanks to all of you guys , who have replied to me, to make the process easy .

Categories