我正在将@dataprovider用于Java和TestNg上的Selenium项目,特别是在实现ucelabs所需功能的TestBase类上。 我担心的是,此代码返回“ NullPointerException”错误,因为从未使用过数据提供程序值。 有人可以指出我的代码有什么问题(使用与示例中相同的代码:https://github.com/saucelabs-sample-test-frameworks/Java-TestNG-Selenium/blob/master/src/test/java/com/yourcompany/Tests/TestBase.java)
public class TestBase {
protected WebDriver driver;
protected static String baseUrl;
protected Application app;
protected ConfigFileReader configRead;
protected PropertyLoader propertyRead;
public Logger Log = Logger.getLogger(BasicTest_Local.class.getName());
public static final String SAUCE_ACCESS_KEY = System.getenv("SAUCE_ACCESS_KEY");
public static final String SAUCE_USERNAME = System.getenv("SAUCE_USERNAME");
private ThreadLocal<WebDriver> webDriver = new ThreadLocal<WebDriver>();
@DataProvider(name = "hardCodedBrowsers", parallel = true)
public static Object[][] sauceBrowserDataProvider(Method testMethod) {
return new Object[][]{
new Object[]{"firefox", "55.0", "Windows 10"},
new Object[]{"chrome", "65.0", "Windows 10"},
};}
public void createDriver(String browser, String version, String os, String methodName)
throws Exception, MalformedURLException, UnexpectedException {
Class<? extends TestBase> SLclass = this.getClass();
DesiredCapabilities capabilities = new DesiredCapabilities();
// set desired capabilities to launch appropriate browser on Sauce
capabilities.setCapability(CapabilityType.BROWSER_NAME, browser);
capabilities.setCapability(CapabilityType.VERSION, version);
capabilities.setCapability(CapabilityType.PLATFORM_NAME, os);
System.out.println(CapabilityType.PLATFORM_NAME + CapabilityType.VERSION + CapabilityType.PLATFORM_NAME);
capabilities.setCapability("seleniumVersion", "3.8.1");
capabilities.setCapability("name", SLclass.getSimpleName());
capabilities.setCapability("screenResolution", "1920x1080");
this.webDriver.set(new RemoteWebDriver(new URL("http://" + SAUCE_USERNAME + ":" + SAUCE_ACCESS_KEY + "@ondemand.saucelabs.com:80/wd/hub"), capabilities));
configRead = new ConfigFileReader();
propertyRead = new PropertyLoader();
baseUrl = propertyRead.getProperty("site.url");
app = new Application(driver);
getURL();
}
private void getURL () {
driver.get(baseUrl);
driver.manage().timeouts().implicitlyWait(40, TimeUnit.SECONDS);
Log.info("Open a site URL");
}
@AfterMethod(description = "Throw the test execution results into saucelabs")
public void afterMethod(ITestResult result) throws Exception {
((JavascriptExecutor) driver).executeScript("sauce:job-result=" + (result.isSuccess() ? "passed" : "failed"));
driver.quit();
}
}
仅供参考:我尝试在“ createDriver”方法之前添加@test注释以及数据提供者名称:
@Test(dataprovider =“ name”) public void createDriver(字符串浏览器,字符串版本,字符串os,字符串methodName)
但是这种方法只会打开浏览器和站点url(baseURL),而不会进入扩展TestBase类的测试类(“登录”):
public class Login_and_CreateOrder extends TestBase {
@Test (groups = {"HappyPath"})
public void Login() {
app.homePage().homePageDisplayed();
Log.info("Validate Home Page");
app.loginPage().Login_as(
configRead.getUserName2(),configRead.getPassword());
Log.info("Login as user");
assertTrue(app.loginPage().welcomeMessage.getText().contains("Hello"),
"Validate user's login.Display Welcome message");
}
}
其他.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE suite SYSTEM "http://testng.org/testng-1.0.dtd">
<suite name="Tests Suite" verbose="2" parallel="methods" thread-count="2">
<test name="SmokeRunTests" parallel="methods" thread-count="2">
<classes>
<class name="com.****.tests.Login"/>
</classes>
</test>
错误:
[ERROR] Tests run: 2, Failures: 2, Errors: 0, Skipped: 0, Time elapsed: 4.448
s <<< FAILURE! - in TestSuite
[ERROR] Login(com.****.tests.Login) Time elapsed: 0.365 s <<< FAILURE!
java.lang.NullPointerException
at com.****.tests.Login.Login(Login.java:13)
[ERROR] afterMethod(com.****.tests.Login) Time elapsed: 0.455 s <<<
FAILURE!
java.lang.NullPointerException
[INFO]
[INFO] Results:
[INFO]
[ERROR] Failures:
[ERROR] Login.Login:13 NullPointer
[ERROR] Login.TestBase.afterMethod:95 » NullPointer
[INFO]
[ERROR] Tests run: 2, Failures: 2, Errors: 0, Skipped: 0
[INFO]
[INFO] ----------------------------------------------------------------------
--
[INFO] BUILD FAILURE
[INFO] -------------------------------------
答案 0 :(得分:0)
您的数据提供者有一个逗号,
@DataProvider(name = "hardCodedBrowsers", parallel = true)
public static Object[][] sauceBrowserDataProvider(Method testMethod) {
return new Object[][]{
new Object[]{"firefox", "55.0", "Windows 10"},
new Object[]{"chrome", "65.0", "Windows 10"}, //no need of comma for the last value
};}
尝试一下,我已经删除了逗号,并且可以正常使用
@DataProvider(name = "hardCodedBrowsers", parallel = true)
public static Object[][] sauceBrowserDataProvider(Method testMethod) {
return new Object[][]{
{"firefox", "55.0", "Windows 10"},
{"chrome", "65.0", "Windows 10"}
};}
答案 1 :(得分:0)
只需编辑@Test注释的值:
@Test (groups = {"HappyPath"}, dataProvider = "hardCodedBrowsers")//you should add data provider
public void Login(String browser, String version, String os, Method method) {
//and add initialization for some browser
this.createDriver(browser, version, os, method.getName());// you missed this init
app.homePage().homePageDisplayed();
Log.info("Validate Home Page");
app.loginPage().Login_as(
configRead.getUserName2(),configRead.getPassword());
Log.info("Login as user");
assertTrue(app.loginPage().welcomeMessage.getText().contains("Hello"),
"Validate user's login.Display Welcome message");
}