Cannot read checkbox when not ticked - node.js

I am trying to check if a checkbox is ticked but when the checkbox is not ticked it wont work and gives the error: "TypeError: Cannot read property 'PrivateCheck' of undefined" and points to the line with this code:
let isprivare = req.body['PrivateCheck'];
This is the check box I am trying to get input from
<form class="fileupload" action="upload" method="post" enctype="multipart/form-data">
<input type="checkbox" id="PrivateCheck" name="PrivateCheck" />
</form>
Thank you.

According to the HTML documentation the value of a checkbox is sent with the form only if it is checked. Otherwise nothing is sent for that checkbox.
In your particular case you can fix it like this:
let isprivate = req.body && req.body['PrivateCheck'];

Related

geb cant find checkbox element

there is this piece of code that provides a checkbox following from a link to the T&C.
<div class="checkbox accept_agreement">
<label class="control-label" for="step_data_accept_agreement">
<input type="hidden" name="step_data[accept_agreement]" value="0">
<input type="checkbox" name="step_data[accept_agreement]" id="step_data_accept_agreement" value="1">
<label for="step_data_accept_agreement">
<a target="_blank" href="/lanevillkor/">
<font><font>I agree, have read and stored the terms and conditions</font></font>
</a>
</label>
</label>
</div>
Now, I am working on a spock test using geb, and I try to retrieve the checkbox element
<input type="checkbox" name="step_data[accept_agreement]" id="step_data_accept_agreement" value="1">
to do so i have tried many things without the expected output. i was expected that something like
$("#step_data_accept_agreement").click() would be pretty straight forward but it is not. in the other side if I put $("[for='step_data_accept_agreement'] label").click() it clicks the link.
I tried to become as more specific but nothing looks to return the element correctly.
one of my last attempts was
termsAndConditionsOption(wait: true) { $("#step_data_accept_agreement", name: "step_data[accept_agreement]") }
and the error message, as in the other cases too, was in one sentence
element not visible
What do I miss?
So the solution was to use js to click the checkbox. The simpliest way is:
def agreeOnTermsAndConditionsAccept() {
String mouseClickEvt = """
var evt = new MouseEvent('click', {
bubbles: true,
cancelable: true,
view: window
});
arguments[0].dispatchEvent(evt);
"""
browser.js.exec(termsAndConditionsOption.firstElement(), mouseClickEvt)
}
Geb provides js support to work with javascript, as we find in the documentation. Also another link shows how to simulate a click event with pure javascript.
This was required as the checkbox cant be found as it is hidden. With javascript we can 'see' the position of the element and perform an action in the location that we want. iniMouseEvent can be used as well as it is documentanted here

How to check in CODED UI if text field is a required field?

I am new in coded UI, I just want to ask if there's any way to check if the text field in a web application is required?
HTML:
<div class="col-sm-7">
<input id="txt_title" type="text" name="Title" value="#Model.Asset.Title" class="form-control" required>
</div>
I would suggest doing this in two steps:
Get the control based on its ID
Ensure that the definition of the control contains "required"
Here's a code example:
HtmlTextArea requiredInput = new HtmlTextArea(browserWindow);
requiredInput.SearchProperties.Add("Id", "txt_title");
string controlDef = requiredInput.ControlDefinition;
Assert.IsTrue(controlDef.Contains("required"), "Test Failed: Input should be required");
Please mark as the answer if it accomplishes what you're saying. Thanks!

how to hide a buttons on page load using javascript

Im new to phone gap,developing an app when a i invoke my app for first time in index page one button should display that is configure the Ip address button .once after the configuring the IP Address, that address will be stored in local storage variable. so when i invoke second time of my application,so in index page their is not required of again configure button instead of that i'm checking the condition that stored variable is null nor not.if it is null again first button will display if not means other three button should display.
how to do that?? i'm not able to hide the button here
Here is my code
HTML:
<div onload="change">
<center> <button class="config" id="config" onclick='return config()'>Configure IP Address</button></center>
<center><input class="username" type="text" placeholder="Username" id="username" value="" ></center>
<center><input class="password" type="password" placeholder="Password" id="password" value=""></center>
<center><button id ="LOGIN" class="login" onclick="loginpostactivity()" >Login</button></center>
JS:
<script>
// Function being used to hide/unhide the button
function changeMessage() {
if (ipAddress!="") {
document.getElementById("config").style.visibility = "visible";
} else {
document.getElementById("username").style.visibility = "hidden";
document.getElementById("password").style.visibility = "hidden";
document.getElementById("LOGIN").style.visibility = "hidden";
}
}
</script>
When i load this page , i want to display the first button if the correct is true, else three button should display .
Perform the two steps below. It probably may solve the issue.
Make the uname pwd and login button hidden in if block
Make the config button visible in else block.

Disable empty search

I have a photography site driven in part by the 'Photoshelter' service, and I put an embedded search bar in my nav.
<form action="http://brettcole.photoshelter.com/search" method="get">
<input type="text" placeholder="search library" size="15" name="I_DSC">
<input type="submit" value="go">
<input type="hidden" name="I_DSC_AND" value="t">
<input type="hidden" name="_ACT" value="search">
</form>
It allows for a search to be executed with the no search term present, which then returns all 12,000 photos in my archive. Is there a best practice for preventing this, such that the user has to type something or nothing will happen when they click search?
It's also present on my advanced search page. This is generated by a search widget shortcode in the Photoshelter back end. I'd like to apply the same thing here, but not sure how the widgetization of it might affect the process.
Many thanks
You can use the onsubmit attribute of the form element to check if the user has entered information in any fields and then prevent submit based on that.
<script>
function checkValues() {
searchBox = document.getElementById("SearchField");
return searchBox.value != ""; // True will allow submission and false will prevent it
}
</script>
With this...
<form onsubmit="checkValues();" action="http://brettcole.photoshelter.com/search" method="get">
<input type="text" id="SearchField" placeholder="search library" size="15" name="I_DSC">
<input type="submit" value="go">
<input type="hidden" name="I_DSC_AND" value="t">
<input type="hidden" name="_ACT" value="search">
</form>
Should do what you need.
See also this answer: How to grab the onSubmit event for a form?
The actual search isn't working
From the contact page for example, it returns this
http://brettcolephotography.com/contact.html?I_DSC=red&I_DSC_AND=t&_ACT=search
the formula for my search returns is
http://brettcole.photoshelter.com/search?I_DSC=red&I_DSC_AND=t&_ACT=search
this search bar is present on all three of my web properties, personal site, blog, and photoshelter site, all three are tightly integrated to where you can't tell when you're switching between them. It needs to work regardless of where the search is being executed from. Thanks
Here is a function I wrote to disable the search form submitting if the search field is empty. It also focuses the cursor on the search field if the form is not submitted so that the user does not think that search is broken.
This is assuming that jQuery is loaded. Hope this helps!
var preventSearchIfEmpty = function() {
$('form[method="get"]').on( 'submit', function( ev ){
var query = $('input[type="text"]').val(),
queryLength = query.length;
if ( 0 === queryLength ) {
// Make the cursor blink so user is aware it's not broken, they need input to search
$('input[type="search"]').focus();
ev.preventDefault();
return;
}
});
}();

text box value to a javascript variable instantly after change

i want to type a numerical value in a text box and after i type it i want it to be assigned to a JavaScript variable instantly without submitting it
so the idea is
type a number
var x = that number instantly
Here is the html
<form id="tools">
<input type="text" id"stk"/>
</form>
now what is the javascript? :D
you can achive that assigning an a handler for the event (onKeyUp)... and that handler should assign the value to the variable. I suggest you to use jQuery instead of pure javascript.
With jQuery should look like this:
var x = 0;
$('#textbox-name').onkeypress(function({
x = $(this).attr('value');
}));
Sorry if I made a mistake with the syntax, but that's the main idea.
Your Jsp :
<form action="someAction" method="post">
<input type="text" name="text" id="text" />
<input type="text" name="value" id="value" />
</form>
Java Script needed is :
var myVar=setInterval(function(){getValue()},5000);
function getValue()
{
document.getElementById("value").value=document.getElementById("text").value;
}
By this type of JavaScript function the value of the text field will be received by JavaScript for every 5 Seconds . If you need instantly you can replace 5000 with 0. I think its little bit clumsy try to understand it

Resources