How to use single quotes for XPath in Selenium Webdriver? - selenium

I'm trying to pass an i value to fetch random selection check boxes with dynamic ids and the code is pasted below with the corresponding error message.
By.xpath("//div[contains(#id,'team_"+randNumber(i)+")]")) != null){
Error appears as:
The given selector //div[contains(#id,'team_'2)] is either invalid or does not result in a WebElement.
Thus the above term should has to display as //div[contains(#id,'team_2')]

This has nothing to do with Selenium, this is purely a Java String manipulation problem!
int rand = randNumber(i);
String id = "team_" + rand;
String xpathh = "//div[contains(#id, '" + id + "']"; // note the nested quotes!
By.xpath(xpathh));
If you want to roll this into one line, copy pasting everything together yields:
By.xpath("//div[contains(#id, '" + "team_" + randNumber(i) + "']"));
A couple of the quotes could be optimized out:
By.xpath("//div[contains(#id, 'team_" + randNumber(i) + "']"));

Should be something like this:
By.xpath("//div[contains(#id,'team_"+randNumber(i)+"')]")

Related

Dynamically create sql where condition on textbox entry

I am working on a C# desktop application. I want to create a search functionality. Now the problem is that i am using around 8 textboxes. Different permutations of textboxes could be populated and the resulting 'sql where' condition should only include those textboxes values which are not null. Now one pathetic way is to use a zillion 'if and else' which obviously is laborious. Any other way to do this?
You need just one query with filled WHERE to use all parameters like this
select ...
from ...
WHERE
(firstNameColumn=:firstNameParam or :firstNameParam is null)
AND (lastNameColumn=:lastNameParam or :lastNameParam is null)
AND (...)
I would like to make a point of first checking is the paramtere null, then use it to compare with column values.
Since you are generating query in C#, try old-Chinese approach from Ming period of using default condition where 1=1 just to avoid checking did you already had first condition :)
string query = "select ... from ... join ... on ... where 1=1";
//suposedly you have value of one search box in variable called "item_name"
if(string.IsNullOrWhiteSpace(item_name) == false)
{
query += " and Order_Line.Name ='" + item_name + "'";
}
and so on for other fields.
What you are trying to do in order to avoid ifs is not really a good approach. Look at this:
string query = " select ... where Order_Line.Name = '" + item_name + "'";
What will be the resulting string if item_name is actually null?
EDIT: the resulting query would be
where Order_Line.Name = '' or Order_Line.Name is null
which is not what you want. You want every row if that search field is empty, menaing it shouldn't have anu effect on search. That's why you need condition to see will you include this column in where clause in the first place.

How to easy create query like types : ( val1 + OR + val2 + OR + val3) with Spring Data Solr (With Dynamic Queries)

I have field "types" multivalue=true.
From client layer I receive list of keys what should be processed.
I want to create FilterQuery to produce fq like fq=types:(6+OR+4+OR+8)
logically I expect to use next code:
final Criteria criteria = new Criteria("types").in(types);
new SimpleFilterQuery(criteria);
But this code produce fq=types:(6+4+8)
It could works, but I use q.op=AND (required for other needs)
Is it any simple way to do something like inOr/inAny ?
Actually I found who ways, but this looks not so beauty as I expect:
First:
Criteria criteria = new SimpleStringCriteria("types:(" + StringUtils.join(statuses, " OR ") + ")");
new SimpleFilterQuery(criteria);
Second:
Criteria criteria = new Criteria("types");
criteria.expression("(" + StringUtils.join(statuses, " OR ") + ")");
new SimpleFilterQuery(criteria);

IF-ELSE Alternative for Multiple SQL criteria for use in BIRT

I want to create a report by using BIRT. I have 5 SQL criterias as the parameter for the report. Usually when I have 3 criterias, I am using nested if-else for the WHERE statement with javascript.
Since right now I have more criteria it becomes more difficult to write the code and also check the possibilities, especially for debug purposes.
For example the criteria for table employee, having these 5 criterias : age, city, department, title and education. All criteria will be dynamic, you can leave it blank to show all contents.
Do anyone know the alternative of this method?
There is a magical way to handle this without any script, which makes reports much easier to maintain! We can use this kind of SQL query:
SELECT *
FROM mytable
WHERE (?='' OR city=? )
AND (?=-1 OR age>? )
AND (?='' OR department=? )
AND (?='' OR title=? )
So each criteria has two dataset parameters, with a "OR" clause allowing to ignore a criteria when the parameter gets a specific value, an empty value or a null value as you like. All those "OR" clauses are evaluated with a constant value, therefore performances of queries can't be affected.
In this example we should have 4 report parameters, 8 dataset parameters (each report parameter is bound to 2 dataset parameters) and 0 script. See a live example of a report using this approach here.
If there are many more criteria i would recommend to use a stored procedure, hence we can do the same with just one dataset parameter per criteria.
Integer parameter handling
If we need to handle a "all" value for an integer column such age: we can declare report parameter "age" as a String type and dataset parameters "age" as an integer. Then, in parameters tab of the dataset use a value expression instead of a "linked to report parameters". For example if we like a robust input which handles both "all" "null" and empty values here is the expression to enter:
(params["age"].value=="all" || params["age"].value=="" || params["age"].value==null)?-1:params["age"].value
The sample report can be downloaded here (v 4.3.1)
Depending on the report requirements and audiance you may find this helpful.
Use text box paramaters and make the defualt value % (which is a wild card)
SELECT *
FROM mytable
WHERE city like ?
AND age like ?
AND department like ?
AND title like ?
This also allows your users to search for partial names. if the value in the city text box is %ville% it would return all the cities with "ville" anyplace in the city name.
If report parameters to be included in SQL-WHERE clause would be named according to some naming convention, for instance query_employee_[table column name], you could write Java-Script code in a generic way, so that you will not have to change it when new reporters being added.
for each param in params {
if param.name starts with query_employee_ {
where_clause += " and " + param.name.substring(after query_employee) + " == '" + param.value + "'";
}
}
You will have to check type of a parameter to make a decision whether you have to quote the parameter value.
The event handler could look as follows (implemented in Java, but it should be possible to port it to JavaScript, if you really need it to be in JavaScript):
public class WhereConditionEventHandler extends DataSetEventAdapter {
#Override
public void beforeOpen(IDataSetInstance dataSet,
IReportContext reportContext) throws ScriptException {
super.beforeOpen(dataSet, reportContext);
String whereClause = " where 1 = 1 ";
SlotHandle prms = reportContext.getDesignHandle().getParameters();
for (int i = 0; i < prms.getCount(); i++) {
if (prms.get(i) instanceof ScalarParameterHandle) {
ScalarParameterHandle prm = (ScalarParameterHandle) prms.get(i);
int n = prm.getName().indexOf("sql_customer_");
if (n > -1) {
String prmValue = "" + reportContext.getParameterValue(prm.getName());
if (DesignChoiceConstants.PARAM_TYPE_STRING.equals(prm.getDataType())) {
prmValue = "'" + prmValue + "'";
}
whereClause += " and " + prm.getName().substring("sql_customer_".length()) + " = " + prmValue;
}
}
}
System.out.println("sql: " + whereClause);
dataSet.setQueryText(dataSet.getQueryText() + whereClause);
}
}
By the way, you can pass in parameters that are not registered as report parameters in the BIRT report design. BIRT will nevertheless put them into "params" array.

DELETE FROM table WHERE var = value does not remove entries

So I fetched some data from a mdb file in c# via
"SELECT * FROM " + listBox1.GetItemText(listBox1.SelectedItem) + " WHERE Note = '" + listBox2.GetItemText(listBox2.SelectedItem).Replace("'","\'") + "'";
which selects the right data, here it is
SELECT * FROM Main WHERE Note ='Hello'
The mdb data structure looks like this being plotted as a CSV-file:
"Record ID";Status;Placement;Private;Category;Note;Blob
14341665;4;2147483647;True;3;"""Hello"" - Neues
But when I try to remove entries with
"DELETE FROM " + listBox1.GetItemText(listBox1.SelectedItem) + " WHERE \"Record ID\" LIKE '" + dr[0] + "';";
or
"DELETE FROM " + listBox1.GetItemText(listBox1.SelectedItem) + " WHERE \"Record ID\" = '" + dr[0] + "';";
which looks like for instance
DELETE FROM Main WHERE "Record ID" LIKE '14341665';
The entries just stay there. I can rerun the select command even restart my application, the mdb is not changed.
Is record ID a numeric field? If so, lose the quotes.
DELETE FROM Main WHERE [Record ID] = 14341665;
Note that spaces in field (column) names will always be a problem. Such columns names have to be enclosed in square brackets, as do columns named with reserved words.
The record id is numeric, so don't put apostrophes around it:
"DELETE FROM " + listBox1.GetItemText(listBox1.SelectedItem) + " WHERE \"Record ID\" = " + dr[0]
Note: You should avoid using select * in production code, you should specify the data that you want returned. Also, you should use parameterised queries instead of concatenating values into the query.
if i remember correctly, "like" only works on string data, please check the data type of Record ID.
If Record ID is numeric, you may want to use database's conversion function to convert it into string before filtering using "like".
btw, remember to make sure that dr[0] is properly escaped.

Formatting existing SQL Data?

I have a table full of a contacts information, and I want to change the phone number from 5555555555 to (555) 555-5555, instead.
I've found a few things online for selecting data and having it be returned in that format, but no way to actually update the records to use the new format. Is this even possible?
it's a bad practice doing so in db. Good practice is to store raw data in your db and to format it when you're selecting it or even better - when you show this data to user.
update YourTable
set PhoneNumber =
'(' + LEFT(PhoneNumber, 3) +
') ' + SUBSTRING(PhoneNumber, 3, 3) +
'-' + RIGHT(PhoneNumber, 4)
This will update your data with the correct format as you specified. This works with SQL Server, but I can't speak for any other RDBMS.
If you want to transfer the old data format to the new one just make loop that read the cell change it to the format that you like and store it back in the cell.
string NewString;
NewString = "(" + TheOriginalString.Substring(0,3) + ") ";
NewString += TheOriginalString.Substring(3,3) + "-";
NewString += TheOriginalString.Substring(6);
Then just store the new string in the same cell.
The example code is in C#.