1

I am fetching Employee count according to locating using Map Highlighted No of employees count is mismatch in Image like If i search in Web table Tokyo there count is 5 but here getting 6 for Edingburg, singapore , syndney count is correct.

Unable to find what is getting wrong in code can anyone explain ?

enter image description here

public class EmployeeMapping extends PredefinedActions {
WebDriver driver;
WebDriverWait wait;
JavascriptExecutor js;

@BeforeTest
void setUp() {
    driver = start("https://editor.datatables.net/examples/extensions/excel");
}
//Clicking on each number 1,2
@Test(priority = 1)
void officeAndNumberMapping1() {
    wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//td[text()='Ashton']")));
    WebElement scrollElement = driver.findElement(By.xpath("//td[text()='Ashton']"));

    js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].scrollIntoView()", scrollElement);
    Map<String, Integer> officeNumberMap = new HashMap<>();

    int totalTableRowsize = driver.findElements(By.xpath("//span/a[@tabindex='0']")).size();

    for (int outerIndex = 1; outerIndex <= totalTableRowsize; outerIndex++) {
        int rowSize = driver.findElements(By.xpath("//tr[@role='row']//td[5]")).size();
        for (int index = 1; index <= rowSize; index++) {            
            String officeLocation = driver.findElement(By.xpath("//tbody//tr[" + index + "]//td[5]")).getText();
            if (officeNumberMap.containsKey(officeLocation)) {
                officeNumberMap.put(officeLocation, officeNumberMap.get(officeLocation) + 1);
            } else {
                officeNumberMap.put(officeLocation, 1);
            }
        }           
        driver.findElement(By.xpath("//div[@id='example_paginate']/span/a["+outerIndex+"]")).click();   
    }
    System.out.println(officeNumberMap);
}}

Link : https://editor.datatables.net/examples/extensions/excel.html

1
driver.get().findElement(By.xpath("//div[@id='example_paginate']/span/a["+outerIndex+"]")).click();   
  1. Here when the page-load you are in index tab one already
  2. After the first iteration, you are again clicking the same tab as outerIndex is still 1
  3. so it counts page one content twice

So for fix, just keep the click action at the start than end of totalTableRowsize loop:

public class EmployeeMapping extends PredefinedActions {
  WebDriver driver;
  WebDriverWait wait;
  JavascriptExecutor js;

  @BeforeTest
  void setUp() {
    driver = start("https://editor.datatables.net/examples/extensions/excel");
  }
  //Clicking on each number 1,2
  @Test(priority = 1)
  void officeAndNumberMapping1() {
    wait = new WebDriverWait(driver, 10);
    wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//td[text()='Ashton']")));
    WebElement scrollElement = driver.findElement(By.xpath("//td[text()='Ashton']"));

    js = (JavascriptExecutor) driver;
    js.executeScript("arguments[0].scrollIntoView()", scrollElement);
    Map < String,
    Integer > officeNumberMap = new HashMap < >();

    int totalTableRowsize = driver.findElements(By.xpath("//span/a[@tabindex='0']")).size();

    for (int outerIndex = 1; outerIndex <= totalTableRowsize; outerIndex++) {
      driver.findElement(By.xpath("//div[@id='example_paginate']/span/a[" + outerIndex + "]")).click();
      int rowSize = driver.findElements(By.xpath("//tr[@role='row']//td[5]")).size();
      for (int index = 1; index <= rowSize; index++) {
        String officeLocation = driver.findElement(By.xpath("//tbody//tr[" + index + "]//td[5]")).getText();
        if (officeNumberMap.containsKey(officeLocation)) {
          officeNumberMap.put(officeLocation, officeNumberMap.get(officeLocation) + 1);
        } else {
          officeNumberMap.put(officeLocation, 1);
        }
      }

    }
    System.out.println(officeNumberMap);
  }
}

Output:

enter image description here

| improve this answer | |
  • 1
    Thanks @PDHide for help. it worked. – Palak Soni Sep 10 at 9:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.