简体   繁体   English

你如何在java selenium中使用PageObjects和@FindBy执行javascript元素

[英]How do you execute javascript element using PageObjects and @FindBy in java selenium

I am creating page objects for navigating through a site and refactoring hashed together Java project.我正在创建页面对象,用于在站点中导航并重构散列在一起的 Java 项目。 I have a piece of code which invokes a JavaScript button.我有一段调用 JavaScript 按钮的代码。 However, I can't work out how to set this up to be used in page object format with @FindBy但是,我不知道如何使用@FindBy将其设置为以页面对象格式使用

Current page object is:当前页面对象是:

public CustomerLogin(WebDriver driver) {
        PageFactory.initElements(driver, this);
    }


    @FindBy(how = How.ID, using = "username")
    private WebElement userName;

    @FindBy(how = How.ID, using = "password")
    private WebElement password;

    @FindBy(how = How.XPATH, using = "//*[@id=\\\"loginPage\\\"]/div[2]/div/div/div[1]/form/div[3]/div/input[1]")
    private WebElement login;

    public void logIn(String userName, String password) {
        this.userName.sendKeys(userName);
        this.password.sendKeys(password);

}}

The part that I can't get in is:我无法进入的部分是:

WebElement element = webDriver.findElement(By.xpath("//*@id=\"loginPage\"]/div[2]/div/div/div[1]/form/div[3]/div/input[1]"));

JavascriptExecutor executor = (JavascriptExecutor) webDriver;
executor.executeScript("arguments[0].click();", element);

This works if I put it in the test script but can't work out how to convert that into a page object version.如果我将它放在测试脚本中,但无法弄清楚如何将其转换为页面对象版本,这会起作用。

Initialize the Javascript Executor in the constructor在构造函数中初始化 Javascript Executor

    public class CustomerLogin{

    WebDriver driver;
    JavascriptExecutor executor;


    public CustomerLogin(WebDriver driver) {
        this.driver = driver;
        this.executor = (JavascriptExecutor) this.driver;
        PageFactory.initElements(driver, this);
        }

    @FindBy(xpath = "//*@id=\"loginPage\"]/div[2]/div/div/div[1]/form/div[3]/div/input[1]")
    private WebElement loginButton

    //method, for clicking loginButton  with JS Executor
    public void clickLoginButton() {
        executor.executeScript("arguments[0].click();", loginButton);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM