- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试在线学习本教程: https://www.youtube.com/watch?v=x5Ru0f8uOqw&list=PL_noPv5wmuO_t6yYbPfjwhJFOOcio89tI&index=14
并完全按照演示对 PageObjects、Feature 文件和 StepDefs 文件进行了编码。但是,当我运行时,我在 @When 方法中的第 16 行 contactPage 处收到空指针异常。
public class StepDefinition {
WebDriver driver = new FirefoxDriver();
LandingPage landingPage;
ContactPage contactPage;
@Given("^I am on the zoo site$")
public void i_am_on_the_zoo_site() throws Throwable {
LandingPage landingPage = new LandingPage(driver);
landingPage.navigateToWebApp();
}
@When("^I click on \"(.*?)\"$")
public void i_click_on(String link) throws Throwable {
contactPage = landingPage.navigateToContactPage(link);
}
...所以我尝试在类的顶部实例化,如下所示:-
WebDriver driver = new FirefoxDriver();
LandingPage landingPage = new LandingPage(driver);
ContactPage contactPage = new ContactPage(driver);
...一切都很愉快。
我应该以这种方式实例化 pageobject 实例吗?最好的做法是什么?而且,为什么演示中的代码不会抛出空指针?
对于上下文,以下是相关的页面对象:摘要页:-
public class AbstractPage {
protected WebDriver driver;
public AbstractPage (WebDriver driver){
this.driver = driver;
}
public LandingPage navigateToWebApp(){
driver.navigate().to("http://thetestroom.com/webapp/");
return new LandingPage(driver);
}
public void closeDriver(){
driver.quit();
}
}
登陆页面:-
public class LandingPage extends AbstractPage {
public LandingPage(WebDriver driver) {
super(driver);
}
public ContactPage navigateToContactPage(String link){
driver.findElement(By.id(link.toLowerCase() + "_link")).click();
return new ContactPage(driver);
}
}
联系页面:-
public class ContactPage extends AbstractPage{
public ContactPage(WebDriver driver) {
super(driver);
}
public ContactPage setNameField(String value){
driver.findElement(By.name("name_field")).sendKeys(value);
return new ContactPage(driver);
}
//more setter methods
最佳答案
是的,您确实需要创建页面对象的新实例,即
LandingPage landingPage = new LandingPage(driver);
ContactPage contactPage = new ContactPage(driver);
这是一项重要的实践,因为:
LandingPage landingPage
意味着你的landingPage变量被隐式分配了一个空值;这就是您收到空指针异常的原因。
关于java - 从 Cucumber StepDefinitions 中实例化 PageObject 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30805040/
我刚开始使用 WebDriver ,我正在尝试学习最佳实践,特别是使用 PageObjects和 PageFactory . 我的理解是PageObjects应该暴露网页上的各种操作,并将WebDri
所以我有一个页面对象,它处理用户所在页面的所有主要功能。我想这样做,这样当调用页面对象的实例时就会发生一些事情。在当前情况下,我有: public MyPageObject MY_SCREEN = n
所以,当出现问题时,我只是在创建一个简单的 selenium/JBehave 代码。 我会先贴出简化的代码,稍后再解释我的问题。 所以这里我们有一个简单的抽象类,它将在我的 PageObjects 上
我正在尝试为 WebDriver 中的页面对象创建某种代码生成。 我读到了它,并观看了下一个讲座: http://www.youtube.com/watch?v=mSCFsUOgPpw 我还看到一些工
如何为页面中的框架实现 pageObject 模式(Selenium)..我有一个主页,有一个左框架和右框架,我想为每个框架创建页面对象.. 例如,我有如下的 LeftFrame 页面对象: Publ
有没有办法从另一个页面对象文件访问一个页面对象文件中定义的元素? 例子:如果我们需要从 LoginPage.ts 文件访问“@usernameInput”,是否需要从 HomePage.ts 复制它?
我想从下面知道哪个是创建 PageObject 类实例的更好选择(例如:LoginPage):- 1) 在所有测试和步骤中创建一个 Pageobject 类的实例(任何需要的地方)? LoginPag
我想我已经掌握了 PageObject 模式的要点,但其中有些部分我还没有掌握。 所以我一直在学习本教程,现在我有了这个 Page 类: public class SignUpPage extends
如果一个按钮可选地显示在页面(或页面的一部分)上,它是否有资格将该部分表示为两个不同的 PageObjects,其中一个 PageObject 提供与按钮交互的方法,而另一个 PageObject 不
我有一些基本的 selenium 和 java 经验,但我正在尝试了解有关页面对象模式的更多信息,并且在完全理解如何在实际项目中使用它时遇到了一些问题。我尝试在谷歌中寻找简单但实用的项目,我可以复制并
page-object gem自动 generates multiple accessors . 例如,除了 link 之外,您还可以使用 links,例如: class MyPage inc
我正在编写一些 Selenium 测试,想知道如何导航到我的 ProfilePage,只需在我的测试中调用它的类即可所以我得到了我的 BasePage 类, class BasePage(object
我正在使用 Cheezy Page Object gem(这也意味着我正在使用 Watir,这也意味着我正在使用 Selenium)。我还明确加载了 watir gem。 无论如何,我有一个网站,我正
将 PageObjects 模式应用于页面组件时的标准方法是什么? 举个例子,假设我正在为亚马逊产品页面上的功能编写测试。 该页面包含大量单独的功能、产品信息、查看此内容的客户、推荐的其他客户等。 我
本文整理了Java中com.atlassian.webdriver.pageobjects.WebDriverTester类的一些代码示例,展示了WebDriverTester类的具体用法。这些代码示
我们的应用程序具有消耗组件的组件,这些组件消耗具有不同复杂性的组件。所以我只想要页面上的输入,以验证何时设置对象文本是否正确。问题是它是这些子组件之一。 我的同事告诉我,有两种方法可以做到这一点,第一
我对 Selenium 非常陌生,我开始自动化登录过程。我为此目的使用了 PageObjects。我有一个 LoginPage ,它有一个这样的方法: public MyHomePage login(
我正在尝试使用 WebdriverIO Docs 中定义的语法定义一个 PageObject : 父页面 //page.js export default class Page { constru
我试图找到这个问题的答案,但没有一个对我有帮助。我确定我遗漏了一个细节,但我无法弄清楚到底是什么。 运行测试用例时出现NullPointerException。我认为当我尝试将驱动程序传递给我的页面对
我正在使用 PageObjects 在 Selenium Webdriver 中制作一个测试套件,但我怀疑即使驱动程序指向不同的框架,WebElement 引用的实例是否也可以工作?我正在为其编写测试
我是一名优秀的程序员,十分优秀!