2014-11-21 30 views
0

我大部分都是自動化和編程新手。Selenium/Java:「幫助程序」方法放在測試腳本中的哪個位置?

我有一個測試腳本,我必須驗證我正在測試的頁面中是否存在某些元素。現在,我有多個try/catch塊來測試每個單獨的元素,但爲了代碼可讀性,我希望有一個「幫助器」方法,我可以在其中調用各種元素。

這就是我到目前爲止......我在哪裏可以放置這個「幫手」方法?

它會被放在主要方法之外嗎?

理想情況下,我想將它放在與我的測試腳本相同的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(); 

} 
+0

你應該看看[Page Object Model。](http://www.toolsqa.com/selenium-webdriver/page-object-model/) – Saifur 2014-11-21 03:27:26

回答

0

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

這樣你就可以將這個方法提供給所有的測試類。

相關問題