简体   繁体   English

将Selenium @ FindBy与继承的类中的值一起使用

[英]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.

相关问题 继承的类和@FindBy类的执行顺序 - Execution order of Inherited class and @FindBy class Selenium FindBy在抽象类中的注释 - Selenium FindBy anotations in abstract class 在没有PageFactory的Selenium中使用@FindBy - Using @FindBy in Selenium without PageFactory 我在继承的 class 中获取驱动程序值 = null,它是从基础 class 继承的,并且还得到 Null 点异常(硒) - im Getting driver value=null in inherited class which is inherited from base class and also getting Null point exception(selenium) 无法在 selenium webdriver java 中使用 @FindBy 进行初始化 - Unable to initialise using @FindBy in selenium webdriver java 使用 lombok 为继承的 class 定义默认值 - Define default value for inherited class using lombok 硒-在同一类中使用@FindBy和传统的driver.findByElement - Selenium - Use @FindBy and the traditional driver.findByElement in the same class 在我自己的类中使用@FindBy,而不是WebElement - Using @FindBy with my own class, not with WebElement Java Selenium FindBy 无法通过唯一的 class 名称找到 div - Java Selenium FindBy cannot find div by unique class name NullPointerException @FindBy 在 PageObjectModel 中使用 ChromeDriver 和 Chrome 通过 Selenium - NullPointerException @FindBy in PageObjectModel using ChromeDriver and Chrome through Selenium
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM