如何在TestNG中分离测试和功能

时间:2014-02-07 02:57:25

标签: java selenium-webdriver testng

我是TestNG Selenium Webdriver的新手,

我想在不同的类中分离测试如何做到这一点,我无法理解从实际中传递每个测试的参数。 这是我的代码

package Examples;


import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import org.testng.annotations.BeforeClass;
import org.openqa.selenium.By;
import org.openqa.selenium.JavascriptExecutor;
import org.openqa.selenium.Point;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.AfterClass;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;

public class Flipkart {
    public static WebDriver driver;


    @BeforeClass
    public void beforeClass()
    {
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(30000, TimeUnit.MILLISECONDS);


    }

    @Test
    public void FlipkartTest() throws InterruptedException
    {


        driver.get("https://www.flipkart.com/");
        driver.manage().window().maximize();

      navback();

      ArrayList<String> arrayList1 = new ArrayList<String>();
      arrayList1.add("Samsung");
      arrayList1.add("Nokia");
      arrayList1.add("Micromax");
      arrayList1.add("Sony");
      arrayList1.add("Brands");
      arrayList1.add("Android");
      arrayList1.add("Windows");
      for(int i=0; i<arrayList1.size(); i++)
      {
      String xpath = "//*[contains(@href,'%s')]";
      String y = arrayList1.get(i);
      String xpathOfElement = String.format(xpath, String.valueOf(y));

         System.out.println("Name 1  "+driver.findElement(By.xpath(xpathOfElement)).getText());
         driver.findElement(By.xpath(xpathOfElement)).click();
        System.out.println("Count 1:  "+driver.findElement(By.xpath("//*[@id='searchCount']")).getText());


        ArrayList<String> arrayList = new ArrayList<String>();

        arrayList.add("price_range");
        arrayList.add("type");
        arrayList.add("screen_size");
        arrayList.add("features");
        arrayList.add("primary_camera");

    for(int j=0; j<arrayList.size(); j++)
        {
            String x = arrayList.get(j);
                WebElement ul = driver.findElement(By.id(x));
        List<WebElement> lis = ul.findElements(By.tagName("li"));
        for (WebElement li : lis)
            {
                System.out.print(" 1st part: "+li.getAttribute("title")); //To get "Rs. 2001 - Rs. 5000"
                System.out.println("  2nd part: "+li.findElement(By.xpath(".//span[@class='count']")).getText()); 
            }
        System.out.println("");
        }
    navback();
      }

        }

   public void navback()
    {


          WebElement we = driver.findElement(By.xpath("//*[contains(@data-key,'electronics')]"));
          Actions action = new Actions(driver);
          action.moveToElement(we).build().perform();
    }
@AfterClass
    public void tear()
    {
       // driver.quit();
   }
}

我希望测试结果如此  FlipkartTest.java,FlipkartTest1.java和FlipkartTest2.java有三个不同的类,我正在使用的函数应该在一个公共文件中存在 - functions.java

public class Flipkart {
    public static WebDriver driver;


    @BeforeClass
    public void beforeClass()
    {
    driver = new FirefoxDriver();
    driver.manage().timeouts().implicitlyWait(30000, TimeUnit.MILLISECONDS);


    }

    @Test
    public void FlipkartTest() throws InterruptedException
    {
    }

    @Test
    public void FlipkartTest1() throws InterruptedException
    {
    }

    @Test
    public void FlipkartTest2() throws InterruptedException
    {
    }   

@AfterClass
    public void tear()
    {
       // driver.quit();
   }
}

请告诉我如何将驱动程序实例传递给每个测试。

1 个答案:

答案 0 :(得分:0)

您可以创建一个Properties类。在该类中,您需要使用BeforeClass Annotation

添加一个函数
public class Properties {
    public static WebDriver driver;
    @BeforeClass(alwaysRun=true)
    public void LaunchBrowser()
    {
        driver = new FirefoxDriver();
    }
}

然后你必须在测试类中继承属性类

public class Flipkart1 entends Properties {}
public class Flipkart2 entends Properties {}