Iterate Form Fields in WatiN - watin

Is there a way to iterate all form field in WatiN? Code snippet would be highly appreciated. Thanks.

string url = "http://localhost//test.htm";
string formId = "myFormId";
IE ie = new IE(url);
Form form = ie.Form(formId);
CheckBoxCollection checkBoxCollection = form.CheckBoxes;
for (int index = 0, total = checkBoxCollection.Count; index < total; index++) {
CheckBox checkBox = checkBoxCollection[index];
this.AddInputControl(checkBox.Id, checkBox);
}
RadioButtonCollection radioButtonCollection = form.RadioButtons;
for (int index = 0, total = radioButtonCollection.Count; index < total; index++) {
RadioButton radioButton = radioButtonCollection[index];
this.AddInputControl(radioButton.Id, radioButton);
}
SelectListCollection selectListCollection = form.SelectLists;
for (int index = 0, total = selectListCollection.Count; index < total; index++) {
SelectList selectList = selectListCollection[index];
this.AddInputControl(selectList.Id, selectList);
}
TextFieldCollection textFieldCollection = form.TextFields;
for (int index = 0, total = textFieldCollection.Count; index < total; index++) {
TextField textField = textFieldCollection[index];
this.AddInputControl(textField.Id, textField);
}

Try this:
string url = "http://localhost//test.htm";
string formId = "myFormId";
IE ie = new IE(url);
Form form = ie.Form(formId);
ElementCollection elements = form.Elements.Filter(Find.ByElement(x =>
{
return x is TextField || x is SelectList || x is RadioButton || x is CheckBox;
}));

Related

How to arrange array of objects with same property value?

I have an person model with two property like this:
int id;
String name;
and some object with this data:
person0 = {1,"James"};
person1 = {2,"James"};
person2 = {3,"James"};
person3 = {4,"Barbara"};
person4 = {5,"Barbara"};
person5 = {6,"Ramses"};
and array contain objects:
firstArray = [person0, person1, person2, person3, person4, person5];
Therefore how can have this array:
secondArray = [
[person0, person1, person2],
[person3, person4],
[person5]
]
Thank you.
If language does not matter.
map = new Map();
for (persona of personas) {
name = persona.name;
arrayForName = map.get(name);
if (arrayForName == null) {
arrayForName = [];
map.put(name, arrayForName);
}
arrayForName.put(persona)
}
The idea is to have a map (which is a collection key->value).
The value of the map should in turn be an array.
To add elements efficiently, you iterate only once through the data, and add arrays each time a new key is discovered (i.e. the name).
In Java it would be something like:
Map<String, List<Persona>> map = new HashMap<>();
for (Persona persona : personas) {
String name = persona.getName();
List<Persona> listForName = map.get(name);
if (listForName == null) {
listForName = new ArrayList<Persona>();
map.put(name, listForName);
}
listForName.add(persona)
}
Try this code in Java Android:
ArrayList<ArrayList<Person>> secondArr = new ArrayList<>();
ArrayList<Course> tempArr = new ArrayList<>();
for (int i = 0; i < firstArr.size(); i++) {
if ((i + 1) >= firstArr.size()) {
tempArr.add(firstArr.get(i));
secondArr.add(tempArr);
} else {
if (firstArr.get(i).name .equals( firstArr.get(i + 1).name) ) {
tempArr.add(firstArr.get(i));
} else {
tempArr.add(firstArr.get(i));
secondArr.add(tempArr);
tempArr = new ArrayList<>();
}
}
}
Finally secondArr prepared.
And if list not sorted we can use code like this:
for (int i = 0; i <firstArr.size() ; i++) {
boolean isAdd = false;
for (int j = 0; j < secondArr.size() ; j++) {
if (secondArr.get(j).get(0).getName().equals(firstArr.get(i).getName())){
secondArr.get(j).add(firstArr.get(i));
isAdd =true;
break;
}
}
if (!isAdd){
ArrayList<Person> arrayList = new ArrayList<>();
arrayList.add(firstArr.get(i));
secondArr.add(arrayList);
}
}

Best and easiest way to export all the data from a datatable to an excel to get it downloaded using C#?

Using C#, which is the best and easiest way to export all the data from a datatable to an excel to get it downloaded?
I use Microsoft Interop.
Here is some sample code to get you started.
There are probably hundreds of tutorials which are better than the below. I have removed much of the code I use so I doubt the following will compile, but with careful reading, and some investigation into tutorials, you should be able to accomplish what you need.
public static void ExportToExcel(DataSet dsTodo, string strPathToSaveFile)
{
try
{
int numSteps = (dsTodo != null ? dsTodo.Tables.Count * 2 : 1);
Microsoft.Office.Interop.Excel.ApplicationClass ExcelApp = new Microsoft.Office.Interop.Excel.ApplicationClass();
Workbook xlWorkbook = ExcelApp.Workbooks.Add(Microsoft.Office.Interop.Excel.XlWBATemplate.xlWBATWorksheet);
if (dsTodo.Tables.Count > 0)
{
for (int i = dsTodo.Tables.Count; i > 0; i--)
{
Sheets xlSheets = null;
Worksheet xlWorksheet = null;
//Create Excel Sheets
xlSheets = ExcelApp.Sheets;
xlWorksheet = (Worksheet)xlSheets.Add(xlSheets[1], Type.Missing, Type.Missing, Type.Missing);
System.Data.DataTable table = dsTodo.Tables[i - 1];
xlWorksheet.Name = table.TableName;
for (int j = 1; j < table.Columns.Count + 1; j++)
{
ExcelApp.Cells[1, j] = table.Columns[j - 1].ColumnName;
}
var excelData = new object[table.Rows.Count, table.Columns.Count];
for (int rowJ = 0; rowJ < table.Rows.Count; rowJ++)
{
for (int colI = 0; colI < table.Columns.Count; colI++)
{
excelData[rowJ, colI] = table.Rows[rowJ][colI];
}
}
int startCol = 1;
int endCol = startCol + table.Columns.Count - 1;
int startRow = 2;
int endRow = startRow + table.Rows.Count - 1;
string startLoc = GetCellFromCoords(startCol, startRow);
string endLoc = GetCellFromCoords(endCol, endRow);
//ExcelApp.get_Range(startLoc, endLoc).Value2 = excelData;
try
{
Range valRange = ExcelApp.get_Range(startLoc, endLoc);
valRange.Value2 = excelData;
}

Count number of emails in outlook folder

I want to count the number of mails i received which has word say 'xyz' in subject line.But I do not want it to count duplicates, I mean if I get a mail with subject 'xyz' and I reply to it including myself I want that to be counted as 1 not 2. Is it possible in outlook?
Yeah that's possible definetly, you did not specify the techno you would use but a sample code using interop could be:
var outlookApp = new Outlook.Application();
var mapi = outlookApp.GetNamespace("MAPI")
var root = mapi.GetDefaultFolder(OlDefaultfolders.olFolderInbox)
var folders = root.Folders;
int count = 0;
for(int i = folders.Count; i >= 1; i--)
{
var folder = folders[i];
count += GetCount(folder, 0);
Marshal.ReleaseComObject(folder);
}
Marshal.ReleaseComObject(folders);
//... function
private int GetCount(MAPIFolder folder, int count)
{
var folderItems = folder.Items;
if(folderItems != null && folderItems.Count >= 1)
{
for(int i = folderItems.Count; i >= 1; i--)
{
//Check item.Subject for string logic
count++;
//Add item in collection variable to check duplicates,
//generate an id with subject + creationtime for tracking?
}
}
if(folder.Folders != null && folder.Folders >= 1)
{
var subFolders = folder.Folders;
for(int i = subfolders.Count; i >= 1; i--)
{
var subFolder = subFolders[i]
count += GetCount(subFolder, count);
Marshal.ReleaseComObject(subfolder);
}
}
Marshal.ReleaseComObject(folderItems);
return count;
}
Hope it helps someone,

how to get static range in your report

In AX Report the User can select different Range. I seek to show that the criteria used to filter user in the report itself even
the expression "parameter!CustomerAccount.Value" don't work because this filter is not a static parameter.
the user can use any table from the query and any field from the table and any criteria.. I'm locking for trick to get for which table, which field and what criteria it uses.
this method work very well ^_^
(( I use it privet and not static ))
static void getQueryRanges2(Args _args)
{
Query query;
QueryRun queryRun;
QueryBuildDataSource qbd;
QueryBuildRange range;
QueryFilter filter;
int cnt, filtercnt, i,j, k;
DictTable dictTable;
DictField dictField;
str fieldLabel;
;
query = new query(queryStr(smmSalesCustItemStatistics));
queryRun = new QueryRun(query);
queryRun.prompt();
query = queryRun.query(); for(i = 1; i <= query.dataSourceCount(); i++)
{
cnt = query.dataSourceNo(i).rangeCount();
filtercnt = 0;
if(!query.dataSourceNo(i).embedded())
{
filtercnt = query.queryFilterCount(query.dataSourceNo(i));
}
dictTable = new DictTable(query.dataSourceNo(i).table());
for (k=1; k<= filtercnt; k++)
{
filter = query.queryFilter(k, query.dataSourceNo(i));
dictField = new DictField(query.dataSourceNo(i).table(), fieldname2id(query.dataSourceNo(i).table(), filter.field()));
info (strFmt("%1, %2. Range = %3", dictTable.label(), dictField.label(), filter.value()));
}
for (j=1; j<=cnt; j++)
{
range = queryRun.query().dataSourceNo(i).range(j);
dictField = new DictField(query.dataSourceNo(i).table(), fieldname2id( query.dataSourceNo(i).table(), range.AOTname()));
if(range.value())
{
info(strfmt("%1, %2. Range = %3",dictTable.label(), dictField.label(), range.value()));
}
}
}
}
enjoy :)

JTable + sorting column, but row is not sorted

I have created a JTable, the table contains 4 rows, and for a specific row, I have overridden the sorting and its working fine only on that column.
Status Scheduled Date Scheduled time Status
false 30/01/2012 02:00:00 Scheduled
false 29/01/2012 14:58:00 Scheduled
false 29/01/2012 15:50:00 Scheduled
For Scheduled Date, which I try to sort, it would sort, but the respecitve rows are not being updated.
Here is my code for sorting
public static void sortColumn(DefaultTableModel model, int colIndex,
boolean sortingOrder) {
Vector<?> data = model.getDataVector();
Object[] colData = new Object[model.getRowCount()];
SortedSet<Object> dataCollected = null;
List<Date> dateCollected;
boolean dateFlag = false;
dateCollected = new ArrayList<Date>();
// Copy the column data in an array
for (int i = 0; i < colData.length; i++) {
Object tempData = ((Vector<?>) data.get(i)).get(colIndex);
if ((colIndex == 1 || colIndex == 4)
&& tempData.toString().contains("/")) {
String[] _scheduledDate1 = ((String) tempData).split("/");
Calendar _cal1 = Calendar.getInstance();
_cal1.set(Integer.parseInt(_scheduledDate1[2]),
Integer.parseInt(_scheduledDate1[1]) - 1,
Integer.parseInt(_scheduledDate1[0]));
dateCollected.add(_cal1.getTime());
dateFlag = true;
} else {
colData[i] = ((Vector<?>) data.get(i)).get(colIndex);
}
}
// DateCompare compare = new DateCompare();
if (!dateFlag) {
dataCollected = new TreeSet<Object>();
dataCollected.add(colData);
dateFlag = false;
}
// Copy the sorted values back into the table model
if ((colIndex == 1 || colIndex == 4) && dateFlag) {
SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy");
sortOrder = !sortOrder;
if (sortOrder) {
Collections.sort(dateCollected);
} else {
Collections.sort(dateCollected, Collections.reverseOrder());
}
colData = dateCollected.toArray();
for (int i = 0; i < colData.length; i++) {
((Vector<Object>) data.get(i)).set(colIndex,
sdf.format(((Date) colData[i]).getTime()));
}
} else {
for (int i = 0; i < colData.length; i++) {
((Vector<Object>) data.get(i)).set(colIndex, colData[i]);
}
}
model.fireTableStructureChanged();
}
How to I get the entire row update accordingly?
I found the problem, my object against I was comparing was wrong, I've change the code for the same it all works fine.
I implemented QuickSort algorithm to sort the vector on the specific column I need.

Resources