0

I'm very new to Selenium and I've been trying to make the test suite gather data from a table. I don't have the slightest clue on how to do this.

Here's the table I am working with: https://i.sstatic.net/Qrnjj.png

New appointments (dates) are randomly added at random times of the day. I've created a test suite that will constantly refresh at this page. The next step, would be to save all the dates in the table, create a loop to compare if the dates after a refresh happen to be different the original stored dates.

If they are different, notify the user.

Here's a theoretical example of what I'm trying to accomplish.

//Navigate to the appointment page

//Store all the current dates from the table

  for (until a new appointment pops up)

    {
     //Refresh the page
    // Compare the dates to the stored dates
       if (the dates =/ stored dates)
         {
          notify the user(me in this case)
         }
    }

I'm also trying to figure out how I can find the element ID of the table.

Here's a screenshot with some of the html code: https://i.sstatic.net/pMtof.png

The statement that is highlighted has the first date stored.

Any advice would be appreciated, thanks!

0

1 Answer 1

1

Tried replicating a similar HTML structure (in fact 2 of them, one after the refresh). Here is a quick solution for you to compare the HTML tables after refresh.

The key here is organizing your table data into a Map<String, List<String>> like data structure.

import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


public class CheckTables {

public WebDriver driver;


public static void main(String[] args) throws Exception {


    CheckTables objTest = new CheckTables();
    objTest.runTest();

}

public void runTest(){

    driver = new FirefoxDriver();

    driver.navigate().to("file:///D:/00_FX_WorkSpace/X_Hour/RoadTest_1.html");
    Map<String, List<String>> objTable_1 = readTable();
    System.out.println("TABLE:1" + objTable_1);

    //event to refresh the table
    driver.navigate().to("file:///D:/00_FX_WorkSpace/X_Hour/RoadTest_2.html");
    Map<String, List<String>> objTable_2 = readTable();
    System.out.println("TABLE:2" + objTable_2);

    compareTables(objTable_1, objTable_2);

}

public Map<String, List<String>> readTable(){

    Map<String, List<String>> objTable = new HashMap<>();

    List<WebElement> objRows = driver.findElements(By.cssSelector("tr#data"));
    for(int iCount=0; iCount<objRows.size(); iCount++){
        List<WebElement> objCol = objRows.get(iCount).findElements(By.cssSelector("td.tableTxt"));
        List<String> columns = new ArrayList<>();
        for(int col=0; col<objCol.size(); col++){
            columns.add(objCol.get(col).getText());
        }
        objTable.put(String.valueOf(iCount), columns);
    }

    return objTable;
}

public void compareTables(Map<String, List<String>> objTable1, Map<String, List<String>> objTable2){


    for(int count=0; count<objTable1.size(); count++){

        List<String> objList1 = objTable1.get(String.valueOf(count));
        System.out.println(objList1);
        List<String> objList2 = objTable2.get(String.valueOf(count));
        System.out.println(objList2);

        if(objList1.containsAll(objList2)){
            System.out.println("Row [" + count + "] is SAME");
        }
        else{
            //notify
            System.out.println("Row [" + count + "] has CHANGED");
        }
    }
}
}

Here are the HTML snippets for RoadTest_1.html and RoadTest_2.html -- https://gist.github.com/anonymous/43c3b1f44817c69bd03d/

Sign up to request clarification or add additional context in comments.

8 Comments

Tried out the code you suggested. Getting an initialization error saying: readTable() should not be static. Also in readTable(), there is an error under 'driver' saying I cannot make a static reference to a non-static field.
I had the entire code in a class and calling the methods from main. Are you creating an object of the class and then calling the methods ? If yes, then the methods should not be static.
Edited the snippet to avoid static objects.
That seemed to have fixed it, program now runs. For the output however, I just get Table 1: {} -refreshes- Table 2: {}
Change the selector in readTable() as per your HTML attributes
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.