Add multiple records using forr loop in Selenium - java

I'm writing a selenium test script for my student management system. I have a situation where I need to enter values and click the same button 15 times. So, I used a for loops for the scenario.
Here is the screen I need to test.
So, I need to add two values to mark range text boxes and select grade from the drop down list and click add button. I need to do this scenario for 15 times.
Here are the values I need to enter
Here is the drop down list.
I tried following scenario for this.
for(int x=95; x<=11; x=x-6){
driver.findElement(By.xpath("//input[#type='number']")).sendKeys(""+x);
for(int y=100; y<=16; y=y-6){
driver.findElement(By.xpath("(//input[#type='number'])[2]")).sendKeys(""+y);
for(int z=1; z<=15; z++){
Select mark2 = new Select(driver.findElement(By.xpath("//select[#id='gradeSelector']")));
mark2.selectByValue(""+z);
driver.findElement(By.xpath("//input[#value='Add']")).click();
}
}
}
but, nothing happens.
Thanks in advance. :)

Try following:
int x=95, y=100;
for(int z=1; z <=15; z++){
driver.findElement(By.xpath("//input[#type='number']")).sendKeys(""+x);
driver.findElement(By.xpath("(//input[#type='number'])[2]")).sendKeys(""+y);
Select mark2 = new Select(driver.findElement(By.xpath("//select[#id='gradeSelector']")));
//mark2.selectByValue(""+z);
mark2.selectByIndex(z);
driver.findElement(By.xpath("//input[#value='Add']")).click();
x=x-6;
y=y-6;
}

It's just the for loop logic is incorrect - the x<=11 condition never evaluates to true, replace:
for(int x=95; x<=11; x=x-6) {
with:
for(int x=95; x>=11; x=x-6) {

Related

Java Mysql table data output formatting

Here is my MySql table:
I want to show the output of the query in commandline as below:
I have written the code below to loop but I am getting only the first row, What i have to modify ??
ResultSet rs2 = stmt.executeQuery(table_retrive);
String[] cols = new String[itemList.size()];
int[] rec =new int[itemList.size()];
for (int i = 0; i < itemList.size(); i++) {
while (rs2.next()) {
cols[i] =(String) itemList.get(i);
rec[i] = rs2.getInt(cols[i]);
System.out.println(rec[i]+" ");
}
}
Your two loops are wrong. Start at i=0 and then iterate once over the whole ResultSet, filling yor first array position. When this is done, i is incremented and you try to iterate the ResultSet a second time but the cursor is at the end of the ResultSet, so rs2.next() returns false and the code will not be executed.
So you have two Solutions:
Handle the loops correctly. Unfortunately I do not know, what you are trying to do anyways because this is some C-like code without OOP, which doesn't show semantics and then you have this itemList which seems to hold preset values and you read out of this list, which column to take for the i-th position. This seems odd. Maybe switching the loops does the desired: Start with the while and nest the for.
Reset the cursor of the ResultSet after the while with rs2.beforeFirst(). WARNING: This could throw a SQLFeatureNotSupportedException. Not all Databases can move the cursor backwards. This is of course a very ugly solution, since you should first parse the whole row a once.
Try to use printf() Or format() method. It is same as printf method in c lang. you can pass parameters and difference. Look at link1
And link 2
Example : System.out.printf("%d%5s%10d", 5,"|",10);
output : 5 | 10
Using this the I got all the values but in one row :
while (rs2.next()) {
for (int i = 0; i < itemList.size(); i++) {
cols[i] =(String) itemList.get(i);
rec[i] = rs2.getInt(cols[i]);
System.out.print(rec[i]+" ");
}
}
But I need to divide like the rows.
Usage of the inner loop is your problem.
You can enhance your code to remove the usage of the second loop in your code, it basically does nothing. You can loop over your result set and in the same loop using the incremented variable to persist the values accordingly.
The code shown half implemented in your question, hence it will be difficult to give you exactly what need to be done. Nevertheless, here's an attempt to resolve the problem for you:
while (rs2.next()) {
System.out.println(rs2.getInt(1) + "\t |" + rs2.getString(2) + "\t |" + rs2.getString(3));
}
Based on the column names from the table in the question, assuming that column2 and column3 are String's.
You can add the necessary details to this code to complete it according to your usecase, but I've just taken the example of showing a record in one line.
EDIT:
OP has his own way of programming, but to satisfy his question in the comment - this is how you can do it.
while (rs2.next()) {
for (int i = 0; i < itemList.size(); i++)
{
cols[i] =(String) itemList.get(i);
rec[i] = rs2.getInt(cols[i]);
System.out.print(rec[i]+"\t |");
}
System.out.println();
}

Displaying multiple lines from while loop in one output box

My assignment asks to print all values from 1-10 using while loops in one output dialog box, with each number appearing on a separate line. So far I have:
int i=1;
while (i <= 10)
{
System.out.println(i);
i++;
}
and it displays 1-10 on separate lines but that's in the command prompt window. I need it to be displayed on one output box. How would I do that?
Forgot to mention, I know how to display the output in a dialog box, however it displays it one by one in separate boxes rather than in just one. How would I make it display in only one and not 10 different boxes?
What you are looking for can be found in the Java Docs. The classes, as well as how to use them can be found here:
http://docs.oracle.com/javase/tutorial/uiswing/components/dialog.html
Be sure to import this:
import javax.swing.JOptionPane
When you're just printing to the console, all you have to do is change System.out.println to System.out.print:
int i = 1;
while (i <= 10)
{
System.out.print(i);
i++;
}
An alternative way to do it is this:
String s = "";
int i = 1;
while (i <= 10) {
s += i;
i++;
}
System.out.println(s);

Select ComboBox Item with only part of the Item

For a Java Schoolproject I would like to have a table from witch you can select a Item that then shows up on a new window. In that window you can change things like ComboBoxes and others. My only problem is, that I dont know how to select the Item of the ComboBox I need. All the ComboBoxItems are Objects and I dont know how to handle this.
My ComboBoxItem looks like this:
Apprentice [person=Person, DB ID: 9, Kappa Kappa, Kappastrasse 21,
CityID: 4521, kappa.kappa#kappa.ch, idpersonen=9,
vertragsstart=2020-01-02, ausbildungsct=2, id=6]
Now, my Question is, how do I select the ComboBoxitem where the id=6, all the things I found needed the whole Object to select a special Item. How would you guys go at this problem?
Good luck and thanks for the Help.
Bono
All I had to do was a really simple while with a for and a if.
int trys = 0;
while (0 == apprenticeComboBoxZeugnis.getItemCount() && trys < 10000) {
System.out.println(apprenticeComboBoxZeugnis.getItemCount());
for (int i = 0; i < apprenticeComboBoxZeugnis.getItemCount(); i++) {
apprenticeComboBoxZeugnis.setSelectedIndex(i - 1);
int spacko = getApprenticeCombo();
if (spacko == lehrlingsid) {
TableFilterListenerZeugnis tableFilterListenerZeugnis = new TableFilterListenerZeugnis(
this);
tableFilterListenerZeugnis.updateNoten();
break;
}
}
trys++;
}
This first trys until the number of Objects is not = 0 and after that looks at every object, cuts out the id with getApprenticeCombo() and compares it with my id I allready have. If they match it breaks out and is done with it.

How Do I Select Everything In a Text Field With WebDriver

I am testing a webpage which has a counter in a reason field and a previous reason in the reason field. If I do a field.clear(); the counter is not reset. So I am trying to do the following:
int reasonPriorCount = reason.getText().length();
reason.click();
reason.sendKeys(Keys.chord(Keys.SHIFT, Keys.ARROW_RIGHT.equals(reasonPriorCount), Keys.DELETE));
Where the reasonPriorCount is the length of the number of characters in the field. Since the counter is only responding to Change or KeyPress I am attempting to send the number of right arrow keys equal to the reasonPriorCount.
However Keys.chord is complaining about the the reasonPriorCount argument in the right arrow key press. Is there a way to do what I need to do? Am I going about this the right way?
Ok so here is what I did to make this work:
int reasonPriorCount = reason.getText().length();
int i = 0;
reason.click();
while(i < reasonPriorCount)
{
reason.sendKeys(Keys.chord(Keys.SHIFT, Keys.ARROW_RIGHT));
i++;
}
reason.sendKeys(Keys.chord(Keys.BACK_SPACE));

Restart current iteration in 'for' loop java

I have a for loop that asks the user to input a number and then does something with it 10 times
I want a check built in that, if the user enters a non accepted input, the loop should restart its current iteration
For example if the user enters something wrong in round 3, round 3 should be restarted.
How do i do that? is there something like a REDO statement in java?
something like this ?
for(int i=0; i<10; i++){
if(input wrong){
i=i-1;
}
}
You have a couple of choices here.
Continue
The continue statement in Java tells a loop to go back to its starting point. If you're using a for loop, however, this will continue on to the next iteration which is probably not what you want, so this works better with a while loop implementation:
int successfulTries = 0;
while(successfulTries < 10)
{
String s = getUserInput();
if(isInvalid(s))
continue;
successfulTries++;
}
Decrement
This approach seems ugly to me, but if you want to accomplish your task using a for loop, you can instead just decrement the counter in your for loop (essentially, 'redo') when you detect bad input.
for(int i = 0; i < 10; i++)
{
String s = getUserInput();
if(isInvalid(s))
{
i--;
continue;
}
}
I recommend you use a while loop.
Prefer avoiding to reassign the i variable, this may lead to confusion.
for(int i=0; i<10; i++){
String command = null;
do{
command = takeCommandFromInput();
} while(isNotValid(command));
process(command);
}

Categories

Resources