How can select different drop down value everytime in for loop - selenium

My aim is to add the different drop down value 3 times in the script.
By using for I am able to select and add the same value 3 times but want to select and add the different drop down value every time.
Right now i am able add the same skill three times but i want to add three.
Please suggest how can different value every time the loop executes
here is the code which i am using right now.
for(int i=0; i<=2; i++){
Select skill = new Select(m1.findElement(By.xpath(".//select[#id='skill']")));
skill.selectByValue(skills);
Select proficiency = new Select(m1.findElement(By.xpath(".//select[#name='proficiency']")));
proficiency.selectByValue("3");
m1.findElement(By.xpath(".//button[#id='addskill']")).click();
Thread.sleep(2000);

If you want add diffrent dropdown value 3 times use dropdown.selectByIndex(index).
Select skill = new Select(m1.findElement(By.xpath(".//select[#id='skill']")));
Select proficiency = new Select(m1.findElement(By.xpath(".//select[#name='proficiency']")));
for(int i = 1; i <= 3; i++){
skill.selectByIndex(i);
proficiency.selectByIndex(i);
m1.findElement(By.xpath(".//button[#id='addskill']")).click();
Thread.sleep(2000);
}
OR
For random value selection in the drop down items.You need know how many items are their in the dropdown first.
Select skill = new Select(m1.findElement(By.xpath(".//select[#id='skill']")));
List<WebElement> listOptionDropdown = skill.getOptions();
int dropdownCount = listOptionDropdown.size();
Select proficiency = new Select(m1.findElement(By.xpath(".//select[#name='proficiency']")));
List<WebElement> listOptionDropdown1 = skill.getOptions();
int dropdownCount1 = listOptionDropdown1.size();
for(int i = 1; i <= 3; i++){
int random = (int)(Math.random());
random = random*dropdownCount +1;
skill.selectByIndex(random);
int random1 = (int)(Math.random());
random1 = random1*dropdownCount1 +1;
proficiency.selectByIndex(random1);
m1.findElement(By.xpath(".//button[#id='addskill']")).click();
Thread.sleep(2000);
}

Related

Unable to set date values in SAP B1 matrix combobox

I am trying to set values in a matrix combobox but I cannot be able to set the first value of date to that combobox. It shows blank and when I select a date, it does not fill the field anyway.
The values I get from the DB are as follows:
Here is my code below including binding the combobox field to a userdatasource:
_expDate = _form.DataSources.UserDataSources.Add("iV_15", SAPbouiCOM.BoDataType.dt_DATE, 100);
oIColumns = oIMatrix.Columns;
_colExpDate = oIColumns.Item("iV_15");
_colExpDate.DataBind.SetBound(true, "", "iV_15");
The below code runs when there is a lost focus change event to the item selection field:
#region Item Change Event Expiry dates
_cmbExpDate = (SAPbouiCOM.ComboBox)oIMatrix.Columns.Item("iV_15").Cells.Item(pVal.Row).Specific;
int count = _cmbExpDate.ValidValues.Count;
if (count > 0)
{
_expDate.ValueEx = "";
for (int j = 0; j <= count - 1; j++)
_cmbExpDate.ValidValues.Remove(0, SAPbouiCOM.BoSearchKey.psk_Index);
}
var expDates = (from oi in _db.OITMs
join ob in _db.OBTNs
on oi.ItemCode equals ob.ItemCode
where ob.ItemCode == _itemNo.ValueEx && oi.OnHand > 0
orderby ob.ExpDate
select new
{
ExpDate = ob.ExpDate
}).Distinct().ToList();
if (expDates.Count > 0)
{
foreach (var item in expDates)
_cmbExpDate.ValidValues.Add(item.ExpDate?.ToString(), item.ExpDate?.ToString());
_cmbExpDate.Select(0, SAPbouiCOM.BoSearchKey.psk_Index);
_expDate.ValueEx = _cmbExpDate.Value;
}
#endregion
What could be wrong. Is there a better way to achieve what I need in SAP B1?
as a test try a different data type for your:
_expDate = _form.DataSources.UserDataSources.Add("iV_15", SAPbouiCOM.BoDataType.dt_DATE, 100);
test it with: SAPbouiCOM.BoDataType.dt_LONG_TEXT
you can also try just selecting the value on the combo, instead of setting uds as well.

how to find one element from one cell of a web table without using any locator like id,xpah,name etc. of that cell in selenium

I have one dynamic table. in row=1 and column=3 the value of the cell is 2. I don't know any xpath, id, name etc.
To use td and tr how can I get that value.
Sample table
WebElement table= driver.findElement(By.id("table"));
/* List of all rows*/
List< WebElement> rows = table.findElement(By.tagName("tr"));
int row_count = rows.size();
/*Looping rows*/
for(int i=0;i< row_count;i++)
{
/* List all columns*/
List < WebElement> columns = rows.findElement(By.tagName("td"));
int column_count= columns.size();
for(int j=0;j< column_count ; j++)
{
String celltext= columns.get(j).getText();
System.out.println(celltext); ///This will print all values inside the table.
////But I want only one value from one cell, suppose from first row and third column
}
}
As you are saying if your td cell value is 2 and no other cell has value 2 then try this xpath :-
//td[text() = '2']
or
//tr[1]/td[text() = '2']
And if you want to get cell value try as below (Amusing you are using java) :-
WebElement table= driver.findElement(By.id("table"));
String cellValue = driver.findElement(By.xpath("//table/tr[1]/td[3]")).getText()
Edited :- you should try to get text from first row third column as below :-
String thirdColumnText = table.findElement(By.tagName("tr")).get(0).findElements(By.tagName(td)).get(3).getText();
Note :- For more better solution, need to share your table HTML
hope it helps..:)

Fetching the column values inside the grid

In my table, having 5 row 4 columns, I want to fetch only 3 rd column data values present in all the rows.
Can anyone give me a code template to achieve this task?
your code will be something like:
List<String> values = new List<String>();
int row =5;
for (int i=1; i<=row; i++)
{
values.add(driver.Find(By.XPath(".//table/tbody/tr[" + i + "]/td[3]").Text) ;
}

Selenium WebDriver how to select a random option from select option menu excluding one option

I have code select option:
Select kraj = new Select (driver.findElement(By.id("kraj")));
kraj.selectByIndex(new Random().nextInt(user_country.getOptions().size()));
This code is ok, but i'd like exclude one of the select option to be random.
How can I do this?
selectByIndex random all index, but I'd like the first index would not be random (first index named always the same: "--your choice--"
add couple of lines of code to define a range
Random randomOption = new Random();
int startOption = 1; //assuming "--your choice--" is index "0"
int endOption = user_country.getOptions().size()); // end of range
int number = startOption + randomOption .nextInt( endOption - startOption);
//then do your stuff
Select kraj = new Select (driver.findElement(By.id("kraj")));
kraj.selectByIndex(number );
#ivan Try This
Select menu = new Select(driver.findElement(By.xpath("")));
menu.selectByVisibleText("Enter the option which you want to select");
You can try this:
int random=0;
do{
random=new Random().nextInt(user_country.getOptions().size());
}while(random==0);
Select kraj = new Select(driver.findElement(By.id("kraj")));
kraj.selectByIndex(random);
The above loop will run till the value of random comes out not equal to '0'. In case the value of random is different, then it will come out of the loop and select the related index of the selection menu.
You can change the the value in while(random==0) from "0" to any other value that comes in the range of '0 to user_country.getOptions().size()-1' and that will be excluded from selection.
Try this
// Assume the select has 5 options
Random random = new Random();
// Get a random number between 0 and 3 (that is size - 1, random.nextInt() generated is 0 inclusive, 4 exclusive.
int optionIndex = random.nextInt(user_country.getOptions().size() - 1);
// Increment optionIndex by 1 to pick an option between 1 to 4
kraj.selectByIndex(optionIndex++);
The above code is essentially the opposite of random.nextInt(range) where 0 is exclusive and range-1 is inclusive.

How to verify character count of text field?

I want to check the number of characters I can insert in a text field, and was thinking of using 'for loop' but it would not help as Selenium tries to insert more than required character the field will not accept but test goes on without any failure, so is there a way to get character count of the text field?
Would this work?
final String myLongString = "Something horrrrribly looooong";
final int longStringLength = myLongString.length();
// assuming driver is a healthy WebDriver instance
WebElement elem = driver.findElement(By.id("myInput"));
elem.sendKeys(myLongString);
// it's possible that you'll first need to lose focus on elem before the next line
int realLength = elem.getValue().length();
assertEquals(longStringLength, realLength);
Using Protractor I captured the actual text in the field and then did a forloop to count each letter.
element(by.css('elementPATH')).getAttribute('value').then(function(words){
//forloop to count each word
var x = 0
for(var i = 0; i < words.length; i++) {
x = x + 1;
};
//check condition
expect(x).toBe(200);
return true;
});
Let me know if this helps.