简体   繁体   English

如何使用Selenium在Java中的方法内部初始化另一个WebElement

[英]How to initialize another WebElement inside the method in Java with Selenium

I'm a bit concerned with the minor problem that I'm facing. 我有点担心我所面临的小问题。 I have a test case where I need to pick up any value from the drop down. 我有一个测试用例,需要从下拉列表中选取任何值。 After selection entire web page reloads and I have 2 options. 选择后,整个网页都会重新加载,我有2个选项。 If warning message appears, I need to click on drop down again and choose another value. 如果出现警告消息,我需要再次单击下拉菜单并选择另一个值。 If warning message doesn't appear, just click on "Continue" button. 如果没有出现警告消息,只需单击“继续”按钮。 All my values inside the drop down have the same id which only differs in ending: 我下拉菜单中的所有值都具有相同的ID,只是结尾不同:

uxMiniFinderVoyageSelect_chzn_o_1
uxMiniFinderVoyageSelect_chzn_o_2
uxMiniFinderVoyageSelect_chzn_o_3
uxMiniFinderVoyageSelect_chzn_o_4

till 30. What I did I called Random class where I set up variable from 1 - 30 直到30。我做了什么,我称之为随机类,在其中我设置了1-30的变量

Random random = new Random();
int x = random.nextInt(30) + 1;

Now I look for my element this way 现在我以这种方式寻找我的元素

WebElement valueFromDropDown = driver.findElement(By.id("uxMiniFinderVoyageSelect_chzn_o_" + x));

But if warning message appears and I need to click on another value, my code picks up the same value over and over. 但是,如果出现警告消息,并且我需要单击另一个值,则我的代码将一遍又一遍地拾取相同的值。 The question is how to correctly and with less code writing click on another element in drop down? 问题是如何正确地用更少的代码单击下拉列表中的另一个元素? The full class looks like this 全班看起来像这样

    public class SomeClassName{

        Random random = new Random();
        int x = random.nextInt(30) + 1;
        @FindBy(xpath = "someXpathExpression") private WebElement dropDown;
        @FindBy(xpath = "someXpathExpression") private WebElement warningMessage;
        @FindBy(xpath = "someXpathExpression") private WebElement continueButton;

public void fillForm() throws Exception{
    WebElement valueFromDropDown = driver.findElement(By.id("uxMiniFinderVoyageSelect_chzn_o_" + x));
    dropDown.click();
    valueFromDropDown.click();
    if(user will see that warningMessage suddenly apppears){
    dropDown.click();
    valueFromDropDown.click(); -> this is where I want to click on another value
    }else{
    contunieButton.click();

Introduce a method to return drop down webelement randomly like below 介绍一种随机返回下拉式Web元素的方法,如下所示

public WebElement getDropDownValueRandomly() {
    Random random = new Random();
    int x = random.nextInt(30) + 1;
    WebElement valueFromDropDown = driver.findElement(By.id("uxMiniFinderVoyageSelect_chzn_o_" + x));

    return valueFromDropDown;

}

Your Class will be like : 您的课程如下:

public class SomeClassName{

    @FindBy(xpath = "someXpathExpression") private WebElement dropDown;
    @FindBy(xpath = "someXpathExpression") private WebElement warningMessage;
    @FindBy(xpath = "someXpathExpression") private WebElement continueButton;

public void fillForm() throws Exception{
    WebElement valueFromDropDown = getDropDownValueRandomly();
    dropDown.click();
    valueFromDropDown.click();
    if(user will see that warningMessage suddenly apppears){
    dropDown.click();
    getDropDownValueRandomly().click(); -> this is where I want to click on another value
    }else{
    contunieButton.click();
    }
}

public WebElement getDropDownValueRandomly() {
    Random random = new Random();
    int x = random.nextInt(30) + 1;
    WebElement valueFromDropDown = driver.findElement(By.id("uxMiniFinderVoyageSelect_chzn_o_" + x));

    return valueFromDropDown;

}

} }

Hope this helps 希望这可以帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM