Java Selenium自定义方法,以参数形式获取WebElement的名称。

huangapple go评论84阅读模式
英文:

Java Selenium custom method to get name of webelement as parameters

问题

使用Java Selenium和Page Object在我的项目中,想要创建一个自定义方法,该方法将获取两个字符串作为参数,将它们组合在一起,并调用正确的webelement执行一些操作。

以下是我的Page Object的示例:

public class Test extends Base {

    public Test() {
        PageFactory.initElements(driver, this);
    }

    @FindBy(xpath = "//div[@id='question1No']")
    WebElement question1Yes;

    @FindBy(xpath = "//div[@id='question1No']")
    WebElement question1No;

    public Object GetField(String answer, String question) {
        Field field = null;
        try {
            field = Test.class.getField((question + answer).replace("\"", ""));
            return field.get(this);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    public void answerQuestion(String answer, String question) {
        WebElement element = (WebElement) GetField(answer, question);
        element.click();
    }
}

然后调用该方法的示例:

answerQuestion("Yes", "question1")

调用这个方法应该执行点击question1Yes元素。

英文:

Using Java Selenium and Page Object in my project, want to create a custom method which will get two strings as parameters, combine them and call right webelement to perform some actions.

Below example of my Page Object

public class Test extends Base {

public Test() {
	PageFactory.initElements(driver, this);
}


@FindBy(xpath = "//div[@id='question1No']")
WebElement question1Yes;

@FindBy(xpath = "//div[@id='question1No']")
WebElement question1No;


public Object GetField(String answer, String question) {
    Field field = null;
    try {
        field = Test.class.getField((question+answer).replace("\"", ""));
        return field.get(this);
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

public void answerQuestion(String answer, String question) {    
    WebElement element = (WebElement)GetField(answer,question);
    element.click();

}

And call that method for example:

answerQuestion("Yes", "question1")

calling this method should perform click on question1Yes element.

答案1

得分: 2

你可以使用反射。

Field field = Test.class.getField("question1Yes");
WebElement element = (WebElement) field.get(this);

然后,你可以对你的 WebElement 做任何你想做的事情。

更新:
如果字段是私有的,请写 field.setAccessible(true);

英文:

You may use reflection.

Field field = Test.class.getField("question1Yes");
WebElement element = (WebElement) field.get(this);

And then you can do whatever you want with your WebElement

upd.
if field is private, write field.setAccessible(true);

答案2

得分: 1

以下是您提供的代码的中文翻译部分:

// 导入必要的包
import java.lang.reflect.Field;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;

public class Test extends Base { // 基于基类的更新

    @FindBy(xpath = "//div[@id='question1No']")
    public WebElement question1Yes; // 问题1的"是"元素

    @FindBy(xpath = "//div[@id='question1No']")
    public WebElement question1No; // 问题1的"否"元素

    public Test() {
        PageFactory.initElements(driver, this);
    }

    public void answerQuestion(String answer, String question) {
        WebElement temp = null;
        try {
            Field field = Test.class.getField(question + answer);
            temp = (WebElement) field.get(this);
            temp.click();
        } catch (Exception e) {
            // 处理异常情况,例如字段错误
            // 抛出 NoSuchFieldException 和 SecurityException 异常
            e.printStackTrace();
        }
    }

    // 也可以使用通用方法来获取字段
    public Object getField(String answer, String question) {
        Field field = null;
        try {
            field = Test.class.getField(question + answer);
            return field.get(this);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }

    // 这样您的代码将是这样的
    public void answerQuestion(String answer, String question) {
        WebElement temp = (WebElement) getField(answer, question);
        temp.click();
    }

    public Object getField(String answer, String question) {
        Field field = null;
        try {
            field = Test.class.getField(question + answer);
            return field.get(this);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

请注意,我已经根据代码中的注释进行了翻译。如果您需要进一步的帮助,请随时告诉我。

英文:

Added to above answer updating it based on OP need along with package name

import java.lang.reflect.Field;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class Test extends base{
@FindBy(xpath = "//div[@id='question1No']")
public WebElement question1Yes;
@FindBy(xpath = "//div[@id='question1No']")
public WebElement question1No;
public Test() {
PageFactory.initElements(driver, this);
}
public void answerQuestion(String answer, String question) {
WebElement temp = null;
try {    			
Field field = Test.class.getField(question + answer);
temp = (WebElement) field.get(this);
temp.click();
} catch (Exception e) {
// Handle the exception here when there is wrong field
// throws NoSuchFieldException, SecurityException
e.printStackTrace();
}
}
}

Also you can use common method for getting the fields

public Object GetField(String answer, String question) {
Field field = null;
try {
field = Test.class.getField(question + answer);
return field.get(this);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}

So that your code will be like

import java.lang.reflect.Field;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
public class Test extends base {
@FindBy(xpath = "//div[@id='question1No']")
public WebElement question1Yes;
@FindBy(xpath = "//div[@id='question1No']")
public WebElement question1No;
public Test() {
PageFactory.initElements(driver, this);
}
public void answerQuestion(String answer, String question) {	
WebElement temp = (WebElement)GetField(answer,question);
temp.click();	
}
public Object GetField(String answer, String question) {
Field field = null;
try {
field = Test.class.getField(question + answer);
return field.get(this);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}

huangapple
  • 本文由 发表于 2020年8月13日 22:40:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/63397516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定