Search code examples
loopscsvxpathjmeterwebdriver

Looping through a set of xpaths and checking the test values


I have around 100 xpaths which only differs only in index at the end.

The sample xpaths range from:

(//div[@class='something']
//div[@class='some other thing'])[1]

to:

(//div[@class='something']
//div[@class='some other thing'])[100] 

And I have a CSV file with expected values in each of these fields.

Note that I am doing this in Webdriver sampler in JMeter, keeping language as javascript.

How do I loop through these xpaths, and get text() of these, and check if they are as per the expected values in the CSV file, using minimum code?


Solution

  • Are you asking how to create a for loop in JavaScript?

    for (var i = 1; i <= 100; i++) {
      var xpath = "(//div[@class='something']//div[@class='some other thing'])[" + i + "]"
      var actualText = WDS.browser.findElement(org.openqa.selenium.By.xpath(xpath)).getText()
      var expectedText = org.apache.commons.io.FileUtils.readLines(new java.io.File('/path/to/your/file.csv')).get(i-1)
      if (actualText != expectedText) {
          WDS.sampleResult.setFailure(true)
          WDS.sampleResult.setFailureMessage("Text mismatch")
      }
    }
    

    With regards to using JavaScript as the language the overall idea is not very great.