How to make parameter in xpath and make codes easier to read if there are several same lines, java, selenium

huangapple go评论77阅读模式
英文:

How to make parameter in xpath and make codes easier to read if there are several same lines, java, selenium

问题

我编写了这个布尔方法,但我想让它更短更智能,因为有3行相同的XPath。有人可以帮我吗?谢谢。

public boolean VerifyKORSecDispaly() {
    boolean a = driver
            .findElement(By
                    .xpath("(//tr[@data-testid='row']//td[@class='kor'])[1]//span[@class='da']"))
            .getText().contains("d");
    boolean b = driver
            .findElement(By
                    .xpath("(//tr[@data-testid='row']//td[@class='kor'])[2]//span[@class='da']"))
            .getText().contains("d");
    boolean c = driver
            .findElement(By
                    .xpath("(//tr[@data-testid='row']//td[@class='kor'])[3]//span[@class='da']"))
            .getText().contains("d");

    return a && b && c;
}

希望这可以帮助您缩短代码并使其更智能。

英文:

I wrote this boolean method but I want to make it shorter and smarter since there are 3 same lines with quite the same XPath.
Could anyone help me with this, please? thanks

	public boolean VerifyKORSecDispaly() {
	
	boolean a = driver
			.findElement(By
					.xpath("(//tr[@data-testid='row']//td[@class='kor'])[1]//span[@class='da']"))
			.getText().contains("d");
	boolean b = driver
			.findElement(By
					.xpath("(//tr[@data-testid='row']//td[@class='kor'])[2]//span[@class='da']"))
			.getText().contains("d");
	boolean c = driver
			.findElement(By
					.xpath("(//tr[@data-testid='row']//td[@class='kor'])[3]//span[@class='da']"))
			.getText().contains("d");

	if (a == true && b == true && c == true) {
		return true;
	} else {
		return false;
	}
}

答案1

得分: 1

你可以使用List<>,因为你在xpath中使用了索引。

//tr[@data-testid=&#39;row&#39;]//td[@class=&#39;kor&#39;] <- 这个选择器会返回多个元素。

基于这些元素,我们可以找到span[@class=&#39;da&#39;]元素。

代码:

public boolean VerifyKORSecDispaly() {
    boolean a = doesRowTextContain(0, &quot;d&quot;);
    boolean b = doesRowTextContain(1, &quot;d&quot;);
    boolean c = doesRowTextContain(2, &quot;d&quot;);

    if (a == true &amp;&amp; b == true &amp;&amp; c == true) {
        return true;
    } else {
        return false;
    }
}

private boolean doesRowTextContain(int index, String expectedString) {
    By spanSelector = By.xpath(&quot;.//span[@class=&#39;da&#39;]&quot;); //点.减小了元素的范围。不再搜索所有元素,而是缩小到父元素的范围
    List&lt;WebElement&gt; dataRows = driver.findElements(By.xpath(&quot;//tr[@data-testid=&#39;row&#39;]//td[@class=&#39;kor&#39;]&quot;));
    
    return dataRows.get(index).findElement(spanSelector).getText().contains(expectedString);
}

还有一件事是 - 你不需要将a, b或ctrue进行比较,因为在if语句中这是默认的预期值。

    if (a &amp;&amp; b &amp;&amp; c) {
        return true;
    } else {
        return false;
    }

甚至可以简化为

return a &amp;&amp; b &amp;&amp; c How to make parameter in xpath and make codes easier to read if there are several same lines, java, selenium

最终的方法可能如下所示:

public boolean VerifyKORSecDispaly() {
    return doesRowTextContain(0, &quot;d&quot;) &amp;&amp; doesRowTextContain(1, &quot;d&quot;) &amp;&amp; doesRowTextContain(2, &quot;d&quot;);
}

private boolean doesRowTextContain(int index, String expectedString) {
    By spanSelector = By.xpath(&quot;.//span[@class=&#39;da&#39;]&quot;); //点.减小了元素的范围。不再搜索所有元素,而是缩小到父元素的范围
    List&lt;WebElement&gt; dataRows = driver.findElements(By.xpath(&quot;//tr[@data-testid=&#39;row&#39;]//td[@class=&#39;kor&#39;]&quot;));
    
    return dataRows.get(index).findElement(spanSelector).getText().contains(expectedString);
}
英文:

You could use List&lt;&gt; since you use indexes in xpath.

//tr[@data-testid=&#39;row&#39;]//td[@class=&#39;kor&#39;] <- this selector would return multiple elements

Based on these elements, we can find span[@class=&#39;da&#39;] element.

Code:

public boolean VerifyKORSecDispaly() {
    boolean a = doesRowTextContain(0, &quot;d&quot;);
    boolean b = doesRowTextContain(1, &quot;d&quot;);
    boolean c = doesRowTextContain(2, &quot;d&quot;);

    if (a == true &amp;&amp; b == true &amp;&amp; c == true) {
        return true;
    } else {
        return false;
    }
}

private boolean doesRowTextContain(int index, String expectedString) {
	By spanSelector = By.xpath(&quot;.//span[@class=&#39;da&#39;]&quot;); //the dot . reduces the scope of the element. Instead of searching through all the elements in source, we&#39;ll reduce the scope to parent element
	List&lt;WebElement&gt; dataRows = driver.findElements(By.xpath(&quot;//tr[@data-testid=&#39;row&#39;]//td[@class=&#39;kor&#39;]&quot;));
	
	return dataRows.get(index).findElement(spanSelector).getText().contains(expectedString);
}

One more thing is - you don't have to compare a, b or c with true as it's default expected value in if statement.

    if (a &amp;&amp; b &amp;&amp; c) {
        return true;
    } else {
        return false;
    }

Or even

return a &amp;&amp; b &amp;&amp; c How to make parameter in xpath and make codes easier to read if there are several same lines, java, selenium

Final methods could look like this:

public boolean VerifyKORSecDispaly() {
    return doesRowTextContain(0, &quot;d&quot;) &amp;&amp; doesRowTextContain(1, &quot;d&quot;) &amp;&amp; doesRowTextContain(2, &quot;d&quot;);
}

private boolean doesRowTextContain(int index, String expectedString) {
	By spanSelector = By.xpath(&quot;.//span[@class=&#39;da&#39;]&quot;); //the dot . reduces the scope of the element. Instead of searching through all the elements in source, we&#39;ll reduce the scope to parent element
	List&lt;WebElement&gt; dataRows = driver.findElements(By.xpath(&quot;//tr[@data-testid=&#39;row&#39;]//td[@class=&#39;kor&#39;]&quot;));
	
	return dataRows.get(index).findElement(spanSelector).getText().contains(expectedString);
}

huangapple
  • 本文由 发表于 2020年8月5日 14:20:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/63259492.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定