我正在尝试在黄瓜jvm中编写一个测试,它从页面抓取客户地址并声明它不为空。
<div class="padder">
<h3>Your details</h3>
<p>
<strong>MR Test Test</strong>
</p>
<p>Selly Road</p>
<p>London</p>
<p>GBR</p>
<p>TR02XZ</p>
</div>
</div>
我使用xpath查找地址的第一行和剩余的p标记:
private static final By CUSTOMER_ADDRESS = By.xpath("//*[@id='yourDetails']/div/p[position() >=2]");
这是我到目前为止所写的,但由于某些原因,即使在xpath中我声明要抓住第二个p标签之后的所有p标签,我的下面的测试不起作用:
public List<WebElement> returnAllText(By element) {
List<WebElement> all = driver.findElements(element);
for (WebElement elements: all) {
System.out.println(elements.getText());
}
return all;
}
当循环运行时,它会找到地址的每一行,但是当运行return语句时它会丢失。如何捕获/存储每个p标记值,然后将其分组为一个字符串值并断言它不为空
我调试了代码,下面是结果
当我调试行并评估表达式时:
List<WebElement> all = driver.findElements(element)
result = {java.util.ArrayList@4562} size = 4
[0] = {org.openqa.selenium.remote.RemoteWebElement@4564}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"
[1] = {org.openqa.selenium.remote.RemoteWebElement@4565}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"
[2] = {org.openqa.selenium.remote.RemoteWebElement@4566}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"
[3] = {org.openqa.selenium.remote.RemoteWebElement@4567}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"
然后我评估:
for(WebElement elements: all)
result = {java.util.ArrayList@4276} size = 4
[0] = {org.openqa.selenium.remote.RemoteWebElement@4288}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"
[1] = {org.openqa.selenium.remote.RemoteWebElement@4289}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"
[2] = {org.openqa.selenium.remote.RemoteWebElement@4290}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"
[3] = {org.openqa.selenium.remote.RemoteWebElement@4291}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"
然后我评估:
System.out.println(elements.getText())
result = {java.lang.String@4598}"London"
value = {char[9]@4599}
hash = 0
hash32 = 0
评估:
return all;
result = {java.util.ArrayList@4276} size = 4
[0] = {org.openqa.selenium.remote.RemoteWebElement@4288}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"
[1] = {org.openqa.selenium.remote.RemoteWebElement@4289}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"
[2] = {org.openqa.selenium.remote.RemoteWebElement@4290}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"
[3] = {org.openqa.selenium.remote.RemoteWebElement@4291}"[[RemoteWebDriver: chrome on MAC (32479ef6b2783086b4c1d1ba0bbd1405)] -> xpath: //*[@id='yourDetails']/div/p[position() >=2]]"
答案 0 :(得分:0)
您的调试结果(仅“伦敦”)和您在问题中的陈述(“循环[...]找到每一行”)相互矛盾。我建议你像这样重写你的方法:
public String returnAllText(By element) {
List<WebElement> all = driver.findElements(element);
StringBuilder sb = new StringBuilder();
int i = 1;
for (WebElement element: all) {
sb.append( i++ ).append( ": " ).append( element.getText() );
}
return sb.toString();
}
让我们看一下从方法返回后产生的内容。 - 编号(i)可以在以后删除,并且可能会添加换行符,具体取决于您希望组合行的方式。
答案 1 :(得分:0)
我设法解决了这个问题。由于某些原因使用xpath检索所有p标签不起作用,所以我使用了更高一级的div id(div class = padder的父级):
private static final String CUSTOMER_DETAILS = "yourDetails";
这允许我检索包含文本的所有元素,直到结束div
<h3>Your details</h3>
<p>
<strong>MR Test Test</strong>
</p>
<p>Selly Road</p>
<p>London</p>
<p>GBR</p>
<p>TR02XZ</p>
然后我用这个方法来构造地址:
// return all text in a list and build into a string
public String returnAddress(By element) {
String[] addressSplit = driver.findElement(element).getText().split("\n");
StringBuilder str = new StringBuilder();
for (int addressLength = 2; addressLength <= addressSplit.length - 1; addressLength++) {
str.append(addressSplit[addressLength] + "\n");
}
return str.toString().trim();
}
// this calls the method above
public String customerAddressIsPresent() {
return returnAddress(By.id(CUSTOMER_DETAILS));
}