[英]Using Selenium@FindBy with a value from an inherited class
I have several inherited classes that all have similar web elements. 我有几个继承的类,都具有相似的Web元素。 It is just that in each sub class, the actual xpath of the elements is different.
只是在每个子类中,元素的实际xpath不同。 All the interactions I want to do are the same among all the subclasses, except for the xpath.
除了xpath以外,所有子类之间我想做的所有交互都是相同的。
I want to do something like 我想做类似的事情
@FindBy(xpath = *somehow passed in constructor from subclass*) private WebElement searchBox;
in the superclass. 在超类中。 Then I can have my methods which act on the elements passed into it.
然后,我可以使用对传递到其中的元素起作用的方法。 I don't think I can pass the element from the subclass because the element will be dynamic, and this will be done statically (or just one time, and the element could change).
我不认为我可以从子类中传递该元素,因为该元素将是动态的,并且将以静态方式进行(或者只有一次,并且该元素可能会发生变化)。
I can't define the xpaths in the super class as the xpaths are different in each subclass. 我无法在超类中定义xpath,因为每个子类中的xpath不同。
I would appreciate any suggestions. 我将不胜感激任何建议。 I suppose I could pass the strings and do my own finds if necessary (driver.findElement(By.xpath(...))
我想我可以传递字符串,并在必要时自行查找(driver.findElement(By.xpath(...))
Oh, this is to be done with Java and Eclipse 哦,这要用Java和Eclipse完成
Should point out that since your WebElement instance is private, then I don't think subclassed page objects will have access to those instances of webelement. 应该指出,由于您的WebElement实例是私有的,所以我认为子类化的页面对象将无法访问这些webelement实例。 If your WebElement was 'protected', then you could access it from subclass and if you declared a new variable in the subclass of the same name, then the new one would override the one in the parent class AFAIK.
如果您的WebElement是“受保护的”,则可以从子类访问它,并且如果您在同名的子类中声明了一个新变量,则新变量将覆盖父类AFAIK中的变量。
Also, you cant pass the elements in the constructor like you are implying: instead, load them in the subclass using PageFactory.initObjects(parentClass), or something like that. 另外,您不能像暗示的那样在构造函数中传递元素:而是使用PageFactory.initObjects(parentClass)或类似的方法将它们加载到子类中。 The annotations can't take variables as arguments, AFAIK.
批注不能将变量作为自变量AFAIK。
class HomePage extends ParentPage {
public HomePage() {
ParentPage pp = PageFactory.initElements(driver, ParentPage.class);
HomePage hp = PageFactory.initElements(driver, this);
....
Never tried that before, but I would assume it might work. 以前从未尝试过,但是我认为它可能会起作用。
You might try a template method pattern where your superclass as an abstract class, which defines abstract methods to return selectors for your WebElements. 您可以尝试使用模板方法模式 ,其中您的超类为抽象类,该类定义了抽象方法以返回WebElement的选择器。 The child classes would include the class-specific @FindBy() definitions of your WebElements.
子类将包含WebElement的特定于类的@FindBy()定义。 (I don't know that this will work...just an idea.)
(我不知道这会起作用...只是一个想法。)
public abstract class MySuperClass {
public abstract By getFooSelector();
public void someMethodThatUsesFoo() {
WebElement foo = driver.findElement(getFooSelector());
...
}
}
public class MySubClass extends MySuperClass {
public By getFooSelector() {
return new By.ByXpath("foo/X/path");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.