Assert given text is not present in Drop Down Combo Box - selenium-webdriver

I had to automate scenario where i get First Name and email address , which i had stored then i need to assert that value with drop down box that its not present.
Here is HTML code for my Page
<select id="Customer" name="Customer" class="valid">
<option value="raj777#gmail.com">123123123 (raj777#gmail.com)</option>
</select>
It contained multiple entries ,
I need to verify that my given text does not exist in it.
I tried this but it does not works
assertNotEquals(fname+" "+em, driver.findElement(By.xpath("//*[#id='Customer']")).getText());
Thanks In Advance !!!

Follow the below process:
Get the text of each element in the dropdown ('option' elements)
Strote the values in a list or array of string.
See the list or array contains your Name & mail id value.
or if u want to assert only, then in for-each loop of the array or list:
Add t
he assert statement in try-catch block.
In catch block, each time execution comes to this block, increase an integer.
After the fr-each loop, check the integer value is equal to yor list or array size.
Code sample for the first scenario:
driver.findElement(By.cssSelector(".trb_outfit_sponsorship_logo_img"))
.click();
System.out.println(dri.findElement(
By.cssSelector(".trb_outfit_sponsorship_logo_img")).getSize());
List<WebElement> dropDownValues = dri.findElements(By
.xpath("//select[#id='Customer']"));
ArrayList<String> dropDownValuesasText = null;
for (WebElement eachValue : dropDownValues) {
dropDownValuesasText.add(eachValue.getText());
}
// boolean result = dropDownValuesasText.contains("Your FirstName & Email value");
//It will pass if your value is present in drop down
assertNotEquals(dropDownValuesasText.contains("Your FirstName & Email value"), true);

Related

How to get all the element count under the div with selenium

I am trying to get the count of the search results returned in MakeMyTrip application by searching the flights from Hyderabad to Bangalore. By using the below I am able to get the text but how to verify how many number of search results returned.
String output = driver.findElement(By.xpath("//*[#id=\"left-side--wrapper\"]/div[3]")).getText();MakeMyTrip Flight Search
System.out.println(output);
Thanks in Advance
You should use driver.findElements(); method like this below:
// your webelement
By eachSearchElement = By.xpath("//*[#id='left-side--wrapper']/div[3]");
// getting all of available elements on the page and store them in List
List <WebElement> allSearchElements = driver.findElements(eachSearchElement);
// then just simply get the size of particular List above
int howManyElements = allSearchElements.size();
System.out.println("There are " + howManyElements + " present on the page");
Hope this will help.

How to use Group by query in laravel?

this is how the data is displayed but i want
Rhugveda desai -> flowers,Sarees,Prasad
In my application i need to use group by clause . But i am getting a syntax error.Also, What should i do if i want quantity column to be multiplied by amount to get the total? My tables are inkind and inkind_items, where inkind.id is foreign key in inkind_items table as inkind_id.
SQLSTATE[42000]: Syntax error or access violation: 1055 Expression #11
of SELECT list is not in GROUP BY clause and contains nonaggregated
column
my inkind_items tabel is inkind_items
my inkind table is inkind
My query is:
$inkinds = DB::table('inkind')
->join('inkind_items', 'inkind.id', '=', 'inkind_items.inkind_id')
->select('inkind.*', 'inkind_items.*')
->groupBy('inkind_items.inkind_id')
->get();
Try using group_concat()
$inkinds = DB::table('inkind')
->join('inkind_items', 'inkind.id', '=', 'inkind_items.inkind_id')
->select('inkind.*', DB::raw('group_concat(inkind_items.name) as items'))
->groupBy('inkind_items.inkind_id')
->get();
Here I'm assuming inkind have field name and inkind_items has fields items.
You can use Laravel collection methods for that.
Just call:
$inkinds->groupBy('inkind_id');
after ->get(). Considering that inkind_id is unique column for both tables
Hi. You asked another question earlier today (about displaying an input when a particular checkbox is checked) but deleted it before I submitted my answer, so I thought I would paste the answer here instead:
Just to get you started, here is an explanation of how to use
addEventListener and createElement to achieve your desired result.
If any part of it is still unclear after studying the code and the
accompanying comments, please search for the name of the still-unclear function on
MDN.
(For example, https://developer.mozilla.org/en-US/docs/Web/API/Document/getElementsByClassName.)
// Sets `box1` to refer to the first element on the page with the class "box".
const box1 = document.getElementsByClassName("box")[0];
// Sets `container` to be the element whose id attribute has the value "container".
// (This is where our new input element, inside a new label element, will be added.)
const container = document.getElementById("container");
// Begins listening for clicks. From now on, whenever the user clicks anywhere
// on the page, our listener will call the `noticeClick` function.
document.addEventListener("click", noticeClick);
function noticeClick(event){
// Because this function's name is the second argument in
// the call to `document.addEventListener`, above, it is
// automatically called every time a 'click' event happens on the
// page (and it automatically receives that event as an argument.)
// The "target" of the event is whatever the user clicked on.
// So we set the variable `targ` to refer to this target, and we check whether:
// 1) the clicked target is our checkbox,
// 2) this click caused the checkbox to gain the "checked" attribute, and
// 3) we have not already given the checkbox the "added" class
const targ = event.target;
if(targ.id == "box1" && targ.checked && !targ.classList.contains("added")){
// If all three conditions are met, we...
// ...set two variables, `label` and `val`
const label = event.target.id;
const val = event.target.value;
// ...call the `createInput` function, passing these variables as its two arguments
createInput(label, val);
// ...give the checkbox the "added" class (so we can avoid accidentally adding it again)
targ.classList.add("added");
}
}
function createInput(newLabel, newValue){
// When the user has checked our checkbox, the `noticeClick` function
// will call this function, which receives two arguments (which we can
// see, by examining the `noticeClick` function, are two strings: the
// `id` attribute of box1 and the `value` attribute of box1.)
// We use `document.creatElement` to create an `input` element and a
// `label` element, and `document.createTextNode` to set some text
// to be used in the label (using the "newLabel" argument.)
const myInput = document.createElement("input");
const myLabel = document.createElement("label");
const myLabelText = document.createTextNode(newLabel + " ");
// We set our new `input` element's value using the "newValue" argument.
myInput.value = newValue;
// We use `appendChild` to put both the text and the input element
// inside the label, and to put the label inside `container`.
myLabel.appendChild(myLabelText);
myLabel.appendChild(myInput);
container.appendChild(myLabel);
}
// This process can be applied to multiple checkboxes on the same page
// by adding a loop inside the `noticeClick` function, where the string
// "box1" should be replaced with a variable that can refer to the id of a
// different checkbox's `id` for each iteration of the loop.
<label>
<input type="checkbox" id="box1" class="box" value="value1" />
Label for box1
</label>
<hr />
<div id="container"></div>

How to check if each and every element has an ID?

We just want to make sure that in the Web codes implemented by Dev team, every element has an ID property value.
How can I check? Thanks!
Test this xpath in your browser: //*[string(#id)] This will not only give you elements which have an id, but also whose id's have some value. It might be possible that the devs forgot to enter the id. Then, use this //* to get all the elements. If you want elements that have an id (blank or filled), then use this //*[#id]
(Assuming you're using Java) there is a method WebElement#getAttribute() where you can get any attribute's value of the element..
We just want to make sure that in the Web codes implemented by Dev team, every element has an ID property value. How can I check?
You need to find all elements first then inside loop you can determine whether every element has attribute ID or not using getAttribute("id") as below :-
public boolean isAllElementsHasId()
{
boolean isAllElementsHasId = true;
List<WebElement> allPageElements = driver.findElements(By.cssSelector("*"));
for(WebElement el : allPageElements)
{
if(el.getAttribute("id") == null)
{
isAllElementsHasId = false;
break;
}
}
return isAllElementsHasId
}
Usage :
if(isAllElementsHasId() == true)
{
System.out.println("All elements in the page has an attribute ID");
}

User Profile Privacy screen with comma separated String

I have a listView with many fields and CheckBox next to each field. Screen similar to a FaceBook Profile screen with Privacy setting to each field. So friends can not see those fields if marked as Private.
On selection of CheckBox, i have to create a comma separated String.
Example,
FirstName Text ---> isFirstNamePrivate boolean
LastName Text ---> isLastNamePrivate boolean
...
I have to create a
String str = "FirstName,LastName"
if both are marked as Private.
If only isFirstNamePrivate is true then
String str = "FirstName"
Also if i receive a comma separated String from Service, with that i have to make those Boolean array.
Given::
String[] fieldNamesArray = "field1","field2","field3","field4","field5"};
Boolean[] isfieldPrivate = {true,false,true,false,true};
// fieldNamesArray.length will be equal to isfieldPrivate.length
Need to create below commaSeparatedStr from above given arrays.
//
String commaSeparatedStr = "field1,field3,field5";
Question is:
1) What is the optimized way to create a comma separated String.
2) What is the optimized way to create the Boolean array from the commaSeparatedString avoiding for loop on commaSeparatedStr .contains(str[n])
Given::
String[] fieldNamesArray = "field1","field2","field3","field4","field5"};
String commaSeparatedStr = "field1,field3,field5";
Need to create below Boolean array with commaSeparatedStr from above 2 arrays.
Boolean[] isfieldPrivate = {true,false,true,false,true};
// fieldNamesArray.length will be equal to isfieldPrivate.length
//
Where is the question part here?
If you are asking for a suggestion:
If there are 5 fields and first 3 are selected, make your string like
{1,1,1,0,0}
and pass it to your webservice. This would lighten the data package size.

How to add values to combobox in C++ Builder?

I want to add values to combobox in C++ builder 6.
I know I can add string to combobox by string list editor.
For example, I have added this list to combobox:
car
ball
apple
bird
I want behind each text, it has their own value, so I can get the value rahter than the text when user selected a text. Just like HTML select.
But when I try to add value to each text:
ComboBox1->Items->Values[0] = "mycar";
ComboBox1->Items->Values[1] = "aball";
etc...
it will add more text to the list, like
car
ball
apple
bird
0=mycar
1=aball
This is not what I want. I don't want the extra text to add to the list.
So, how can I add values to each text properly, and get the value?
If you want to store the values in the ComboBox itself, then you need to use the Objects[] property instead of the Values[] property, for example:
ComboBox1->Items->Objects[0] = (TObject*) new String("mycar");
ComboBox1->Items->Objects[1] = (TObject*) new String("aball");
...
String value = * (String*) ComboBox1->Items->Objects[ComboBox1->ItemIndex];
...
delete (String*) ComboBox1->Items->Objects[0];
delete (String*) ComboBox1->Items->Objects[1];
As you can see, this requires managing dynamically allocated String objects. A better option would be to store the values in a separate list, such as a TStringList or std::vector, like PoweRoy suggested. As long as that list has the same number of items as the ComboBox, you can use ComboBox indexes to access the values, for example:
TStringList *MyValues = new TStringList;
...
MyValues->Add("mycar");
MyValues->Add("aball");
...
String value = MyValues->Strings[ComboBox1->ItemIndex];
...
delete MyValues;
Or:
#include <vector>
std::vector<String> MyValues;
...
MyValues.push_back("mycar");
MyValues.push_back("aball");
...
String value = MyValues[ComboBox1->ItemIndex];
...
hold a list(vector/array whatever you want) containing the name and value pairs. When selecting a name look the value up in the list.

Resources