How Do I Select Everything In a Text Field With WebDriver - java

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));

Related

Variable in for loop is giving a message that "The value of the local variable i is not used"

I wrote a for loop that is supposed to determine if there is user input. If there is, it sets the 6 elements of int[] valueArr to the input, a vararg int[] statValue. If there is no input, it sets all elements equal to -1.
if (statValue.length == 6) {
for (int i = 0; i < 6; i++) {
valueArr[i] = statValue[i];
}
} else {
for (int i : valueArr) {
i = -1;
}
}
I am using Visual Studio Code, and it is giving me a message in for (int i : valueArr) :
"The value of the local variable i is not used."
That particular for loop syntax is still new to me, so I may be very well blind, but it was working in another file:
for(int i : rollResults) {
sum = sum + i;
}
I feel that I should also mention that the for loop giving me trouble is in a private void method. I'm still fairly new and just recently started using private methods. I noticed the method would give the same message when not used elsewhere, but I do not see why it would appear here.
I tried closing and reopening Visual Studio Code, deleting and retyping the code, and other forms of that. In my short experience, I've had times where I received errors and messages that should not be there and fixed them with what I mentioned, but none of that worked here.
for (int i : valueArr) {
.... CODE HERE ...
}
This sets up a loop which will run CODE HERE a certain number of times. Inside this loop, at the start of every loop, an entirely new variable is created named i, containing one of the values in valueArr. Once the loop ends this variable is destroyed. Notably, i is not directly the value in valueArr - modifying it does nothing - other than affect this one loop if you use i later in within the block. It does not modify the contents of valueArr.
Hence why you get the warning: i = -1 does nothing - you change what i is, and then the loop ends, which means i goes away and your code hasn't changed anything or done anything, which surely you didn't intend. Hence, warning.
It's not entirely clear what you want to do here. If you intend to set all values in valueArr to -1, you want:
for (int i = 0; i < valueArr.length; i++) valueArr[i] = -1;
Or, actually, you can do that more simply:
Arrays.fill(valueArr, -1);
valueArr[i] = -1 changes the value of the i-th value in the valueArr array to -1. for (int i : valueArr) i = -1; does nothing.

Add multiple records using forr loop in Selenium

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) {

for loop jumps to last number in java android development

Hi I am making an android App, I want to add some values to a database and I want to do N times so I used a for loop as seen below:
private void addCodeToDataBase() {
for (int i = 1; i <= 100; i++) {
//indexnumber is a TextView
indexNumber.setText("Please enter the TAN code for Index number " + i);
//tanCode is an EditText
if (tanCode.getText().toString() != null) {
//index here is just an int so i can use the i inside the onClick
index = i;
//add is a button
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String codeText = tanCode.getText().toString();
dbHandler.addcode(index, codeText);
}
});
} else {
Toast.makeText(addcode.this, "Please enter your code !!!", Toast.LENGTH_LONG).show();
}
}
}
but what I am facing here is the for loop jumps to 100 at the first run, What I mean is the text will show :
Please enter the TAN code for Index number 100
it skips 99 numbers!! how would I fix it ?
It's Because your for loop executes so fast that you can't notice that the change of the text.First i is 0,and then it becomes 1,then the text will be "Please enter the TAN code for Index number 1" ......
your loop is working correctly but it is replacing text on each iteration that's why you think that it is jumping on last value please use break point and debug you will see each value on each iteration or use log in which you will see each value
It's not easy to imagine what your code does without seeing your declarations of indexNumber, tanCode, index, and, in particular, add. So, e.g., we don't know how often your if condition yields true.
However, most probably, the problem is that your assignment add.setOnClickListener(...) is just iterated with no user interaction in between. Now if you repeatedly assign something to your add (whatever that is), the last assignment will win.
If you want 100 buttons, you'll need to have an array or List of buttons to press, where each has a different tan code. If you want one button that repeatedly asks for the different tans, then you have to assign the data for click i + 1 only after click i has been handled, i.e. in the on click listener.
To give more specific help, we would need to know how your user interface should look (how many widgets of what kind) and how each widget should behave.

Use Selenium to press tab and then write with java

I want selenium to press "TAB" for me and then write something in the focused field, now I know that I can use
sendKeys(Keys.TAB)
But as I understand it That require a locator behind it, I want to test the tab order of my page and to do so, I want to be able to focus on my first element only, then tab my way through the page like this:
--THE ELEMENTS IN THE TAB ORDER THEY ARE SUPOSED TO BE--
String[] elementArray = {"firstname","lastname", "phone", "email"};
for(int x = 0; x < 4; x = x+1)
{
WebElement theElement = driver.findElement(By.id(elementArray[x]));
if (x == 0) {driver.theElement.sendKeys(x)}
else{driver.(TheCurrentlyFocusedElement).sendKeys(x)}
String elementval = theElement.getAttribute("value");
assertEquals(x, elementval);
(TheCurrentlyFocusedElement).sendKeys(Keys.TAB);
}
So the question is, is there a method I can use that allows me to use the currently focused element as a locator? i.e.:
driver.focusedElement().sendKeys(Keys.TAB); ?
This is what you are looking for -
driver.switchTo().activeElement();
I have just had to do something similar but found that sending tab didn't actually change focus (it did when manual testing, just not via Selenium).
I wanted to prove the order of input fields on screen (positioning was more important then tabbing in this case), so had to do something like the following;
String[] elementArray = {"firstname","lastname", "phone", "email"};
List<WebElement> inputFields = driver.findElements(By.cssSelector("input"));
for(int x = 0; x < 4; x++)
{
assertEquals(elementArray[x], inputFields.get[x]);
}
I would probably create a list rather than an array, and compare those, but you should get the gist.
Yes, I know that this is quite a simplified approach and that this doesn't actually prove that fields are definitely in that order once rendered, but it gives you some security for regression.

Problem with converting a string to an integer and passing as an ID to a view

There are 20 buttons in an Activity .
The ids are R.id.ButtonR1C1; R.id.ButtonR1C2 .. and so on ie. Row 1, Col 1..
now earlier I had created 20 buttons.
private Button b1,b2,b3...;
and then
b1=(Button)findViewbyId(R.id.ButtonR1C1);
b2=(Button)findViewbyId(R.id.ButtonR1C2);
.... and so on.
Finally
b1.setOnClickListener(this);
b2.setOnClickListener(this);
... 20
so I thought I'd create a Button Array
Button barray[][]=new Button{4][5];
for(int i=1;i<=4;i++) {
for (int j=1;j<=5;j++) {
String t="R.id.ButtonR"+i+"C"+j;
barray[i-1][j-1]=(Button)findViewbyId(Integer.parseInt(t));
}
}
Gives an error..
Any help??
If you have copied your code directly, your syntax is wrong.
Button barray[][]=new Button{4][5];
should be
Button barray[][]=new Button[4][5];
Note the curly bracket instead of square bracket before the 4.
Your next problem, is that you are trying to get the value of R.id.something, but you only have the string representation of it. So, the only way I know to do this is using reflection.
What you need to do is,
Button barray[][]=new Button{4][5];
for(int i=1;i<=4;i++) {
for (int j=1;j<=5;j++) {
Class rId = R.id.getClass();
// this gets the id from the R.id object.
int id = rId.getField("ButtonR"+i+"C"+j).getInt(R.id);
barray[i-1][j-1]=(Button)findViewbyId(id);
}
}
String t="R.id.ButtonR"+i+"C"+j;
Integer.parseInt(t);
This part of your code will definitly throw a runtime exception, because t does not contain a numeric value.
I don't know the format/value of your ID values, but this might work:
Button barray[][]=new Button[4][5]; // type corrected
for(int i=1; i<4; i++) { // changed the for loop...
for (int j=1; j<5; j++) { // changed the for loop...
String t="R.id.ButtonR"+i+"C"+j;
barray[i-1][j-1]=(Button)findViewbyId(t); // t *is* the id (?)
}
}
The Problem is here:
String t="R.id.ButtonR"+i+"C"+j;
barray[i-1][j-1]=(Button)findViewbyId(Integer.parseInt(t));
You're trying to convert a String t to an Integer (while the String is not numeric at all).
This will result in NumberFormatException. You should make sure that t is absolutely numeric.
I used this code:
Class c = Class.forName("com.test.R$id");
Integer i = new Integer(c.getField("ToolsbuttonR3C4").getInt(new R.id()));
System.out.println("HEXA="+i.toHexString(i));
and the ids are in sequential order. so it can be done.
Thanks ppl
So you are trying to convert the String "R.id.ButtonR1C1" to a NUMBER...
That will most certainly give you a NumberFormatException since it is CLEARLY NOT A NUMBER! :)

Categories

Resources