Jmeter Groovy在文件中搜索文件夹和块路径

时间:2018-06-15 06:57:22

标签: groovy jmeter

我正在使用JSR223 Assertion,我试图在文件中找到这两行的位置,如果没有收到则断言。

  <BlockPath>XYZ\abc\a1\abc</BlockPath>
  <FolderPath>XYZ\abc\a1</FolderPath>

我试过了:

int pos1 = tstStr.indexOf("<BlockPath>XYZ\abc\a1\abc</BlockPath>");
int pos2 = tstStr.indexOf("<FolderPath>XYZ\abc\a1</FolderPath>");

我收到此错误

Assertion failure message: javax.script.ScriptException: 
org.codehaus.groovy.control.MultipleCompilationErrorsException: startup failed:
Script32.groovy: 7: unexpected char: '\' @ line 7, column 50.
ted.indexOf("<BlockPath>XYZ\abc\a1\ab

我需要从字面上寻找这两条线并获得它们的位置。确保不是-1或它们存在于文件中。我该怎么办?

1 个答案:

答案 0 :(得分:0)

在Groovy中,您需要使用另一个反斜杠转义反斜杠,因此您需要修改代码,如:

library(maptools)
library(sp)
#make df.sp a spatialdataframe
coordinates( df.sp ) <- c( "longitude", "latitude" )

#create a list per id
id.list <- sp::split( df.sp, df.sp[["id"]] )

#initialisation of counter
id <- 1

#for each id, create a line that connects all points with that id
for ( i in id.list ) {
  event.lines <- SpatialLines( list( Lines( Line( i[1]@coords ), ID = id ) ),
                               proj4string = CRS( "+init=epsg:4326" ) )
  if ( id == 1 ) {
    sp_lines  <- event.lines
  } else {
    sp_lines  <- spRbind( sp_lines, event.lines )
  }
  id <- id + 1
}

演示:

JMeter Groovy Escape String

此外,您的回复似乎基于XML,因此您可以考虑使用XPath Assertion,它允许使用XPath query语言断言响应数据,以便您可以检查元素的存在像:

head(sp_lines,1)

# An object of class "SpatialLines"
# Slot "lines":
#   [[1]]
# An object of class "Lines"
# Slot "Lines":
#   [[1]]
# An object of class "Line"
# Slot "coords":
#   longitude latitude
# [1,] -124.0540 40.79115
# [2,] -124.1826 40.79096
# 
# Slot "ID":
#   [1] "1"
# 
# Slot "bbox":
#   min        max
# x -124.18256 -124.05401
# y   40.79096   40.79115
# 
# Slot "proj4string":
#   CRS arguments:
#   +init=epsg:4326 +proj=longlat +datum=WGS84 +no_defs +ellps=WGS84 +towgs84=0,0,0 

library(leaflet)
leaflet()%>%
  addTiles() %>%
  addCircles(df,lng = df$Event_lon, lat = df$Event_lat, radius=20, color = "red", group = "events") %>% 
  addCircles(df,lng = df$Location_longitude, lat = df$Location_latitude, radius=20, color = "blue", group = 'Locations') %>%
  addPolylines( data = sp_lines )

查看How to Use JMeter Assertions in Three Easy Steps文章了解详情。