Skip to main content
Code Review

Return to Answer

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I would call them XslWriter and PdfWriter. (See also #3 here #3 here.)

I would call them XslWriter and PdfWriter. (See also #3 here.)

I would call them XslWriter and PdfWriter. (See also #3 here.)

Source Link
palacsint
  • 30.3k
  • 9
  • 82
  • 157
  1. Method names in Java are usually camelCase as well as variable names. So,
  • WriteToXSL.WriteTo should be WriteToXSL.writeTo,
  • WebElement Element should be WebElement element,
  • Testgoal should be testgoal.
  1. WriteToXSL and WriteToPDF aren't the best class names. The Java Language Specification, Java SE 7 Edition, 6.1 Declarations says the following:

Class and Interface Type Names

Names of class types should be descriptive nouns or noun phrases, not overly long, in mixed case with the first letter of each word capitalized.

I would call them XslWriter and PdfWriter. (See also #3 here.)

Also related: Effective Java, 2nd edition, Item 56: Adhere to generally accepted naming conventions

  1. element is a String here:
WriteToXSL.WriteTo(Testgoal, "NOK", element.toString());

Calling toString() on a String is not too useful, you could remove that. Actually, I'd rename the variable to emptyViewText to describe its exact content.

  1. As far as I see driver.findElements() never returns null, so you could remove the null check here:
List<WebElement> allSearchResults = driver
 .findElements(By
 .xpath("//*[contains(@class, 'teaser-list')]/li/article/div[2]/h3"));
//Check if xpath contains any WebElements, if not check if no results message is displayed
if(allSearchResults == null || allSearchResults.size() < 1 ){

The allSearchResults.size() < 1 condition could be changed to allSearchResults.isEmpty().

  1. Comments on the closing curly braces are unnecessary and rather disturbing. Modern IDEs could show blocks.
 }// close if
}// close for loop

["// ..." comments at end of code block after } - good or bad?][4]

  1. You could avoid the flag here with a return statement:
boolean resultsFound = false;
for (WebElement Element: allSearchResults) {
 if (Element.getText().contains(title)) {
 WriteToXSL.WriteTo(Testgoal, "NOK", "");
 WriteToPDF.addRow(Testgoal, "NOK", "");
 resultsFound = true;
 break;
 }
}
if (resultsFound == false) {
 WriteToXSL.WriteTo(Testgoal, "OK", "");
 WriteToPDF.addRow(Testgoal, "OK", "");
}

Consider this:

 for (WebElement Element: allSearchResults) {
 if (Element.getText().contains(title)) {
 WriteToXSL.WriteTo(Testgoal, "NOK", "");
 WriteToPDF.addRow(Testgoal, "NOK", "");
 return;
 }
 }
 WriteToXSL.WriteTo(Testgoal, "OK", "");
 WriteToPDF.addRow(Testgoal, "OK", "");
  1. Actually, I would restructure the code to remove the duplicated XSL/PDF writing calls. I have changed them with two exceptions:

     public static void checkAbsentSearchResult(String title, String testGoal) throws IOException {
     try {
     doCheckAbsentSearchResult(title, testGoal);
     WriteToXSL.WriteTo(testGoal, "OK", "");
     WriteToPDF.addRow(testGoal, "OK", "");
     } catch (Exception e) {
     WriteToXSL.WriteTo(testGoal, "NOK", e.toString());
     WriteToPDF.addRow(testGoal, "NOK", "");
     }
     }
     public static void doCheckAbsentSearchResult(String title, String testGoal) throws IOException {
     final List<WebElement> allSearchResults = driver.findElements(By
     .xpath("//*[contains(@class, 'teaser-list')]/li/article/div[2]/h3"));
     // Check if xpath contains any WebElements, if not check if no
     // results message is displayed
     if (allSearchResults.isEmpty()) {
     final String emptyViewText = driver.findElement(By.xpath("//div[@class='view-empty']")).getText();
     if (!emptyViewText.contains("no results found")) {
     throw new RuntimeException(emptyViewText);
     }
     } else {
     for (final WebElement searchResultElement: allSearchResults) {
     if (searchResultElement.getText().contains(title)) {
     throw new RuntimeException("");
     }
     }
     }
     }
    

Note that it changes the output a little bit when there is an error. I guess it could be fine for a report.

lang-java

AltStyle によって変換されたページ (->オリジナル) /