Selenium WebDriver和PageFactory初始化WebElement从属性文件 [英] Selenium WebDriver and PageFactory Initialization WebElement From Property File

查看:95
本文介绍了Selenium WebDriver和PageFactory初始化WebElement从属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用硒WebDiver和PageFacotry初始化webelement,但是我想在运行时初始化element,但是值应该来自xml文件或属性文件.

I am using selenium WebDiver and PageFacotry to initialize webelement,but I want to initialize element at runtime but value should come form xml file or property file.

下面的语句初始化LogIn_PG_POF在LogIn_PG_POF类中声明的所有元素,但是我想从属性文件或xml文件中初始化部分(作为FindBy批注的属性),因此以后我可以轻松更改而无需更改在代码部分.

Below statement initialize LogIn_PG_POF all the element which are declare in LogIn_PG_POF class but I want to initialize how(it is a attribute of FindBy annotation) portion from the properties file or xml file so later on I can change easily no need to change in code portion.

LogIn_PG_POF LoginPage = PageFactory.initElements(driver, LogIn_PG_POF.class);

这是一个LogIn_PG_POF

This is a LogIn_PG_POF

public class LogIn_PG_POF {

    final WebDriver driver;

    @FindBy(how = How.ID, using = "log")
    public WebElement txtbx_UserName;

    @FindBy(how = How.ID, using = "pwd")
    public WebElement txtbx_Password;

    @FindBy(how = How.NAME, using = "submit")
    public WebElement btn_Login ;

    // This is a constructor, as every page need a base driver to find web elements
    public LogIn_PG_POF(WebDriver driver){ 
        this.driver = driver; 
    } 

}

推荐答案

我有类似的要求,并且创建了自己的注释和PageFactory类.我在注释中传递键值,该注释的值稍后从XML和PageFactory类中获取,该值在initElements(WebDriver d,Page.class)方法中说,它使用反射来检索所有用您的自定义注释注释的字段,并取值然后从XML中检索相应的值并实例化该类的每个元素.它还可以让您灵活地从XML本身传递诸如id,css之类的定位器类型.

I had a similar requirement and I have created my own annotation and PageFactory Class. I am passing the key value in the annotation whose value I am getting later from the XML and in PageFactory class say in initElements(WebDriver d, Page.class) method, using reflections retrieve all the fields annotated with your custom annotation, take the value and then retrieve the corresponding value from the XML and instantiate each element of the class. It can also give you flexibility to pass locator type like id, css from the XML itself.

最近开始了解硒中的ElementLocator,但是我不确定如何使用它.都没有找到关于其用法的好话题

Lately came to know about ElementLocator in selenium, but I am not sure how to use it. Neither found any good topics on its usage

这篇关于Selenium WebDriver和PageFactory初始化WebElement从属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆