I would like to verify the partial value of a string I get from a web page. I give an example the string is "Incident 1946721 Updated" and I would like to insert a check that verifies that the two words Incident are present as prefix and Updated as suffix. how can I do?
the html code where the text is present this is:
<div class="modal-body"><button type="button" class="bootbox-close-button close" data-dismiss="modal" aria-hidden="true" style="margin-top: -10px;">×</button><div class="bootbox-body">Incident 1946721 Updated</div></div>
Use below code :
String value = driver.findElement(By.xpath("//div[#class='bootbox-body']")).getText();
if(value.startsWith("Incident") && value.endsWith("Updated")) {
System.out.println("Test Pass");
}else {
System.out.println("Test Fail");
}
Let me know if you have further query.
Related
I'm trying to create a fileupload. My file input:
<input class="form-control" name="file" type="file" size="20" accept="application/pdf, image/jpeg, image/png, text/plain" multiple >
And this for-loop in the servlet iterate over the Parts to get their total size. It iterate over the selected files and a additional element with size 1 and content type null. This cause an errror.
for (Part part : request.getParts()) {
System.out.println("Dateigrösse: " + part.getSize());
uploadSize = uploadSize + part.getSize();
System.out.println("Dateityp: " + part.getContentType());
if(part.getContentType().equals("application/pdf") || part.getContentType().equals("text/plain") || part.getContentType().equals("image/jpeg") || part.getContentType().equals("image/png")) {
} else {
types = false;
}
}
First it worked, but know there's this null element. What should I do? Where does it comes from? I don't change anything. I'm using the code from oracle EE7 doc and a famous online article. I don't change anything before this for-loop.
Thank you in advance.
The getParts() returns all named Inputs too. So it returns me the choosen subject as a Part. Now i'm filtering for inputs named "file".
Okay so I have two Java Servlets, one for letting the user select which images to delete (DeleteImages) and another for actually deleting the images (HandleDelete). DeleteImages displays all the images in the container with a checkbox HTML form for the user to select which images to delete. Then, using POST the servlet passes that information along to HandleDelete which iterates over which images it received and deletes them.
I actually had this working, but then I tried to change the structure of the code (have DeleteImages forward to a .jsp file that output the HTML form which would then forward to HandleDelete) but that didn't work out, so I'm trying to go back to the old way and now it's not working even though I'm pretty sure it's the same as I had before.
From DeleteImages:
// retrieve image files
List<? extends SwiftObject> objs = os.objectStorage().objects().list("imageFiles");
out.println("<!DOCTYPE html><html><head><title>Object Storage - Delete</title><link rel='stylesheet' href='stylesheet.css' type='text/css' /></head>"
+ "<body>
<h1>Select images to Delete from container</h1>
<form method='POST' enctype='multipart/form-data' action='/ImageUpload/OSHandleDelete'>");
for (SwiftObject o : objs) {
// omitted code that gets the image's name, date last modified, and filepath (all Strings, I know this works)
out.println("<b>Name:</b> " + name + "<br/> <b>Time of Upload:</b> " + date + "<br/>"
+ "<input type='checkbox' name='"+name+"'> "
+ "<img src='"+filepath+"' alt='' style='max-width:800px;' /> <br/> <br/> <br/> ");
}
out.println("<input type='submit' value='Delete Images' /></form>");
out.println("</form> <br/> <br/> <div><br/><a class='return' href='index.jsp'><b>Click here to return home</b></a><br/><br/></div> <br/> <br/></body></html>");
From HandleDelete:
Enumeration<String> parameterNames = request.getParameterNames();
if (!parameterNames.hasMoreElements())
System.out.println("no parameters (null)");
while (parameterNames.hasMoreElements()) {
String paramName = parameterNames.nextElement();
System.out.println("*****************");
System.out.println("parameter: " + paramName);
String[] vals = request.getParameterValues(paramName);
if (vals == null)
System.out.println(" vals is null for " + paramName);
else {
for (int i = 0; i < vals.length; i++) {
System.out.println(" vals["+i+"]: " + vals[i]);
}
}
Right now I don't have HandleDelete actually doing anything besides print statements. This is because I use request.getParameter("<name>") to find out whether the user checked image of <name> (i.e. if it's null it was not checked but if it's not null it was checked).
The HTML form displays perfectly with the images and everything. My problem is that no matter what's checked in the HTML form, HandleDelete always prints to the console no parameters (null) meaning nothing was passed from DeleteImages to HandleDelete. I have a feeling the problem comes from either (1) the setAttribute statement in DeleteImages or (2) something with the HTML form. I've done a lot of searching and I'm pretty confident what I have is right though and I really can't figure out what's causing this issue (especially since I'm pretty sure this is exactly what I had before and it worked). Does anyone have any ideas?
I found the error: I included enctype=multipart/form-data
Not exactly sure why that caused the error but I removed that part and now it's passing the parameters perfectly. Thanks for your help guys!
I need to make sure that a certain website code contains a div like this one:
<div id="iframe-content"> some code here... </div>
I tried this way:
elementTry = doc.select("div#iframe-content");
and next I stored it into the String
String someText;
someText = elementTry.toString();
and tried to show the output via Toast, but it gives me "null". I am sure the website contains the div with this id, so
what can I do to fix it?
EDIT:
Toast code
Toast.makeText(getContext(), "Passed: "+ someText, Toast.LENGTH_LONG).show();
Try using this
elementTry = doc.select("div#iframe-content");
String someText;
someText = elementTry.text();
Hope this helps
String a=driver.findElement(By.id(locator))).getAttribute("value");
System.out.println("the value is "+ a);
Log.info(" Required object is present in the applicaiton"+ object);
<p locale="" isnumeric="false" basetype="String" class="outputText twControl twOutputText" id="InputText12">**MANTPLAP29102014100955627**</p>
I need to get the MANTPLAP29102014100955627 value from the above HTML code.
Please help..
Use
driver.findElement(By.id(locator))).getText();
Edit
There is a difference between getAttribute and getText methods
Consider an html tag like this
<input type = "text" id = "id" name = "bird">Peagon</input>
Now using
driver.findElement(By.id("id"))).getAttribute("name") will return value as bird (which is the value of attribute 'name')
and using
driver.findElement(By.id(locator))).getText(); will return a value as Peagon (Text between the starting and closing <input> tags)
I am retrieving all the contacts of a user from gmail and yahoo, I have added the checkbox, the user needs to select the desired email id to which he need to send email. I need to collect all user selected email id's and save them in a string send that string to another servlet where I am sending emails.
I was able to add the check box dynamically but I am not able to collect the emails and save them in a string. This is the code I have written to add check box before all the emails,
kindly help me to put those selected email id's in a string
I used the following code, but still I am not able to do it.You can have a look at the demo of this app http://ec2-50-16-183-101.compute-1.amazonaws.com/SocialAuthNew/
To get the contacts from Gmail type google in the text box and for yahoo type yahoo and click on submit button
List<Contact> contactsList = provider.getContactList();
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<head><script type='text/javascript'>");
out.println("function getAllContacts(size){ var selected_list='';");
out.println("for(var c=0;c<size;c++){if(document.getElementById('mailCheckbox'+c).checked==true){selected_list=selected_list+document.getElementById('lblmail'+c).innerHTML+':';}}");
out.println("document.getElementById('final_mailing_list').innerHTML=selected_list;}</script>");
out.println("<title></title>");
out.println("</head>");
out.println("<body>");
for(int i=0;i<contactsList.size();i++){
System.out.println(contactsList.get(i).getFirstName()+" : "+contactsList.get(i).getLastName()+":"+contactsList.get(i).getEmail());
out.println("<h1> Imported conatcts from your mail are:-</h1>");
out.println("<input type='checkbox' id='mailCheckBox"+i+"' name='mailCheckbox'></input>");
/* out.println(contactsList.get(i).getFirstName());
out.println(contactsList.get(i).getLastName());*/
out.println("<label id='lblmail"+i+"'>"+contactsList.get(i).getEmail()+"</label>");
}
int size=contactsList.size();
out.println("<input type='button' value='GetContact' onclick='getAllContacts("+size+");'/> ");
out.println("<div id='final_mailing_list'></div></body>");
out.println("</html>");
}
Try this:
1) Wrap your email in a DOM element to make it easier to access
out.println("<span>" + contactsList.get(i).getEmail() + "</span>");
2) Using something like e.g. JQuery for normalizing access to the DOM on the client, do
function getSelectedEmails() {
var emails = [];
$('body').find('input[name="mailCheckbox"]:checked').each(function() {
emails.push($(this).closest('span').text());
});
return emails;
}
This returns the emails in an array - which you can easily concatenate into a string if you want with e.g.
var emailString = emails.join(", ");
...although I think using an array is usually better (perhaps JSON encoded if you need to serialize it).
s using array is much easier. i have used array and gave the check box name as check1 and on the click of submit button i have called the following function. this function alerts the value of the selected check boxes and passes the action to servlet
<script>
function onCallForInterview()
{
var selectedIds;
var count=0;
for (i=0; i<document.frm.check1.length; i++)
{
if (document.frm.check1[i].checked==true)
{
if(count==0){
selectedIds=document.frm.check1[i].value;
count=count+1;
}
else
selectedIds=selectedIds+","+document.frm.check1[i].value;
}
}
alert(selectedIds);
document.frm.action="<%=contextPath%>/SearchCandInfo? action=selectcanforinterview&ids="+selectedIds;
document.frm.submit();
}
</script>
A slightly more primitive way to achieve this,
Create a bunch of Check-Box as you are doing now but with the difference that all of htem should have the same name i.e. do the following correction in your code
out.println("<input type='checkbox' id='mailCheckBox' name='mailCheckbox'></input>");
Now retrieve all the values of such text boxes on your server side using following call on request object,
String[] emailIds = request.getParameterValues("mailCheckBox");
Hope this helps.