I'm trying to find an appropriate ExpectedConditions method for this situation. I have a chart and I want to check the text in each row after re-sorting the chart. Problem is, when the chart is refreshing, the text still exists, it's just greyed out. So when I click a button to get the chart to be re-sorted, then look for the text that I'm looking for right away, the test fails because the text hasn't changed yet. I can't use visibilityOfElementLocated because the element is still visible when the chart is refreshing, I'm just waiting for the element to change.
Not sure if any of that makes sense!! It is a really difficult issue to explain.
A little background: I'm using Selenium Java and testing using Chrome. Here is my method thus far. It works fine, I just need to figure out how to make the program wait long enough for the chart to refresh WITHOUT using a sleep statement.
Thanks a bunch everyone! I know that wasn't as clear as it could be, but please let me know if you need any clarification.
public void Check_for_text_in_column(String text, String row, String column)
{
By by = By.xpath("//*[#id=\"table_Table_table_ktg\"]/tbody/tr[" + row + "]/td[" + column + "]/div/div/span");
WebDriverWait wait = new WebDriverWait(getWebDriver(), WAIT_TIME);
//This is the line that I need to change:
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
if(!element.getText().equals(text))
{
fail("\nDid not find text: " + text + "\nFound text: " + element.getText() + "\n");
}
}
Cheers!
You can replace
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
with
WebElement element = wait.until(ExpectedConditions.textToBePresentInElement(by, text));
EDIT:
Your WAIT_TIME is the timeout for your wait.
If the expected condition hasn't returned true before timing out according to your WAIT_TIME, then element will be null.
So, your check could look something like this:
if(element == null)
{
fail("\nDid not find text: " + text + "\nFound text: " + element.getText() + "\n");
}
EDIT:
Perhaps another option could be something like this:
public void Check_for_text_in_column(String text, String row, String column)
{
By by = By.xpath("//*[#id=\"table_Table_table_ktg\"]/tbody/tr[" + row + "]/td[" + column + "]/div/div/span");
WebDriverWait wait = new WebDriverWait(getWebDriver(), WAIT_TIME);
// your original find
WebElement element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
// flag to set when text is found, for exiting loop
boolean hasText = false;
// counter for # of times to loop, finally timing out
int tries = 0;
// until text is found or loop has executed however many times...
while (hasText == false && tries < 20) {
// get the element
element = wait.until(ExpectedConditions.visibilityOfElementLocated(by));
// if text is not present, wait 250 millis before trying again
if(!element.getText().equals(text){
Thread.sleep(250);
tries++;
}
else{
// text found, so set flag to exit loop
hasText = true;
}
}
if(!element.getText().equals(text))
{
fail("\nDid not find text: " + text + "\nFound text: " + element.getText() + "\n");
}
}
I know you said you don't want sleep statements, but I assume you meant that you just don't want a single unnecessarily long one. Even ExpectedConditions are using sleep internally. They sleep for a few milliseconds between polling for changes - and that's exactly what this does, just without the ExpectedCondition type wrapper.
Related
I am getting ~20'000 rows out of a database with JDBC in Java, Eclipse IDE.
I go through the result set step by step (ordered by id). Every row contains information about its previous and succeeding "identifier" (unique string of a row). So for every row I check if the chain is broken or not, b/c of whatever reason.
My approach beneath works, and don't quote me on that, but it seems it works better for the first 10'000 than for the following. Error quota is 335 for 19'999 entries. I checked with my own eyes if the reported errors are in accordance with reality, but they are definitely not, at least for 1 error.
Do I miss something important? Why does this happen? It almost looks like it is the result of parallelization, multithreading, etc.?
int i = 0;
String actualprevious = "", previous = "", next = "";
boolean first = true; // we can't check if the first is in line because it is the first
int errors = 0;
while (rs.next())
{
if (i%10000==0) { System.out.println("Checked "+(i/10000)+" myriads."); } // inform
String current = rs.getString("identifier");
if (!current.equals(next)) { System.out.println("Current is: "+current); System.out.println("Expected "+next+" to be next, but "+current+" is."); errors++; } // inform
// ignore: Document doc = Jsoup.parse(rs.getString("source"));
next = rs.getString("next");
if (next==null) { System.out.println("There is no next listed in row "+current+"."); errors++; } // inform
previous = rs.getString("previous");
if (!first && !actualprevious.equals(previous)) { System.out.println("Expected "+actualprevious+" to be listed as previous, but "+previous+" was in document "+current+"."); errors++; } // inform
actualprevious = current;
i++;
first = false;
}
There is a table with application numbers and user data. at the beginning of the table, the application number at the end is a green arrow. There can be an infinite number of fields in a table and it always changes.
I need to find the green arrow by the application number and click on it accordingly.
tried to do something like
String myorder = "629/0000/000000021059";
if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div/div[1]/table/tbody/tr/td[5]\n")).getText())){
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[1]/table/tbody/tr/td[18]/a/img\n")).click();
} if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[2]/table/tbody/tr/td[18]/a/img\n")).click();
} if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[3]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[3]/table/tbody/tr/td[18]/a/img\n")).click();
and so on up to 100+
The first problem is that I really don't like the option of writing if else 100 times. And there is only one digit in the diva where the id of the application and one in the digit in the diva where the green arrow is different(img)
The second problem is that no matter how I insert break; when finding the right application, he clicks on the desired arrow, but after apparently trying to continue searching the page, but the page has already changed and the error falls
(WARNING: The server did not provide any stacktrace information)
if I use the code that was attached above or just an indication of the element (which goes right after the one that turned out to be correct and the page has changed) with a note that I can not find it, if I use else if with break;
tried to do so
for (int i = 1; i < 25; i++) {
String myorder = "629/6300/000000412067";
if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div["+ i++ +"]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div["+ i++ +"]/table/tbody/tr/td[18]/a/img\n")).click();
break;
}
But if the item is located, then it clicks on the very first arrow in the table, and not on the one in the same column as the application number
I would really appreciate your help!
XPaths are 1-based not 0, so you are correct in thinking you need to add one, however, these two are not equal as i++ modifies the value of i (even inline like you have it), which you do twice inside your loop. Therefore the 2nd iteration of your loop i will be 3, not 1 as expected. In your loop replace i++ with i + 1 and you should be good to go.
for (int i = 1; i < 25; i++) {
String myorder = "629/3500/000000329976";
if (myorder.equalsIgnoreCase(driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[" + i + 1 + "]/table/tbody/tr/td[5]\n")).getText())) {
driver.findElement(By.xpath("/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[" + i + 1 + "]/table/tbody/tr/td[18]/a/img\n")).click();
break;
}
}
Unable to locate element: {"method":"xpath","selector":"/html/body/table/tbody/tr[1]/td[2]/div[2]/div/div[2]/div[2]/div/div/div/div[1]/div[31]/table/tbody/tr/td[5]
"}
So it was searching untill 31. And it's strange because i used for i < 25 and there are only 25 rows in the table.
but still dont work for me;(
I've decided to programm a search system for finding students and teachers in a school via GUI. It is an OOP and need some tweaking here and there, but there is one issue which doesn't seem logical to me. When I'm searching for a teacher, I have to type there name or surname into a JTextField and press the Search button which runs a method that loops through an ArrayList of teacher-objects and checks if their names match with the one in the Textfield. Then it checks if these teachers have multiple subjects and grades and it goes through nested if-statements. After the teacher is found, their information is displayed on a GUI with several Texfields. Theoretically if the name I typed into the TextField doesn't match one from the teacher objects, a Error Message should pop-up saying the teacher I'm looking for isn't in the system. But even though I type in the correct name and get all the information displayed, it sends me to the Error Message everytime. I tried to fix it with a break statement but that didn't work either. Can someone please help me with this.
Here is the code I'm talking about:
public void lehrerSuche()
{
String lehrername = tfSuchfeldLehrer.getText();
for(int i = 0; i < td.getFachliste(0).getListenLaengeLehrerListe();i++)
{
if(td.getFachliste(0).getLehrerliste(i).getName().equals(lehrername) || td.getFachliste(0).getLehrerliste(i).getNachname().equals(lehrername))
{
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() && td.getFachliste(0).getLehrerliste(i).isMehrereklassen())
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfKlasse_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlass2().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
tfFach_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach2().getFachbezeichnung());
}
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() == false && td.getFachliste(0).getLehrerliste(i).isMehrereklassen())
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfKlasse_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlass2().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
}
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() && td.getFachliste(0).getLehrerliste(i).isMehrereklassen()==false)
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
tfFach_2Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach2().getFachbezeichnung());
}
if(td.getFachliste(0).getLehrerliste(i).isMehrerefaecher() == false && td.getFachliste(0).getLehrerliste(i).isMehrereklassen()==false)
{
tfNameLehrer.setText(td.getFachliste(0).getLehrerliste(i).getName() + " " + td.getFachliste(0).getLehrerliste(i).getNachname());
tfKürzelLehrer.setText(td.getFachliste(0).getLehrerliste(i).getKuerzel() + ".");
tfKlasse_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getKlasse().getBezeichnung());
tfFach_1Lehrer.setText(td.getFachliste(0).getLehrerliste(i).getFach().getFachbezeichnung());
}
break;
}
else
{
switchPanels_3(panelErrorLehrer);
}
}
}
I've uploaded my project to GitHub. Methods and variables are written in German, so I'm really sorry if you can't understand what I have written. If u have questions please hit me up. I use Eclipse to code.
This link should direct you to my GitHub:
https://github.com/Gonzo-CR/Home-Projects.git
If the link doesn't work, look for Gonzo-CR on GitHub and check out my Home-projects repository where I uploaded all the files.
For better undestanding these are the Object oriented classes:
Person(Abstract)
Schueler
Lehrer
Fach
Schulklasse
Spezial
Sprecher
GUI classes:
Suchsystem
Testdaten(A class which generates all my objects)
The problem is likely that if td.getFachliste(0).getLehrerliste(i).getName().equals(lehrername) is not true the very first time the loop runs, switchPanels_3(panelErrorLehrer); will be triggered - regardless of whether the condition is met on a later iteration of the loop.
What you need is to check a sentinel value after the loop finishes - e.g.:
bool lehrerGefunden = false;
for(int i = 0; i < td.getFachliste(0).getListenLaengeLehrerListe();i++){
if(td.getFachliste(0).getLehrerliste(i).getName().equals(lehrername) || td.getFachliste(0).getLehrerliste(i).getNachname().equals(lehrername)){
//etc.
lehrerGefunden = true;
break;
}
}
if(!lehrerGefunden){
switchPanels_3(panelErrorLehrer);
}
That way, you check every entry in the list before deciding whether to show the error.
The following code reads the spreadsheet cell values with the JXL plugin and then compares these values with the values on the page and chooses the matching value from the combo box.
The code I have works but it is case sensitive, the value must be identical.
I want to improve this code to search the combo box faster and select the closest value without being identical. Currently it runs through all values slowly.
String valDesejado = tipopromocao;
String valorComboBox = "";
Select verificaOpt = new Select(driver.findElement(By.name("tipoDePromocaoPromocao")));
int tamanhoBox = verificaOpt.getOptions().size();
int variavelVerificadora1 = 0;
System.out.println("Tamanho: " + tamanhoBox);
for (int i = 0; i < tamanhoBox; i++)
{
verificaOpt.selectByIndex(i);
valorComboBox = verificaOpt.getFirstSelectedOption().getText().toString();
if (valDesejado.equalsIgnoreCase(valorComboBox))
{
i = tamanhoBox;
variavelVerificadora1 = 1;
}
}
if (variavelVerificadora1 == 0)
{
System.out.println("ALERTA: The Option + valDesejado + " no comboBox \"tipoDePromocaoPromocao\" not found.");
}
I put some comments in the code that explains what I'm doing and makes corrections on a few things.
Instead of using an int and setting it to 0/1, use a boolean and set it to true/false.
This loop should be faster because I'm not selecting each option as I loop. You can examine the text of each option without selecting it... then once you find a match, select the match.
Use break to exit a loop instead of setting the counter to max value.
Give this code a try.
String valDesejado = tipopromocao;
boolean variavelVerificadora1 = false; // use boolean instead of int set to 0/1
Select verificaOpt = new Select(driver.findElement(By.name("tipoDePromocaoPromocao")));
System.out.println("Tamanho: " + verificaOpt.getOptions().size());
// as this loops, the variable 'option' contains the current loops' OPTION element
// you don't need to select the option to get its text so this loop should be much faster
// it selects the OPTION once the correct one is found
for (WebElement option : verificaOpt.getOptions())
{
if (valDesejado.equalsIgnoreCase(option.getText()))
{
verificaOpt.selectByVisibleText(option.getText()); // select the OPTION match
variavelVerificadora1 = true; // set the boolean to true to indicate we found a match
break; // exits the for loop
}
}
if (!variavelVerificadora1) // this is the equivalent of variavelVerificadora1 == false, it's basically saying if not true
{
System.out.println("ALERTA: The Option" + valDesejado + " no comboBox \"tipoDePromocaoPromocao\" not found.");
}
I am trying to manipulate this blocks of codes:
List<WebElement> more = driver.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button"));
if(more.size()!=0){
driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button")).click();
}else {
WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
System.out.println("Total Number of TR: " + list.size());
}
What Im trying to do hopefully is that before I execute what is in the IF statement I want to loop that everytime it sees the element /html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button it will be click and if it is not available then ill execute this:
WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
System.out.println("Total Number of TR: " + list.size());
For further info what I am trying to do here is this.
I am on a listview for a specific module, and then at the button there is a "Click here for more records" -> its XPath is /html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button
I want that if i am in the listview and that button is present as mentioned above -I want to click it. And if in the listview there is no button "Click here for more records" (for example the records is composed of 5records only so there is no pagination clearly) I want to execute a blocks of code.
What I understood from your query is that you want to loop your IF condition statement multiple times until your else condition satisfies. For this, you can try following code:
(1) try{
(2) String xpathVAL="/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[4]/button";
(3) int i=1;
(4) while(i!=0){
(5) if(driver.findElements(By.xpath(xpathVAL)).size() != 0)
(6) {
(7) driver.findElement(By.xpath(xpathVAL).click();
(8) Thread.sleep(2000);
(9) }
(10) else{
(11) Thread.sleep(2000);
(12) WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
(13) List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
(14) LOGGER.debug("Total Number of TR: " + list.size());
(15) i=0;
(16) }
(17) }
(18) }catch(Exception e){
(19) LOGGER.debug("Exception Caught");
(20) WebElement present = driver.findElement(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody"));
List<WebElement> list = present.findElements(By.xpath("/html/body/div[1]/div/div[3]/div/div/div[1]/div/div[2]/div[2]/div/div[3]/div/table/tbody/tr"));
}
I think your requirement would be to keep click a button until some window appears and the button disappears. What I suggest for this is, apply "Thread.sleep(//some 3-4 seconds//)". However, it is poor style of coding and using forceful thread is not suggested by many standards. You can put some other implicit wait there. Also, if your element is not found after certain period of time, you can apply break condition after certain time interval, else it will go to infinite loop. If you are having trouble writing the code, let me know and I will help you in that.
EDIT
Try following points to remove element not present in cache:-
Use Explicit wait(Thread.sleep) at line 8 and line 11
Change your xpath from position based to some id/name/class type. Changing it to NAME based gets more clearer picture of the element even if its position is changed.
Again re-initialize the element if exception is caught at line 20.