package defaultTest;
import java.io.FileInputStream;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.testng.Assert;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.DataProvider;
public class NewTest{
static WebDriver driver;
XSSFWorkbook wb;
XSSFSheet sheet1;
int numrow;
@BeforeClass
public void setUp() {
driver = new FirefoxDriver();
//gmail URL
driver.get("https://accounts.google.com/ServiceLogin?service=mail&continue=https://mail.google.com/mail/");
}
@DataProvider(name = "testdata")
public Object[][] dp() throws Exception {
Object[][] tabArray = null;
try {
FileInputStream fis = new FileInputStream(
"C:\\Users\\gaurav\\workspace\\Test\\src\\Resources\\LoginDataExcelFile.xlsx");
wb = new XSSFWorkbook(fis);
sheet1 = wb.getSheet("TestSheet");
for (int count = 0; count <= sheet1.getLastRowNum(); count++) {
XSSFRow row = sheet1.getRow(count);
//System.out.println("ID: "+row.getCell(0).toString()+"\t password : "+row.getCell(1).toString());
testLogin(row.getCell(0).toString(), row.getCell(1).toString());
}
fis.close();
} catch (Exception e) {
System.out.println(e);
}
return tabArray;
}
@Test(dataProvider = "testdata")
public void testLogin(String UserName, String Password)throws Exception {
driver.findElement(By.id("Email")).clear();
Thread.sleep(2000);
driver.findElement(By.id("Email")).sendKeys(UserName);
Thread.sleep(2000);
driver.findElement(By.id("Passwd")).clear();
Thread.sleep(2000);
driver.findElement(By.id("Passwd")).sendKeys(Password);
Thread.sleep(2000);
driver.findElement(By.name("signIn")).click();
Thread.sleep(20000);
WebElement name = driver
.findElement(By
.xpath(".//*[@id=':hw']/div/div"));
try {
Assert.assertEquals("COMPOSE", name.getText());//Used some assertion
System.out.println(name.getText());
} catch(Exception e) {
System.out.println(e);
}
}
@AfterMethod
public void afterMethod() {
driver.close();
driver.quit();
}
}
這是一個測試應用程序。在運行測試之前,請確保excel文件只有一個沒有任何標題的loginID和密碼。
問題是,我只有一個記錄在Excel文件中,我的上面的腳本運行成功一次,但是當for循環再次運行時,它在Excel中找不到記錄,並且我的測試被跳過,因此另一個測試失敗。完成所有說明後跳過Selenium webdriver測試,並給出java.lang.NullPointerException的錯誤,我該如何解決它?
> [TestNG] Running:
> C:\Users\gaurav\AppData\Local\Temp\testng-eclipse-1831957551\testng-customsuite.xml
>
> COMPOSE
>
> SKIPPED: testLogin java.lang.NullPointerException at
> org.testng.internal.MethodInvocationHelper.invokeDataProvider(MethodInvocationHelper.java:149)
> at
> org.testng.internal.Parameters.handleParameters(Parameters.java:429)
> at org.testng.internal.Invoker.handleParameters(Invoker.java:1383)
> at org.testng.internal.Invoker.createParameters(Invoker.java:1075)
> at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:1180)
> at
> org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:127)
> at
> org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:111)
> at org.testng.TestRunner.privateRun(TestRunner.java:767) at
> org.testng.TestRunner.run(TestRunner.java:617) at
> org.testng.SuiteRunner.runTest(SuiteRunner.java:334) at
> org.testng.SuiteRunner.runSequentially(SuiteRunner.java:329) at
> org.testng.SuiteRunner.privateRun(SuiteRunner.java:291) at
> org.testng.SuiteRunner.run(SuiteRunner.java:240) at
> org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52) at
> org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:86) at
> org.testng.TestNG.runSuitesSequentially(TestNG.java:1224) at
> org.testng.TestNG.runSuitesLocally(TestNG.java:1149) at
> org.testng.TestNG.run(TestNG.java:1057) at
> org.testng.remote.RemoteTestNG.run(RemoteTestNG.java:111) at
> org.testng.remote.RemoteTestNG.initAndRun(RemoteTestNG.java:204) at
> org.testng.remote.RemoteTestNG.main(RemoteTestNG.java:175)
>
>
> ===============================================
> Default test
> Tests run: 1, Failures: 0, Skips: 1
> ===============================================
我沒有改變註釋,使私人webdriver的嘗試,但仍是問題是一樣的,我認爲主要的罪魁禍首是「for循環」,我的Excel只有一個記錄,腳本工作正常,該記錄,但是當'for循環'條件失敗時,我的測試會得到SKIPPED,在'循環'中被調用,剩下的測試會失敗。 – 2015-03-03 04:14:07
是的,我很抱歉,你的問題在DataProvider中。 – 2015-03-03 17:26:32