Selenium POM 模型:java.lang.NullPointerException 错误。无法为 Web 元素或 Select 元素输入值
Selenium POM Model: java.lang.NullPointerException Error. Not Able To Enter Values For Webelements Or Select Elements
我是第一次实现 POM 模型 sleneium。
我正在使用骨架函数来初始化我的 WebDriver。如下:
File pathToBinary = new File("<path>\firefox.exe");
FirefoxBinary binary = new FirefoxBinary(pathToBinary);
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.http", "<proxyaddress>");
profile.setPreference("network.proxy.http_port", "<portnumber>");
driver = new FirefoxDriver(binary, new FirefoxProfile());
driver.manage().window().maximize();
我的应用程序正常启动,我可以登录。但是当我遍历到一个不同的 link 时,我收到 java.lang.NullPointerException.
我已经推断出这个问题是因为 class(不同页面)的所有对象都在 selenium 套件启动期间被初始化,因此我的元素 class 被执行在第二个或第三个位置尚未初始化。
下面代码中的第一行用于获取驱动程序实例,其余代码用于创建对象。
WebDriver driver = Driver.AppDriver.getInstance();
FirstClass obFirstClass = new FirstClass();
SecondClass objSecondClass = new SecondClass ();
ThirdClass objThirdClass = new ThirdClass ();
使用 objFirstClass,我能够登录到我的系统并验证我的登录。
使用 objSecondClass,我可以打印字符串来表示登录成功。
但是使用 objThirdClass,我无法为 WebElements 或 Select 对象输入值。
它给出空异常错误。
public class TestClass
{
WebDriver driver = Driver.AppDriver.getInstance();
FirstClass obFirstClass = new FirstClass();
SecondClass objSecondClass = new SecondClass ();
ThirdClass objThirdClass = new ThirdClass (driver);
@Test(priority=2)
public void method()
{
objThirdClass.action1();
System.out.println("after action"); //-> This line is being printed
objThirdClass.action2(param1, param2, param3);
}
}
public class ThirdClass {
WebDriver driver = Driver.AppDriver.getInstance();
public ThirdClass(WebDriver _driver){
//This initElements method will create all WebElements
driver = _driver;
PageFactory.initElements(driver, this);
}
@FindBy(xpath=<xpath>)
WebElement elementCreate;
@FindBy(id=<id1>)
Select selectElement1;
@FindBy(id=<id2>)
Select selectElement2;
@FindBy(id=<id3>)
Select selectElement3;
@FindBy(id="submit")
WebElement elementSubmit;
public void action1()
{
JavascriptExecutor executor2 = (JavascriptExecutor)driver;
executor2.executeScript("arguments[0].click();", elementCreate);
System.out.println("Create link found");
}
public void setElement1(String str1)
{
selectElement1.selectByVisibleText(str1);
}
public void setElement1(String str2)
{
selectElement2.selectByVisibleText(str2);
}
public void setElement1(String str3)
{
selectElement3.selectByVisibleText(str3);
}
public void submit()
{
submit.click();
}
public void action2(String string1, String string2, String string3,)
{
this.setElement1(str1);
this.setElement2(str2);
this.setElement3( str3)
this.submit();
}
}
问题似乎出在 driver
实例上。您需要使用测试中的驱动程序覆盖 PageObject 中的驱动程序。准确地说,您应该创建一个 BaseClass 来处理所有常用方法、驱动程序实例化、pageFactory 和 Elements 实例化,并从每个 PageObjects 继承它以减少混淆和重复。如果有帮助,我有一个示例 here。
public class TestClass(){
WebDriver driver = Driver.AppDriver.getInstance();
driver = new ChromeDriver();
ThirdClass objThirdClass = new ThirdClass (driver);
public void method()
{
objThirdClass.action1();
System.out.println("after action"); //-> This line is being printed
objThirdClass.action2(param1, param2, param3);
}
}
public class ThirdClass {
WebDriver driver = Driver.AppDriver.getInstance();
public ThirdClass(WebDriver _driver){
driver = _driver;
//This initElements method will create all WebElements
PageFactory.initElements(driver, this);
}
}
我在下面提到了link:
selenium webdriver select element
Select(WebElement element)
所以如果你这样做:
@FindBy(id="foo")
private WebElement wannabeSelect;
Select realSelect = new Select(wannabeSelect);
realSelect.selectByValue("myValue");
应该可以。
顺便说一句,我在 "workaround" 中使用与您相同的方法,因为我不想在需要 Select 对象时转换新的 WebElement 对象。但无论如何,
sDriver.findElement(By.id("foo"));
returns WebElement, so thats why its working. You can also do this:
WebElement wannabeSelect = sDriver.findElement(By.id("foo"));
Select foo = new Select(wannabeSelect);
它解决了问题。
我是第一次实现 POM 模型 sleneium。
我正在使用骨架函数来初始化我的 WebDriver。如下:
File pathToBinary = new File("<path>\firefox.exe");
FirefoxBinary binary = new FirefoxBinary(pathToBinary);
FirefoxProfile profile = new FirefoxProfile();
profile.setPreference("network.proxy.http", "<proxyaddress>");
profile.setPreference("network.proxy.http_port", "<portnumber>");
driver = new FirefoxDriver(binary, new FirefoxProfile());
driver.manage().window().maximize();
我的应用程序正常启动,我可以登录。但是当我遍历到一个不同的 link 时,我收到 java.lang.NullPointerException.
我已经推断出这个问题是因为 class(不同页面)的所有对象都在 selenium 套件启动期间被初始化,因此我的元素 class 被执行在第二个或第三个位置尚未初始化。
下面代码中的第一行用于获取驱动程序实例,其余代码用于创建对象。
WebDriver driver = Driver.AppDriver.getInstance();
FirstClass obFirstClass = new FirstClass();
SecondClass objSecondClass = new SecondClass ();
ThirdClass objThirdClass = new ThirdClass ();
使用 objFirstClass,我能够登录到我的系统并验证我的登录。 使用 objSecondClass,我可以打印字符串来表示登录成功。 但是使用 objThirdClass,我无法为 WebElements 或 Select 对象输入值。
它给出空异常错误。
public class TestClass
{
WebDriver driver = Driver.AppDriver.getInstance();
FirstClass obFirstClass = new FirstClass();
SecondClass objSecondClass = new SecondClass ();
ThirdClass objThirdClass = new ThirdClass (driver);
@Test(priority=2)
public void method()
{
objThirdClass.action1();
System.out.println("after action"); //-> This line is being printed
objThirdClass.action2(param1, param2, param3);
}
}
public class ThirdClass {
WebDriver driver = Driver.AppDriver.getInstance();
public ThirdClass(WebDriver _driver){
//This initElements method will create all WebElements
driver = _driver;
PageFactory.initElements(driver, this);
}
@FindBy(xpath=<xpath>)
WebElement elementCreate;
@FindBy(id=<id1>)
Select selectElement1;
@FindBy(id=<id2>)
Select selectElement2;
@FindBy(id=<id3>)
Select selectElement3;
@FindBy(id="submit")
WebElement elementSubmit;
public void action1()
{
JavascriptExecutor executor2 = (JavascriptExecutor)driver;
executor2.executeScript("arguments[0].click();", elementCreate);
System.out.println("Create link found");
}
public void setElement1(String str1)
{
selectElement1.selectByVisibleText(str1);
}
public void setElement1(String str2)
{
selectElement2.selectByVisibleText(str2);
}
public void setElement1(String str3)
{
selectElement3.selectByVisibleText(str3);
}
public void submit()
{
submit.click();
}
public void action2(String string1, String string2, String string3,)
{
this.setElement1(str1);
this.setElement2(str2);
this.setElement3( str3)
this.submit();
}
}
问题似乎出在 driver
实例上。您需要使用测试中的驱动程序覆盖 PageObject 中的驱动程序。准确地说,您应该创建一个 BaseClass 来处理所有常用方法、驱动程序实例化、pageFactory 和 Elements 实例化,并从每个 PageObjects 继承它以减少混淆和重复。如果有帮助,我有一个示例 here。
public class TestClass(){
WebDriver driver = Driver.AppDriver.getInstance();
driver = new ChromeDriver();
ThirdClass objThirdClass = new ThirdClass (driver);
public void method()
{
objThirdClass.action1();
System.out.println("after action"); //-> This line is being printed
objThirdClass.action2(param1, param2, param3);
}
}
public class ThirdClass {
WebDriver driver = Driver.AppDriver.getInstance();
public ThirdClass(WebDriver _driver){
driver = _driver;
//This initElements method will create all WebElements
PageFactory.initElements(driver, this);
}
}
我在下面提到了link:
selenium webdriver select element
Select(WebElement element)
所以如果你这样做:
@FindBy(id="foo")
private WebElement wannabeSelect;
Select realSelect = new Select(wannabeSelect);
realSelect.selectByValue("myValue");
应该可以。
顺便说一句,我在 "workaround" 中使用与您相同的方法,因为我不想在需要 Select 对象时转换新的 WebElement 对象。但无论如何,
sDriver.findElement(By.id("foo"));
returns WebElement, so thats why its working. You can also do this:
WebElement wannabeSelect = sDriver.findElement(By.id("foo"));
Select foo = new Select(wannabeSelect);
它解决了问题。