Quantcast
Channel: Active questions tagged selenium - Stack Overflow
Viewing all articles
Browse latest Browse all 98494

How can I improve the performance of my code? Selenium + JSON in Java

$
0
0

I am trying to write a script to test a website with selenium webdriver but I have a problem. There is a table on the website. If I hover over a field, I get some informations about the field. The information is saved in a json-file. There is a lot of data in the json file(about 23000 json objects). The file looks like this:

  {
    "origin": "13212",
    "destination": "15512",
    "time": 17,
    "days": "17",
    "people": "25"
  },
  {
    "origin": "34123",
    "destination": "15122",
    "time": 18,
    "days": "17",
    "people": "79"
  },
  {
    "origin": "13212",
    "destination": "15512",
    "time": 10,
    "days": "17",
    "people": "39"
  },  ...

I filled the data in 5 different arraylists with this method:

public void fillArrays() {
        File file = new File(dirPath);
        try {
            String content = new String(Files.readAllBytes(Paths.get(file.toURI())), "UTF-8");
            JSONArray array = new JSONArray(content);
            Map<String, List<Object>> map = new HashMap<>();
            for (int i = 0; i < array.length(); i++) {
                JSONObject obj = array.getJSONObject(i);
                for (String key : obj.keySet()) {
                    switch (key) {
                    case "origin":
                        map.put(key, originList);
                        originList.add(obj.get(key));
                        break;
                    case "destination":
                        map.put(key, destinationList);
                        destinationList.add(obj.get(key));
                        break;
                    case "time":
                        map.put(key, timesList);
                        timesList.add(obj.get(key));
                        break;
                    case "days":
                        map.put(key, daysList);
                        daysList.add(obj.get(key));
                        break;
                    case "people":
                        map.put(key, peopleList);
                        peopleList.add(obj.get(key));
                        break;
                    }
                }
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

To test the field of the table I used this code:

for (int arrayChecker = 0; arrayChecker < originList.size(); arrayChecker++) {
    if (originList.get(arrayChecker).toString().equals(firstEle.getText())
            && destinationList.get(arrayChecker).toString().equals(lastEle.getText())) {
        totalPeople += Integer.parseInt(peopleList.get(arrayChecker).toString());
    }
}
numberAsString = numberFormat.format(totalPeople);
assertEquals(infoElement.getText(), "Information: Total time: " + numberAsString + " people");

The code works absolutely fine but is really really slow. To test a field of the table the code needs like 2 minutes.

Is there any way to improve the performance and make the tests faster?


Viewing all articles
Browse latest Browse all 98494

Latest Images

Trending Articles



Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>