简体   繁体   中英

Selenium/Java:Where does the “helper” method get placed within a test script?

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 against 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?

Ideally, I would like for it to be placed within the same Java class file as my test script.

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();

}

It will be better if you place it in a separate class and instantiate or extend it in your test classes.

This way you will have the method available to all your test-classes.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM