gpt4 book ai didi

java - 抽象类中的 Selenium FindBy 注释

转载 作者:行者123 更新时间:2023-12-01 04:17:10 24 4
gpt4 key购买 nike

我正在使用 Selenium for Java 开发测试自动化框架。我遇到的情况是,不同的页面共享非常相似的功能。我想出了创建具有一些常见实现的抽象类的想法,如下所示:

abstract class Foo{

// this is the problem, how to annotate below elements?
// at this point there is now way to know "how" and "using"
// @FindBy(how = ???, using = ???)
private WebElement element1;
// @FindBy(how = ???, using = ???)
private WebElement element2;

public void setElement1(String v){
this.element1.sendKeys(v);
}

public void clickElement2(){
this.element2.submit()
}
}

public class Bar extends Foo{
...
}

public class Baz extends Foo{
...
}

我想避免为每个页面实现相同的结构。有什么聪明的方法可以做到这一点吗?我是 Java 的初学者。提前致谢。

最佳答案

如果您的页面不共享通用元素,则此设置没有意义。而是创建一个帮助器类来为您执行方法逻辑,您将在其中传递 WebElement 。大致如下:

您的类代表页面/ View :

public class Bar {

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

}

public class Baz {

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

}

您的实用程序类:

public class SeleniumUtil {

public static void setElement1(WebElement el, String value){
el.sendKeys(value);
}

}

关于java - 抽象类中的 Selenium FindBy 注释,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19336902/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com