Java 如何在TestNG中分离测试和函数
我是TestNG Selenium Webdriver的新手 我想在不同的类中分离测试如何做到这一点,但我无法从实际情况中了解每个测试要通过的参数。 这是我的密码Java 如何在TestNG中分离测试和函数,java,selenium-webdriver,testng,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.openq
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();
}
}
请告诉我如何将驱动程序实例传递给每个测试。您可以创建一个属性类。在该类中,需要添加带有BeforeClass注释的函数
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 {}
public class Flipkart1 entends Properties {}
public class Flipkart2 entends Properties {}