这是我第一次实现POM模型。
我使用骨架函数来初始化我的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();
我的应用程序正在正常启动,我可以登录。但是当我遍历到不同的链接时,我收到java.lang.NullPointerException。
我已经推断出这个问题,因为它是由于类(不同页面)的所有对象在启动selenium套件期间被初始化而引起的,因此我的元素类在第二或第三个位置执行尚未初始化。
下面代码的第一行用于获取驱动程序的实例,其余代码用于重新创建对象。
WebDriver driver = Driver.AppDriver.getInstance();
FirstClass obFirstClass = new FirstClass();
SecondClass objSecondClass = new SecondClass ();
ThirdClass objThirdClass = new ThirdClass ();
使用objFirstClass,我可以登录到我的系统并验证我的登录。使用objSecondClass,我可以打印字符串来表示登录成功。但是使用objThirdClass,我无法为web元素或选择对象输入值。
给出Null Exception错误。
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中继承它们,以减少混淆和重复。我这里有一个例子,如果你有帮助。
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);
}
}
我指的是下面的链接:
selenium webdriver select element
Select(WebElement element)
如果你这样做:
@FindBy(id="foo")
private WebElement wannabeSelect;
Select realSelect = new Select(wannabeSelect);
realSelect.selectByValue("myValue");
应该可以。
顺便说一句,我在"变通"中使用与您相同的方法,因为当我需要选择对象时,我不想转换新的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);
问题解决