我正在拧PageFactory
使用maven+TestNG的网站框架,
我有页面明智PageObject
其中存在特定于页面的所有 Web 元素和操作的类,例如 LoginPageObject
, AccountSelectionPageObject
...
我有一个“Base”类,其中存在 WebDriver、Logger 等常见元素。
我有一个类“BasePage”,其中存在单击、滚动、选择、刷新等常见操作
MyTestng.xml 有单独的 <class>
所有单独页面的条目。
只是我正在初始化@BeforeSuiit
中的浏览器对象并将其存储/放置在基类中,该类正在我的测试类中扩展
以下是我为我的项目提出的流程/架构。
问题:
我的每个测试类中有多个@Test。 当我的测试类单独执行时,所有@Test脚本都会执行, 但是当我连续执行它们时,即我的 testng 文件对于所有测试类都有单独的条目,我的执行失败。错误说无法在页面上找到元素,我有等待语句,但它仍然不起作用。
我尝试过调试代码,但无法找到原因,因为流程在第二页开始时停止,并出现异常,提示找不到元素
代码:
@FindBy(id="listAccounts")
WebElement accountDropdown;
public void selectAccount(){
logger.info("Selecting Account");
implicitwait(10);
Select dropdown = new Select(accountDropdown);
logger.info("Drop down is multiple::"+ dropdown.isMultiple());
}
预期:
即使代码页分开,代码也应该完全执行。
实际:
当我将所有页面代码放在一个测试类中时,代码就会被执行。
但是当我将它们单独放置在页面测试类中时,会抛出未找到元素异常。
org.openqa.selenium.NoSuchElementException: no such element: Unable to locate element: {"method":"id","selector":"listAccounts"}
最佳答案
在使用PageFactory框架的POM中,应该在PageClasses的构造函数中初始化PageFactory。请查找以下可能适合您的情况的代码片段。
public class LoginPage extends TestBase {
public LoginPage() {
PageFactory.initElements(driver, this);//Here driver is initialised in TestBase class and inherited in LoginPage class
}
//your code
@FindBy(id="listAccounts")
WebElement accountDropdown;
public void selectAccount(){
logger.info("Selecting Account");
implicitwait(10);
Select dropdown = new Select(accountDropdown);
logger.info("Drop down is multiple::"+ dropdown.isMultiple());
}
}
关于java - 放置在单独的测试类中时代码不执行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56919104/