verifying drop down values using WebDriver - webdriver

I need to verify drop down values using WebDriver. i have expected values in a String array
String[] exp = {"--Title--","Mr","Mrs","Miss","Ms","Dr","Prof"};
I need to write a function that return all the values from drop down and i need to assert with expected values, Below is the code that i have written to print the values from drop down, but i need to assert those values with expected ones:-
WebElement dropdown = driver.findElement(By.id("ddlNights"));
Select select = new Select(dropdown);
List<WebElement> options = select.getOptions();
for(WebElement we:options)
{
System.out.println(we.getText());
}
Can anyone help me in writing a method that returns String array of drop down values, so that we can reuse the method for validating values in every drop down using
Assert.assertTrue(Arrays.equals(Expected,Actual))
Thanks in Advance!!!

Try this
String[] exp = {"--Title--","Mr","Mrs","Miss","Ms","Dr","Prof"};
WebElement dropdown = driver.findElement(By.id("ddlNights"));
Select select = new Select(dropdown);
List<WebElement> options = select.getOptions();
for(WebElement we:options)
{
boolean match = false;
for (int i=0; i<exp.length(); i++){
if (we.getText().equals(exp[i]){
match = true;
}
}
Assert.assertTrue(match);
}
It should compare each element with every possibility in the expected Strings. The Match will be true only in "found" state. You can play around with the message with the Assert, because it can fail anytime. So you can do something like
Assert.assertTrue(match, we.getText());
Which should write you on which webElement it did not find any match - I am not 100% sure with that line - i dont have any IDE running to verify it.

WebElement element= driver.findElement(By.xpath("//select[#class='ui-selectonemenu']"));
ArrayList<String> array = new ArrayList();
array.add("Select Tool");
array.add("Selenium");
array.add("Playwright");
Select dropdown = new Select(element);
System.out.println("Before dropdwon selection");
List<WebElement> options = dropdown.getOptions();
for(WebElement we:options)
{
int i;
boolean flag=false;
System.out.println("getting text"+we.getText());
for (i=0; i<array.size(); i++){
if (we.getText().equals(array.get(i))){
//System.out.println("string text is available in dropdown"+array.get(i)+"\n");
array.remove(i);
flag = true;
}
}
System.out.println("flag value"+flag);
if(flag==true)
{
System.out.println("flag is true"+we.getText()+"\n");
}
else
{
System.out.println("flag is false"+we.getText()+"\n");
}
}

Related

Return list of Integers with SQLiteDatabase.rawQuery

I'm trying to write a function that returns a list of Integers using SQLiteDatabase.rawQuery.
This is how i'm imagining it but doesn't work..
public List<Integer> queryInt(String sql, String[] whereArgs){
//fetch string array
List<Integer> r = new ArrayList<Integer> ();
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.rawQuery(
sql,
whereArgs
);
return c.toInt(); //something that does this
}
If someone has a clue, thanks for the help !
You need to move within the Cursor before you can access any of the data (initially a Cursor will be positioned at before the first row (position -1)).
So your queryInt method could be :-
public List<Integer> queryInt(String sql, String[] whereArgs){
//fetch string array
List<Integer> r = new ArrayList<>();
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.rawQuery(
sql,
whereArgs
);
// Loop through the Cursor
while(c.moveToNext()) {
r.add(c.getInt(0)); //<<<< see note
}
c.close(); //<<<< Should always close a Cursor when done with it.
return r;
}
Note 0 assumes that the data is to be extracted from the first column. However it is considered better practice to not hard code the column offset but to get the column offset based upon the column name so r.add(c.getInt(c.getColumnIndex(your_column_name_as_a_string))); would be recommended.
If there are no rows then the above would return an empty List, so you may need to check the returned List's size.

X++ passing current selected records in a form for your report

I am trying to make this question sound as clear as possible.
Basically, I have created a report, and it now exists as a menuitem button so that the report can run off the form.
What I would like to do, is be able to multi-select records, then when I click on my button to run my report, the current selected records are passed into the dialog form (filter screen) that appears.
I have tried to do this using the same methods as with the SaleLinesEdit form, but had no success.
If anyone could point me in the right direction I would greatly appreciate it.
Take a look at Axaptapedia passing values between forms. This should help you. You will probably have to modify your report to use a form for the dialog rather than using the base dialog methods of the report Here is a good place to start with that!
Just wanted to add this
You can use the MuliSelectionHelper class to do this very simply:
MultiSelectionHelper selection = MultiSelectionHelper::createFromCaller(_args.caller());
MyTable myTable = selection.getFirst();
while (myTable)
{
//do something
myTable = selection.getNext();
}
Here is the resolution I used for this issue;
Two methods on the report so that when fields are multi-selected on forms, the values are passed to the filter dialog;
private void setQueryRange(Common _common)
{
FormDataSource fds;
LogisticsControlTable logisticsTable;
QueryBuildDataSource qbdsLogisticsTable;
QueryBuildRange qbrLogisticsId;
str rangeLogId;
set logIdSet = new Set(Types::String);
str addRange(str _range, str _value, QueryBuildDataSource _qbds, int _fieldNum, Set _set = null)
{
str ret = _range;
QueryBuildRange qbr;
;
if(_set && _set.in(_Value))
{
return ret;
}
if(strLen(ret) + strLen(_value) + 1 > 255)
{
qbr = _qbds.addRange(_fieldNum);
qbr.value(ret);
ret = '';
}
if(ret)
{
ret += ',';
}
if(_set)
{
_set.add(_value);
}
ret += _value;
return ret;
}
;
switch(_common.TableId)
{
case tableNum(LogisticsControlTable):
qbdsLogisticsTable = element.query().dataSourceTable(tableNum(LogisticsControlTable));
qbrLogisticsId = qbdsLogisticsTable.addRange(fieldNum(LogisticsControlTable, LogisticsId));
fds = _common.dataSource();
for(logisticsTable = fds.getFirst(true) ? fds.getFirst(true) : _common;
logisticsTable;
logisticsTable = fds.getNext())
{
rangeLogId = addrange(rangeLogId, logisticsTable.LogisticsId, qbdsLogisticsTable, fieldNum(LogisticsControlTable, LogisticsId),logIdSet);
}
qbrLogisticsId.value(rangeLogId);
break;
}
}
// This set the query and gets the values passing them to the range i.e. "SO0001, SO0002, SO000£...
The second methods is as follows;
private void setQueryEnableDS()
{
Query queryLocal = element.query();
;
}
Also on the init method this is required;
public void init()
{
;
super();
if(element.args() && element.args().dataset())
{
this.setQueryRange(element.args().record());
}
}
Hope this helps in the future for anyone else who has the issue I had.

write condition statements in a sharepoint SPListItemCollection before binding the method to asp.net repeater

My situation is like this:
I have a function that returns a value as SPListItemCollection and I bind this function to a repeater.
My problem is how can I do some conditional formatting before the return value?
SPListItemCollection GetListItems()
{
SPWeb curWeb = SPContext.Current.Site.RootWeb;
string siteUrl = SPContext.Current.Web.Url;
SPListItemCollection curItems = GetDep(ListName, department);
// write condition here so that it checks if the item count is higher or
//lower than a specified number.
return curItems;
}
thank you for your help.
Im not 100% sure what you want to do before the return. If you only want to check if the resultItem.Count is bigger then 100 for example, you can do this:
SPListItemCollection GetListItems()
{
SPWeb curWeb = SPContext.Current.Site.RootWeb;
string siteUrl = SPContext.Current.Web.Url;
SPListItemCollection curItems = GetDep(ListName, department);
if (curItems.Count > 100)
{
// change the items or do whatever you want. after that, return:
foreach(SPListItem item in curItems)
{
//format/change
}
return curItems;
}
// return, without any changes
return curItems;
}
Try smth like
SPListItemCollection GetListItems()
{
SPWeb curWeb = SPContext.Current.Site.RootWeb;
string siteUrl = SPContext.Current.Web.Url;
SPListItemCollection curItems = GetDep(ListName, department);
var itemsForDepartment = curItems.GetDataTable().Rows.Where(r => r["Department"] == department); // you can try to do this is caml too
if(itemsForDepartment.Count > itemCount) {
// insert the "show me more" link
}
var itemsForDepartment = itemsForDepartment.Take(itemCount);
// bind itemsForDepartment to a Repeater
return curItems;
}
I have not compiled this code so you will have to correct some syntax mistakes ;)

Most efficient way to check to see if there is any data in a SQL row

The code below works, but I know it can't be the most efficient. Is there another way to ask if there are any rows rather than using Any()?
I'd like to have the NoResults Div hidden by default and only turned on when no rows are present, likewise have the repeater show up by default and only hidden when no results are listed.
using (AgileEntities context = new AgileEntities())
{
int StoryID = Convert.ToInt32(Request["StoryID"]);
var tasks = from t in context.Tasks
where t.StoryId == StoryID
orderby t.Number
select t;
rptTasks.DataSource = tasks;
rptTasks.DataBind();
if (tasks.Any())
{
rptTasks.Visible = true;
NoResults.Visible = false;
}
else
{
rptTasks.Visible = false;
NoResults.Visible = true;
}
}
Caution - calling .Any() may re-execute your query
I would do this a bit 'safer' to ensure single execution.
//force execution once
var taskList = tasks.ToList();
rptTasks.Visible = taskList.Count>0;
NoResults.Visible = taskList.Count==0;
And
rptTasks.DataSource = tasksList;
rptTasks.DataBind();
The problem with Any() and Count() is they cause your code to execute over and over - a test case
static void Main(string[] args)
{
//Populate the test class
List list = new List(1000);
for (int i=0; i o.CreateDate.AddSeconds(5) > DateTime.Now);
while (newList.Any())
{
//Note - are actual count keeps decreasing.. showing our 'execute' is running every time we call count.
Console.WriteLine(newList.Any());
System.Threading.Thread.Sleep(500);
}
}
You can replace Any() with Count() above to show. Basically the code keeps evaluating the query when you call Any() - I'm not sure if this applies to Linq to Sql though if there is any different caching mechanism.
var tasks = from t in context.Tasks
where t.StoryId == StoryID
orderby t.Number
select t;
var tasksList = tasks.ToList();
rptTasks.DataSource = tasksList;
rptTasks.DataBind();
if (tasksList.Count > 0)
{
rptTasks.Visible = true;
NoResults.Visible = false;
}
else
{
rptTasks.Visible = false;
NoResults.Visible = true;
}
The ToList() call will execute the query and create a list of tasks objects
Your DataBind() call has already caused the query to be executed, so calling Any() on top of that shouldn't cost you anything further.
You can change this with :
rptTasks.Visible = tasks.Any();
NoResults.Visible = !rptTasks.Visible;

How to know which value items where selected from a CheckBoxList using Request.Form?

How to get which value items where selected from a CheckBoxList using Request.Form?
I see these 2 form keys:
[12]: "ctl00$MainContent$cblTimeOfDay$0"
[13]: "ctl00$MainContent$cblTimeOfDay$3"
0 and 3 are the selected values from my check box list which has 4 items.
I'd need to find those values programmaticlaly on Page_Init
thanks,
I'm not sure about accessing these via the Request.Form. Can't you access the strongly-typed CheckBoxList control itself? This article provides a simply method that accepts a CheckBoxList and returns all the selected values; you may update this to return a reference to the selected item, or any other specifics you require:
public string[] CheckboxListSelections(System.Web.UI.WebControls.CheckBoxList list)
{
ArrayList values = new ArrayList();
for(int counter = 0; counter < list.Items.Count; counter++)
{
if(list.Items[counter].Selected)
{
values.Add(list.Items[counter].Value);
}
}
return (String[]) values.ToArray( typeof( string ) );
}
So, within your Page_Init event handler, call like so:
var selectedValues = CheckboxListSelections(myCheckBoxList);
Where myCheckBoxList is a reference to your CheckBoxList control.
I wrote this method which works but not with the best performance:
public static TimeOfDay Create(NameValueCollection httpRequestForm, string checkBoxId)
{
var result = new TimeOfDay();
var selectedCheckBoxItems = from key in httpRequestForm.AllKeys
where key.Contains(checkBoxId)
select httpRequestForm.Get(key);
if (selectedCheckBoxItems.Count() == 0)
{
result.ShowFull = true;
return result;
}
foreach (var item in selectedCheckBoxItems)
{
var selectedValue = int.Parse(item.Substring(item.Length));
switch (selectedValue)
{
case 0:
result.ShowAm = true;
break;
case 1:
result.ShowPm = true;
break;
case 2:
result.ShowEvening = true;
break;
case 3:
result.ShowFull = true;
break;
default:
throw new ApplicationException("value is not supported int the check box list.");
}
}
return result;
}
and use it like this:
TimeOfDay.Create(this.Request.Form, this.cblTimeOfDay.ID)

Resources