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

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

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

问题

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

  1. public boolean VerifyKORSecDispaly() {
  2. boolean a = driver
  3. .findElement(By
  4. .xpath("(//tr[@data-testid='row']//td[@class='kor'])[1]//span[@class='da']"))
  5. .getText().contains("d");
  6. boolean b = driver
  7. .findElement(By
  8. .xpath("(//tr[@data-testid='row']//td[@class='kor'])[2]//span[@class='da']"))
  9. .getText().contains("d");
  10. boolean c = driver
  11. .findElement(By
  12. .xpath("(//tr[@data-testid='row']//td[@class='kor'])[3]//span[@class='da']"))
  13. .getText().contains("d");
  14. return a && b && c;
  15. }

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

英文:

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

  1. public boolean VerifyKORSecDispaly() {
  2. boolean a = driver
  3. .findElement(By
  4. .xpath("(//tr[@data-testid='row']//td[@class='kor'])[1]//span[@class='da']"))
  5. .getText().contains("d");
  6. boolean b = driver
  7. .findElement(By
  8. .xpath("(//tr[@data-testid='row']//td[@class='kor'])[2]//span[@class='da']"))
  9. .getText().contains("d");
  10. boolean c = driver
  11. .findElement(By
  12. .xpath("(//tr[@data-testid='row']//td[@class='kor'])[3]//span[@class='da']"))
  13. .getText().contains("d");
  14. if (a == true && b == true && c == true) {
  15. return true;
  16. } else {
  17. return false;
  18. }
  19. }

答案1

得分: 1

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

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

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

代码:

  1. public boolean VerifyKORSecDispaly() {
  2. boolean a = doesRowTextContain(0, &quot;d&quot;);
  3. boolean b = doesRowTextContain(1, &quot;d&quot;);
  4. boolean c = doesRowTextContain(2, &quot;d&quot;);
  5. if (a == true &amp;&amp; b == true &amp;&amp; c == true) {
  6. return true;
  7. } else {
  8. return false;
  9. }
  10. }
  11. private boolean doesRowTextContain(int index, String expectedString) {
  12. By spanSelector = By.xpath(&quot;.//span[@class=&#39;da&#39;]&quot;); //点.减小了元素的范围。不再搜索所有元素,而是缩小到父元素的范围
  13. List&lt;WebElement&gt; dataRows = driver.findElements(By.xpath(&quot;//tr[@data-testid=&#39;row&#39;]//td[@class=&#39;kor&#39;]&quot;));
  14. return dataRows.get(index).findElement(spanSelector).getText().contains(expectedString);
  15. }

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

  1. if (a &amp;&amp; b &amp;&amp; c) {
  2. return true;
  3. } else {
  4. return false;
  5. }

甚至可以简化为

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

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

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

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:

  1. public boolean VerifyKORSecDispaly() {
  2. boolean a = doesRowTextContain(0, &quot;d&quot;);
  3. boolean b = doesRowTextContain(1, &quot;d&quot;);
  4. boolean c = doesRowTextContain(2, &quot;d&quot;);
  5. if (a == true &amp;&amp; b == true &amp;&amp; c == true) {
  6. return true;
  7. } else {
  8. return false;
  9. }
  10. }
  11. private boolean doesRowTextContain(int index, String expectedString) {
  12. 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
  13. List&lt;WebElement&gt; dataRows = driver.findElements(By.xpath(&quot;//tr[@data-testid=&#39;row&#39;]//td[@class=&#39;kor&#39;]&quot;));
  14. return dataRows.get(index).findElement(spanSelector).getText().contains(expectedString);
  15. }

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.

  1. if (a &amp;&amp; b &amp;&amp; c) {
  2. return true;
  3. } else {
  4. return false;
  5. }

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:

  1. public boolean VerifyKORSecDispaly() {
  2. return doesRowTextContain(0, &quot;d&quot;) &amp;&amp; doesRowTextContain(1, &quot;d&quot;) &amp;&amp; doesRowTextContain(2, &quot;d&quot;);
  3. }
  4. private boolean doesRowTextContain(int index, String expectedString) {
  5. 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
  6. List&lt;WebElement&gt; dataRows = driver.findElements(By.xpath(&quot;//tr[@data-testid=&#39;row&#39;]//td[@class=&#39;kor&#39;]&quot;));
  7. return dataRows.get(index).findElement(spanSelector).getText().contains(expectedString);
  8. }

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:

确定