Selenium webdriver 我在页面工厂模型中的@FindBy注释下为webelement定义了空指针异常
我是硒学习新手。当我尝试使用web元素-Selenium webdriver 我在页面工厂模型中的@FindBy注释下为webelement定义了空指针异常,selenium-webdriver,testng-eclipse,Selenium Webdriver,Testng Eclipse,我是硒学习新手。当我尝试使用web元素-Milestone\u Tile\u Text时,出现null指针异常在我的代码中,但使用 LoginTestScript.fd.findElement(By.linkText("Milestone")).click(); 请参阅下面我使用的代码PageFactory.initElements,但不确定如何解决此错误 public class MilestoneTileModel { GenerateTestData objtestdata
Milestone\u Tile\u Text时,出现null指针异常代码>在我的代码中,但使用
LoginTestScript.fd.findElement(By.linkText("Milestone")).click();
请参阅下面我使用的代码PageFactory.initElements
,但不确定如何解决此错误
public class MilestoneTileModel
{
GenerateTestData objtestdata = new GenerateTestData() ;
public MilestoneTileModel() //constructor
{
PageFactory.initElements(LoginTestScript.fd, this);
}
@FindBy(xpath="//a[text()='Milestone']")
WebElement Milestone_Tile_Text;
public void Milestone_Tile_Click()
{
Milestone_Tile_Text.click();
LoginTestScript.fd.findElement(By.linkText("Milestone")).click();
LoginTestScript.fd.findElement(By.xpath("//*@id='CPH_btnAddNewMilestoneTop']")).click();
}
}
使用init方法时,可能会更频繁地出现计时问题
计时问题是,当您初始化一个元素时,驱动程序会立即尝试查找元素,如果失败,您将不会收到任何警告,但元素将引用null
例如,出现上述情况可能是因为页面未完全呈现,或者驱动程序看到页面的旧版本
修复方法是将元素定义为属性,并在属性的get
上使用驱动程序从页面获取元素
请注意,selenium不保证驱动程序可以看到页面的最新版本,因此即使这样也可能会出现问题,在某些情况下,重试也会起作用。我看到的第一个问题:您没有设置LoginTestScript
以下文档首先需要设置PageObject变量:
GoogleSearchPage=PageFactory.initElements(驱动程序,GoogleSearchPage.class)代码>
丰富这一点的最好方法是单独的页面对象模型和场景scipt
第一个文件POM应包含:
罗金泰斯特通
public class LoginTestPOM {
@FindBy(xpath="//a[text()='Milestone']")
WebElement MilestoneTileText;
public void clickMilestoneTitleText(){
MilestoneTitleText.click();
}
}
测试脚本
import LoginTestPOM
public class TestLogin {
public static void main(String[] args) {
// Create a new instance of a driver
WebDriver driver = new HtmlUnitDriver();
// Navigate to the right place
driver.get("http://www.loginPage.com/");
// Create a new instance of the login page class
// and initialise any WebElement fields in it.
LoginTestPOM page = PageFactory.initElements(driver, LoginTestPOM.class);
// And now do the page action.
page.clickMilestoneTitleText();
}
}
public class UglyTestLogin {
public static void main(String[] args) {
// Create a new instance of a driver
WebDriver driver = new HtmlUnitDriver();
// Navigate to the right place
driver.get("http://www.loginPage.com/");
// DON'T create a new instance of the login page class
// and DON'T initialise any WebElement fields in it.
// And do the page action.
driver.findElement(By.xpath("//a[text()='Milestone']").click()
}
}
这是页面对象模式的基础
注意:我只在浏览器中编写代码,所以它可能包含一些错误
链接:
没有页面对象模式的“丑陋”解决方案是:
丑陋的测试脚本
import LoginTestPOM
public class TestLogin {
public static void main(String[] args) {
// Create a new instance of a driver
WebDriver driver = new HtmlUnitDriver();
// Navigate to the right place
driver.get("http://www.loginPage.com/");
// Create a new instance of the login page class
// and initialise any WebElement fields in it.
LoginTestPOM page = PageFactory.initElements(driver, LoginTestPOM.class);
// And now do the page action.
page.clickMilestoneTitleText();
}
}
public class UglyTestLogin {
public static void main(String[] args) {
// Create a new instance of a driver
WebDriver driver = new HtmlUnitDriver();
// Navigate to the right place
driver.get("http://www.loginPage.com/");
// DON'T create a new instance of the login page class
// and DON'T initialise any WebElement fields in it.
// And do the page action.
driver.findElement(By.xpath("//a[text()='Milestone']").click()
}
}
使用xpath是否可以单击该链接?LoginTestScript.fd.findElement(By.xpath(“//a[text()='Milestone']”)。单击();请检查Milestone_Tile_click()方法的最后一次单击,您的xpath不正确,需要在*@peterpawar…后面打开[括号]。是的,链接是可单击的。@lauda…复制粘贴错误,存在[在代码中。您应该添加一个等待元素的方法,findElement没有等待,如果找不到元素,则将返回null,并将导致null指针异常。计时问题返回隐式超时。