Tapestry5 Ajaxformloop limit number of rows - java

I am trying to limit the number of rows that a user can add in an ajaxformloop.
Short example:
For example, the loop found in the tapestry 5 documentation here: http://tapestry.apache.org/5.3/apidocs/org/apache/tapestry5/corelib/components/AjaxFormLoop.html
If for example I would only like the user to be able to enter 3 phone numbers, how can that be done?
What I have tried:
1) I tried returning null from the onAddRow event, this causes an exception and the exception report page to display - these events shouldn't return null I don't think.
2) I tried adding my own add row button like this:
<p:addRow>
<t:addrowlink>Add another</t:addrowlink>
</p:addRow>
And then putting a t:if around it, like this:
<t:if test="canAddMorePhones()">
<p:addRow>
<t:addrowlink>Add another</t:addrowlink>
</p:addRow>
</t:if>
In this case, the "add another" reverts to the default "Add row" button and my add row link doesn't show.
3)I tried moving that t:if inside the , this had similar results.
--------------------------
I am sure that this is a fairly common aim, is there any simple way to do it? Perhaps someone can provide an example, and if possible this can help to go in the documentation as i'm sure i'm not going to be the only one trying to do this.
Note: I did also ask on the T5 users mailing list and had one answer but I can't seem to get it working after the response from Lance (Which I am sure is probably correct, but i'm not sure how to use the AjaxResponseRenderer as per my reply last week, this is probably due to my own technical limitations or understanding of some parts of T5).
http://apache-tapestry-mailing-list-archives.1045711.n5.nabble.com/Ajaxformloop-add-row-link-max-size-tt5730840.html

I also tried using ajaxResponseRenderer.addRender as you did in your mailing list code, but it doesn't work because it seems that Tapestry has some problems dealing with updating a component that's busy updating another component. However, AjaxResponseRenderer also supports execution of JavaScript. Taking this approach on the AjaxFormLoop example in the docs, specify the addrowlink as follows:
<p:addrow>
<t:if test="canAddMorePhones()">
<t:addrowlink id="addRowLink" t:id="addRowLink">Add another</t:addrowlink>
</t:if>
</p:addrow>
Then add the following code right before return phone; in onAddRowFromPhones():
ajaxResponseRenderer.addCallback(new JavaScriptCallback() {
public void run(JavaScriptSupport javascriptSupport) {
if (!canAddMorePhones()) {
javascriptSupport.addScript("document.getElementById('addRowLink').style.display = 'none';");
}
}
});
This example was tested successfully in Tapestry 5.3.7.

Related

What command do I use to fix this problem?

My main goal is to create a program that can automate an android app to do everything you would with your fingers.
I wrote this line of code so that if that switch is turned ON, the code will switch it OFF.
driver.findElement(By.id("com.offerup:id/shipping_checkbox")).click();
But when I run my code again, that switch is now OFF by default.
I need a command that leaves the switch alone if it is already OFF.
Does anyone know what command to use?
You need to check whether the switch is ON or not. You could achieve this using the following code:
if(driver.findElement(By.id("com.offerup:id/shipping_checkbox")).isSelected()){
driver.findElement(By.id("com.offerup:id/shipping_checkbox")).click();
}
Updated:
As mentioned by Muzammil, isSelected() is not working as expected. Alternatively, you could use the checked attribute:
MobileElement shippingCheckbox= driver.findElement(By.id("com.offerup:id/shipping_checkbox"));
if(shippingCheckbox.getAttribute("checked").equalsIgnoreCase("true")){
shippingCheckbox.click();
}
There are 2 ways to check slider is on or off.
This is Node details of Slider.
Option-1: By using text
Here we will click only if slider is On.
MobileElement sliderElement=driver.findElement(By.id("com.offerup:id/shipping_checkbox"));
String sliderStatus = sliderElement.getText();
if (sliderStatus.equalsIgnoreCase("On")) {
sliderElement.click();
}
Option-2: By using Attribute
Here we will click only if slider is On.
MobileElement sliderElement=driver.findElement(By.id("com.offerup:id/shipping_checkbox"));
String sliderStatus=sliderElement.getAttribute("checked");
if (sliderStatus.equalsIgnoreCase("true")) {
sliderElement.click();
}
}
Edit: I made a mistake in reading this, and missed that it was an Android app, so Javascript is likely unavailable. This comment will only help someone with the same issue on web browser testing, or any situation where Javascript can be executed on the application.
Additional possibility for checking. Not that it is superior to other options.
using OpenQA.Selenium.Support.Extensions;
string selector = "com.offerup:id/shipping_checkbox";
if(driver.ExecuteJavascript<bool>($"return document.getElementById('{selector}').checked")) {
driver.findElement(By.id($"{selector}")).click();
}

Selenium Assert questions - Java

How are you?
I'm starting with selenium, and I'm trying hard to deal with "AssertTrue (boolean condition)", but there's something wrong.
I need the Junit gets green(ok) if I access successfully one page, and I want it get red(nok) if it's not possible to access.
But all I got is Green, every single time!
For example:
If I access the payment page it's ok (I assert that there's a buy button to be sure is the payment page)
If I try to access without being logged then the system displays an error message (If I do not find that buy button I won't be in the payment page)
So here's what I'm trying to do in the end of my public boolean method:
if(buybutton.isDisplayed()){
return true;
} else {
return false;
}
So in my test class I'm trying to assert:
Shoes is what I'm buying, and the Shoeslink.com is where I can find it. I put this two variables there because there's an IF - if the user wants to buy shoes selenium will expect some elements, if is a shirt selenium will expect others. But I guess it doesn't matter, because the assert is defined by the code I put above, right? And below is the assertcode I'm talking about:
assertTrue(buy.AccessProduct("**Shoes**", "**Shoeslink.com**"));
Well, if you guys have another logic or way to do it, that's all I need by now: A successfull test if I find an element or an error test if I don't.
Thank you!
I think you wrongly wrote parameters of assertTrue:
assertTrue("Error Message in case of false", buy.AccessProduct("**Shoes**", "**Shoeslink.com**"));

Wicket DropDownChoice NOT working correctly with PropertyModels

I have been trying to debug why my DropDownChoice in a simple form with just the DropDown and a Submit Button hasn't been working correctly for hours now.
It has a very weird behaviour. Where the first value selected in the drop down choice is sent successfully to the server after which any other choice select is not updated by the model. ie if I have a List persons, and I select the 2nd person, it submits this successfully. However, on select of another person and trying to submit it again it keeps showing the first selected option.
Snippets of code here:
ChoiceRenderer<Empowerment> empowermentChoiceRenderer = new ChoiceRenderer<>("name", "id");
final DropDownChoice<Empowerment> empowermentDropDownChoice =
new DropDownChoice<>("empowerment", new PropertyModel<Empowerment>(this, "empowerment"), empowermentList, empowermentChoiceRenderer);
empowermentDropDownChoice.setRequired(true);
add(empowermentDropDownChoice);
Only way I am able to get a decent behaviour is if I set the empowerment variable above to null. In this case, on submit the empowerment is reinitialised to null and then a new submit works correctly.
empowerment is just a JPA entity.
I'll be glad to know if this is a known issue. I experienced it in wicket 6.9.1 and wicket 6.12
Finally, found the solution to the problem. Above code is correct, but problem lied in the entity class itself - Empowerment needs to implement Equals and Hashcode correctly.
The DropDownChoice works just fine after this.
Add an OnChangeAjaxBehavior to your DropDownChoice. This will update the model value on every selection change you make on the drop down:
empowermentDropDownChoice .add(new OnChangeAjaxBehavior() {
#Override
protected void onUpdate(AjaxRequestTarget art) {
//just model update
}
});

jquery-mobile spinner loading

i tried to do that while js is creating a timetable... it shows spinner loading..
i tried to do this $.mobile.pageLoading(); $.mobile.pageLoading(true); and also tried to use this plugin http://contextllc.com/tools/jQuery-showLoading
The result is the same.. it shows spinner after he generate time table.. and i don't know why...here is where i tried to use it...
$('#timeDropList').change(function() {
$.mobile.pageLoading(false);
$('div.addedEntry').remove();
drawTemplate();
});
Are you using the right version of jQuery Mobile? Looks like they took away the pageLoading method in 1.0b1 in favor of two separate methods: showPageLoadingMsg and hidePageLoadingMsg. The last reference I see to pageLoading is in 1.0a4.1.
$('#home').live('pagebeforecreate',function(event){
alert('This page was just enhanced by jQuery Mobile!');
$.mobile.loadingMessage = "pagebeforeshow Page...";
$.mobile.showPageLoadingMsg();
});

Calling Java method from HTML link

I'm currently building a Twitter client in Java using the Twitter4J API. To create a Twitter "timeline", I am currently pulling data from Twitter such as profile images, tweets and usernames, then displaying them in a JTextPane, formatted using HTML. Code example below:
StringBuilder out = new StringBuilder();
try {
List<Status> statuses = HandleEvents.instance().twitter.getHomeTimeline();
out.append("<html>");
for (Status status : statuses)
{
out.append("<img src=\"").append(status.getUser().getProfileImageURL())
.append("\" width=30 height=30><b>").append(status.getUser().getName())
.append(":</b> ").append(status.getText())
.append("<br><br>");
}
out.append("</html>");
tweetsTextPane.setText(out.toString());
This displays a timeline of 20 tweets, separated by two line breaks. Under each tweet, I would like to place a simple hyperlink, called "Retweet", which calls one of my Java methods - HandleEvents.instance().twitter.retweetStatus(status.getId())
How would I got about doing this? Can the call be made directly between the tags, or do I have to make the call using JavaScript?
Any help would be appreciated. Many thanks.
You don't really need to have a hyperlink do you? Since it's a Swing app you could just add a JLabel that only looks like a hyperlink (but if you put in a little effort, it could behave like one as well). Add a listener for mouse clicks on that JLabel and you've can hook your current handler there.
On the other hand, if you do want actual HTML links, what you can do is implement your own HyperlinkListener.
Here are a couple of examples:
http://www.java2s.com/Tutorial/Java/0240__Swing/HyperlinkListenerExample.htm
http://www.java2s.com/Code/Java/Event/DemonstratingtheHyperlinkListener.htm
http://www.devx.com/tips/Tip/12997

Categories