簡體   English   中英

Selenium / Java:“ helper”方法放在測試腳本中的哪里?

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

在很大程度上,我是自動化和編程的新手。

我有一個測試腳本,必須在其中驗證正在測試的頁面中某些元素的存在。

目前,我有多個try / catch塊來測試每個單獨的元素,但是出於代碼可讀性的考慮,我希望有一個“輔助”方法,可以在其中調用各種元素。

這就是我到目前為止所擁有的。...我應該在哪里放置這種“幫助”方法?

它將放置在main方法之外嗎?

理想情況下,我希望將其放置在與測試腳本相同的Java類文件中。

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

}

如果將其放在單獨的類中並在測試類中實例化或擴展它會更好。

這樣,您將擁有適用於所有測試課程的方法。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM