Using select.getOptions() with Selenium to get values - java

I am using selenium to verify a web site where doctors' names appear in a drop-down box. If the name in the database has extra spaces (like "Kildare , Jack , MD") the blanks should be removed like "Kildare, Jack MD". I am using the selenium method select.getOptions() (where select is the web element of a select box).
When I retrieve the date from the box and print it, it shows the names as still having spaces (see Alois Alzheimer and Sigmund Freud below). In the text box visually it shows Sigmund Freud formatted correctly, but Alois Alzhemier still has a space after Alois (see the attached picture). I am not sure what is happening here. Looks like the select box occasionally removes blanks itself and sometimes doesn't. Any clues?
Here is the debug text from select box:
DEBUG [PP21644] ----<Alzheimer, Alois , DO>-----
DEBUG [PP21644] ----<Freud, Sigmund Schlomo, Jr, MD>-----
DEBUG [PP21644] ----<Mayo, Charles Mayo, MMIN>-----
And the picture of how it looks is attached. Sigmund is formatted correctly but Alois is not (see the space highlighted with a yellow square). And yes, I had to run this with famous doctors' names to protect the names of the actual providers.

There are a few ways you could do this
You could examine the HTML of the OPTIONs and figure out where the spaces are coming from, parse it, etc. to get rid of the spaces. I can't really offer code here since I don't know what the HTML looks like.
Another way given what you provided is to use regex and .replace() to remove repeated spaces and to remove a space before a comma.
public static void main(String[] args)
{
String s1 = "Alzheimer, Alois , DO";
String s2 = "Freud, Sigmund Schlomo, Jr, MD";
String s3 = "Mayo, Charles Mayo, MMIN";
System.out.println(cleanup(s1));
System.out.println(cleanup(s2));
System.out.println(cleanup(s3));
}
public static String cleanup(String s)
{
return s.replaceAll(" +", " ").replace(" ,", ",");
}

Related

Fast variable insertion into string

Is there any Intellij Idea hotkey or a plugin to quickly insert variable into string?
e.g. i have string
"My code is working, yay! Result is = ",
and i have to add construction "+variable +", resulting in
"My code is working, yay! Result is = "+ variable +"."
to properly insert a variable.
When i have to insert variables into 20+ string, its driving me nuts :)
tried to find solution on google or plugin repository, no result
PS: i know about soutv hotkey, but it cant help me since i have to change already existing strings in code
Added Live Template
Abbreviation: ++
Description: Insert variable into string
Expand with: Enter
Template text: "+ $EXPR$ +" (do not forget double quotes)
Edit Template Variable:
expression:variableOfType("") default Value: "expr"
settings screen
You type ++ in a string, press Enter, and template text is added, and you only need to choose a variable. Works like a charm.
Thanks for the idea, #Meo

How to shorten filename displayed in DSpace

I would like to shorten the file name that is being displayed in the simple item view.
If I have a very long file name, this is displayed in the simple item record:
This example file name is shortened if you view the full item record:
But if you edit this item and click the Item Bitstreams tab, the file name is being displayed like this:
My goal is to apply what is being displayed in the edit bitstream (the 3rd picture) to the simple and full item view. I don't know where this transformation is being generated. I looked at administrative.xsl and I can't find anything on the shortening of the file name. Please advice on how to achieve this or where to look for this transformation.
The rewrite of the file name in the "Item Bitstreams" tab is done in Java code, not in the XSL. It's here: EditItemBitstreamsForm.java.
Your item page screenshot looks like you're working in XMLUI / Mirage 2, is that right? Your best bet is to use the shortenString method in org.dspace.app.xmlui.utils.XSLUtils (code). Actually maybe you aren't using Mirage 2 because Mirage 2 does just that, see item-view.xsl:
<xsl:value-of select="util:shortenString(mets:FLocat[#LOCTYPE='URL']/#xlink:title, 30, 5)"/>
Thanks to Andrea for the answer. Here's my code and how I used it.
I created a new method shortenFileName in org.dspace.app.xmlui.utils.XSLUtils
public static String shortenFileName(String string, String middle, int targetLength) {
targetLength = Math.abs(targetLength);
if (string != null && string.length() > targetLength) {
// If the file name is too long then shorten it so that it will display nicely.
return StringUtils.abbreviateMiddle(string, middle, targetLength);
}
else
return string;
}
and then used it in item-view.xsl like this:
<xsl:value-of select="util:shortenFileName(mets:FLocat[#LOCTYPE='URL']/#xlink:title, ' … ', 20)" />
The file name now looked like this:

create basic line breaks with an Xpath Expression from Java to XML file

Throghout learning how to program Stack Overflow has provided a wealth of information, and a staple of learning how to figure out different things. However for this problem, after searching for a few days and coming up empty handed I've decided to create my first post.
The problem, creating a mulit line value in an XML file from a java string using Xpath's Node.setTextContent. Its writing to a value tag in the XML file that Xpath reports as an element node. Now when writing the string value to the node, it does indeed write the string value to the node, however its just one long string value, as the setTextContent method according to the API does not parse any thing in the string and just writes the text. An example of the data I'm trying to write is:(edited 2-16, just noticed the format option wasn't showing the main post) formated like below with the street, city state and phone broken down like the info below (with out the spaces in between the lines) to match the format as listed in the first the xml code example.
496 Vivid Ave.
Some City, State, 11111
111-222-3333
When the java string is wrote to xml file its entered as:
496 Vivid Ave. Some City, State, 11111 111-222-3333
An example of the node created from the parent program (not my java app) is :
<field sid="ADDRESS">
<itemlocation>
<ae>
<ae>absolute</ae>
<ae>33</ae>
<ae>168</ae>
</ae>
</itemlocation>
<value>496 Vivid Ave.
Some City, State, 11111
111-222-3333</value>
<borderwidth>0</borderwidth>
<fontinfo>
<ae>Times New Roman</ae>
<ae>10</ae>
<ae>plain</ae>
</fontinfo>
<scrollhoriz>wordwrap</scrollhoriz>
<scrollvert>fixed</scrollvert>
<format>
<ae>string</ae>
<ae>optional</ae>
</format>
<acclabel>6. leave address.
enter street, city, state, zip code and phone number.
</acclabel>
<size>
<ae>35</ae>
<ae>3</ae>
</size>
<previous>FIELD5</previous>
<next>ORDINARY</next>
</field>
When i run my app the ouput of raw xml data looks like this:
<field sid="ADDRESS">
<itemlocation>
<ae>
<ae>absolute</ae>
<ae>33</ae>
<ae>168</ae>
</ae>
</itemlocation>
<value>496 Vivid Ave. Some City, State, 11111 111-222-3333</value>
<borderwidth>0</borderwidth>
<fontinfo>
<ae>Times New Roman</ae>
<ae>10</ae>
<ae>plain</ae>
</fontinfo>
<scrollhoriz>wordwrap</scrollhoriz>
<scrollvert>fixed</scrollvert>
<format>
<ae>string</ae>
<ae>optional</ae>
</format>
<acclabel>6. leave address.
enter street, city, state, zip code and phone number.
</acclabel>
<size>
<ae>35</ae>
<ae>3</ae>
</size>
<previous>FIELD5</previous>
<next>ORDINARY</next>
</field>
I have tried to manually enter the escaped characters for line feed and carriage return in the string , and all it does is prting the code values, not the value.
As the parent program reads my Java code it does not break down the address and phone number on separate lines.
The java i'm using looks like:
public void setFieldValue(String sid, String value){
Node resultNode = null;
XPath xPath = XPathFactory.newInstance().newXPath();
try{
resultNode = (Node)xPath.evaluate(makeFieldString(sid), doc,XPathConstants.NODE);
}catch(XPathExpressionException xpee){System.out.println(xpee + "Could not retrive node");}
if (resultNode != null){
resultNode.setTextContent(value);
}
else System.out.println("Epic fail");
}
public String makeFieldString(String sid){
String output=null;
if (sid.equals("POPUP2")){
output="//popup[#sid='" + sid + "']/value";
}
else{
output= "//field[#sid='" + sid + "']/value";
}
return output;
}
If anyone has any ideas on how to get the Java string to include line breaks that the setTextContent will recognize I would appreaciate it. I have tossed the idea around of trying to use text Normalize and white space data, but not really sure how to implement it, and not sure if those methods would be striped out from the setTextContent method anyway.
Thanks for any help! I hope I was clear enough in my question to not get flamed!
I found an xpather cheetsheet at http://xpath.alephzarro.com/content/cheatsheet.html which showed the special characters in regular expressions, whereas Xpath would not insert the unicoode value of a line break, it did recognize \n for new line and now the address block is formatted correctly.

gettext-common example doesn't work

I need to translate my app, so i want to use gettext-common from http://code.google.com/p/gettext-commons
I checked out the svn and tried to compile the example:
javac -classpath ../java I18nExample.java
java -classpath ../../target/gettext-commons-0.9.6.jar:. I18nExample
The program does not give me the targeted output; I have absolutely no idea whats going on!
It seems that the de.properties is completly ignored. If I set the Properties file to "de" in the Factory's constructor, I get partly the output I want to see.
Is there anywhere in the internet a working example of gettext for java?
this is the output from the example script:
First run
This text is marked for translation and is translated
This text is marked for translation but not translated
This text is marked for translation but not translated
Four: 4
chat
chat
1 file is open
2 files are open
Second run
This text is marked for translation and is translated
This text is marked for translation but not translated
This text is marked for translation but not translated
Four: 4
chat
chat
1 file is open
2 files are open
There are a couple of issues, perhaps due to the build process.
First, for the message lookup to work, I needed to move the en and de resources into Messages_en.properties and Messages_de.properties in order to make a real resource bundle.
Second, the example code tries to use messages with no translations available, like the "file is open" stuff. Here's an updated version of what I tried; this all appears to work with the above modification:
public static void main(String[] args) {
I18n i18n = I18nFactory.getI18n(I18nExample.class, "Messages");
for (int i = 0; i < 2; i++) {
if (i == 0) {
print("First run");
} else {
print("Second run");
i18n.setLocale(Locale.GERMAN);
}
print("Current locale: " + i18n.getLocale());
print(i18n.tr("This text is marked for translation and is translated"));
String mark = i18n.marktr("This text is marked for translation but not translated");
print(mark);
print(i18n.tr(mark));
mark = i18n.tr("This is the {0}. text to be translated", "chat (noun)");
print(mark);
mark = i18n.tr("This is the {0}. text to be translated", "chat (verb)");
print(mark);
print(i18n.tr("chat (noun)"));
print(i18n.tr("chat (verb)"));
print("");
}
}
Note also that to insert translated words, you need something like this:
print(i18n.tr("This is the {0}. text to be translated", i18n.tr("chat (noun)")));
print(i18n.tr("This is the {0}. text to be translated", i18n.tr("chat (verb)")));
However, without un-banging (removing the ! and providing an English translation in Messages_en.properties, it shows up as chat (noun), which... strikes me as being almost useless.
The documentation is lacking on this aspect.

convert at symbol ("#") to CharSequence

I am testing site with selenium and I need to send an e-mail to one of the fields. So far I am using this Java method:
String email = "test#example.com"
WebElement emailField = driver.findElement(By.id("mainForm:accountPanelTabId:1:accountEmails");
emailField.sendKeys(email);
But from (to me) uknown reason, this is sending exactly this value to the field:
testvexample.com
(so basically the "#" got replaced by "v")
Just out of curiosity: I am Czech and have Czech keyboard. One shortcut to write "#" symbol is rightAlt + v so I believe this can be connected...
So I am searching any "bulletproof" methot which always writes "#" symbol. Any help appreciated.
EDIT
the sendKeys is method of Selenium, and it simulates typing on keyboard. The javadoc is here: http://selenium.googlecode.com/svn/trunk/docs/api/java/org/openqa/selenium/WebElement.html#sendKeys%28java.lang.CharSequence...%29
The following should work: String email = "test\u0040example.com";
Apologies for misreading the question earlier.
I think you will have to call sendKeys using the proper values from the Keys enum to simulate the way you get your at-sign. Use Keys.ALT with your "v" in a chord:
sendKeys(Keys.chord(Keys.ALT, "v"));

Categories