import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;我有一个页面对象模型,每个页面对象都需要这些导入语句。为每个类文件重复编写导入语句是一项繁重的任务。
是否有一种方法可以减少代码行并导入或使用常用的导入,方法是使用一行代码,如:
import package.commonlyusedimports;commonlyusedimports类定义为:
package xxx;
import java.util.Properties;
import org.openqa.selenium.By;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
public class commonlyusedimports{
}注意:我尝试过继承,即使这样,父类导入对子类也不可见。
发布于 2019-06-15 09:18:23
您可以从正在导入的包中为所需的行为注入一个工厂*
public interface WebFactory {
public Element findElement(Locator loc);
....
}进口也将是接口..。
public interface Element {
public String getText();
...
}然后,您可以通过等待Selenium来实现这些接口。
public class SeleniumElement implements Element{
public String getText() {
...
}
}然后实施工厂:
public class SeleniumWebFactory implements WebFactory {
public Element findElement(Locator loc) {
....
}
}最后,只需向页面对象注入一个工厂:
@BeforeSuite
....
this.factory = new SeleniumWebFactory();
@Test()
....
Page page = new Page(this.factory)这样,页面对象就可以调用泛型行为,例如WebFactory.findElement和Element.getText,而无需显式地要求它们。
这是一个很好的实践,因为目前您正在将所有代码绑定到Selenium。你可以用反转此依赖关系,这样Selenium就不能跟你上床了。
发布于 2019-07-17 08:08:26
为每个类文件重复编写导入语句是一项繁重的任务。
真的很忙吗?如果这是真的话,这将使大多数开发人员疯狂。了解如何使用IDE来帮助您加快添加正确的导入。
批量添加导入的Java:https://crunchify.com/java-simple-way-to-import-all-missing-packages-at-once/
https://sqa.stackexchange.com/questions/39581
复制相似问题