我是Selenium
的新用户,并在“代码”下面编写了从ITestContext
组中获取参数的代码。
代码:
import java.util.concurrent.TimeUnit;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.ITestContext;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
public class ITestGroupism {
WebDriver driver;
@BeforeTest(groups={"A","B"})
public void setup()
{
System.setProperty("webdriver.chrome.driver", "E:\\Automation Jars\\chromedriver_win32\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.google.co.in");
}
@Test(dataProvider="SearchProvider", groups="A")
public void testMethodA(String author, String searchKey) throws InterruptedException
{
WebElement searchText = driver.findElement(By.id("sb_ifc0"));
Actions actions = new Actions(driver);
actions.moveToElement(searchText);
actions.click();
actions.sendKeys(searchKey);
actions.build().perform();
System.out.println("Welcome ->"+author+" Your search key is "+searchKey);
driver.navigate().back();
driver.navigate().forward();
System.out.println("thread will sleep now");
Thread.sleep(2000);
}
@Test(dataProvider="SearchProvider", groups="B")
public void testMethodB(String searchKey) throws InterruptedException
{
WebElement searchText = driver.findElement(By.id("sb_ifc0"));
Actions actions = new Actions(driver);
actions.moveToElement(searchText);
actions.click();
actions.sendKeys(searchKey);
actions.build().perform();
//searchText.sendKeys(searchKey);
System.out.println("Welcome Professor"+"Your search key is "+searchKey);
driver.navigate().back();
driver.navigate().forward();
System.out.println("thread will sleep now");
Thread.sleep(2000);
}
@DataProvider(name="SearchProvider")
public Object[][] ff(ITestContext c)
{
Object[][] groupArray =null;
for(String group : c.getIncludedGroups())
{
if(group.equalsIgnoreCase("A"))
{
groupArray = new Object[][]
{
{"Aakash", "India"},
{"Aayush", "US"},
{"Raveena", "UK"}
};
break;
}else if(group.equalsIgnoreCase("B"))
{
groupArray= new Object[][]
{
{"Canada"},
{"New Zealand"},
{"Russia"}
};
}break;
}
return groupArray;
}
}
但我得到以下例外:
SKIPPED:testMethodA java.lang.NullPointerException at org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:151) 在 org.testng.internal.Parameters.handleParameters(Parameters.java:430) 在org.testng.internal.Invoker.handleParameters(Invoker.java:1243) 在org.testng.internal.Invoker.createParameters(Invoker.java:992)at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1082)at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:129) 在 org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:112) 在org.testng.TestRunner.privateRun(TestRunner.java:778)at org.testng.TestRunner.run(TestRunner.java:632)at org.testng.SuiteRunner.runTest(SuiteRunner.java:366)at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:361)at at org.testng.SuiteRunner.privateRun(SuiteRunner.java:319)at at org.testng.SuiteRunner.run(SuiteRunner.java:268)at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86)at org.testng.TestNG.runSuitesSequentially(TestNG.java:1225)at at org.testng.TestNG.runSuitesLocally(TestNG.java:1150)at at org.testng.TestNG.runSuites(TestNG.java:1075)at org.testng.TestNG.run(TestNG.java:1047)at org.testng.remote.AbstractRemoteTestNG.run(AbstractRemoteTestNG.java:126) 在org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:137) 在org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:58)
SKIPPED:testMethodB java.lang.NullPointerException at ....
答案 0 :(得分:2)
使用组时,需要在XML文件中定义这些组。
<suite name="Group Suite" verbose="1">
<test name="Test">
<groups>
<run>
<include name="A" />
<include name="B" />
</run>
</groups>
<classes>
<class name="fully.qualied.package.ITestGroupism" />
</classes>
</test>
</suite>
更新代码:
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.interactions.Actions;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.DataProvider;
import org.testng.annotations.Test;
import java.io.File;
import java.lang.reflect.Method;
import java.util.concurrent.TimeUnit;
public class ITestGroupism {
WebDriver driver;
@BeforeTest(groups = {"A", "B"})
public void setup() {
System.setProperty("webdriver.chrome.driver", new File("src/test/resources/driver/chromedriver")
.getAbsolutePath());
driver = new ChromeDriver();
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.get("https://www.google.co.in");
}
@Test(dataProvider = "SearchProvider", alwaysRun = true, groups = "A")
public void testMethodA(String author, String searchKey) throws InterruptedException {
WebElement searchText = driver.findElement(By.id("sb_ifc0"));
Actions actions = new Actions(driver);
actions.moveToElement(searchText);
actions.click();
actions.sendKeys(searchKey);
actions.build().perform();
System.out.println("Welcome ->" + author + " Your search key is " + searchKey);
driver.navigate().back();
driver.navigate().forward();
System.out.println("thread will sleep now");
Thread.sleep(2000);
}
@Test(dataProvider = "SearchProvider", alwaysRun = true, groups = "A")
public void testMethodB(String searchKey) throws InterruptedException {
WebElement searchText = driver.findElement(By.id("sb_ifc0"));
Actions actions = new Actions(driver);
actions.moveToElement(searchText);
actions.click();
actions.sendKeys(searchKey);
actions.build().perform();
//searchText.sendKeys(searchKey);
System.out.println("Welcome Professor" + "Your search key is " + searchKey);
driver.navigate().back();
driver.navigate().forward();
System.out.println("thread will sleep now");
Thread.sleep(2000);
}
@DataProvider(name = "SearchProvider")
public Object[][] ff(Method method) {
Object[][] groupArray = null;
if (method.getName().equalsIgnoreCase("testMethodA")) {
groupArray = new Object[][]
{
{"Aakash", "India"},
{"Aayush", "US"},
{"Raveena", "UK"}
};
} else if (method.getName().equalsIgnoreCase("testMethodB")) {
groupArray = new Object[][]
{
{"Canada"},
{"New Zealand"},
{"Russia"}
};
}
return groupArray;
}
}