admin管理员组文章数量:1389826
I have created a void method which prints out the required responses from an url (using Java and Playwright) but I actually need to receive these values for making junit assertions and I am trying to convert the method into a String method, but I can't get the returned value.
I get the following error: Index 0 out of bounds for length 0. I was trying various methods, but I always get either an empty result or an '0 out of bounds for length 0', so it seems I don't really add any values to my array list.
The printing out shows that the values are correct though..
Could somebody please point out what is wrong here?
This is my code:
public String getResponseAgain()
{
List<String> anotherArray = new ArrayList<>();
String[] extractedValue ={""};
page.onResponse(response -> {
if (response.url().contains("myUrlIsHere")) {
String value = response.url().substring(response.url().lastIndexOf("someValue") + 2,
response.url().indexOf("anotherValue"));
String[] splitText = value.split("bySomeValue");
List<String> arrayOfStrings = new ArrayList<>(Arrays.asList(splitText));
extractedValue[0] = String.join(" ", arrayOfStrings);
anotherArray.addAll(List.of(extractedValue));
for(String arr : anotherArray){
System.out.println("Arr is " + arr);
}
}
});
return anotherArray.get(0);
}
I use the method in a test class:
public void responsesTest(){
////some code that accesses url and is ok////
Assertions.assertEquals("expected value", page.getResponseAgain());
}
This is the original method that works and prints out the correct values:
public void getSomeResponse(){
page.onResponse(response -> {
if (response.url().contains("myUrlIsHere")) {
String value = response.url().substring(response.url().lastIndexOf("someValue") + 2, response.url().indexOf("anotherValue"));
String[] splitText = value.split("bySomeValue");
List<String> arrayOfStrings = new ArrayList<>(Arrays.asList(splitText));
String extractedValue = String.join(" ", arrayOfStrings);
System.out.println(extractedValue);
}
});
}
I have created a void method which prints out the required responses from an url (using Java and Playwright) but I actually need to receive these values for making junit assertions and I am trying to convert the method into a String method, but I can't get the returned value.
I get the following error: Index 0 out of bounds for length 0. I was trying various methods, but I always get either an empty result or an '0 out of bounds for length 0', so it seems I don't really add any values to my array list.
The printing out shows that the values are correct though..
Could somebody please point out what is wrong here?
This is my code:
public String getResponseAgain()
{
List<String> anotherArray = new ArrayList<>();
String[] extractedValue ={""};
page.onResponse(response -> {
if (response.url().contains("myUrlIsHere")) {
String value = response.url().substring(response.url().lastIndexOf("someValue") + 2,
response.url().indexOf("anotherValue"));
String[] splitText = value.split("bySomeValue");
List<String> arrayOfStrings = new ArrayList<>(Arrays.asList(splitText));
extractedValue[0] = String.join(" ", arrayOfStrings);
anotherArray.addAll(List.of(extractedValue));
for(String arr : anotherArray){
System.out.println("Arr is " + arr);
}
}
});
return anotherArray.get(0);
}
I use the method in a test class:
public void responsesTest(){
////some code that accesses url and is ok////
Assertions.assertEquals("expected value", page.getResponseAgain());
}
This is the original method that works and prints out the correct values:
public void getSomeResponse(){
page.onResponse(response -> {
if (response.url().contains("myUrlIsHere")) {
String value = response.url().substring(response.url().lastIndexOf("someValue") + 2, response.url().indexOf("anotherValue"));
String[] splitText = value.split("bySomeValue");
List<String> arrayOfStrings = new ArrayList<>(Arrays.asList(splitText));
String extractedValue = String.join(" ", arrayOfStrings);
System.out.println(extractedValue);
}
});
}
Share
Improve this question
edited Mar 14 at 12:30
Mina
asked Mar 14 at 12:00
MinaMina
851 silver badge10 bronze badges
3
|
1 Answer
Reset to default 2You’re returning anotherArray.get(0)
immediately, but your page.onResponse
callback is asynchronous. By the time the method returns, anotherArray
is still empty. You need to wait for the network response before returning the value (e.g. use page.waitForResponse(...)
or some other synchronization) so anotherArray
gets populated before you try to read from it.
public String getResponseAgain() {
// Wait for the needed response instead of using an asynchronous listener
Response neededResponse = page.waitForResponse(r -> r.url().contains("myUrlIsHere"));
// Parse the URL to extract the value
String value = neededResponse.url().substring(
neededResponse.url().lastIndexOf("someValue") + 2,
neededResponse.url().indexOf("anotherValue")
);
String[] splitText = value.split("bySomeValue");
String extractedValue = String.join(" ", splitText);
return extractedValue;
}
本文标签:
版权声明:本文标题:arraylist - Java with Playwright onResponse method - can't get returned String value, IndexOutOfBoundsException - Stack 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744657991a2618079.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
anotherArray.get(0);
is invoked before the lambda passed topage.onResponse
is invoked. – Mark Rotteveel Commented Mar 14 at 12:29