@FindBy错误使用类选择

问题描述 投票:0回答:2

我正在尝试使用类@FindBy中的注释Select实现Page Object。在Eclipse中,它显示以下消息:

类型By中的方法id(String)不适用于参数(WebElement)。

我不知道这个消息来了。按照下面的代码和错误图像。

类InvoicingGeTratamentoOsPage

public class FaturamentoGeTratamentoOsPage {
    WebDriver driver;

    @FindBy(id = "cboMotivo")
    WebElement CBOMotivo;
    public FaturamentoGeTratamentoOsPage(WebDriver driver) {
        this.driver = driver;
    }
    public void preencherCampoMotivo(String CampoMotivo) {
        // Campo Motivo
        WebElement campoMotivo = driver.findElement(By.id(CBOMotivo));
        Select slcMotivo = new Select(campoMotivo);
        slcMotivo.selectByVisibleText(CampoMotivo);
    }
    public void preencherCampoSubmotivo(String CampoSubMotivo) throws Exception {
    }
}

BillingConnectivityFacilitiesTest类

public class FaturamentoGeConectividadeFacilidadesTest {
    static WebDriver driver;
    @Before
    public void setUp() throws Exception {
        SelecionarNavegador nav = new SelecionarNavegador();
        driver = nav.iniciarNavegador("chrome", "http://10.5.9.45/BkoMais_Selenium/");
    }
    @Test
    public void selecionarFacilidades() throws Exception {
        // Logando na aplicação
        LogarBkoMaisPage login = new LogarBkoMaisPage(driver);
        login.logar("844502", "Bcc201707");

        // BackOffice >> FaturamentoGe >> Conectividade
        FaturamentoGeConectividadeFacilidadesPage menu = new FaturamentoGeConectividadeFacilidadesPage(driver);
        menu.logarFaturamentoGeConectividade();

        //Registro >> Novo caso
        RegistroNovoCasoPage reg = new RegistroNovoCasoPage(driver);
        reg.registrarCaso();

        //Preencher campos
        FaturamentoGeTratamentoOsPage campo = new FaturamentoGeTratamentoOsPage(driver);
        campo.preencherCampoMotivo(" Concluido ");
    }
    @After
    public void tearDown() throws Exception {
        Thread.sleep(5000);
        driver.quit();
    }
}
selenium-webdriver pageobjects
2个回答
1
投票

您需要添加pagefactory.init来初始化webelement。

public FaturamentoGeTratamentoOsPage(WebDriver driver) {    this.driver = driver;
PageFactory.initElements(driver, this);
}

没有使用下面的行...因为CBOMotive只直接返回一个webelement

WebElement campoMotivo = driver.findElement(By.id(CBOMotivo));

0
投票

你得到的错误是因为By.id()期待一个String,而不是WebElement。你已经将CBOMotivo定义为WebElement但是它就像String一样对待它。

以下是正确的用法

WebElement campoMotivo = driver.findElement(By.id("cboMotivo"));

你想要的是什么

public void preencherCampoMotivo(String CampoMotivo) {
    // Campo Motivo
    Select slcMotivo = new Select(CBOMotivo);
    slcMotivo.selectByVisibleText(CampoMotivo); 
}
© www.soinside.com 2019 - 2024. All rights reserved.