Я написал два класса Java в моем скрипте Selenium WebDriver; Один из них - это класс POM, в котором я определил все элементы веб-страницы, такие как firstName и lastName, email и т. Д. c .... Другой класс - это мой тестовый класс, в который я передаю значения из моих листов Excel. При выполнении моего скрипта TestClass показывает NullPointerException в последнем методе @Test. Я пытаюсь получить тестовые данные с листа Excel (скажем, например, FirstName), и я отправляю их в поле ввода firstName, используя объект класса POM. Но тест не проходит с NullPointerException
My POMClass. Java:
package com.datadriventest;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.ui.Select;
public class POMClass
{
WebDriver driver;
public POMClass(WebDriver webDriver)
{
driver = webDriver;
}
By regLink = By.linkText("REGISTER");
By firstName = By.name("firstName");//----> Identifying firstName element in POM Class.
By lastName = By.name("lastName");
By phone = By.name("phone");
By addressFirstLine = By.name("address1");
By addressSecondLine = By.name("address2");
By city = By.name("city");
By state = By.name("state");
By postalCode = By.name("postalCode");
By country = By.name("country");
By email = By.name("email");
By password = By.name("password");
By confirmPassword = By.name("confirmPassword");
By regSubmit = By.name("register");
public void goToRegLink()
{
driver.findElement(regLink).click();
}
public void setFirstName(String fname)//-->POM class method to send the firstName from testClass
{
driver.findElement(firstName).sendKeys(fname);
}
public void setLastName(String lname)
{
driver.findElement(lastName).sendKeys(lname);
}
public void setPhone(String phoneNo)
{
driver.findElement(phone).sendKeys(phoneNo);
}
public void setAddressFirstLine(String firstAddress)
{
driver.findElement(addressFirstLine).sendKeys(firstAddress);
}
public void setAddressSecondLine(String secondAddress)
{
driver.findElement(addressSecondLine).sendKeys(secondAddress);
}
public void setCity(String cityString)
{
driver.findElement(city).sendKeys(cityString);
}
public void setState(String stateString)
{
driver.findElement(state).sendKeys(stateString);
}
public void setPostalCode(String code)
{
driver.findElement(postalCode).sendKeys(code);
}
public void setCountry(String countryString)
{
WebElement countryDD = driver.findElement(country);
Select dropDown = new Select(countryDD);
dropDown.selectByVisibleText(countryString);
}
public void setEmail(String emailAddress)
{
driver.findElement(email).sendKeys(emailAddress);
}
public void setPassword(String passwordString)
{
driver.findElement(password).sendKeys(passwordString);
}
public void setConfirmPassword(String confPassword)
{
driver.findElement(confirmPassword).sendKeys(confPassword);
}
public void clickRegBtn()
{
driver.findElement(regSubmit).click();
}
}
My TestClass. Java:
package com.datadriventest;
import com.mavconfig.excel.utility.Xls_Reader;
import io.github.bonigarcia.wdm.config.DriverManagerType;
import io.github.bonigarcia.wdm.managers.ChromeDriverManager;
import io.github.bonigarcia.wdm.managers.FirefoxDriverManager;
import org.openqa.selenium.By;
import org.openqa.selenium.StaleElementReferenceException;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.firefox.FirefoxDriver;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;
import org.testng.annotations.*;
import java.util.concurrent.TimeUnit;
public class DataDrivenClass
{
WebDriver driver;
POMClass pomClass;
WebElement waitElement;
Xls_Reader dataReader;
String firstName,lastName,phone,email,address1,address2,city,state,postalCode,country,userName,password,confirmPassword;
@BeforeTest
@Parameters({"browser"})
void browserSetup(String browser)
{
if (browser.equals("Chrome"))
{
ChromeDriverManager.getInstance(DriverManagerType.CHROME).setup();
driver = new ChromeDriver();
}
else if (browser.equals("Firefox"))
{
FirefoxDriverManager.getInstance(DriverManagerType.FIREFOX).setup();
driver = new FirefoxDriver();
}
}
@BeforeClass
@Parameters({"url"})
void launchURL(String url)
{
System.out.println(url);
driver.manage().timeouts().implicitlyWait(15,TimeUnit.SECONDS);
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
driver.get(url);
}
@Test
void goToRegPage()
{
waitElement = driver.findElement(By.linkText("REGISTER"));
waitforVisiblity(driver, waitElement,15);
if (waitElement.isDisplayed())
{
pomClass = new POMClass(driver);----> Here's the place where i'm declaring the pomclass by passing the testclass driver instance as constructor for the POM Class..
pomClass.goToRegLink();//--> This click event is working fine,
}
}
@Test
void getExcelData()
{
dataReader = new Xls_Reader("C:\\Backup\\Selenium - Workspace\\IdeaProjects\\MavenSeleniumConfig\\src\\test\\java\\com\\testdata\\testData.xlsx");
firstName = dataReader.getCellData("RegData","firstname",2);
System.out.println("The first name is " +firstName);
lastName = dataReader.getCellData("RegData","lastname",2);
System.out.println("The last name is : " +lastName);
phone = dataReader.getCellData("RegData","phone",2);
System.out.println("The Phone Number : " +phone);
email = dataReader.getCellData("RegData","email",2);
System.out.println("The email address is : " +email);
address1 = dataReader.getCellData("RegData","Address1",2);
System.out.println("The First Address Line is : " +address1);
address2 = dataReader.getCellData("RegData","Address2",2);
System.out.println("The Second Address Line is : " +address2);
city = dataReader.getCellData("RegData","city",2);
System.out.println("The City is : " +city);
state = dataReader.getCellData("RegData","state",2);
System.out.println("The State is : " +state);
postalCode = dataReader.getCellData("RegData","postalcode",2);
System.out.println("The postal code is : " +postalCode);
country = dataReader.getCellData("RegData","country",2);
System.out.println("The Country is : " +country);
userName = dataReader.getCellData("RegData","username",2);
System.out.println("The Username is : " +userName);
password = dataReader.getCellData("RegData","password",2);
System.out.println("The password is : " +password);
confirmPassword = dataReader.getCellData("RegData","confirmpassword",2);
System.out.println("The confirm password is : " +confirmPassword);
}
@Test
void fillTheForm()
{
pomClass.setFirstName(firstName);//------> This is the place I'm trying to send the firstName value to the input textfield, which shows NullPointerException.
}
@AfterClass
void tearDown() throws InterruptedException {
Thread.sleep(5000);
driver.quit();
}
private void waitforVisiblity(WebDriver driver, WebElement element, int timeout)
{
new WebDriverWait(driver,timeout).ignoring(StaleElementReferenceException.class).until(ExpectedConditions.visibilityOf(element));
}
}
My LogCat:
"C:\Program Files\Java\jdk-13.0.2\bin\java.exe" -ea -Didea.test.cyclic.buffer.size=1048576 "-javaagent:C:\Program Files (x86)\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar=56527:C:\Program Files (x86)\IntelliJ IDEA Community Edition 2020.1\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files (x86)\IntelliJ IDEA Community Edition 2020.1\lib\idea_rt.jar;C:\Program Files (x86)\IntelliJ IDEA Community Edition 2020.1\plugins\testng\lib\testng-rt.jar;C:\Backup\Selenium - Workspace\IdeaProjects\MavenSeleniumConfig\target\test-classes;C:\Backup\Selenium - Workspace\IdeaProjects\MavenSeleniumConfig\target\classes;C:\Users\arunp\.m2\repository\org\seleniumhq\selenium\selenium-java\3.141.59\selenium-java-3.141.59.jar;C:\Users\arunp\.m2\repository\org\seleniumhq\selenium\selenium-api\3.141.59\selenium-api-3.141.59.jar;C:\Users\arunp\.m2\repository\org\seleniumhq\selenium\selenium-chrome-driver\3.141.59\selenium-chrome-driver-3.141.59.jar;C:\Users\arunp\.m2\repository\org\seleniumhq\selenium\selenium-edge-driver\3.141.59\selenium-edge-driver-3.141.59.jar;C:\Users\arunp\.m2\repository\org\seleniumhq\selenium\selenium-firefox-driver\3.141.59\selenium-firefox-driver-3.141.59.jar;C:\Users\arunp\.m2\repository\org\seleniumhq\selenium\selenium-ie-driver\3.141.59\selenium-ie-driver-3.141.59.jar;C:\Users\arunp\.m2\repository\org\seleniumhq\selenium\selenium-opera-driver\3.141.59\selenium-opera-driver-3.141.59.jar;C:\Users\arunp\.m2\repository\org\seleniumhq\selenium\selenium-remote-driver\3.141.59\selenium-remote-driver-3.141.59.jar;C:\Users\arunp\.m2\repository\org\seleniumhq\selenium\selenium-safari-driver\3.141.59\selenium-safari-driver-3.141.59.jar;C:\Users\arunp\.m2\repository\org\seleniumhq\selenium\selenium-support\3.141.59\selenium-support-3.141.59.jar;C:\Users\arunp\.m2\repository\net\bytebuddy\byte-buddy\1.8.15\byte-buddy-1.8.15.jar;C:\Users\arunp\.m2\repository\org\apache\commons\commons-exec\1.3\commons-exec-1.3.jar;C:\Users\arunp\.m2\repository\com\google\guava\guava\25.0-jre\guava-25.0-jre.jar;C:\Users\arunp\.m2\repository\com\google\code\findbugs\jsr305\1.3.9\jsr305-1.3.9.jar;C:\Users\arunp\.m2\repository\org\checkerframework\checker-compat-qual\2.0.0\checker-compat-qual-2.0.0.jar;C:\Users\arunp\.m2\repository\com\google\errorprone\error_prone_annotations\2.1.3\error_prone_annotations-2.1.3.jar;C:\Users\arunp\.m2\repository\com\google\j2objc\j2objc-annotations\1.1\j2objc-annotations-1.1.jar;C:\Users\arunp\.m2\repository\org\codehaus\mojo\animal-sniffer-annotations\1.14\animal-sniffer-annotations-1.14.jar;C:\Users\arunp\.m2\repository\com\squareup\okhttp3\okhttp\3.11.0\okhttp-3.11.0.jar;C:\Users\arunp\.m2\repository\com\squareup\okio\okio\1.14.0\okio-1.14.0.jar;C:\Users\arunp\.m2\repository\io\github\bonigarcia\webdrivermanager\4.0.0\webdrivermanager-4.0.0.jar;C:\Users\arunp\.m2\repository\org\slf4j\slf4j-api\1.7.30\slf4j-api-1.7.30.jar;C:\Users\arunp\.m2\repository\commons-io\commons-io\2.6\commons-io-2.6.jar;C:\Users\arunp\.m2\repository\com\google\code\gson\gson\2.8.6\gson-2.8.6.jar;C:\Users\arunp\.m2\repository\org\apache\commons\commons-lang3\3.10\commons-lang3-3.10.jar;C:\Users\arunp\.m2\repository\org\apache\httpcomponents\client5\httpclient5\5.0\httpclient5-5.0.jar;C:\Users\arunp\.m2\repository\org\apache\httpcomponents\core5\httpcore5\5.0\httpcore5-5.0.jar;C:\Users\arunp\.m2\repository\org\apache\httpcomponents\core5\httpcore5-h2\5.0\httpcore5-h2-5.0.jar;C:\Users\arunp\.m2\repository\commons-codec\commons-codec\1.13\commons-codec-1.13.jar;C:\Users\arunp\.m2\repository\org\rauschig\jarchivelib\1.0.0\jarchivelib-1.0.0.jar;C:\Users\arunp\.m2\repository\org\jsoup\jsoup\1.13.1\jsoup-1.13.1.jar;C:\Users\arunp\.m2\repository\org\testng\testng\6.14.3\testng-6.14.3.jar;C:\Users\arunp\.m2\repository\com\beust\jcommander\1.72\jcommander-1.72.jar;C:\Users\arunp\.m2\repository\org\apache-extras\beanshell\bsh\2.0b6\bsh-2.0b6.jar;C:\Users\arunp\.m2\repository\org\apache\poi\poi-ooxml\4.0.0\poi-ooxml-4.0.0.jar;C:\Users\arunp\.m2\repository\org\apache\poi\poi\4.0.0\poi-4.0.0.jar;C:\Users\arunp\.m2\repository\org\apache\commons\commons-collections4\4.2\commons-collections4-4.2.jar;C:\Users\arunp\.m2\repository\org\apache\poi\poi-ooxml-schemas\4.0.0\poi-ooxml-schemas-4.0.0.jar;C:\Users\arunp\.m2\repository\org\apache\xmlbeans\xmlbeans\3.0.1\xmlbeans-3.0.1.jar;C:\Users\arunp\.m2\repository\org\apache\commons\commons-compress\1.18\commons-compress-1.18.jar;C:\Users\arunp\.m2\repository\com\github\virtuald\curvesapi\1.04\curvesapi-1.04.jar;C:\Program Files (x86)\IntelliJ IDEA Community Edition 2020.1\plugins\testng\lib\jcommander-1.27.jar" com.intellij.rt.testng.RemoteTestNGStarter -usedefaultlisteners false -socket56526 @w@C:\Users\arunp\AppData\Local\Temp\idea_working_dirs_testng.tmp -temp C:\Users\arunp\AppData\Local\Temp\idea_testng.tmp
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Starting ChromeDriver 83.0.4103.39 (ccbf011cb2d2b19b506d844400483861342c20cd-refs/branch-heads/4103@{#416}) on port 25152
Only local connections are allowed.
Please see https://chromedriver.chromium.org/security-considerations for suggestions on keeping ChromeDriver safe.
ChromeDriver was started successfully.
May 26, 2020 4:21:30 PM org.openqa.selenium.remote.ProtocolHandshake createSession
INFO: Detected dialect: W3C
http://newtours.demoaut.com
java.lang.NullPointerException
at com.datadriventest.DataDrivenClass.fillTheForm(DataDrivenClass.java:109)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:567)
at org.testng.internal.MethodInvocationHelper.invokeMethod(MethodInvocationHelper.java:124)
at org.testng.internal.Invoker.invokeMethod(Invoker.java:583)
at org.testng.internal.Invoker.invokeTestMethod(Invoker.java:719)
at org.testng.internal.Invoker.invokeTestMethods(Invoker.java:989)
at org.testng.internal.TestMethodWorker.invokeTestMethods(TestMethodWorker.java:125)
at org.testng.internal.TestMethodWorker.run(TestMethodWorker.java:109)
at org.testng.TestRunner.privateRun(TestRunner.java:648)
at org.testng.TestRunner.run(TestRunner.java:505)
at org.testng.SuiteRunner.runTest(SuiteRunner.java:455)
at org.testng.SuiteRunner.runSequentially(SuiteRunner.java:450)
at org.testng.SuiteRunner.privateRun(SuiteRunner.java:415)
at org.testng.SuiteRunner.run(SuiteRunner.java:364)
at org.testng.SuiteRunnerWorker.runSuite(SuiteRunnerWorker.java:52)
at org.testng.SuiteRunnerWorker.run(SuiteRunnerWorker.java:84)
at org.testng.TestNG.runSuitesSequentially(TestNG.java:1208)
at org.testng.TestNG.runSuitesLocally(TestNG.java:1137)
at org.testng.TestNG.runSuites(TestNG.java:1049)
at org.testng.TestNG.run(TestNG.java:1017)
at com.intellij.rt.testng.IDEARemoteTestNG.run(IDEARemoteTestNG.java:66)
at com.intellij.rt.testng.RemoteTestNGStarter.main(RemoteTestNGStarter.java:110)
The first name is Arunprasadh
The last name is : S
The Phone Number : 7.402191727E9
The email address is : arunprasadh.s@gmail.com
The First Address Line is : Naval nagar, Vennaimalai(PO)
The Second Address Line is : Karur
The City is : Karur
The State is : Tamil Nadu
The postal code is : 639006.0
The Country is : INDIA
The Username is : Arun
The password is : arun1234
The confirm password is : arun1234
===============================================
My Maven Suite
Total tests run: 3, Failures: 1, Skips: 0
===============================================
Process finished with exit code 0
Пожалуйста, дайте мне знать, где я допустил ошибку, ожидая ваших ответов.