1

I'm new to automation, and programming, for the most part.

I have a test script where I have to verify the presence of certain elements within a page I am testing.

At the moment, I have multiple try/catch blocks to test each individual element, but for the sake of code readability, I'd like to have one "helper" method, in which I can call for the various elements...

This is what I have so far.... Where would I place this "helper" method?

Would it be placed outside of the main method?

package automationFramework;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.*;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.Select;
public class Serp34Check {


private static WebDriver driver = null;

public static void main(String[] args) {

    // Create a new instance of the Firefox driver
    driver = new FirefoxDriver();

    //Put a Implicit wait, this means that any search for elements on the page could take the time the implicit wait is set for before throwing exception
    driver.manage().timeouts().implicitlyWait(15, TimeUnit.SECONDS);

    //Launch the TestURL -- !!! This must be changed prior to adding script to story
    driver.get("<URL TO TEST>"); 


    //Output list of navigation links found within the page
    System.out.println("These are the links found within the SERP's Navigation bar:");
    WebElement navBar = driver.findElement(By.id("topnav"));
    List<WebElement> navigationLinks = navBar.findElements(By.tagName("li"));
    int navLinksListSize = navigationLinks.size();
    for(int i=0; i<navLinksListSize; i++)   {
     String sValue = navigationLinks.get(i).getText();
     System.out.println(sValue);
    }

    //check for pricegrabber feed
    try
    {
        WebElement priceGrabber = driver.findElement(By.xpath("//div[contains(@class, 'pricegrabber_cont_block')]"));
        if(priceGrabber != null)
            System.out.println("Pricegrabber feed is Present");
    }catch(Exception e){
        System.out.println("Pricegrabber feed is Absent");
    }

    //check for offers.com feed on sidebar
    try
    {
        WebElement offersSidebar = driver.findElement(By.xpath("//div[contains(@class, 'offers_cont_block')]"));
        if(offersSidebar != null)       
            System.out.println("Offers.com sidebar feed is Present");
    }catch (Exception e){
        System.out.println("Offers.com sidebar feed is Absent");
        }


    //check for wikipedia block
    try
    {
        WebElement wikiBlock = driver.findElement(By.xpath("//div[contains(@class, 'wiki_cont_block')]"));
        if(wikiBlock != null)
            System.out.println("Wikipedia.com sidebar feed is Present");
    }catch (Exception e){
        System.out.println("Wikipedia.com sidebar feed is Absent");
        }

  //check for social icons
    try
    {
        WebElement socialIcons = driver.findElement(By.xpath("//div[contains(@id, 'socialattach')]"));
        if(socialIcons != null)
            System.out.println("Social icons sidebar feed is Present");
    }catch (Exception e){
        System.out.println("Social icons sidebar feed is Absent");
        }

        // Close the driver
           driver.quit();

}

}

3

When I first need a helper method, I put it in the same class as the test code that calls it. In your example, you could implement the helper method as a private method inside the Serp34Check class:

private static void checkForFeed(xpath, feedName)
{
    try
    {
        WebElement feedElement = driver.findElement(By.xpath(xpath));
        System.out.println(feedName + " is present");
    }
    catch (WebDriverException ex)
    {
        System.out.println(feedName + " feed is absent");
    }
}

Then in your existing code, you can replace the equivalent code with a call:

checkForFeed("//div[contains(@class, 'pricegrabber_cont_block')]", "Pricegrabber");

As you get more and more tests, you will probably find it useful eventually to move such utility methods to their own class. I will move the helper method to a new class as soon as:

  • Another test class needs to call the helper method.
  • Several helper methods "fit" together in a nameable way.
  • The test class becomes bloated with helper methods that distract readers from the essence of the tests.

I might move a helper method to a base class that is common to all of the test classes that use it. But as I write more and more helper methods, that tends to lead to bloated base classes.

So my preference is to put helper methods into separate classes. When a test needs a helper, it creates an object from the appropriate helper class and calls it.

In some cases, I will make a helper method static, so that the test code can call it directly, without creating an object.

| improve this answer | |
  • How would I call this helper class from my test script? For example, I have an Eclipse project named "Automation Project". Within that project I have all my different test scripts. I'm assuming the helper class would be placed within the same "project" (I'm using Eclipse), in it's own class? – kevin Nov 20 '14 at 1:02
  • I'm sorry but this isn't very helpful. – kevin Nov 21 '14 at 0:52
  • The remaining parts of the answer (e.g. how to call a method in another class) are more about Java basics than about test automation. I recommend spending a few hours with a book or tutorial about Java basics. Here is a good one from Oracle: docs.oracle.com/javase/tutorial/java/javaOO/index.html Once you know how to declare classes and methods, and how to call methods in other classes and objects, revisit my answer. I'm sure you will find it more helpful then. – Dale Emery Nov 21 '14 at 1:11
  • I added some example code to clarify - you should see it as soon as someone reviews my edits. In general, though, I agree with Mr. Emery - your questions are Java programming questions more than they are testing questions, so you're likely to get more/better responses on StackOverflow than this forum. – Bruce Nov 21 '14 at 19:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.