我使用页面对象模型来创建我的代码,但WebElement为null,无法使用注释进行实例化。为什么这样的错误?
代码:
public class Login {
private String homePage = "http://localhost:63916/";
private Login_Page obj;
public Login() {
obj = new Login_Page();
}
@Given("^Volare Collector Home Page opens in browser$")
public void loadPage() {
try {
WebDriverManager.createDriver();
WebDriverManager.driver.get(homePage);
WebDriverManager.driver.manage().window().maximize();
} catch (Exception ex) {
LogManager.logger.log(Level.INFO, "Exception: " + ex.getMessage());
}
}
@When("^I login to Volare Collector with Username and Password$")
public void login() {
try {
obj.enterUsername();
WebDriverManager.driver.findElement(By.xpath("//*[@id=\"inputPassword\"]")).sendKeys("1");
WebDriverManager.driver.findElement(By.xpath("/html/body/div[1]/div/div/form/div/div[5]/div[2]/div/div[2]/button")).click();
} catch (Exception ex) {
LogManager.logger.log(Level.INFO, "Exception: " + ex.getMessage());
}
}
Login_Page
public class Login_Page {
@FindBy(xpath = "//*[@id=\\\"inputName\\\"]")
WebElement username;
PropertiesManager proMgr;
public Login_Page() {
proMgr = PropertiesManager.createInstance();
}
public void enterUsername() {
String u = proMgr.getProperties().getProperty("username");
username.sendKeys(u);
}
我正在使用Junit来运行测试。请帮忙。谢谢。
答案 0 :(得分:0)
我已经解决了这个问题。主要的是我们必须为每个页面制作一个像主页这样的构造函数,我们必须定义一个主页构造函数并传递Webdriver对象。对于页面对象模型,我们必须定义所有Web元素,根据需要创建方法,并使用相同构造函数中的页面工厂初始化所有元素。查看我的主页类以获取该参考。
执行此操作后,我们只需在步骤定义中调用主页类构造函数。在主页类之后检查我的步骤定义类。
如果你们有任何其他建议,请在这里分享。
这是我实现的代码。 这是我的主页类------------------->
package com.qa.pages;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.How;
import org.openqa.selenium.support.PageFactory;
import com.qa.utill.TestBase;
public class Homepage {
WebDriver driver;
@FindBy(how = How.XPATH, using = "//*[@id='email_create']")
WebElement enteremail;
@FindBy(xpath = "//button[@id='SubmitCreate']")
WebElement createaccountbtn;
**public Homepage(WebDriver driver)
{
System.out.println("driver is initializing");
PageFactory.initElements(driver, this);
System.out.println("driver is initialized");
}**
public void EnterEmailID()
{
System.out.println("Page maximaxe");
enteremail.sendKeys("thakkarsne@gmail");
}
public void clickonSignup()
{
createaccountbtn.click();
}
}
这是我的步骤定义文件----------------------->
package com.qa.Stepdef;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeDriver;
import com.qa.pages.Homepage;
import com.qa.utill.TestBase;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class MyStepDef extends TestBase{
@Given("^i am on signup page$")
public void i_am_on_signup_page() {
//System.setProperty("webdriver.chrome.driver", "/Users/snehalthakkar/Downloads/chromedriver");
//driver = new ChromeDriver();
//driver.get("http://automationpractice.com/index.php?controller=authentication&back=my-account");
TestBase.initialization();
// System.out.println("driver having"+ driver);
}
@When("^user enter emailid$")
public void user_enter_emailid() {
// driver.findElement(By.xpath("//*[@id='email_create']")).sendKeys("thakkarsne@gmail.com");
// homepage.EnterEmailID();
//System.out.println(driver);
//System.out.println("Catching user email");
Homepage homepage = new Homepage(driver);
homepage.EnterEmailID();
System.out.println("Catch user mail");
}
@Then("^click on button to creat an account$")
public void click_on_button_to_creat_an_account() {
System.out.println("I am clicking on button");
}
}